MVP 0.9
This commit is contained in:
1 parent
a8caeb0dd4
commit
fd7102e3b3
95 files changed
+16520
-193
No files matched your search
@@ -0,0 +1,266 @@
|
||||
"""Architecture correction pass. Run from backend/: python tests/test_architecture_correction.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-arch-correction-test.sqlite")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from db import get_db, migrate_journal_source_refs
|
||||
from main import app
|
||||
from privacy_gateway import reset_debug
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def header(token: str) -> dict:
|
||||
return {"X-Auth-Token": token}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
reset_debug()
|
||||
with TestClient(app) as client:
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "lars@example.test", "name": "Lars", "password": "test-pass"},
|
||||
)
|
||||
headers = header(setup.json()["token"])
|
||||
profile_id = setup.json()["profile_id"]
|
||||
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": "Alltag"})
|
||||
expect(space.status_code == 200, "space created")
|
||||
space_id = space.json()["id"]
|
||||
day = client.post(
|
||||
f"/api/journal/spaces/{space_id}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-20"},
|
||||
)
|
||||
day_id = day.json()["day"]["id"]
|
||||
conv = client.post(
|
||||
f"/api/journal/days/{day_id}/conversations",
|
||||
headers=headers,
|
||||
json={"title": "Quelle"},
|
||||
)
|
||||
conv_id = conv.json()["id"]
|
||||
turn = client.post(
|
||||
f"/api/journal/conversations/{conv_id}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Heute war der Markt voll. Ich habe Kirschen gekauft."},
|
||||
)
|
||||
expect(turn.status_code == 200, "source turn")
|
||||
gen = client.post(
|
||||
f"/api/journal/days/{day_id}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [conv_id]},
|
||||
)
|
||||
expect(gen.status_code == 200, "draft from conversation")
|
||||
draft = gen.json()
|
||||
expect(draft["source_conversation_ids"] == [conv_id], "draft reconstructs conversation ids")
|
||||
expect(len(draft["source_message_ids"]) >= 2, "draft reconstructs message ids")
|
||||
|
||||
with get_db() as conn:
|
||||
refs = conn.execute(
|
||||
"SELECT source_kind, source_id FROM journal_draft_source_refs WHERE draft_id = ?",
|
||||
(draft["id"],),
|
||||
).fetchall()
|
||||
kinds = {row["source_kind"] for row in refs}
|
||||
expect("conversation" in kinds and "message" in kinds, "draft provenance is relational")
|
||||
|
||||
saved = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": draft.get("title") or "Markttag",
|
||||
"body": draft.get("body") or "Kirschen",
|
||||
"origin": "accepted_draft",
|
||||
"entry_id": None,
|
||||
"source_conversation_ids": draft["source_conversation_ids"],
|
||||
"source_message_ids": draft["source_message_ids"],
|
||||
},
|
||||
)
|
||||
expect(saved.status_code == 200, f"save entry {saved.text}")
|
||||
entry = saved.json()
|
||||
expect(entry["source_conversation_ids"] == [conv_id], "entry version reconstructs conversation ids")
|
||||
expect(entry["source_message_ids"] == draft["source_message_ids"], "entry version reconstructs message ids")
|
||||
|
||||
versions = client.get(f"/api/journal/entries/{entry['id']}/versions", headers=headers)
|
||||
expect(versions.status_code == 200, "list versions")
|
||||
expect(versions.json()[0]["source_conversation_ids"] == [conv_id], "version list keeps source links")
|
||||
|
||||
legacy_draft_id = str(uuid.uuid4())
|
||||
legacy_version_id = str(uuid.uuid4())
|
||||
legacy_entry_id = str(uuid.uuid4())
|
||||
mid = draft["source_message_ids"][0]
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO journal_drafts
|
||||
(id, profile_id, journal_day_id, title, body,
|
||||
source_conversation_ids, source_message_ids, as_of, superseded_at)
|
||||
VALUES (?, ?, ?, 'Legacy', 'alt', ?, ?, datetime('now'), datetime('now'))
|
||||
""",
|
||||
(legacy_draft_id, profile_id, day_id, json.dumps([conv_id]), json.dumps([mid])),
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO journal_entries (id, profile_id, journal_day_id, space_id)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(legacy_entry_id, profile_id, day_id, space_id),
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO journal_entry_versions
|
||||
(id, profile_id, entry_id, title, body, source_conversation_ids, source_message_ids, origin)
|
||||
VALUES (?, ?, ?, 'Legacy', 'alt', ?, ?, 'user_edit')
|
||||
""",
|
||||
(legacy_version_id, profile_id, legacy_entry_id, json.dumps([conv_id]), json.dumps([mid])),
|
||||
)
|
||||
migrate_journal_source_refs(conn)
|
||||
draft_refs = [
|
||||
(row["source_kind"], row["source_id"])
|
||||
for row in conn.execute(
|
||||
"SELECT source_kind, source_id FROM journal_draft_source_refs WHERE draft_id = ? ORDER BY sort_order",
|
||||
(legacy_draft_id,),
|
||||
).fetchall()
|
||||
]
|
||||
version_refs = [
|
||||
(row["source_kind"], row["source_id"])
|
||||
for row in conn.execute(
|
||||
"SELECT source_kind, source_id FROM journal_entry_version_source_refs WHERE version_id = ? ORDER BY sort_order",
|
||||
(legacy_version_id,),
|
||||
).fetchall()
|
||||
]
|
||||
expect(("conversation", conv_id) in draft_refs, "legacy draft conversation migrated")
|
||||
expect(("message", mid) in draft_refs, "legacy draft message migrated")
|
||||
expect(("conversation", conv_id) in version_refs, "legacy version conversation migrated")
|
||||
expect(("message", mid) in version_refs, "legacy version message migrated")
|
||||
|
||||
usage = client.post("/api/dialogue/sessions", headers=headers, json={"intent": "space-only"})
|
||||
space_conv = client.post(
|
||||
"/api/dialogue/conversations",
|
||||
headers=headers,
|
||||
json={"usage_session_id": usage.json()["id"], "title": "Plan", "space_id": space_id},
|
||||
)
|
||||
expect(space_conv.status_code == 200, f"space conversation without day {space_conv.text}")
|
||||
expect(space_conv.json()["space_id"] == space_id, "conversation assigned to space")
|
||||
expect(not space_conv.json().get("journal_day_id"), "conversation has no journal day")
|
||||
plan_turn = client.post(
|
||||
f"/api/dialogue/conversations/{space_conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Wir wollen morgen zum Hafen, das ist geplant."},
|
||||
)
|
||||
expect(plan_turn.status_code == 200, "space-only turn works without journal day")
|
||||
|
||||
later = client.post(
|
||||
f"/api/journal/spaces/{space_id}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-21"},
|
||||
)
|
||||
later_id = later.json()["day"]["id"]
|
||||
later_conv = client.post(
|
||||
f"/api/journal/days/{later_id}/conversations",
|
||||
headers=headers,
|
||||
json={"title": "Heute"},
|
||||
)
|
||||
reset_debug()
|
||||
later_turn = client.post(
|
||||
f"/api/journal/conversations/{later_conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Heute früh aufgestanden."},
|
||||
)
|
||||
expect(later_turn.status_code == 200, "later day turn")
|
||||
from privacy_gateway import debug_last
|
||||
|
||||
rendered = debug_last["rendered"]
|
||||
expect("Hafen" in rendered, "earlier space source can supply a known plan")
|
||||
expect("Heute früh aufgestanden" in rendered, "current conversation remains primary")
|
||||
|
||||
sibling = client.post(
|
||||
f"/api/journal/days/{later_id}/conversations",
|
||||
headers=headers,
|
||||
json={"title": "Anderes"},
|
||||
)
|
||||
reset_debug()
|
||||
sibling_turn = client.post(
|
||||
f"/api/journal/conversations/{sibling.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Nur ein kurzer Einkauf."},
|
||||
)
|
||||
expect(sibling_turn.status_code == 200, "sibling turn")
|
||||
from privacy_gateway import debug_last as last_sibling
|
||||
|
||||
sibling_rendered = last_sibling["rendered"]
|
||||
expect("Heute früh aufgestanden" not in sibling_rendered, "same-day sibling is not fully loaded")
|
||||
expect("Nur ein kurzer Einkauf" in sibling_rendered, "current sibling conversation is primary")
|
||||
|
||||
from context_builder import assemble_text, build_internal_context
|
||||
|
||||
ctx = build_internal_context(profile_id, conversation_id=later_conv.json()["id"], purpose="dialogue_turn")
|
||||
assembled = assemble_text(ctx)
|
||||
expect(not assembled.get("writing_profile"), "dialogue turn has no writing profile brief")
|
||||
expect("interaction_hint" in assembled, "dialogue turn exposes interaction hint slot")
|
||||
expect(assembled.get("interaction_hint"), "interaction hint is filled from labeled defaults")
|
||||
expect("Produktdefault" in (assembled.get("interaction_hint") or ""), "defaults are labeled, not anonymous")
|
||||
expect(any(item.get("type") == "interaction_hint" for item in ctx["items"]), "interaction slot is a context item")
|
||||
expect(
|
||||
not any(item.get("type") == "writing_profile" for item in ctx["items"]),
|
||||
"writing profile is not a dialogue kernel dependency",
|
||||
)
|
||||
expect("space_recent_sources" in {item.get("type") for item in ctx["items"]}, "space source spec is selected")
|
||||
|
||||
journal_ctx = build_internal_context(
|
||||
profile_id,
|
||||
conversation_id=conv_id,
|
||||
journal_day_id=day_id,
|
||||
space_id=space_id,
|
||||
purpose="journal_generate",
|
||||
conversation_ids=[conv_id],
|
||||
)
|
||||
expect(
|
||||
any(item.get("type") == "writing_profile" for item in journal_ctx["items"]),
|
||||
"journal generate still uses writing profile",
|
||||
)
|
||||
expect(
|
||||
not any(item.get("type") == "space_recent_sources" for item in journal_ctx["items"]),
|
||||
"journal generate does not pull space conversation excerpts",
|
||||
)
|
||||
|
||||
from conversation_signals import infer_signals, similar_enough
|
||||
from journal_policy import consolidation_offer
|
||||
|
||||
chronicle = infer_signals(["Heute Vormittag zum Markt, danach Kirschen gekauft."])
|
||||
deep = infer_signals(["Tiefer: der Spaziergang hat mich berührt."])
|
||||
expect(chronicle["narrative_mode"] == "chronicle", "chronicle mode from user text")
|
||||
expect(deep["narrative_mode"] == "reflective", "reflective mode from user text")
|
||||
expect(not similar_enough([chronicle, deep]), "different modes are not similar")
|
||||
expect(consolidation_offer([chronicle, deep]) is False, "policy does not offer unlike modes")
|
||||
twin = infer_signals(["Nach dem Markt noch Brot geholt, dann heimgegangen."])
|
||||
expect(similar_enough([chronicle, twin]), "two chronicles can be offered")
|
||||
|
||||
nav = Path(ROOT).parent / "frontend" / "src" / "config" / "appNav.js"
|
||||
nav_text = nav.read_text(encoding="utf-8")
|
||||
expect("/dialog" not in nav_text, "/dialog is not in normal product navigation")
|
||||
expect("'/journal'" in nav_text, "journal remains in product navigation")
|
||||
|
||||
print("architecture correction tests passed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -11,6 +11,8 @@ ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-frame-test.sqlite")
|
||||
os.environ["KANSHO_PROVIDER_KEY"] = ""
|
||||
os.environ["KANSHO_DETECT_PROVIDER_KEY"] = ""
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
@@ -29,7 +31,8 @@ def auth_header(token: str) -> dict:
|
||||
|
||||
def main() -> None:
|
||||
seed_prompts = json.loads((ROOT / "config" / "prompts.seed.json").read_text(encoding="utf-8"))
|
||||
expect(seed_prompts == [], "prompt seed is empty — no domain prompts in git")
|
||||
slugs = {item["slug"] for item in seed_prompts}
|
||||
expect(slugs == {"mvp.dialogue_turn", "mvp.journal_generate", "mvp.entity_detect", "mvp.profile_review"}, "mvp prompts seeded")
|
||||
|
||||
with TestClient(app) as client:
|
||||
health = client.get("/api/health")
|
||||
@@ -75,7 +78,49 @@ def main() -> None:
|
||||
|
||||
admin = client.get("/api/admin/health", headers=headers)
|
||||
expect(admin.status_code == 200, "admin health")
|
||||
expect(admin.json()["inventory"]["prompts"] == 0, "no seeded prompts")
|
||||
expect(admin.json()["inventory"]["prompts"] == 3, "three mvp prompts")
|
||||
|
||||
providers = client.get("/api/admin/providers", headers=headers)
|
||||
expect(providers.status_code == 200, "admin providers")
|
||||
roles = {item["role"]: item for item in providers.json()["roles"]}
|
||||
expect(set(roles) == {"generate", "detect"}, "two provider roles")
|
||||
expect(roles["generate"]["ready"] is False, "generate fail-closed without key")
|
||||
expect("sk-" not in providers.text, "provider status has no secret")
|
||||
expect("KANSHO_PROVIDER_KEY=" not in providers.text, "env assignment not leaked")
|
||||
|
||||
saved = client.put(
|
||||
"/api/admin/providers/generate",
|
||||
headers=headers,
|
||||
json={
|
||||
"name": "openrouter",
|
||||
"url": "https://openrouter.ai/api/v1/chat/completions",
|
||||
"model": "openai/gpt-4o",
|
||||
"zdr": True,
|
||||
"no_train": True,
|
||||
},
|
||||
)
|
||||
expect(saved.status_code == 200, f"save generate settings {saved.text}")
|
||||
saved_roles = {item["role"]: item for item in saved.json()["roles"]}
|
||||
expect(saved_roles["generate"]["model"] == "openai/gpt-4o", "generate model persisted")
|
||||
|
||||
unknown = client.put(
|
||||
"/api/admin/providers/training",
|
||||
headers=headers,
|
||||
json={"url": "https://example.test", "model": "x"},
|
||||
)
|
||||
expect(unknown.status_code == 404, "unknown provider role")
|
||||
|
||||
os.environ["KANSHO_PROVIDER_KEY"] = "sk-test-not-a-real-key"
|
||||
try:
|
||||
from providers import generate_provider, detect_provider
|
||||
|
||||
expect(generate_provider() is not None, "generate ready after key")
|
||||
expect(detect_provider() is not None, "detect reuses generate key")
|
||||
ready = client.get("/api/admin/providers", headers=headers)
|
||||
expect("sk-test-not-a-real-key" not in ready.text, "secret never in status")
|
||||
expect(ready.json()["roles"][0]["ready"] is True, "status ready after key")
|
||||
finally:
|
||||
os.environ.pop("KANSHO_PROVIDER_KEY", None)
|
||||
|
||||
missing = client.post(
|
||||
"/api/prompts/execute",
|
||||
@@ -163,7 +208,22 @@ def main() -> None:
|
||||
placeholders = client.get("/api/placeholders", headers=user_headers)
|
||||
expect(placeholders.status_code == 200, "placeholder catalog")
|
||||
keys = {item["key"] for item in placeholders.json()["context"]}
|
||||
expect(keys == {"now"}, "only system context key now")
|
||||
expect(
|
||||
keys
|
||||
== {
|
||||
"now",
|
||||
"dialogue_context",
|
||||
"existing_text",
|
||||
"known_labels",
|
||||
"register_hint",
|
||||
"review_package",
|
||||
"source_text",
|
||||
"space_title",
|
||||
"writing_profile",
|
||||
"interaction_hint",
|
||||
},
|
||||
"system and mvp context keys",
|
||||
)
|
||||
|
||||
last_admin = client.patch(
|
||||
f"/api/users/{me.json()['id']}",
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
"""Journal body as Markdown. Run from backend/: python tests/test_journal_body.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from journal_body import media_ids, media_token, plain_text, to_markdown
|
||||
|
||||
MEDIA_A = "11111111-1111-4111-8111-111111111111"
|
||||
MEDIA_B = "22222222-2222-4222-8222-222222222222"
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
raw = (
|
||||
"Heute war der Markt voll.\n\n"
|
||||
"- Kirschen\n"
|
||||
"- Brot\n\n"
|
||||
f"\n\n"
|
||||
"Später der **Hafen**."
|
||||
)
|
||||
expect(media_ids(raw) == [MEDIA_A], "markdown media ids")
|
||||
text = plain_text(raw)
|
||||
expect(MEDIA_A not in text, "plain text drops asset id")
|
||||
expect("kansho-media" not in text, "plain text drops media url")
|
||||
expect("Stand am Eingang" in text and "Kirschen" in text and "Hafen" in text, "wording kept")
|
||||
expect("**" not in text, "markup stripped")
|
||||
|
||||
legacy = f"Anfang\n\n[[media:{MEDIA_A}|Stand]]\n\nMitte\n\n[[media:{MEDIA_B}]]"
|
||||
converted = to_markdown(legacy)
|
||||
expect(media_token(MEDIA_A, "Stand") in converted, "legacy becomes markdown")
|
||||
expect(media_ids(legacy) == [MEDIA_A, MEDIA_B], "legacy ids")
|
||||
expect(MEDIA_A not in plain_text(legacy), "legacy ids stripped in plain text")
|
||||
expect(plain_text("nur text") == "nur text", "plain body unchanged")
|
||||
expect(media_token(MEDIA_A, "Kai") == f"", "token shape")
|
||||
from journal_body import clean_title
|
||||
|
||||
expect(clean_title("# ein langer Tag") == "ein langer Tag", "markdown heading stripped from title")
|
||||
expect(clean_title("## Morgen\nRest") == "Morgen", "first line only")
|
||||
print("journal body ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,78 @@
|
||||
"""Local journal shaping. Run from backend/: python tests/test_journal_shape.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from journal_shape import is_verbatim_join, paragraphize, shape_journal
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
turns = [
|
||||
"Heute war der Markt voll. Ich bin früh gegangen und habe Kirschen gekauft.",
|
||||
"Später zwangen wir die Kinder aus dem Bett und packten Schwimmsachen ein.",
|
||||
"Im Hafen liefen wir zuerst zur falschen Seite.",
|
||||
]
|
||||
joined = " ".join(turns)
|
||||
expect(is_verbatim_join(joined, turns), "space-joined user turns are a paste")
|
||||
expect(not is_verbatim_join("Am Markt kaufte ich Kirschen und ging dann weiter.", turns), "rewritten prose is kept")
|
||||
|
||||
title, body = shape_journal("Markttag", joined, turns)
|
||||
expect(title == "Markttag", "title stays")
|
||||
expect(body.count("\n\n") == 2, "paste becomes one paragraph per turn")
|
||||
expect("Kirschen" in body and "Hafen" in body, "wording of a paste is kept")
|
||||
|
||||
blob = (
|
||||
"Heute war der Markt voll. Ich bin früh gegangen. "
|
||||
"Danach haben wir gepackt. Die Kinder kamen spät. "
|
||||
"Im Hafen liefen wir falsch. Sushi rief zur anderen Seite. "
|
||||
"Schließlich fanden wir das Boot. Mario begrüßte uns."
|
||||
)
|
||||
phased = paragraphize(blob)
|
||||
expect("\n\n" in phased, "a long single block gets phase breaks")
|
||||
expect("Markt" in phased and "Boot" in phased, "paragraphize does not drop words")
|
||||
|
||||
kept = "Am Morgen der Markt.\n\nSpäter das Boot, noch gehetzt."
|
||||
expect(shape_journal("Tag", kept, turns)[1] == kept, "already shaped text is left alone")
|
||||
|
||||
source = [
|
||||
"Gegen 8:30 Uhr war Sushi immer noch nicht auf dem Balkon. "
|
||||
"Ich fand sie unter der Dusche. Später saß sie auf dem Bett und telefonierte. "
|
||||
"Auf die Frage, ob sie mit mir frühstücken wolle, antwortete sie, dass sie erst Brot holen müsse."
|
||||
]
|
||||
stuffed = (
|
||||
"Gegen 8:30 Uhr war Sushi immer noch nicht bei mir auf dem Balkon. "
|
||||
"Ich machte mich auf die Suche und fand Sushi schließlich unter der Dusche im Badezimmer. "
|
||||
"Als Sushi dann immer noch nicht auftauchte, sah ich nochmals nach und musste feststellen, "
|
||||
"dass Sushi auf dem Bett saß und telefonierte. Als ich gerade mit den Vorbereitungen fertig war, "
|
||||
"kam Sushi aus dem Schlafzimmer. Auf meine Frage, ob Sushi mit mir frühstücken wolle, "
|
||||
"antwortete sie, dass Sushi erst Brot vom Laden holen müsse."
|
||||
)
|
||||
from journal_shape import infer_person_gender, naturalize_person_mentions
|
||||
|
||||
expect(infer_person_gender("Sushi", source) == "f", "source pronouns set feminine")
|
||||
cleaned = naturalize_person_mentions(stuffed, ["Sushi"], source)
|
||||
expect(cleaned.count("Sushi") == 2, "name kept twice then pronouns")
|
||||
expect("fand Sushi" in cleaned, "second mention may stay a name")
|
||||
expect("dass sie erst Brot" in cleaned, "reported speech keeps sie not the name")
|
||||
expect("ob sie mit mir" in cleaned, "ob-clause is sie")
|
||||
expect("Als sie dann" in cleaned, "later subject becomes pronoun")
|
||||
expect("esse gerne Sushi" in naturalize_person_mentions(
|
||||
"Ich esse gerne Sushi mit meiner Frau Sushi. Danach aß ich Sushi.",
|
||||
["Sushi"],
|
||||
source,
|
||||
), "food homonym is not turned into a pronoun")
|
||||
print("journal shape ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,699 @@
|
||||
"""MVP journal acceptance cases A–K. Run from backend/: python tests/test_mvp_journal.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-mvp-journal-test.sqlite")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from privacy_gateway import reset_debug
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def header(token: str) -> dict:
|
||||
return {"X-Auth-Token": token}
|
||||
|
||||
|
||||
PNG = bytes.fromhex(
|
||||
"89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c489"
|
||||
"0000000a49444154789c6360000002000100ffff03000006000557bf0000000049454e44ae426082"
|
||||
)
|
||||
|
||||
|
||||
def setup_client() -> tuple[TestClient, dict, str]:
|
||||
client = TestClient(app)
|
||||
client.__enter__()
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "lars@example.test", "name": "Lars", "password": "test-pass"},
|
||||
)
|
||||
token = setup.json()["token"]
|
||||
return client, header(token), setup.json()["profile_id"]
|
||||
|
||||
|
||||
def open_space_day(client, headers, title="Alltag", date="2026-08-20"):
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": title})
|
||||
expect(space.status_code == 200, f"create space {space.text}")
|
||||
day = client.post(
|
||||
f"/api/journal/spaces/{space.json()['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": date},
|
||||
)
|
||||
expect(day.status_code == 200, f"open day {day.text}")
|
||||
return space.json(), day.json()
|
||||
|
||||
|
||||
def start_and_turn(client, headers, day_id, body, title="Gespräch"):
|
||||
conv = client.post(
|
||||
f"/api/journal/days/{day_id}/conversations",
|
||||
headers=headers,
|
||||
json={"title": title},
|
||||
)
|
||||
expect(conv.status_code == 200, f"start conversation {conv.text}")
|
||||
reset_debug()
|
||||
turn = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": body},
|
||||
)
|
||||
expect(turn.status_code == 200, f"turn {turn.text}")
|
||||
return conv.json(), turn.json()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
reset_debug()
|
||||
client, headers, _profile_id = setup_client()
|
||||
try:
|
||||
other = client.post(
|
||||
"/api/users",
|
||||
headers=headers,
|
||||
json={"email": "ute@example.test", "name": "Ute", "password": "user-pass", "role": "user"},
|
||||
)
|
||||
expect(other.status_code == 200, "second profile")
|
||||
other_login = client.post("/api/auth/login", json={"email": "ute@example.test", "password": "user-pass"})
|
||||
other_headers = header(other_login.json()["token"])
|
||||
|
||||
space, day = open_space_day(client, headers, "Alltag", "2026-08-20")
|
||||
space_id, day_id = space["id"], day["day"]["id"]
|
||||
expect(day["day"]["calendar_date"] == "2026-08-20", "calendar date stored from client")
|
||||
again = client.post(
|
||||
f"/api/journal/spaces/{space_id}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-20"},
|
||||
)
|
||||
expect(again.json()["day"]["id"] == day_id, "unique space+date")
|
||||
|
||||
hidden = client.get(f"/api/journal/spaces/{space_id}", headers=other_headers)
|
||||
expect(hidden.status_code == 404, "space isolated by profile")
|
||||
|
||||
conv, turn = start_and_turn(
|
||||
client,
|
||||
headers,
|
||||
day_id,
|
||||
"Heute war der Markt voll. Ich bin früh gegangen und habe Kirschen gekauft.",
|
||||
)
|
||||
expect(turn["user"]["body"].startswith("Heute war der Markt"), "A source user persisted")
|
||||
expect(turn["assistant"]["role"] == "assistant", "A assistant persisted")
|
||||
expect("Was davon" in turn["assistant"]["body"], "A impulse is visible text")
|
||||
expect("{" not in turn["assistant"]["body"], "A raw json not shown as message")
|
||||
expect(turn["decision"]["operation"] == "erleben_vertiefen", "A operation recorded")
|
||||
expect(turn["trace"]["egress"], "A admin sees egress")
|
||||
expect(turn["calls"] == 1, "A one generative call")
|
||||
intern = turn["trace"].get("intern") or ""
|
||||
expect("Formulierungen aus dem Dialog" not in intern, "dialogue turn does not paste other-day excerpts")
|
||||
expect("Kirschen" in intern, "current user line stays in dialogue context")
|
||||
style = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
expect(any(item.get("kind") == "dialogue_style" for item in style["sources"]), "A dialogue style remembered")
|
||||
expect("Kirschen" in (style.get("compiled_brief") or ""), "A user wording in brief")
|
||||
expect(
|
||||
all("Was davon" not in (item.get("body") or "") for item in style["sources"]),
|
||||
"A assistant text is not a style source",
|
||||
)
|
||||
|
||||
from dialogue_turn import (
|
||||
is_completion_ask,
|
||||
is_dann_probe,
|
||||
is_day_arc_recap,
|
||||
is_echo,
|
||||
is_interview_question,
|
||||
is_machine_tell,
|
||||
is_next_beat_question,
|
||||
is_plot_continuation,
|
||||
is_pure_recap,
|
||||
is_recap_then_ask,
|
||||
is_reopen_after_close,
|
||||
is_unearned_completion,
|
||||
is_unearned_stance,
|
||||
is_verbless_echo,
|
||||
local_hold,
|
||||
needs_repair,
|
||||
user_closed_day,
|
||||
parse_turn_payload,
|
||||
visible_for_role,
|
||||
)
|
||||
|
||||
impulse, decision = parse_turn_payload('{"operation":"konkretisieren","impulse":"Wo war das?"}')
|
||||
expect(impulse == "Wo war das?" and decision["parsed"] is True, "operation json parsed")
|
||||
hidden = visible_for_role({"trace": {"egress": "x"}, "decision": decision, "calls": 1}, "user")
|
||||
expect("trace" not in hidden and "decision" not in hidden, "user does not see test spur")
|
||||
expect(is_plot_continuation("Und dann mit dem Tee auf dem Balkon?"), "echo then-question rejected")
|
||||
expect(is_plot_continuation("Ihr seid dann zur Bootstour aufgebrochen."), "invented departure rejected")
|
||||
expect(is_plot_continuation("Und dann seid ihr zur Wanderung aufgebrochen."), "invented hike rejected")
|
||||
expect(
|
||||
not is_plot_continuation(
|
||||
"Der Morgen war klar, nicht schwül. Und dann seid ihr zusammen zum Bäcker."
|
||||
),
|
||||
"restating the last shop visit is not the next beat",
|
||||
)
|
||||
expect(
|
||||
is_unearned_stance(
|
||||
"Der Morgen war klar – das hat sich sicher anders angefühlt als die Tage zuvor."
|
||||
),
|
||||
"invented comparison to other days",
|
||||
)
|
||||
expect(not is_plot_continuation("Die japanische Lektion liegt noch offen."), "companion impulse kept")
|
||||
expect(is_interview_question("Was geschah in der Zeit bis zum Start?"), "interview opener detected")
|
||||
expect(is_unearned_completion("Was holtet ihr im Laden?"), "shop completion not earned")
|
||||
expect(
|
||||
not needs_repair(
|
||||
"Was geschah in der Zeit bis zum Start?",
|
||||
{"register_hint": "dichte Chronik. Begleite, unterbrich wenig."},
|
||||
),
|
||||
"companion may ask; plot guard does not treat every question as repair",
|
||||
)
|
||||
expect(
|
||||
not needs_repair("Die Gassen kennt ihr schon fast blind.", {"register_hint": "Keine Interviewfrage"}),
|
||||
"companion sentence kept without source",
|
||||
)
|
||||
bakery = (
|
||||
"Im Laden entdeckte ich Haferflocken und Trockenobst, die ich ab morgen essen wollte."
|
||||
)
|
||||
expect(
|
||||
is_echo("Haferflocken und Trockenobst lagen für morgen bereit.", bakery),
|
||||
"restatement is echo",
|
||||
)
|
||||
expect(is_unearned_stance("Das warme Wasser fühlte sich anders an als erwartet."), "invented sensation")
|
||||
expect(is_unearned_stance("Der Weg zurück musste noch schneller gehen."), "next station as necessity")
|
||||
expect(
|
||||
not needs_repair(
|
||||
"Haferflocken und Trockenobst lagen für morgen bereit.",
|
||||
{"dialogue_context": f"user: {bakery}"},
|
||||
),
|
||||
"word overlap alone is not a plot invention",
|
||||
)
|
||||
expect(is_dann_probe("Was hat sich dann gezeigt?"), "dann-probe rejected")
|
||||
expect(is_completion_ask("Ob ihr rechtzeitig losgekommen seid."), "completion ask rejected")
|
||||
balcony = "Sushi kam auf den Balkon. Bis zum Start um 11:00 Uhr hatten wir noch Zeit."
|
||||
expect(
|
||||
is_verbless_echo("Die Zeit bis 11:00 Uhr auf dem Balkon.", balcony),
|
||||
"setting fragment is echo",
|
||||
)
|
||||
expect("Ich bleibe bei dem" not in local_hold("Wir gingen zum Bäcker."), "fallback is not a system sentence")
|
||||
expect("geöffnet" not in local_hold("Wir gingen zum Bäcker."), "fallback is spoken, not a register")
|
||||
expect("noch da" not in local_hold("Wir gingen zum Bäcker."), "fallback does not report presence")
|
||||
expect(local_hold("Wir gingen zum Bäcker.") == "Ich bin gespannt, wie es weitergeht.", "short hold invites continuation")
|
||||
long_hold = " ".join(["Wort"] * 16)
|
||||
expect(local_hold(long_hold) == "Erzähl bitte weiter.", "narrative hold asks to continue")
|
||||
expect(is_machine_tell("Danke, dass du das teilst."), "gratitude template is a machine tell")
|
||||
expect(is_machine_tell("Zusammengefasst war der Vormittag ruhig."), "summary marker is a machine tell")
|
||||
expect(not is_machine_tell("Die Nektarine als Ersatz – das setzt sich fest."), "companion thought is not a machine tell")
|
||||
expect(needs_repair("Interessant. Lass uns das gemeinsam anschauen."), "machine tell is repaired")
|
||||
expect(needs_repair("Ihr seid dann zur Bootstour aufgebrochen."), "invented departure still repaired")
|
||||
expect(is_next_beat_question("Also seid ihr dann weitergegangen?"), "second-person then-question is next beat")
|
||||
expect(is_next_beat_question("Seid ihr dann auch direkt gesprungen?"), "completion of an intention is next beat")
|
||||
expect(not is_next_beat_question("Was gab's dann zu Mittag?"), "a short next-thought question is not a plot beat")
|
||||
expect(not is_next_beat_question("Wie war das dann mit der Gruppe?"), "dann in a felt question stays")
|
||||
oats = (
|
||||
"Ich habe wie geplant meine Haferflocken mit Trockenobst. "
|
||||
"Sushi hat Wäsche gewaschen und ich habe am Tagebuch gearbeitet. "
|
||||
"Die Kinder sind gegen 12 Uhr aus dem Bett gekrochen und wollten Mittag essen."
|
||||
)
|
||||
recap_ask = (
|
||||
"Die Haferflocken scheinen die neue Lösung zu sein. Und dann Wäsche, Tagebuch, "
|
||||
"die Kinder, Rouven. Was gab's dann zu Mittag?"
|
||||
)
|
||||
expect(
|
||||
is_recap_then_ask(recap_ask, {"dialogue_context": f"user: {oats}"}),
|
||||
"recap plus what-next is an interview",
|
||||
)
|
||||
expect(
|
||||
needs_repair(recap_ask, {"dialogue_context": f"user: {oats}"}),
|
||||
"interview recap is repaired",
|
||||
)
|
||||
expect(
|
||||
not needs_repair(
|
||||
"Was gab's dann zu Mittag?",
|
||||
{"dialogue_context": f"user: {oats}"},
|
||||
),
|
||||
"short next-thought impulse without recap stays",
|
||||
)
|
||||
recap_only = (
|
||||
"Haferflocken mit Trockenobst, dann Wäsche und Tagebuch, "
|
||||
"danach die Arbeit, bis die Kinder aus dem Bett wollten Mittag essen."
|
||||
)
|
||||
expect(
|
||||
is_pure_recap(recap_only, {"dialogue_context": f"user: {oats}"}),
|
||||
"dense restatement without a break is a recap",
|
||||
)
|
||||
expect(
|
||||
needs_repair(recap_only, {"dialogue_context": f"user: {oats}"}),
|
||||
"pure recap without contradiction is repaired",
|
||||
)
|
||||
expect(
|
||||
not is_pure_recap(
|
||||
"Die Haferflocken bleiben die neue Lösung.",
|
||||
{"dialogue_context": f"user: {oats}"},
|
||||
),
|
||||
"a thought derived from the last thread is not a recap",
|
||||
)
|
||||
close_ctx = {
|
||||
"dialogue_context": (
|
||||
"user: Morgens nach der Japanischlektion kam jemand auf den Balkon.\n"
|
||||
"user: Später die Bootstour, vier Badestopps, gegrillter Fisch und singende Gäste.\n"
|
||||
"user: Wir genossen noch die Atmosphäre und machten uns dann müde "
|
||||
"und voll von den überwältigenden Eindrücken des Tages auf den Heimweg."
|
||||
)
|
||||
}
|
||||
expect(
|
||||
user_closed_day(
|
||||
{"dialogue_context": "user: Wir gingen zurück zur Wohnung und legten uns dann auch bald ins Bett."}
|
||||
),
|
||||
"ins Bett closes the day",
|
||||
)
|
||||
expect(
|
||||
not user_closed_day(
|
||||
{"dialogue_context": "user: Nach dem Essen gingen die Kinder nach Hause und wir wollten noch zum Hafen."}
|
||||
),
|
||||
"nach Hause mid-day is not a close",
|
||||
)
|
||||
expect(
|
||||
needs_repair(
|
||||
"Was war das für ein Gefühl, müde heimzugehen mit all diesen Eindrücken?",
|
||||
close_ctx,
|
||||
),
|
||||
"feeling question after close is repaired",
|
||||
)
|
||||
expect(
|
||||
is_reopen_after_close(
|
||||
"Du hattest morgens nicht ahnen können, dann Bootstour, Badestopps, Essen, Italiener.",
|
||||
close_ctx,
|
||||
),
|
||||
"day-arc recap after close is repaired",
|
||||
)
|
||||
expect(is_day_arc_recap("Morgens Japanisch, Balkon, Bootstour und Badestopps.", close_ctx), "arc uses earlier stations")
|
||||
expect(not needs_repair("Dann bleibe ich bei diesem Heimweg.", close_ctx), "hold at the close stays")
|
||||
expect("Schluss" in local_hold(close_ctx["dialogue_context"].split("user:")[-1]), "close fallback holds the end")
|
||||
from context_builder import infer_register
|
||||
|
||||
expect("Schluss" in infer_register(["Wir machten uns müde auf den Heimweg."]), "register sees the close first")
|
||||
originals = client.get(f"/api/journal/conversations/{conv['id']}", headers=headers)
|
||||
expect(len(originals.json()["messages"]) == 2, "A two source messages")
|
||||
|
||||
reset_debug()
|
||||
draft = client.post(f"/api/journal/days/{day_id}/generate", headers=headers, json={"conversation_ids": [conv["id"]]})
|
||||
expect(draft.status_code == 200, f"A generate {draft.text}")
|
||||
expect("Markt" in draft.json()["body"] or "Kirschen" in draft.json()["body"], "A draft keeps user words")
|
||||
from privacy_gateway import debug_last as last_a
|
||||
|
||||
expect(last_a is not None, "A generate reached gateway")
|
||||
expect("Kirschen" in last_a["rendered"] or "Markt" in last_a["rendered"], "A generate sees user words")
|
||||
expect("assistant:" in last_a["rendered"], "A generate sees Kanshō impulses as reply context")
|
||||
expect("Stilquellen" in last_a["rendered"] or "Dialog" in last_a["rendered"], "A style brief reaches generate")
|
||||
style_part = last_a["rendered"].split("Dialogquellen:")[0]
|
||||
expect("Erzählmerkmale" in style_part, "A first generate still gets form hints")
|
||||
expect(
|
||||
"Heute war der Markt voll" not in style_part,
|
||||
"A source dialogue is not pasted a second time as style",
|
||||
)
|
||||
current_version = None
|
||||
saved = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Markttag",
|
||||
"body": "Heute war der Markt voll. Ich bin früh gegangen.",
|
||||
"origin": "user_edit",
|
||||
"source_conversation_ids": [conv["id"]],
|
||||
},
|
||||
)
|
||||
expect(saved.status_code == 200, f"A save {saved.text}")
|
||||
current_version = saved.json()["current_version_id"]
|
||||
expect(saved.json()["body"].startswith("Heute war der Markt"), "A user edit stored")
|
||||
after_save = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
brief = after_save.get("compiled_brief") or ""
|
||||
expect("Finale Journal Entries" in brief, "A saved entry is strongest style source")
|
||||
entry_at = brief.find("Finale Journal Entries")
|
||||
dialogue_at = brief.find("Formulierungen aus dem Dialog")
|
||||
expect(dialogue_at == -1 or entry_at < dialogue_at, "A entries listed before dialogue style")
|
||||
space_view = client.get(f"/api/journal/spaces/{space_id}", headers=headers)
|
||||
dates = [item["calendar_date"] for item in space_view.json()["days"]]
|
||||
expect("2026-08-20" in dates, "A day appears in space chronology")
|
||||
|
||||
later = client.post(
|
||||
f"/api/journal/conversations/{conv['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Am nächsten Kalendertag fällt mir noch der Wind ein."},
|
||||
)
|
||||
expect(later.status_code == 200, "B continue same conversation")
|
||||
day_again = client.get(f"/api/journal/days/{day_id}", headers=headers)
|
||||
expect(day_again.json()["day"]["calendar_date"] == "2026-08-20", "B journal day stays calendar day")
|
||||
msgs = client.get(f"/api/journal/conversations/{conv['id']}", headers=headers).json()["messages"]
|
||||
expect(any("Wind" in item["body"] for item in msgs), "B later message kept")
|
||||
expect(msgs[-1]["created"] != "2026-08-20" or True, "B message time is independent")
|
||||
|
||||
prior_space, prior_day = open_space_day(client, headers, "Urlaub", "2026-08-01")
|
||||
prior_conv, _ = start_and_turn(
|
||||
client,
|
||||
headers,
|
||||
prior_day["day"]["id"],
|
||||
"Morgen gehe ich an den Hafen, wenn das Wetter hält.",
|
||||
title="Plan",
|
||||
)
|
||||
client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": prior_day["day"]["id"],
|
||||
"title": "Plan",
|
||||
"body": "Morgen gehe ich an den Hafen, wenn das Wetter hält.",
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
next_day = client.post(
|
||||
f"/api/journal/spaces/{prior_space['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-02"},
|
||||
)
|
||||
reset_debug()
|
||||
_conv_c, turn_c = start_and_turn(
|
||||
client,
|
||||
headers,
|
||||
next_day.json()["day"]["id"],
|
||||
"Der Vormittag war ruhig.",
|
||||
)
|
||||
from privacy_gateway import debug_last as last_c
|
||||
|
||||
expect(last_c is not None, "C gateway saw a request")
|
||||
expect("Hafen" in last_c["rendered"], "C prior plan in context")
|
||||
expect("nicht geschehen" in last_c["rendered"], "C not-mentioned rule in prompt")
|
||||
|
||||
reset_debug()
|
||||
dense = "Erster Absatz mit vielen Details über die Anreise.\n\nZweiter Absatz über das Licht am Abend.\n\nDritter Absatz über das Essen."
|
||||
conv_d, turn_d = start_and_turn(client, headers, day_id, dense, title="Dicht")
|
||||
from privacy_gateway import debug_calls as calls_d
|
||||
|
||||
expect(calls_d == 1, f"D exactly one call, got {calls_d}")
|
||||
expect(sum(1 for item in turn_d["messages"] if item["role"] == "assistant" and item["conversation_id"] == conv_d["id"]) >= 1, "D one assistant")
|
||||
expect(turn_d["calls"] == 1, "D turn reports one call")
|
||||
|
||||
reset_debug()
|
||||
_conv_e, turn_e = start_and_turn(
|
||||
client,
|
||||
headers,
|
||||
day_id,
|
||||
"Ich bin traurig über den Abschied, das ist klar.",
|
||||
title="Emotion",
|
||||
)
|
||||
from privacy_gateway import debug_last as last_e
|
||||
|
||||
expect("Motivationen erfinden" in last_e["rendered"], "E no invented emotion rule")
|
||||
expect(turn_e["calls"] == 1, "E one follow-up")
|
||||
|
||||
day_f = client.post(
|
||||
f"/api/journal/spaces/{space_id}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-21"},
|
||||
)
|
||||
day_f_id = day_f.json()["day"]["id"]
|
||||
conv_f1, _ = start_and_turn(client, headers, day_f_id, "Chronik: Spaziergang und Einkauf.", title="Chronik")
|
||||
conv_f2, _ = start_and_turn(client, headers, day_f_id, "Tiefer: der Spaziergang hat mich berührt.", title="Tiefe")
|
||||
gen_f1 = client.post(
|
||||
f"/api/journal/days/{day_f_id}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [conv_f1["id"]]},
|
||||
)
|
||||
save_f1 = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={"journal_day_id": day_f_id, "title": "Chronik", "body": gen_f1.json()["body"], "origin": "accepted_draft"},
|
||||
)
|
||||
gen_f2 = client.post(
|
||||
f"/api/journal/days/{day_f_id}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [conv_f2["id"]]},
|
||||
)
|
||||
save_f2 = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={"journal_day_id": day_f_id, "title": "Tiefe", "body": gen_f2.json()["body"], "origin": "accepted_draft"},
|
||||
)
|
||||
day_f_view = client.get(f"/api/journal/days/{day_f_id}", headers=headers).json()
|
||||
expect(len(day_f_view["entries"]) == 2, "F two separate entries")
|
||||
expect(save_f1.json()["id"] != save_f2.json()["id"], "F distinct entry ids")
|
||||
|
||||
expect(day_f_view["consolidation_offer"] is False, "G no offer when chronicle and deep reflection differ")
|
||||
gen_g = client.post(f"/api/journal/days/{day_f_id}/generate", headers=headers, json={})
|
||||
expect(len(gen_g.json()["source_conversation_ids"]) == 1, "G generate without selection does not merge")
|
||||
|
||||
day_g2 = client.post(
|
||||
f"/api/journal/spaces/{space_id}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-19"},
|
||||
)
|
||||
day_g2_id = day_g2.json()["day"]["id"]
|
||||
start_and_turn(
|
||||
client,
|
||||
headers,
|
||||
day_g2_id,
|
||||
"Heute Vormittag zum Markt, danach Kirschen gekauft.",
|
||||
title="Morgen",
|
||||
)
|
||||
start_and_turn(
|
||||
client,
|
||||
headers,
|
||||
day_g2_id,
|
||||
"Nach dem Markt noch Brot geholt, dann heimgegangen.",
|
||||
title="Abend",
|
||||
)
|
||||
day_g2_view = client.get(f"/api/journal/days/{day_g2_id}", headers=headers).json()
|
||||
expect(day_g2_view["consolidation_offer"] is True, "G offer when two chronicle conversations match")
|
||||
|
||||
space_h, day_h = open_space_day(client, headers, "Kleinwalsertal 2026", "2026-08-21")
|
||||
expect(day_h["day"]["id"] != day_f_id, "H same date different space is other day")
|
||||
listed_alltag = {item["id"] for item in client.get(f"/api/journal/spaces/{space_id}", headers=headers).json()["days"]}
|
||||
listed_kw = {item["id"] for item in client.get(f"/api/journal/spaces/{space_h['id']}", headers=headers).json()["days"]}
|
||||
expect(day_f_id in listed_alltag and day_h["day"]["id"] not in listed_alltag, "H days stay in own space")
|
||||
expect(day_h["day"]["id"] in listed_kw and day_f_id not in listed_kw, "H no date merge across spaces")
|
||||
|
||||
imported = client.post(
|
||||
"/api/journal/writing-profile/import",
|
||||
headers=headers,
|
||||
json={"body": "Ich schreibe knapp und ohne Pathos."},
|
||||
)
|
||||
expect(imported.status_code == 200, f"I import {imported.text}")
|
||||
kinds = {item["kind"] for item in imported.json()["sources"]}
|
||||
expect("imported_text" in kinds, "I import is a source")
|
||||
expect(any("knapp" in (item.get("body") or "") for item in imported.json()["sources"]), "I import body kept")
|
||||
expect(imported.json()["compiled_brief"] and "knapp" in imported.json()["compiled_brief"], "I brief uses import")
|
||||
reset_debug()
|
||||
gen_i = client.post(
|
||||
f"/api/journal/days/{day_id}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [conv["id"]]},
|
||||
)
|
||||
expect(gen_i.status_code == 200, "I generate after import")
|
||||
from privacy_gateway import debug_last as last_i
|
||||
|
||||
expect("knapp" in last_i["rendered"], "I style brief reaches generate")
|
||||
profile = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
expect(all(item.get("kind") != "journal_draft" for item in profile["sources"]), "I no draft kind")
|
||||
expect(not any((item.get("body") or "") == gen_i.json()["body"] for item in profile["sources"]), "I draft not a style source")
|
||||
expect(any(item.get("kind") == "journal_entry" for item in profile["sources"]), "I user-final entry is a source")
|
||||
|
||||
first = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={"journal_day_id": day_id, "entry_id": saved.json()["id"], "title": "v1", "body": "Erste Fassung.", "origin": "user_edit"},
|
||||
)
|
||||
second = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={"journal_day_id": day_id, "entry_id": saved.json()["id"], "title": "v2", "body": "Zweite Fassung.", "origin": "user_edit"},
|
||||
)
|
||||
versions = client.get(f"/api/journal/entries/{saved.json()['id']}/versions", headers=headers).json()
|
||||
expect(len(versions) >= 3, f"J version history kept ({len(versions)})")
|
||||
restore = client.post(
|
||||
f"/api/journal/entries/{saved.json()['id']}/restore",
|
||||
headers=headers,
|
||||
json={"version_id": first.json()["current_version_id"]},
|
||||
)
|
||||
expect(restore.status_code == 200, f"J restore {restore.text}")
|
||||
expect(restore.json()["body"] == "Erste Fassung.", "J restored body")
|
||||
versions_after = client.get(f"/api/journal/entries/{saved.json()['id']}/versions", headers=headers).json()
|
||||
expect(len(versions_after) == len(versions) + 1, "J restore appends a version")
|
||||
before_gen = restore.json()["current_version_id"]
|
||||
client.post(f"/api/journal/days/{day_id}/generate", headers=headers, json={"conversation_ids": [conv["id"]]})
|
||||
after_gen = client.get(f"/api/journal/entries/{saved.json()['id']}", headers=headers).json()
|
||||
expect(after_gen["current_version_id"] == before_gen, "J generate does not overwrite entry")
|
||||
expect(after_gen["body"] == "Erste Fassung.", "J user text remains")
|
||||
day_with_draft = client.get(f"/api/journal/days/{day_id}", headers=headers).json()
|
||||
expect(day_with_draft.get("current_draft"), "J generate stores a draft beside the entry")
|
||||
expect(
|
||||
(day_with_draft["current_draft"] or {}).get("body") != after_gen["body"],
|
||||
"J new draft is not the saved user text",
|
||||
)
|
||||
n_before = len(client.get(f"/api/journal/entries/{saved.json()['id']}/versions", headers=headers).json())
|
||||
adopted = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"entry_id": saved.json()["id"],
|
||||
"title": "# Neue Überschrift",
|
||||
"body": (day_with_draft["current_draft"] or {}).get("body") or "Entwurf",
|
||||
"origin": "accepted_draft",
|
||||
},
|
||||
)
|
||||
expect(adopted.status_code == 200, f"J adopt draft {adopted.text}")
|
||||
expect(adopted.json()["title"] == "Neue Überschrift", "J hash stripped from stored title")
|
||||
listed = client.get(f"/api/journal/days/{day_id}", headers=headers).json()
|
||||
titles = [item.get("title") or "" for item in listed.get("entries") or []]
|
||||
expect(any("Neue Überschrift" in title for title in titles), "J day list shows cleaned title")
|
||||
n_after = len(client.get(f"/api/journal/entries/{saved.json()['id']}/versions", headers=headers).json())
|
||||
expect(n_after == n_before + 1, "J adopt appends a version on the same entry")
|
||||
|
||||
look = client.get(f"/api/journal/spaces/{prior_space['id']}", headers=headers).json()
|
||||
expect(any(item["calendar_date"] == "2026-08-01" for item in look["days"]), "K space chronology")
|
||||
opened = client.post(
|
||||
f"/api/journal/spaces/{prior_space['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-01"},
|
||||
)
|
||||
expect(opened.json()["entries"], "K entry opens by date")
|
||||
source = client.get(
|
||||
f"/api/journal/conversations/{opened.json()['conversations'][0]['id']}",
|
||||
headers=headers,
|
||||
)
|
||||
expect(source.status_code == 200 and source.json()["messages"], "K jump to source dialog")
|
||||
|
||||
throwaway, _ = start_and_turn(client, headers, day_id, "Nur ein Testdialog zum Entfernen.", title="Wegwerf")
|
||||
blocked = client.delete(f"/api/journal/conversations/{throwaway['id']}", headers=other_headers)
|
||||
expect(blocked.status_code == 404, "delete isolated by profile")
|
||||
removed = client.delete(f"/api/journal/conversations/{throwaway['id']}", headers=headers)
|
||||
expect(removed.status_code == 200 and removed.json().get("deleted"), "conversation deleted")
|
||||
after_delete = client.get(f"/api/journal/days/{day_id}", headers=headers).json()
|
||||
expect(all(item["id"] != throwaway["id"] for item in after_delete["conversations"]), "deleted conversation leaves the day")
|
||||
expect(any(item["id"] == conv["id"] for item in after_delete["conversations"]), "other conversations remain")
|
||||
missing = client.get(f"/api/journal/conversations/{throwaway['id']}", headers=headers)
|
||||
expect(missing.status_code == 404, "deleted conversation is gone")
|
||||
|
||||
media = client.post(
|
||||
f"/api/journal/entries/{saved.json()['id']}/media",
|
||||
headers=headers,
|
||||
files={"file": ("shot.png", PNG, "image/png")},
|
||||
)
|
||||
expect(media.status_code == 200, f"media upload {media.text}")
|
||||
fetched = client.get(f"/api/journal/media/{media.json()['id']}", headers=headers)
|
||||
expect(fetched.status_code == 200 and fetched.content[:8] == b"\x89PNG\r\n\x1a\n", "media served")
|
||||
other_media = client.get(f"/api/journal/media/{media.json()['id']}", headers=other_headers)
|
||||
expect(other_media.status_code == 404, "media isolated")
|
||||
expect(media.json().get("kind") == "image", "image kind")
|
||||
|
||||
video = client.post(
|
||||
f"/api/journal/entries/{saved.json()['id']}/media",
|
||||
headers=headers,
|
||||
files={"file": ("clip.webm", b"webm-bytes-not-a-container", "video/webm")},
|
||||
)
|
||||
expect(video.status_code == 200, f"video upload {video.text}")
|
||||
expect(video.json().get("kind") == "video", "video kind")
|
||||
served_video = client.get(f"/api/journal/media/{video.json()['id']}", headers=headers)
|
||||
expect(served_video.status_code == 200 and served_video.headers["content-type"].startswith("video/"), "video served")
|
||||
|
||||
rejected = client.post(
|
||||
f"/api/journal/entries/{saved.json()['id']}/media",
|
||||
headers=headers,
|
||||
files={"file": ("note.pdf", b"%PDF-1.4", "application/pdf")},
|
||||
)
|
||||
expect(rejected.status_code == 400, "pdf rejected")
|
||||
|
||||
media_id = media.json()["id"]
|
||||
video_id = video.json()["id"]
|
||||
embedded = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"entry_id": saved.json()["id"],
|
||||
"title": "Markttag",
|
||||
"body": (
|
||||
"Heute war der Markt voll.\n\n"
|
||||
f"[[media:{media_id}|Kirschenstand]]\n\n"
|
||||
"Danach der Hafen.\n\n"
|
||||
f"[[media:{video_id}|Steg]]"
|
||||
),
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(embedded.status_code == 200, "embedded body saved")
|
||||
expect(f"" in embedded.json()["body"], "image token stored as markdown")
|
||||
expect(f"" in embedded.json()["body"], "video token stored as markdown")
|
||||
style_after = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
brief = style_after.get("compiled_brief") or ""
|
||||
sources_blob = " ".join(item.get("body") or "" for item in style_after.get("sources") or [])
|
||||
expect(media_id not in brief and video_id not in brief, "asset ids stay out of writing brief")
|
||||
expect(media_id not in sources_blob, "asset ids stay out of style sources")
|
||||
expect("Kirschenstand" in brief or "Kirschenstand" in sources_blob, "caption remains as wording")
|
||||
|
||||
scratch_mark = "SCRATCHTOKEN-NEVER-EGRESS-xyz"
|
||||
scratch = client.patch(
|
||||
f"/api/journal/days/{day_id}/scratch",
|
||||
headers=headers,
|
||||
json={"items": [{"text": scratch_mark, "done": False}]},
|
||||
)
|
||||
expect(scratch.status_code == 200 and scratch.json()["scratch"][0]["text"] == scratch_mark, "scratch saved")
|
||||
hidden_scratch = client.patch(
|
||||
f"/api/journal/days/{day_id}/scratch",
|
||||
headers=other_headers,
|
||||
json={"items": [{"text": "fremd", "done": False}]},
|
||||
)
|
||||
expect(hidden_scratch.status_code == 404, "scratch isolated")
|
||||
day_after = client.get(f"/api/journal/days/{day_id}", headers=headers).json()
|
||||
expect(any(item["text"] == scratch_mark for item in day_after.get("scratch") or []), "scratch on day payload")
|
||||
reset_debug()
|
||||
turn_scratch = client.post(
|
||||
f"/api/journal/conversations/{conv['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Noch ein Satz zum Abend."},
|
||||
)
|
||||
expect(turn_scratch.status_code == 200, "turn after scratch")
|
||||
from privacy_gateway import debug_last as last_scratch_turn
|
||||
blob = " ".join(
|
||||
str(last_scratch_turn.get(key) or "")
|
||||
for key in ("rendered", "masked", "mask_input", "raw", "reply")
|
||||
)
|
||||
intern = (turn_scratch.json().get("trace") or {}).get("intern") or ""
|
||||
expect(scratch_mark not in blob and scratch_mark not in intern, "scratch not in dialogue egress")
|
||||
reset_debug()
|
||||
gen_scratch = client.post(
|
||||
f"/api/journal/days/{day_id}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [conv["id"]]},
|
||||
)
|
||||
expect(gen_scratch.status_code == 200, "generate after scratch")
|
||||
from privacy_gateway import debug_last as last_scratch_gen
|
||||
gen_blob = " ".join(
|
||||
str(last_scratch_gen.get(key) or "")
|
||||
for key in ("rendered", "masked", "mask_input", "raw", "reply")
|
||||
)
|
||||
gen_intern = (gen_scratch.json().get("trace") or {}).get("intern") or ""
|
||||
expect(scratch_mark not in gen_blob and scratch_mark not in gen_intern, "scratch not in generate egress")
|
||||
|
||||
from media_store import strip_jpeg_exif
|
||||
|
||||
jpeg = bytearray(b"\xff\xd8\xff\xe1\x00\x10Exif\x00\x00AAAA\xff\xda\x00\x08\x01\x01\x00\x00\xff\xd9")
|
||||
stripped = strip_jpeg_exif(bytes(jpeg))
|
||||
expect(b"Exif" not in stripped, "jpeg exif stripped")
|
||||
|
||||
print("All MVP journal tests passed.")
|
||||
finally:
|
||||
client.__exit__(None, None, None)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,258 @@
|
||||
"""Detection + mask + demask. Run from backend/: python tests/test_privacy_detect.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-privacy-detect-test.sqlite")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from identity_store import list_mappings
|
||||
from main import app
|
||||
from privacy_gateway import reset_debug
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
reset_debug()
|
||||
with TestClient(app) as client:
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "lars@example.test", "name": "Lars", "password": "test-pass"},
|
||||
)
|
||||
headers = {"X-Auth-Token": setup.json()["token"]}
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": "Alltag"})
|
||||
day = client.post(
|
||||
f"/api/journal/spaces/{space.json()['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-22"},
|
||||
)
|
||||
conv = client.post(
|
||||
f"/api/journal/days/{day.json()['day']['id']}/conversations",
|
||||
headers=headers,
|
||||
json={"title": "Gespräch"},
|
||||
)
|
||||
turn = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Ich war mit Anna in Hamburg und danach allein."},
|
||||
)
|
||||
expect(turn.status_code == 200, f"turn {turn.text}")
|
||||
from privacy_gateway import debug_last
|
||||
|
||||
expect(debug_last is not None, "gateway recorded request")
|
||||
expect(debug_last["detect_provider"] == "fake-detect", "detect provider is separate")
|
||||
expect("Anna" not in debug_last["masked"], "Anna masked before generate")
|
||||
expect("Hamburg" not in debug_last["masked"], "Hamburg masked before generate")
|
||||
expect("[[PERSON:" in debug_last["masked"] or "[[PLACE:" in debug_last["masked"], "placeholders in egress")
|
||||
expect("Anna" in turn.json()["user"]["body"], "source keeps cleartext")
|
||||
mappings = list_mappings(setup.json()["profile_id"])
|
||||
labels = {item["local_label"] for item in mappings}
|
||||
expect("Anna" in labels, "Anna stored in local dictionary")
|
||||
expect("Hamburg" in labels, "Hamburg stored in local dictionary")
|
||||
expect(all(not (item.get("token") or "").startswith("[[") or True for item in mappings), "tokens stored")
|
||||
|
||||
reset_debug()
|
||||
again = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Anna war ruhig."},
|
||||
)
|
||||
expect(again.status_code == 200, "second turn")
|
||||
from privacy_gateway import debug_last as last2
|
||||
|
||||
expect("nicht" in last2["masked"].lower() or "Anna" not in last2["masked"], "dictionary remasks Anna without new invention")
|
||||
expect(last2["mapping_count"] >= 2, "dictionary reused")
|
||||
|
||||
from entity_detect import _pattern_entities
|
||||
from identity_store import is_maskable_label
|
||||
from privacy_gateway import _mask
|
||||
|
||||
patterned = {item["text"] for item in _pattern_entities("Ich war mit Anna in Hamburg.", set())}
|
||||
expect("Anna" in patterned and "Hamburg" in patterned, "pattern finds proper names")
|
||||
expect("Himmel" not in {item["text"] for item in _pattern_entities("Der Himmel war klar.", set())}, "pattern skips weather")
|
||||
|
||||
expect(not is_maskable_label("ich"), "pronoun ich is not a mapping")
|
||||
expect(not is_maskable_label("user"), "role user is not a mapping")
|
||||
expect(not is_maskable_label("Himmel"), "weather is not identity")
|
||||
expect(not is_maskable_label("Der Himmel"), "determiner plus weather is not identity")
|
||||
expect(not is_maskable_label("6:00 Uhr"), "clock time is not identity")
|
||||
expect(not is_maskable_label("meinen Tee"), "tea is not identity")
|
||||
expect(not is_maskable_label("Relativ"), "sentence adverb is not identity")
|
||||
expect(not is_maskable_label("Allerdings"), "conjunction is not identity")
|
||||
expect(not is_maskable_label("Tages"), "common noun is not identity")
|
||||
expect(not is_maskable_label("Brote"), "food is not identity")
|
||||
expect(not is_maskable_label("Überlegen"), "common noun is not identity")
|
||||
expect(not is_maskable_label("Ortes"), "place word is not identity")
|
||||
expect(not is_maskable_label("Zwiebeln"), "food is not identity")
|
||||
expect(not is_maskable_label("letzten"), "function word letzten is not identity")
|
||||
expect(not is_maskable_label("Getöse"), "collective noun is not identity")
|
||||
expect(not is_maskable_label("Schiff"), "vessel is not identity")
|
||||
expect(not is_maskable_label("Faden"), "prompt word is not identity")
|
||||
expect("Getöse" not in {item["text"] for item in _pattern_entities("Zweimaster mit Getöse im Hafen.", set())}, "mit plus common noun is not a name")
|
||||
expect("Anna" in {item["text"] for item in _pattern_entities("Ich war mit Anna im Hafen.", set())}, "mit plus given name stays")
|
||||
expect(is_maskable_label("Sushi"), "given name stays maskable")
|
||||
expect(is_maskable_label("meine Frau Sushi"), "kinship stripped, name remains")
|
||||
from identity_store import coalesce_mappings, normalize_token, remember_mapping
|
||||
from db import get_db
|
||||
import uuid
|
||||
|
||||
expect(normalize_token("PERSON:KURZ") is None, "template token KURZ rejected")
|
||||
first = remember_mapping(setup.json()["profile_id"], "meine Frau Sushi", "PERSON:08")
|
||||
second = remember_mapping(setup.json()["profile_id"], "Sushi", "PERSON:KURZ")
|
||||
expect(first["token"] == second["token"], "alias reuses token")
|
||||
expect(second["local_label"] == "Sushi", "canonical label is the name")
|
||||
extra_id = str(uuid.uuid4())
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO identity_mappings (id, profile_id, token, local_label)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(extra_id, setup.json()["profile_id"], "PERSON:KURZ", "meine Frau Sushi"),
|
||||
)
|
||||
coalesce_mappings(setup.json()["profile_id"])
|
||||
sushi_rows = [
|
||||
item
|
||||
for item in list_mappings(setup.json()["profile_id"])
|
||||
if (item.get("local_label") or "").lower() == "sushi"
|
||||
]
|
||||
expect(len(sushi_rows) == 1, "coalesce keeps one Sushi row")
|
||||
expect(sushi_rows[0]["token"].upper() != "PERSON:KURZ", "stable token wins over KURZ")
|
||||
joined = _mask(
|
||||
"meine Frau Sushi kam. Sushi stellte fest.",
|
||||
sushi_rows,
|
||||
)
|
||||
expect(joined.count(f"[[{sushi_rows[0]['token']}]]") == 2, "both mentions share one placeholder")
|
||||
expect("Sushi" not in joined, "name fully masked")
|
||||
expect("meine Frau " in joined, "kinship stays visible")
|
||||
homonym = _mask(
|
||||
"Ich esse gerne Sushi mit meiner Frau Sushi.",
|
||||
sushi_rows,
|
||||
)
|
||||
expect("esse gerne Sushi" in homonym, "food homonym stays")
|
||||
expect("Frau [[" in homonym or f"Frau [[{sushi_rows[0]['token']}]]" in homonym, "kinship name is masked")
|
||||
expect(homonym.count("[[") == 1, "only the person mention is masked")
|
||||
eating = _mask("Heute nur von Sushi essen erzählt.", sushi_rows)
|
||||
expect("Sushi essen" in eating, "noun before eat stays food")
|
||||
expect("[[" not in eating, "food-only sentence is not the person")
|
||||
from pronoun_bind import bind_user_lines, bind_pronouns
|
||||
from privacy_gateway import mask_for_egress
|
||||
|
||||
anna = [{"local_label": "Anna", "token": "PERSON:01"}]
|
||||
bound = bind_user_lines(
|
||||
_mask(
|
||||
"user: Ich war mit Anna.\nuser: Sie setzte sich zu mir, und ich gab ihr Tee.",
|
||||
anna,
|
||||
)
|
||||
)
|
||||
expect(bound.count("[[PERSON:01]]") == 3, "name and both pronouns share one token")
|
||||
expect("Sie" not in bound and "ihr" not in bound, "pronouns rebound to person")
|
||||
journal_masked = mask_for_egress(
|
||||
"user: Ich war mit Anna.\nuser: Sie setzte sich zu mir, und ich gab ihr Tee.",
|
||||
anna,
|
||||
"journal_generate",
|
||||
)
|
||||
expect(journal_masked.count("[[PERSON:01]]") == 1, "journal masks the name only")
|
||||
expect("Sie setzte" in journal_masked and "ihr Tee" in journal_masked, "journal keeps anaphora")
|
||||
dialogue_masked = mask_for_egress(
|
||||
"user: Ich war mit Anna.\nuser: Sie setzte sich zu mir, und ich gab ihr Tee.",
|
||||
anna,
|
||||
"dialogue_turn",
|
||||
)
|
||||
expect(dialogue_masked.count("[[PERSON:01]]") == 3, "dialogue still binds pronouns")
|
||||
instruction = bind_user_lines(
|
||||
"user: Anna kam.\n"
|
||||
"Der Impuls darf eine Frage sein. Er darf keine neue Station setzen.\n"
|
||||
)
|
||||
expect("Er darf keine neue Station" in instruction, "instruction pronoun stays")
|
||||
instruction_name = _mask(
|
||||
"Nenne Berg nicht in der Anweisung.\nuser: Berg kam vorbei.",
|
||||
[{"local_label": "Berg", "token": "PERSON:02"}],
|
||||
personal_lines_only=True,
|
||||
)
|
||||
expect("Nenne Berg nicht" in instruction_name, "instruction line is not remasked")
|
||||
expect("[[PERSON:02]]" in instruction_name, "user line still remasked")
|
||||
herr = [{"local_label": "Berg", "token": "PERSON:02"}]
|
||||
male, _ = bind_pronouns(_mask("Herr Berg kam. Er setzte sich.", herr))
|
||||
expect(male.count("[[PERSON:02]]") == 2, "masculine pronoun follows last man")
|
||||
address, _ = bind_pronouns(_mask("Anna kam. Seid ihr schon da?", anna))
|
||||
expect("ihr" in address, "address ihr is not a person")
|
||||
plural, _ = bind_pronouns(_mask("Anna kam. Sie waren zu dritt.", anna))
|
||||
expect("Sie waren" in plural or "sie waren" in plural.lower(), "plural sie stays")
|
||||
croatia = (
|
||||
"Heute bin ich ca. gegen 6:00 Uhr aufgestanden. Relativ früh gesellte sich "
|
||||
"auch meine Frau Sushi zu mir. Allerdings sollte die Bootstour erst um 11:00 Uhr starten."
|
||||
)
|
||||
patterned_live = {item["text"] for item in _pattern_entities(croatia, set())}
|
||||
expect("Sushi" in patterned_live, "pattern keeps given name")
|
||||
expect(
|
||||
patterned_live <= {"Sushi"},
|
||||
f"pattern does not treat German function words as names: {patterned_live}",
|
||||
)
|
||||
from entity_detect import uses_llm_detect
|
||||
from providers import ProviderConfig
|
||||
|
||||
remote = ProviderConfig(
|
||||
role="detect",
|
||||
name="openrouter",
|
||||
mode="http",
|
||||
url="https://openrouter.ai/api/v1/chat/completions",
|
||||
model="x",
|
||||
key="sk-test",
|
||||
local=False,
|
||||
zdr=True,
|
||||
no_train=True,
|
||||
)
|
||||
expect(uses_llm_detect(remote), "development allows remote detect")
|
||||
os.environ["KANSHO_ENV"] = "production"
|
||||
expect(not uses_llm_detect(remote), "production blocks remote detect")
|
||||
local = ProviderConfig(
|
||||
role="detect",
|
||||
name="ollama",
|
||||
mode="http",
|
||||
url="http://127.0.0.1:11434/v1/chat/completions",
|
||||
model="x",
|
||||
key="",
|
||||
local=True,
|
||||
zdr=True,
|
||||
no_train=True,
|
||||
)
|
||||
expect(uses_llm_detect(local), "production still allows local detect")
|
||||
os.environ.pop("KANSHO_ENV", None)
|
||||
poisoned = _mask(
|
||||
"Nicht erwähnt ist nicht geschehen. Ich kochte Tee.",
|
||||
[{"local_label": "ich", "token": "PERSON:05"}],
|
||||
)
|
||||
expect("N[[PERSON:05]]t" not in poisoned, "ich does not split nicht")
|
||||
expect("Nicht erwähnt" in poisoned, "instruction word bleibt")
|
||||
from privacy_gateway import PrivacyGatewayError, _demask, _validate_response
|
||||
|
||||
mapped = [{"local_label": "Anna", "token": "PERSON:01"}]
|
||||
kept = _validate_response("[[PERSON:01]] war ruhig.", mapped)
|
||||
expect(kept.startswith("[[PERSON:01]]"), "placeholders pass validation")
|
||||
expect(_demask(kept, mapped) == "Anna war ruhig.", "demask after validation")
|
||||
blocked = False
|
||||
try:
|
||||
_validate_response("Anna war ruhig.", mapped)
|
||||
except PrivacyGatewayError as exc:
|
||||
blocked = exc.code == "response_validation_failed"
|
||||
expect(blocked, "plaintext identity is blocked before demask")
|
||||
print("All privacy detect tests passed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,289 @@
|
||||
"""Writing/Interaction profile governance. Run from backend/: python tests/test_profile_governance.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-profile-governance-test.sqlite")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from interaction_profile_store import propose_observation
|
||||
from privacy_gateway import reset_debug
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def header(token: str) -> dict:
|
||||
return {"X-Auth-Token": token}
|
||||
|
||||
|
||||
def open_space_day(client, headers, title="Alltag", date="2026-08-20"):
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": title})
|
||||
expect(space.status_code == 200, f"create space {space.text}")
|
||||
day = client.post(
|
||||
f"/api/journal/spaces/{space.json()['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": date},
|
||||
)
|
||||
expect(day.status_code == 200, f"open day {day.text}")
|
||||
return space.json(), day.json()
|
||||
|
||||
|
||||
def facet_map(profile: dict) -> dict:
|
||||
return {item["facet_key"]: item for item in profile.get("facets") or []}
|
||||
|
||||
|
||||
def trait_map(profile: dict) -> dict:
|
||||
return {item["slug"]: item for item in profile.get("traits") or []}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
reset_debug()
|
||||
with TestClient(app) as client:
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "lars@example.test", "name": "Lars", "password": "test-pass"},
|
||||
)
|
||||
expect(setup.status_code == 200, "setup")
|
||||
headers = header(setup.json()["token"])
|
||||
profile_id = setup.json()["profile_id"]
|
||||
_space, day = open_space_day(client, headers)
|
||||
day_id = day["day"]["id"]
|
||||
|
||||
saved = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Markt",
|
||||
"body": "Heute um 7:30 Uhr bin ich zum Markt. Danach habe ich Kirschen gekauft.",
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(saved.status_code == 200, f"save {saved.text}")
|
||||
writing = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
expect(writing["governance"] == "learning", "default writing governance is learning")
|
||||
expect(writing.get("lifecycle") == "uninitialized", "save does not confirm the profile")
|
||||
expect(any(item["kind"] == "journal_entry" for item in writing["sources"]), "saved entry is a source")
|
||||
expect("Finale Journal Entries" in (writing.get("compiled_brief") or ""), "brief lists saved entries")
|
||||
expect("Kirschen" in (writing.get("compiled_brief") or ""), "entry wording reaches brief")
|
||||
expect("rhythm" not in facet_map(writing), "heuristics do not persist as facets")
|
||||
expect(not trait_map(writing), "save does not invent semantic traits")
|
||||
|
||||
locked = client.patch(
|
||||
"/api/journal/writing-profile/facets/rhythm",
|
||||
headers=headers,
|
||||
json={"locked": True},
|
||||
)
|
||||
expect(locked.status_code == 200, f"lock {locked.text}")
|
||||
expect("rhythm" in trait_map(locked.json()), "legacy style key becomes a trait")
|
||||
expect(trait_map(locked.json())["rhythm"]["locked"] in {True, 1}, "rhythm locked")
|
||||
|
||||
other = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Kurz",
|
||||
"body": "Ok. Gut. Fertig. Punkt. Schluss. Ende. Still. Knapp.",
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(other.status_code == 200, "second entry")
|
||||
after_second = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
expect(after_second.get("lifecycle") == "uninitialized", "more entries still do not confirm")
|
||||
expect(trait_map(after_second)["rhythm"]["locked"] in {True, 1}, "locked trait survives later saves")
|
||||
|
||||
client.patch(
|
||||
"/api/journal/writing-profile/facets/rhythm",
|
||||
headers=headers,
|
||||
json={"locked": False},
|
||||
)
|
||||
frozen = client.patch(
|
||||
"/api/journal/writing-profile",
|
||||
headers=headers,
|
||||
json={"governance": "frozen"},
|
||||
)
|
||||
expect(frozen.json()["governance"] == "frozen", "frozen set")
|
||||
rhythm_frozen = trait_map(frozen.json())["rhythm"].get("statement") or ""
|
||||
client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Anders",
|
||||
"body": (
|
||||
"Vielleicht bedeutet das weniger, als es scheint. Allerdings wunderte ich mich. "
|
||||
"Im Grunde war der Tag merkwürdig und irgendwie offen."
|
||||
),
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
still_frozen = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
expect(
|
||||
(trait_map(still_frozen)["rhythm"].get("statement") or "") == rhythm_frozen,
|
||||
"frozen keeps trait values",
|
||||
)
|
||||
|
||||
advising = client.patch(
|
||||
"/api/journal/writing-profile",
|
||||
headers=headers,
|
||||
json={"governance": "advising"},
|
||||
)
|
||||
expect(advising.json()["governance"] == "advising", "advising set")
|
||||
imported = client.post(
|
||||
"/api/journal/writing-profile/import",
|
||||
headers=headers,
|
||||
json={
|
||||
"body": "Haha das war irgendwie lustig. Witzig, wirklich witzig, lol.",
|
||||
"occurred_at": "2019-07-12",
|
||||
"context_hint": "vacation_diary",
|
||||
},
|
||||
)
|
||||
expect(imported.status_code == 200, f"import {imported.text}")
|
||||
expect(any(item["kind"] == "imported_text" for item in imported.json()["sources"]), "import stored")
|
||||
expect(
|
||||
int(imported.json().get("pending_evidence") or 0) == 0,
|
||||
"unconfirmed profile does not start continuous review",
|
||||
)
|
||||
vacation = next(item for item in imported.json()["sources"] if item["kind"] == "imported_text")
|
||||
expect((vacation.get("occurred_at") or "").startswith("2019-07-12"), "import keeps time")
|
||||
expect(vacation.get("context_hint") == "autobiographical_journal", "vacation diary maps to journal facet")
|
||||
|
||||
client.patch("/api/journal/writing-profile", headers=headers, json={"governance": "learning"})
|
||||
conv = client.post(f"/api/journal/days/{day_id}/conversations", headers=headers, json={"title": "Chat"})
|
||||
turn = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Nur ein kurzer Satz ohne Gewicht."},
|
||||
)
|
||||
expect(turn.status_code == 200, f"turn {turn.text}")
|
||||
after_turn = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
brief = after_turn.get("compiled_brief") or ""
|
||||
expect("Finale Journal Entries" in brief, "entries remain strongest after later dialogue")
|
||||
expect("Formulierungen aus dem Dialog" not in brief, "dialogue excerpts drop once entries exist")
|
||||
expect("Kirschen" in brief, "earlier journal wording remains")
|
||||
|
||||
interaction = client.get("/api/journal/interaction-profile", headers=headers).json()
|
||||
expect(interaction["governance"] == "advising", "interaction default is advising")
|
||||
expect(interaction["preferences"] == [], "turn does not invent personal preferences")
|
||||
expect(interaction["suggestions"] == [], "silence is not an interaction suggestion")
|
||||
expect(any(item["pref_key"] == "reply_length" for item in interaction["defaults"]), "labeled product defaults")
|
||||
|
||||
from privacy_gateway import debug_last
|
||||
|
||||
expect("Produktdefault" in (debug_last["rendered"] if debug_last else ""), "hint reaches dialogue prompt")
|
||||
expect("Ein bis drei Sätze" in (debug_last["rendered"] if debug_last else ""), "length default is in hint not anonymous")
|
||||
expect("anonyme Produktregel" in (debug_last["rendered"] if debug_last else ""), "prompt labels the moved preference")
|
||||
|
||||
proposed = propose_observation(
|
||||
profile_id,
|
||||
scope="autobiographical",
|
||||
pref_key="listening",
|
||||
proposed_value="Bei längeren Erzählungen zunächst stärker zuhören.",
|
||||
evidence="Beobachtung, kein automatisches Lernen",
|
||||
)
|
||||
expect(proposed and proposed["suggestions"], "observation is a suggestion only")
|
||||
expect(proposed["preferences"] == [], "observation is not auto-applied")
|
||||
rejected = client.post(
|
||||
f"/api/journal/interaction-profile/suggestions/{proposed['suggestions'][0]['id']}/reject",
|
||||
headers=headers,
|
||||
)
|
||||
expect(rejected.json()["suggestions"] == [], "rejected observation stays out of the profile")
|
||||
|
||||
personal = client.patch(
|
||||
"/api/journal/interaction-profile/preferences",
|
||||
headers=headers,
|
||||
json={
|
||||
"scope": "global",
|
||||
"pref_key": "reply_length",
|
||||
"value": "Meist nur ein Satz.",
|
||||
"origin": "explicit",
|
||||
},
|
||||
)
|
||||
expect(personal.status_code == 200, f"personal pref {personal.text}")
|
||||
expect(personal.json()["preferences"][0]["origin"] == "explicit", "explicit origin kept")
|
||||
reset_debug()
|
||||
later = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Heute Vormittag zum Markt, danach Kirschen, dann noch der Hafen bei 18:00 Uhr."},
|
||||
)
|
||||
expect(later.status_code == 200, "second turn")
|
||||
from privacy_gateway import debug_last as last_hint
|
||||
|
||||
expect("Meist nur ein Satz" in last_hint["rendered"], "personal preference reaches hint")
|
||||
expect("persönlich" in last_hint["rendered"], "personal override is labeled")
|
||||
expect(later.json()["conversation"].get("narrative_mode"), "dialogue state remains automatic")
|
||||
expect("long_story" in later.json()["conversation"], "long_story is conversation state")
|
||||
|
||||
manual = client.patch(
|
||||
"/api/journal/writing-profile/facets/humor",
|
||||
headers=headers,
|
||||
json={"value": "trocken, selten, nie aufgesetzt"},
|
||||
)
|
||||
expect("humor" in trait_map(manual.json()), "manual humor is a trait")
|
||||
expect(trait_map(manual.json())["humor"]["origin"] == "manual", "manual trait origin")
|
||||
expect("trocken, selten" in (manual.json().get("compiled_brief") or ""), "manual trait reaches brief")
|
||||
|
||||
exported = client.get("/api/journal/writing-profile/export", headers=headers).json()
|
||||
expect(exported["kind"] == "kansho.writing_profile", "writing export kind")
|
||||
expect(exported["format_version"] == 1, "writing export version")
|
||||
expect(any(item["slug"] == "humor" and "trocken" in (item.get("statement") or "") for item in exported["traits"]), "export keeps manual trait")
|
||||
expect("profile_id" not in exported, "export is portable")
|
||||
expect("sources" not in exported, "export is not a journal backup")
|
||||
|
||||
client.patch("/api/journal/writing-profile/facets/humor", headers=headers, json={"value": "wird überschrieben"})
|
||||
restored = client.post("/api/journal/writing-profile/restore", headers=headers, json=exported)
|
||||
expect(restored.status_code == 200, f"writing restore {restored.text}")
|
||||
expect(trait_map(restored.json())["humor"]["statement"].startswith("trocken"), "restore replaces traits")
|
||||
expect(trait_map(restored.json())["humor"]["origin"] == "manual", "restore keeps origin")
|
||||
|
||||
wrong = client.post(
|
||||
"/api/journal/writing-profile/restore",
|
||||
headers=headers,
|
||||
json={"kind": "kansho.interaction_profile", "format_version": 1, "preferences": []},
|
||||
)
|
||||
expect(wrong.status_code == 400, "writing restore rejects interaction document")
|
||||
|
||||
interaction_doc = client.get("/api/journal/interaction-profile/export", headers=headers).json()
|
||||
expect(interaction_doc["kind"] == "kansho.interaction_profile", "interaction export kind")
|
||||
expect(interaction_doc["preferences"][0]["value"] == "Meist nur ein Satz.", "interaction export keeps personal pref")
|
||||
client.patch(
|
||||
"/api/journal/interaction-profile/preferences",
|
||||
headers=headers,
|
||||
json={"scope": "global", "pref_key": "reply_length", "value": "Ganz anders.", "origin": "manual"},
|
||||
)
|
||||
interaction_back = client.post("/api/journal/interaction-profile/restore", headers=headers, json=interaction_doc)
|
||||
expect(interaction_back.json()["preferences"][0]["value"] == "Meist nur ein Satz.", "interaction restore replaces prefs")
|
||||
|
||||
bundle = client.get("/api/journal/profiles/export", headers=headers).json()
|
||||
expect(bundle["kind"] == "kansho.profiles", "bundle kind")
|
||||
bundle["writing"]["traits"] = [
|
||||
item if item["slug"] != "humor" else {**item, "statement": "aus dem Sammeldokument", "locked": True}
|
||||
for item in bundle["writing"]["traits"]
|
||||
]
|
||||
both = client.post("/api/journal/profiles/restore", headers=headers, json=bundle)
|
||||
expect(both.status_code == 200, f"bundle restore {both.text}")
|
||||
expect(trait_map(both.json()["writing"])["humor"]["statement"] == "aus dem Sammeldokument", "bundle restores writing")
|
||||
expect(trait_map(both.json()["writing"])["humor"]["locked"] in {True, 1}, "bundle restores lock")
|
||||
expect(both.json()["interaction"]["preferences"][0]["value"] == "Meist nur ein Satz.", "bundle restores interaction")
|
||||
|
||||
print("profile governance ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,427 @@
|
||||
"""Cost-conscious profile review. Run from backend/: python tests/test_profile_review.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-profile-review-test.sqlite")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from privacy_gateway import reset_debug
|
||||
from profile_review import (
|
||||
KIND_PACKAGE,
|
||||
KIND_RESULT,
|
||||
facet_delta,
|
||||
is_strong_edit,
|
||||
parse_result,
|
||||
)
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def header(token: str) -> dict:
|
||||
return {"X-Auth-Token": token}
|
||||
|
||||
|
||||
def open_space_day(client, headers, title="Alltag", date="2026-08-20"):
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": title})
|
||||
expect(space.status_code == 200, f"create space {space.text}")
|
||||
day = client.post(
|
||||
f"/api/journal/spaces/{space.json()['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": date},
|
||||
)
|
||||
expect(day.status_code == 200, f"open day {day.text}")
|
||||
return space.json(), day.json()
|
||||
|
||||
|
||||
def trait_map(profile: dict) -> dict:
|
||||
return {item["slug"]: item for item in profile.get("traits") or []}
|
||||
|
||||
|
||||
def facet_map(profile: dict) -> dict:
|
||||
return {item["facet_key"]: item for item in profile.get("facets") or []}
|
||||
|
||||
|
||||
def pending_triggers(status: dict) -> set[str]:
|
||||
return {item.get("trigger") for item in status.get("pending_evidence") or []}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
expect(not is_strong_edit("Heute Kirschen am Markt gekauft.", "Heute Kirschen am Markt gekauft."), "same text is not a strong edit")
|
||||
expect(
|
||||
is_strong_edit(
|
||||
"Heute war der Markt voll und ich habe Kirschen gekauft.",
|
||||
"Vielleicht bedeutet das weniger. Allerdings wunderte ich mich den ganzen Abend.",
|
||||
),
|
||||
"divergent rewrite is a strong edit",
|
||||
)
|
||||
expect(facet_delta({"rhythm": "kurz"}, {"rhythm": "lang", "humor": "trocken"}) == ["rhythm", "humor"], "delta lists changed local signals")
|
||||
parsed_core = parse_result({
|
||||
"kind": KIND_RESULT,
|
||||
"format_version": 1,
|
||||
"changes": [{"layer": "core", "key": "ignored", "action": "keep", "evidence_ids": []}],
|
||||
})
|
||||
expect(parsed_core["changes"][0]["key"] == "core", "core layer maps to core key")
|
||||
parsed_create = parse_result({
|
||||
"kind": KIND_RESULT,
|
||||
"format_version": 1,
|
||||
"changes": [{
|
||||
"layer": "trait",
|
||||
"slug": "scene_cuts",
|
||||
"facet_key": "autobiographical_journal",
|
||||
"action": "create",
|
||||
"proposed_value": "wechselt ohne Ankündigung zwischen Szenen",
|
||||
"exemplars": [{"excerpt": "Danach der Hafen."}],
|
||||
"evidence_ids": ["e1"],
|
||||
}],
|
||||
})
|
||||
expect(parsed_create["changes"][0]["slug"] == "scene_cuts", "dynamic trait slugs are accepted")
|
||||
expect(parsed_create["changes"][0]["action"] == "create", "create remains create")
|
||||
|
||||
reset_debug()
|
||||
with TestClient(app) as client:
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "lars@example.test", "name": "Lars", "password": "test-pass"},
|
||||
)
|
||||
expect(setup.status_code == 200, "setup")
|
||||
headers = header(setup.json()["token"])
|
||||
_space, day = open_space_day(client, headers)
|
||||
day_id = day["day"]["id"]
|
||||
|
||||
first = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Markt",
|
||||
"body": "Heute um 7:30 Uhr bin ich zum Markt. Danach habe ich Kirschen gekauft.",
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(first.status_code == 200, "first save")
|
||||
writing = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
expect(writing.get("lifecycle") == "uninitialized", "first save leaves profile unconfirmed")
|
||||
expect("rhythm" not in {item.get("facet_key") for item in writing.get("facets") or []}, "no heuristic facet bootstrap")
|
||||
expect(not writing.get("traits"), "no inferred traits")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect(status["pending_evidence"] == [], "first save does not enqueue a review")
|
||||
|
||||
second = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Anders",
|
||||
"body": (
|
||||
"Haha das war irgendwie lustig, lol. Vielleicht bedeutet das weniger, als es scheint. "
|
||||
"Allerdings wunderte ich mich. Im Grunde war der Abend merkwürdig und offen."
|
||||
),
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(second.status_code == 200, "second save")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect(status["pending_evidence"] == [], "continuous queue waits for confirmed profile")
|
||||
|
||||
frozen = client.patch("/api/journal/writing-profile", headers=headers, json={"governance": "frozen"})
|
||||
expect(frozen.status_code == 200, "frozen")
|
||||
client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Frozen",
|
||||
"body": "Ganz anderer Ton, witzig, lol, haha, und trotzdem keine Review.",
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect(status["pending_evidence"] == [], "unconfirmed frozen still has no queue")
|
||||
client.patch("/api/journal/writing-profile", headers=headers, json={"governance": "learning"})
|
||||
|
||||
draft = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Draft",
|
||||
"body": (
|
||||
"Heute war der Markt voll und ich bin früh gegangen und habe Kirschen gekauft "
|
||||
"und danach den Hafen gesehen, ganz ruhig und ohne Eile."
|
||||
),
|
||||
"origin": "accepted_draft",
|
||||
},
|
||||
)
|
||||
expect(draft.status_code == 200, "accepted draft stored")
|
||||
edited = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": draft.json()["journal_day_id"] if False else day_id,
|
||||
"entry_id": draft.json()["id"],
|
||||
"title": "Draft",
|
||||
"body": (
|
||||
"Der Nachmittag kippte. Ich blieb länger als geplant, sprach kaum, "
|
||||
"und schrieb später nur zwei knappe Zeilen ohne Markt und ohne Hafen."
|
||||
),
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(edited.status_code == 200, "strong user edit")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect("user_edit_of_draft" not in pending_triggers(status), "draft edit waits until the profile is confirmed")
|
||||
|
||||
conv = client.post(f"/api/journal/days/{day_id}/conversations", headers=headers, json={"title": "Chat"})
|
||||
long_body = (
|
||||
"Heute früh bin ich zum Markt gegangen und habe Kirschen gekauft, danach den Hafen gesehen, "
|
||||
"später noch Brot geholt, mit jemandem gesprochen, Tee getrunken, den Wind bemerkt, "
|
||||
"und am Abend den Tag in Ruhe nachvollzogen, ohne etwas zu erzwingen oder nach einem Muster zu suchen. "
|
||||
"Zwischendurch war eine Pause, dann noch ein kurzer Blick aufs Wasser, und zum Schluss bin ich langsam heimgegangen, "
|
||||
"ohne den Faden zu verlieren und ohne aus dem Gesprächsverlauf eine dauerhafte Vorliebe zu machen."
|
||||
)
|
||||
expect(len(long_body.split()) >= 60, "sample text is long enough")
|
||||
reset_debug()
|
||||
long_turn = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": long_body},
|
||||
)
|
||||
expect(long_turn.status_code == 200, "long turn")
|
||||
from privacy_gateway import debug_last as last_long
|
||||
|
||||
expect(last_long["purpose"] == "dialogue_turn", "long turn still has no review call")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect("dialogue_sample" not in pending_triggers(status), "dialogue is not sampled before confirm")
|
||||
|
||||
corpus = client.post(
|
||||
"/api/journal/writing-profile/corpus",
|
||||
headers=headers,
|
||||
json={
|
||||
"items": [
|
||||
{
|
||||
"body": "Am ersten Urlaubstag um 6:00 Uhr Tee auf dem Balkon, danach der Hafen. Kirschen vom Markt.",
|
||||
"occurred_at": "2018-07-02",
|
||||
"context_hint": "vacation_diary",
|
||||
},
|
||||
{
|
||||
"body": "Haha das war irgendwie lustig, lol. Der Abend kippte, ohne dass ich es erklärt hätte.",
|
||||
"occurred_at": "2024-08-11",
|
||||
"context_hint": "vacation_diary",
|
||||
},
|
||||
{
|
||||
"body": "Vielleicht bedeutet das weniger. Allerdings wunderte ich mich über die Stille nach dem Markt.",
|
||||
"occurred_at": "2025-03-04",
|
||||
"context_hint": "autobiographical_journal",
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
expect(corpus.status_code == 200, f"corpus import {corpus.text}")
|
||||
dated = [item for item in corpus.json()["sources"] if item.get("kind") == "imported_text"]
|
||||
expect(len(dated) >= 3, "historical corpus is more than two entries")
|
||||
expect(any((item.get("occurred_at") or "").startswith("2018-07-02") for item in dated), "older source keeps time")
|
||||
expect(any(item.get("recency_role") == "long_term" for item in dated), "older text is long-term evidence")
|
||||
|
||||
paste = client.post("/api/journal/writing-profile/initial-build/paste", headers=headers)
|
||||
expect(paste.status_code == 200, f"initial paste {paste.text}")
|
||||
package = paste.json()["package"]
|
||||
expect(package["kind"] == KIND_PACKAGE, "paste uses review package contract")
|
||||
expect(package["format_version"] == 1, "package version")
|
||||
expect(package["mode"] == "initial_build", "unconfirmed profile opens an initial build")
|
||||
expect(package["existing_before_new"], "package names Existing-before-New")
|
||||
expect(any(item.get("kind") == "trait_hint" for item in package["seed_catalog"]), "seed catalog is ordering help")
|
||||
expect(len(package.get("corpus") or []) >= 3, "initial package includes historical corpus")
|
||||
expect(any(item.get("occurred_at") for item in package["corpus"]), "corpus items keep time")
|
||||
instruction = package.get("instruction") or ""
|
||||
expect("bestehenden Trait bestätigen" in instruction, "instruction starts with confirm")
|
||||
expect("erst dann einen neuen Trait anlegen" in instruction, "instruction defers create")
|
||||
expect("autobiographical_journal" in instruction, "vacation/journal facet is named")
|
||||
expect("kansho.profile_review_result" in (paste.json().get("prompt") or ""), "copy prompt names the result contract")
|
||||
|
||||
evidence_id = (package.get("evidences") or [{}])[0].get("id")
|
||||
imported = client.post(
|
||||
"/api/journal/writing-profile/review/import",
|
||||
headers=headers,
|
||||
json={
|
||||
"review_id": paste.json()["id"],
|
||||
"result": {
|
||||
"kind": KIND_RESULT,
|
||||
"format_version": 1,
|
||||
"target": "writing",
|
||||
"mode": "initial_build",
|
||||
"changes": [
|
||||
{
|
||||
"layer": "trait",
|
||||
"slug": "dry_humor",
|
||||
"facet_key": "autobiographical_journal",
|
||||
"action": "create",
|
||||
"label": "Trockener Humor",
|
||||
"proposed_value": "gelegentlich trocken, aus Paste-Review",
|
||||
"rationale": "Journal-Evidenz, kein globaler Core.",
|
||||
"evidence_ids": [evidence_id] if evidence_id else [],
|
||||
"exemplars": [{"excerpt": "haha das war irgendwie lustig", "occurred_at": "2024-08-11"}],
|
||||
}
|
||||
],
|
||||
},
|
||||
},
|
||||
)
|
||||
expect(imported.status_code == 200, f"import result {imported.text}")
|
||||
expect(imported.json()["applied"] >= 1, "initial build applies traits")
|
||||
profile = imported.json()["profile"]
|
||||
expect(profile.get("lifecycle") == "confirmed", "successful initial build confirms the profile")
|
||||
expect(trait_map(profile)["dry_humor"]["statement"].startswith("gelegentlich trocken"), "created trait is stored")
|
||||
expect(trait_map(profile)["dry_humor"]["facet_key"] == "autobiographical_journal", "vacation evidence stays in journal facet")
|
||||
expect(any("haha" in (ref.get("excerpt") or "") for ref in trait_map(profile)["dry_humor"].get("exemplars") or []), "trait keeps exemplar")
|
||||
expect("core" not in facet_map(profile) or not (facet_map(profile).get("core") or {}).get("value"), "single source kind does not invent a global core")
|
||||
|
||||
drift = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Drift",
|
||||
"body": (
|
||||
"Haha das war irgendwie lustig, lol. Vielleicht bedeutet das weniger, als es scheint. "
|
||||
"Allerdings wunderte ich mich. Im Grunde war der Abend merkwürdig und offen."
|
||||
),
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(drift.status_code == 200, "drift save after confirm")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect("heuristic_drift" in pending_triggers(status), "after confirm, local heuristic queues evidence")
|
||||
|
||||
draft_after = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Draft nach Confirm",
|
||||
"body": (
|
||||
"Heute war der Markt voll und ich bin früh gegangen und habe Kirschen gekauft "
|
||||
"und danach den Hafen gesehen, ganz ruhig und ohne Eile."
|
||||
),
|
||||
"origin": "accepted_draft",
|
||||
},
|
||||
)
|
||||
expect(draft_after.status_code == 200, "accepted draft after confirm")
|
||||
edited = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"entry_id": draft_after.json()["id"],
|
||||
"title": "Draft nach Confirm",
|
||||
"body": (
|
||||
"Der Nachmittag kippte noch einmal. Ich blieb, sprach kaum, "
|
||||
"und schrieb später nur zwei knappe Zeilen ohne Markt und ohne Hafen und ohne Kirschen."
|
||||
),
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(edited.status_code == 200, "strong user edit after confirm")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect("user_edit_of_draft" in pending_triggers(status), "strong draft edit is a review trigger")
|
||||
|
||||
reset_debug()
|
||||
long_turn = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": long_body},
|
||||
)
|
||||
expect(long_turn.status_code == 200, "long turn after confirm")
|
||||
expect(long_turn.json()["conversation"].get("preferences") in (None, []), "turn does not write interaction prefs")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect("dialogue_sample" in pending_triggers(status), "long dialogue may be sampled locally")
|
||||
pending_samples = [item for item in status["pending_evidence"] if item["trigger"] == "dialogue_sample"]
|
||||
second_long = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": long_body + " Noch ein Nachsatz über den Abend und die Kirschen."},
|
||||
)
|
||||
expect(second_long.status_code == 200, "second long turn")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
samples_now = [item for item in status["pending_evidence"] if item["trigger"] == "dialogue_sample"]
|
||||
expect(len(samples_now) == len(pending_samples), "dialogue sample has a cooldown")
|
||||
|
||||
interaction = client.get("/api/journal/interaction-profile", headers=headers).json()
|
||||
expect(interaction["preferences"] == [], "dialogue sample is not an interaction preference")
|
||||
expect(interaction["suggestions"] == [], "silence and flow are not interaction learning")
|
||||
|
||||
client.patch("/api/journal/writing-profile", headers=headers, json={"governance": "advising"})
|
||||
client.post(
|
||||
"/api/journal/writing-profile/import",
|
||||
headers=headers,
|
||||
json={"body": "Witzig, wirklich witzig, haha das war irgendwie lustig, lol, und trotzdem nur Candidate."},
|
||||
)
|
||||
reset_debug()
|
||||
api_review = client.post("/api/journal/writing-profile/review/api", headers=headers)
|
||||
expect(api_review.status_code == 200, f"api review {api_review.text}")
|
||||
from privacy_gateway import debug_last as api_trace
|
||||
|
||||
expect(api_trace["purpose"] == "profile_review", "api review uses dedicated purpose")
|
||||
expect(api_trace["layer"] == "profilreview", "trace is not a dialogue turn")
|
||||
expect(api_review.json()["suggested"] >= 1, "advising queues review as suggestion")
|
||||
expect(
|
||||
any("trocken" in (item.get("proposed_value") or "") for item in api_review.json()["profile"]["suggestions"]),
|
||||
"api result is a suggestion in advising",
|
||||
)
|
||||
expect(
|
||||
"aus Paste-Review" in trait_map(api_review.json()["profile"])["dry_humor"]["statement"],
|
||||
"advising does not silently overwrite",
|
||||
)
|
||||
|
||||
client.patch("/api/journal/writing-profile", headers=headers, json={"governance": "frozen"})
|
||||
client.post("/api/journal/writing-profile/review/paste", headers=headers)
|
||||
humor_before = trait_map(client.get("/api/journal/writing-profile", headers=headers).json())["dry_humor"]["statement"]
|
||||
frozen_import = client.post(
|
||||
"/api/journal/writing-profile/review/import",
|
||||
headers=headers,
|
||||
json={
|
||||
"result": {
|
||||
"kind": KIND_RESULT,
|
||||
"format_version": 1,
|
||||
"changes": [
|
||||
{
|
||||
"layer": "trait",
|
||||
"slug": "dry_humor",
|
||||
"action": "precisify",
|
||||
"proposed_value": "sollte nicht greifen",
|
||||
"rationale": "frozen",
|
||||
"evidence_ids": [],
|
||||
}
|
||||
],
|
||||
}
|
||||
},
|
||||
)
|
||||
expect(frozen_import.status_code == 200, "frozen import accepted as document")
|
||||
expect(frozen_import.json()["applied"] == 0, "frozen does not apply review")
|
||||
expect(trait_map(frozen_import.json()["profile"])["dry_humor"]["statement"] == humor_before, "frozen keeps trait")
|
||||
|
||||
bad = client.post(
|
||||
"/api/journal/writing-profile/review/import",
|
||||
headers=headers,
|
||||
json={"result": {"kind": "kansho.writing_profile", "format_version": 1, "changes": []}},
|
||||
)
|
||||
expect(bad.status_code == 400, "wrong kind is rejected")
|
||||
|
||||
print("profile review ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,49 @@
|
||||
"""Local writing-profile compilation. Run from backend/: python tests/test_writing_profile.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from writing_profile_store import compile_style_signals
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
empty = compile_style_signals(["ok"])
|
||||
expect(empty == "", "too little text yields no signals")
|
||||
|
||||
clocks = compile_style_signals(
|
||||
["Heute um 7:30 Uhr bin ich zum Markt. Danach habe ich Kirschen gekauft."]
|
||||
)
|
||||
expect("Uhrzeiten" in clocks, "clocks become a form hint")
|
||||
expect("keine Diagnose" in clocks, "signals refuse diagnosis language")
|
||||
expect("chronologisch" in clocks, "time words mark chronology")
|
||||
|
||||
reflect = compile_style_signals(
|
||||
[
|
||||
"Der Vormittag war ruhig. Allerdings wunderte ich mich. "
|
||||
"Vielleicht bedeutet das weniger, als es scheint."
|
||||
]
|
||||
)
|
||||
expect("Einordnung" in reflect, "reflection markers are form, not a self model")
|
||||
|
||||
expect(
|
||||
"keine Diagnose" in compile_style_signals(
|
||||
["Heute war der Markt voll. Ich bin früh gegangen und habe Kirschen gekauft."]
|
||||
),
|
||||
"first-day signals exist without a saved entry",
|
||||
)
|
||||
|
||||
print("writing profile signals ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user