"""Display enrichments must not rewrite an existing character or its history.""" import json import pytest from fastapi.testclient import TestClient from test_api import CREATE, make_app, turn from cybergril.api.app import HOURS, BirthInput, compile_birth from cybergril.core.bazi import pillars_from_lunar @pytest.mark.parametrize("birth", ["1900-01-01", "2000-02-04", "2000-02-05", "2001-05-23"]) @pytest.mark.parametrize("hour", HOURS) def test_calendar_and_hour_boundaries_preserve_existing_chart(birth, hour): color, pillars, _ = compile_birth(BirthInput(name="星河", birth=birth, hour=hour)) y, m, d = map(int, birth.split("-")) assert pillars == pillars_from_lunar(y, m, d, HOURS.index(hour) * 2) assert color["calendar"]["solar"] == birth assert color["calendar"]["calculation_time"] == f"{HOURS.index(hour) * 2:02d}:00" if birth == "2001-05-23": assert color["calendar"]["leap_month"] is True assert "闰四月初一" in color["calendar"]["lunar"] if birth == "2000-02-05": assert "正月初一" in color["calendar"]["lunar"] assert len(color["sections"]) == 6 assert all(len(s["text"]) > 70 and s["basis"] for s in color["sections"]) def test_legacy_read_uses_saved_parameters_without_writes_or_recompilation(tmp_path, monkeypatch): app, provider = make_app(tmp_path / "old.db") with TestClient(app) as client: client.post("/api/companion", json=CREATE) store = app.state.store identity = json.loads(store.query_one("SELECT identity_json FROM companion")[0]) identity["color"] = {k: identity["color"][k] for k in ("pillars", "traits")} saved = json.loads(store.query_one("SELECT derived FROM bazi_core")[0]) saved["expressiveness"] = .1 with store.conn: store.execute("UPDATE companion SET identity_json=?", (json.dumps(identity),)) store.execute("UPDATE bazi_core SET derived=?", (json.dumps(saved),)) raw_identity = store.query_one("SELECT identity_json FROM companion")[0] raw_bazi = tuple(store.query_one("SELECT * FROM bazi_core")) monkeypatch.setattr("cybergril.api.app.compile_pillars", lambda _: pytest.fail("recompiled")) monkeypatch.setattr("cybergril.core.birth_view.ten_god_distribution", lambda _: pytest.fail("recomputed")) view = client.get("/api/companion").json() assert "先观察" in view["color"]["sections"][0]["text"] assert client.get("/api/companion").json() == view assert store.query_one("SELECT identity_json FROM companion")[0] == raw_identity assert tuple(store.query_one("SELECT * FROM bazi_core")) == raw_bazi turn(client) prompt = provider.calls[-1][0].content # R2 injects saved raw facts independently; six UI explanations are not a persona. assert '"expressiveness": 0.1' in prompt assert json.loads(raw_bazi[2])["day"] in prompt assert all(s["text"] not in prompt for s in view["color"]["sections"]) history = [tuple(r) for r in store.query("SELECT * FROM chat_message")] updated = client.patch("/api/relation", json={"relation": "恋人"}).json() assert updated["color"] == view["color"] stored_after = json.loads(store.query_one("SELECT identity_json FROM companion")[0]) assert stored_after["color"] == identity["color"] assert tuple(store.query_one("SELECT * FROM bazi_core")) == raw_bazi assert [tuple(r) for r in store.query("SELECT * FROM chat_message")] == history def test_profiles_are_deterministic_and_differ_across_births(): colors = [compile_birth(BirthInput(name="星河", birth=b, hour=h))[0] for b, h in [("2000-10-18", "亥时"), ("1992-02-10", "卯时"), ("1988-07-23", "子时")]] assert colors[0] == compile_birth(BirthInput(name="另一名字", birth="2000-10-18", hour="亥时"))[0] assert len({json.dumps(c["sections"], ensure_ascii=False) for c in colors}) == 3 assert len({c["sections"][0]["text"] for c in colors}) > 1