MVP 0.92
This commit is contained in:
1 parent
60f0d2830d
commit
53b1eff833
62 files changed
+6578
-355
No files matched your search
@@ -186,9 +186,7 @@ def main() -> None:
|
||||
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"]
|
||||
rendered = (later_turn.json().get("trace") or {}).get("intern") or ""
|
||||
expect("Hafen" in rendered, "earlier space source can supply a known plan")
|
||||
expect("Heute früh aufgestanden" in rendered, "current conversation remains primary")
|
||||
|
||||
@@ -204,9 +202,7 @@ def main() -> None:
|
||||
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"]
|
||||
sibling_rendered = (sibling_turn.json().get("trace") or {}).get("intern") or ""
|
||||
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")
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ def auth_header(token: str) -> dict:
|
||||
def main() -> None:
|
||||
seed_prompts = json.loads((ROOT / "config" / "prompts.seed.json").read_text(encoding="utf-8"))
|
||||
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")
|
||||
expect(slugs == {"mvp.dialogue_turn", "mvp.journal_generate", "mvp.journal_reconstruct", "mvp.entity_detect", "mvp.profile_review"}, "mvp prompts seeded")
|
||||
|
||||
with TestClient(app) as client:
|
||||
health = client.get("/api/health")
|
||||
@@ -78,7 +78,7 @@ def main() -> None:
|
||||
|
||||
admin = client.get("/api/admin/health", headers=headers)
|
||||
expect(admin.status_code == 200, "admin health")
|
||||
expect(admin.json()["inventory"]["prompts"] == 3, "three mvp prompts")
|
||||
expect(admin.json()["inventory"]["prompts"] == 5, "five seeded prompts")
|
||||
|
||||
providers = client.get("/api/admin/providers", headers=headers)
|
||||
expect(providers.status_code == 200, "admin providers")
|
||||
@@ -216,6 +216,8 @@ def main() -> None:
|
||||
"existing_text",
|
||||
"known_labels",
|
||||
"register_hint",
|
||||
"opening_hint",
|
||||
"reconstruction",
|
||||
"review_package",
|
||||
"source_text",
|
||||
"space_title",
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,257 @@
|
||||
"""First journal impulse invariants. Run from backend/: python tests/test_journal_opening.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-opening-test.sqlite")
|
||||
os.environ["KANSHO_MEDIA_ROOT"] = str(Path(tempfile.gettempdir()) / "kansho-opening-media")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
Path(os.environ["KANSHO_MEDIA_ROOT"]).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from journal_opening import (
|
||||
NEUTRAL_OPENING,
|
||||
attested_plans_from_user_texts,
|
||||
collect_opening_context,
|
||||
is_attested_plan,
|
||||
)
|
||||
from journal_reconstruct import assign_source_ids
|
||||
from retrieval import pair_user_priority_messages
|
||||
|
||||
|
||||
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, date):
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": title})
|
||||
day = client.post(
|
||||
f"/api/journal/spaces/{space.json()['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": date},
|
||||
)
|
||||
return space.json(), day.json()
|
||||
|
||||
|
||||
def start_conv(client, headers, day_id):
|
||||
return client.post(f"/api/journal/days/{day_id}/conversations", headers=headers, json={"title": "Gespräch"})
|
||||
|
||||
|
||||
def main() -> None:
|
||||
expect(is_attested_plan("Morgen wollen wir nach Cres fahren."), "future intent is a plan")
|
||||
expect(is_attested_plan("Wir haben vor, später an den Hafen zu gehen."), "vorhaben is a plan")
|
||||
expect(not is_attested_plan("Heute war der Hafen ruhig. Kirschen am Stand."), "past report is not a plan")
|
||||
expect(not is_attested_plan("Wir standen vor dem Hafen und aßen Kirschen."), "vor without haben is not a plan")
|
||||
expect(not is_attested_plan("Hafen Kirschen Wind"), "word overlap is not a plan")
|
||||
plans = attested_plans_from_user_texts(
|
||||
["Heute war der Hafen ruhig.", "Morgen wollen wir nach Cres fahren.", "Hafen Kirschen"]
|
||||
)
|
||||
expect(len(plans) == 1 and "Cres" in plans[0], "only attested plan sentences are kept")
|
||||
expect(
|
||||
not any(is_attested_plan(text) for text in ["Was davon möchtest du festhalten?", "Die Fahrt nach Cres war schön."]),
|
||||
"assistant-like completion is not treated as a user plan by the helper",
|
||||
)
|
||||
|
||||
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"])
|
||||
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_headers = header(
|
||||
client.post("/api/auth/login", json={"email": "ute@example.test", "password": "user-pass"}).json()["token"]
|
||||
)
|
||||
|
||||
empty_space, empty_day = open_space_day(client, headers, "Leer", "2026-08-21")
|
||||
empty = start_conv(client, headers, empty_day["day"]["id"])
|
||||
expect(empty.status_code == 200, f"empty opening {empty.text}")
|
||||
expect(empty.json().get("opening", {}).get("kind") == "local_neutral", "no context uses local neutral")
|
||||
expect(empty.json()["opening"]["assistant"]["body"] == NEUTRAL_OPENING, "neutral impulse stored")
|
||||
expect(empty.json()["opening"]["calls"] == 0, "neutral opening does not call a model")
|
||||
empty_msgs = empty.json().get("messages") or []
|
||||
expect(len(empty_msgs) == 1 and empty_msgs[0]["role"] == "assistant", "neutral opening is assistant-first")
|
||||
|
||||
plan_space, plan_day = open_space_day(client, headers, "Urlaub", "2026-08-01")
|
||||
plan_conv = start_conv(client, headers, plan_day["day"]["id"])
|
||||
client.post(
|
||||
f"/api/journal/conversations/{plan_conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Morgen wollen wir nach Cres fahren. Die Fähre ist schon bedacht."},
|
||||
)
|
||||
later = client.post(
|
||||
f"/api/journal/spaces/{plan_space['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-02"},
|
||||
)
|
||||
planned = start_conv(client, headers, later.json()["day"]["id"])
|
||||
facts = collect_opening_context(setup.json()["profile_id"], planned.json()["id"])
|
||||
expect(facts["has_relevant_context"], "attested plan counts as relevant context")
|
||||
expect(any("Cres" in item and is_attested_plan(item) for item in facts["attested_plans"]), "plan comes from user wording")
|
||||
expect(all(item in facts["user_texts"] for item in facts["attested_plans"]), "plans are a subset of user texts")
|
||||
expect(planned.json().get("opening", {}).get("kind") == "model", "attested plan uses the dialogue path")
|
||||
intern = (planned.json().get("opening") or {}).get("trace") or {}
|
||||
intern_text = intern.get("intern") or ""
|
||||
expect("Belegte Vorhaben" in intern_text, "opening policy reaches the prompt")
|
||||
expect("kein bewiesenes wiederkehrendes Muster" in intern_text or "kein bewiesenes" in intern_text, "pattern rule is in the opening hint")
|
||||
|
||||
thread_space, thread_day = open_space_day(client, headers, "Tagfaden", "2026-08-10")
|
||||
first = start_conv(client, headers, thread_day["day"]["id"])
|
||||
client.post(
|
||||
f"/api/journal/conversations/{first.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Morgens am Markt, danach noch offen, wie der Nachmittag wird."},
|
||||
)
|
||||
second = start_conv(client, headers, thread_day["day"]["id"])
|
||||
thread_facts = collect_opening_context(setup.json()["profile_id"], second.json()["id"])
|
||||
expect(thread_facts["open_day_points"], "same-day user text is an open point")
|
||||
expect(
|
||||
any("Markt" in item or "Nachmittag" in item for item in thread_facts["open_day_points"]),
|
||||
"open point is the day's user wording",
|
||||
)
|
||||
expect(second.json().get("opening", {}).get("kind") == "model", "open day thread uses the dialogue path")
|
||||
|
||||
overlap_space, overlap_day = open_space_day(client, headers, "Overlap", "2026-08-03")
|
||||
overlap_conv = start_conv(client, headers, overlap_day["day"]["id"])
|
||||
client.post(
|
||||
f"/api/journal/conversations/{overlap_conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Heute war der Hafen ruhig. Kirschen am Stand, Wind von See."},
|
||||
)
|
||||
client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": overlap_day["day"]["id"],
|
||||
"title": "Hafen",
|
||||
"body": "Heute war der Hafen ruhig. Kirschen am Stand.",
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
later_overlap = client.post(
|
||||
f"/api/journal/spaces/{overlap_space['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-04"},
|
||||
)
|
||||
overlap_open = start_conv(client, headers, later_overlap.json()["day"]["id"])
|
||||
overlap_facts = collect_opening_context(setup.json()["profile_id"], overlap_open.json()["id"])
|
||||
expect(not overlap_facts["attested_plans"], "random overlap is not an attested plan")
|
||||
expect(not overlap_facts["has_relevant_context"], "recency without plan or open day is not relevant opening context")
|
||||
expect(overlap_open.json().get("opening", {}).get("kind") == "local_neutral", "overlap falls back to neutral")
|
||||
expect("Muster" not in (overlap_open.json().get("opening") or {}).get("assistant", {}).get("body", ""), "neutral impulse does not claim a pattern")
|
||||
|
||||
asst_space, asst_day = open_space_day(client, headers, "Assistent", "2026-08-05")
|
||||
asst_conv = start_conv(client, headers, asst_day["day"]["id"])
|
||||
from dialogue_store import append_message
|
||||
|
||||
append_message(
|
||||
setup.json()["profile_id"],
|
||||
asst_conv.json()["id"],
|
||||
"Morgen wollen wir nach Cres fahren.",
|
||||
role="assistant",
|
||||
)
|
||||
next_asst = client.post(
|
||||
f"/api/journal/spaces/{asst_space['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-06"},
|
||||
)
|
||||
asst_open = start_conv(client, headers, next_asst.json()["day"]["id"])
|
||||
asst_facts = collect_opening_context(setup.json()["profile_id"], asst_open.json()["id"])
|
||||
expect(not asst_facts["attested_plans"], "assistant wording is not a user plan")
|
||||
expect("Cres" not in " ".join(asst_facts["attested_plans"]), "assistant plan sentence stays out of attested plans")
|
||||
expect(asst_facts["assistant_is_not_user_fact"], "adapter marks assistant as non-fact")
|
||||
|
||||
pair_space, pair_day = open_space_day(client, headers, "Paarung", "2026-08-07")
|
||||
pair_conv = start_conv(client, headers, pair_day["day"]["id"])
|
||||
expect((pair_conv.json().get("messages") or [])[0]["role"] == "assistant", "assistant-first stored")
|
||||
user_turn = client.post(
|
||||
f"/api/journal/conversations/{pair_conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Nach der Fähre kamen wir spät an. Anna kaufte Brot."},
|
||||
)
|
||||
expect(user_turn.status_code == 200, "user turn after opening")
|
||||
msgs = user_turn.json()["messages"]
|
||||
users = [item for item in msgs if item["role"] == "user"]
|
||||
expect(len(users) == 1, "exactly one user source after opening plus turn")
|
||||
paired = pair_user_priority_messages(msgs)
|
||||
paired_users = [item for item in paired if item["role"] == "user"]
|
||||
expect([item["body"] for item in paired_users] == [item["body"] for item in users], "pairing keeps every user source")
|
||||
labeled = assign_source_ids(paired)
|
||||
expect(
|
||||
[item["source_id"] for item in labeled if item["role"] == "user"] == ["u1"],
|
||||
"first user source is u1 even after an opening assistant",
|
||||
)
|
||||
draft = client.post(
|
||||
f"/api/journal/days/{pair_day['day']['id']}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [pair_conv.json()["id"]]},
|
||||
)
|
||||
expect(draft.status_code == 200, f"generate after opening {draft.text}")
|
||||
reconstruct = None
|
||||
for stage in (draft.json().get("trace") or {}).get("stages") or []:
|
||||
if stage.get("purpose") == "journal_reconstruct":
|
||||
reconstruct = stage
|
||||
intern = (reconstruct or {}).get("intern") or ""
|
||||
expect("Anna kaufte Brot" in intern, "user source reaches reconstruction")
|
||||
expect("[u1]" in intern, "user source id assigned")
|
||||
narrate = None
|
||||
for stage in (draft.json().get("trace") or {}).get("stages") or []:
|
||||
if stage.get("purpose") == "journal_generate":
|
||||
narrate = stage
|
||||
narrate_intern = (narrate or {}).get("intern") or ""
|
||||
expect("verified_artifact" in narrate_intern or "sources" in narrate_intern, "stage 2 consumes verified artifact")
|
||||
expect("Nach der Fähre kamen wir spät an" in intern, "user wording not dropped after opening")
|
||||
expect(users[0]["body"].startswith("Nach der Fähre"), "user source is the user message, not the opening")
|
||||
|
||||
stolen = client.post(
|
||||
f"/api/journal/days/{empty_day['day']['id']}/conversations",
|
||||
headers=other_headers,
|
||||
json={"title": "fremd"},
|
||||
)
|
||||
expect(stolen.status_code == 404, "foreign profile cannot open a conversation")
|
||||
stolen_turn = client.post(
|
||||
f"/api/journal/conversations/{empty.json()['id']}/turn",
|
||||
headers=other_headers,
|
||||
json={"body": "Hallo"},
|
||||
)
|
||||
expect(stolen_turn.status_code == 404, "foreign profile cannot turn into the opening conversation")
|
||||
user_login_space = client.post("/api/journal/spaces", headers=other_headers, json={"title": "Ute"})
|
||||
user_day = client.post(
|
||||
f"/api/journal/spaces/{user_login_space.json()['id']}/days",
|
||||
headers=other_headers,
|
||||
json={"calendar_date": "2026-08-21"},
|
||||
)
|
||||
user_open = start_conv(client, other_headers, user_day.json()["day"]["id"])
|
||||
expect(user_open.status_code == 200, "non-admin can start their own day")
|
||||
expect("trace" not in (user_open.json().get("opening") or {}), "non-admin opening has no trace")
|
||||
expect("opening_context" not in (user_open.json().get("opening") or {}), "non-admin opening has no opening_context")
|
||||
expect("trace" not in user_open.json(), "non-admin response has no top-level trace")
|
||||
|
||||
start = client.get("/api/journal/continuity", headers=headers)
|
||||
expect(start.status_code == 200 and start.json().get("continuable"), "continuable day for the author")
|
||||
expect(start.json()["continuable"]["space_id"] == start.json()["continuable"]["space_id"], "continuity names a space")
|
||||
other_start = client.get("/api/journal/continuity", headers=other_headers)
|
||||
expect(other_start.json()["continuable"]["space_id"] == user_login_space.json()["id"], "continuity is profile-scoped")
|
||||
stolen_cont = client.get(f"/api/journal/spaces/{plan_space['id']}", headers=other_headers)
|
||||
expect(stolen_cont.status_code == 404, "continuity space stays isolated")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,155 @@
|
||||
"""Journal trash, undelete, and hard delete. Run from backend/: python tests/test_journal_trash.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-trash-test.sqlite")
|
||||
os.environ["KANSHO_MEDIA_ROOT"] = str(Path(tempfile.gettempdir()) / "kansho-trash-media")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
Path(os.environ["KANSHO_MEDIA_ROOT"]).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from media_store import media_root
|
||||
|
||||
|
||||
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 main() -> None:
|
||||
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"])
|
||||
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_headers = header(client.post("/api/auth/login", json={"email": "ute@example.test", "password": "user-pass"}).json()["token"])
|
||||
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": "Alltag"})
|
||||
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": "Gespräch"})
|
||||
client.post(f"/api/journal/conversations/{conv.json()['id']}/turn", headers=headers, json={"body": "Heute war der Markt voll."})
|
||||
saved = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={"journal_day_id": day_id, "title": "Markt", "body": "Heute war der Markt voll.", "origin": "user_edit"},
|
||||
)
|
||||
entry_id = saved.json()["id"]
|
||||
second = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={"journal_day_id": day_id, "title": "Abend", "body": "Später am Hafen.", "origin": "user_edit"},
|
||||
)
|
||||
second_id = second.json()["id"]
|
||||
media = client.post(
|
||||
f"/api/journal/entries/{entry_id}/media",
|
||||
headers=headers,
|
||||
files={"file": ("shot.png", PNG, "image/png")},
|
||||
)
|
||||
expect(media.status_code == 200, "media attached")
|
||||
media_id = media.json()["id"]
|
||||
rel = media.json()["rel_path"]
|
||||
expect((media_root() / rel).is_file(), "media file exists")
|
||||
|
||||
listed = client.get(f"/api/journal/spaces/{space_id}/entries", headers=headers).json()
|
||||
expect(len(listed) == 2, "two active entries")
|
||||
expect(listed[0]["calendar_date"] == "2026-08-20", "same day")
|
||||
expect(listed[0]["id"] != listed[1]["id"], "same-day entries are distinct")
|
||||
expect(all(item["id"] in {entry_id, second_id} for item in listed), "list is the two entries")
|
||||
|
||||
hidden_list = client.get(f"/api/journal/spaces/{space_id}/entries", headers=other_headers)
|
||||
expect(hidden_list.status_code == 404, "entry list isolated")
|
||||
hidden_trash = client.get(f"/api/journal/spaces/{space_id}/trash", headers=other_headers)
|
||||
expect(hidden_trash.status_code == 404, "trash list isolated")
|
||||
|
||||
deleted = client.delete(f"/api/journal/entries/{entry_id}", headers=headers)
|
||||
expect(deleted.status_code == 200, "soft delete")
|
||||
after_soft = client.get(f"/api/journal/spaces/{space_id}/entries", headers=headers).json()
|
||||
expect(all(item["id"] != entry_id for item in after_soft), "soft-deleted entry leaves chronology")
|
||||
expect(any(item["id"] == second_id for item in after_soft), "other entry remains")
|
||||
trash = client.get(f"/api/journal/spaces/{space_id}/trash", headers=headers).json()
|
||||
expect(any(item["id"] == entry_id for item in trash), "soft-deleted appears in trash")
|
||||
missing = client.get(f"/api/journal/entries/{entry_id}", headers=headers)
|
||||
expect(missing.status_code == 404, "deleted entry is not a normal GET")
|
||||
|
||||
stolen_delete = client.delete(f"/api/journal/entries/{second_id}", headers=other_headers)
|
||||
expect(stolen_delete.status_code == 404, "foreign profile cannot soft-delete")
|
||||
stolen_undelete = client.post(f"/api/journal/entries/{entry_id}/undelete", headers=other_headers, json={})
|
||||
expect(stolen_undelete.status_code == 404, "foreign profile cannot undelete")
|
||||
stolen_purge = client.post(f"/api/journal/entries/{entry_id}/purge", headers=other_headers, json={"confirm": True})
|
||||
expect(stolen_purge.status_code == 404, "foreign profile cannot purge")
|
||||
|
||||
version_restore = client.post(
|
||||
f"/api/journal/entries/{entry_id}/restore",
|
||||
headers=headers,
|
||||
json={"version_id": saved.json()["current_version_id"]},
|
||||
)
|
||||
expect(version_restore.status_code == 404, "version restore is not trash undelete")
|
||||
|
||||
back = client.post(f"/api/journal/entries/{entry_id}/undelete", headers=headers, json={})
|
||||
expect(back.status_code == 200, f"undelete {back.text}")
|
||||
expect(back.json().get("deleted_at") in (None, ""), "undelete clears deleted_at")
|
||||
restored_list = client.get(f"/api/journal/spaces/{space_id}/entries", headers=headers).json()
|
||||
expect(any(item["id"] == entry_id for item in restored_list), "undeleted entry returns to chronology")
|
||||
expect(client.get(f"/api/journal/spaces/{space_id}/trash", headers=headers).json() == [], "trash empty after undelete")
|
||||
versions = client.get(f"/api/journal/entries/{entry_id}/versions", headers=headers).json()
|
||||
expect(len(versions) == 1, "undelete does not append a version")
|
||||
|
||||
client.delete(f"/api/journal/entries/{entry_id}", headers=headers)
|
||||
unconfirmed = client.post(f"/api/journal/entries/{entry_id}/purge", headers=headers, json={"confirm": False})
|
||||
expect(unconfirmed.status_code == 400, "purge without confirm fails closed")
|
||||
still = client.get(f"/api/journal/spaces/{space_id}/trash", headers=headers).json()
|
||||
expect(any(item["id"] == entry_id for item in still), "unconfirmed purge leaves the entry")
|
||||
|
||||
active_purge = client.post(f"/api/journal/entries/{second_id}/purge", headers=headers, json={"confirm": True})
|
||||
expect(active_purge.status_code == 400, "purge of active entry is rejected")
|
||||
|
||||
conv_before = client.get(f"/api/journal/conversations/{conv.json()['id']}", headers=headers)
|
||||
expect(conv_before.status_code == 200, "source dialogue still there before purge")
|
||||
purged = client.post(f"/api/journal/entries/{entry_id}/purge", headers=headers, json={"confirm": True})
|
||||
expect(purged.status_code == 200 and purged.json().get("purged"), f"purge {purged.text}")
|
||||
expect(purged.json().get("conversations_untouched") is True, "source dialogues are not deleted")
|
||||
expect(client.get(f"/api/journal/conversations/{conv.json()['id']}", headers=headers).status_code == 200, "dialogue remains")
|
||||
expect(client.get(f"/api/journal/entries/{entry_id}", headers=headers).status_code == 404, "purged entry gone")
|
||||
expect(client.get(f"/api/journal/spaces/{space_id}/trash", headers=headers).json() == [], "purged entry leaves trash")
|
||||
expect(not (media_root() / rel).exists(), "unreferenced media file removed")
|
||||
expect(client.get(f"/api/journal/media/{media_id}", headers=headers).status_code == 404, "media row gone")
|
||||
|
||||
again = client.post(f"/api/journal/entries/{entry_id}/purge", headers=headers, json={"confirm": True})
|
||||
expect(again.status_code == 404, "second purge of same id is not found")
|
||||
again_undelete = client.post(f"/api/journal/entries/{entry_id}/undelete", headers=headers, json={})
|
||||
expect(again_undelete.status_code == 404, "cannot undelete a purged entry")
|
||||
|
||||
leftover = client.get(f"/api/journal/spaces/{space_id}/entries", headers=headers).json()
|
||||
expect(all(item["id"] != entry_id for item in leftover), "purged id absent from chronology")
|
||||
expect(any(item["id"] == second_id for item in leftover), "untouched entry remains")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,161 @@
|
||||
"""Local backup/restore. Run from backend/: python tests/test_local_backup.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from local_backup import BackupError, create_backup, restore_backup, verify_archive
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def write_db(path: Path, value: str) -> None:
|
||||
conn = sqlite3.connect(path)
|
||||
conn.execute("CREATE TABLE IF NOT EXISTS note (id INTEGER PRIMARY KEY, body TEXT)")
|
||||
conn.execute("DELETE FROM note")
|
||||
conn.execute("INSERT INTO note (body) VALUES (?)", (value,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
def read_db(path: Path) -> str:
|
||||
conn = sqlite3.connect(path)
|
||||
row = conn.execute("SELECT body FROM note").fetchone()
|
||||
conn.close()
|
||||
return row[0] if row else ""
|
||||
|
||||
|
||||
def main() -> None:
|
||||
with tempfile.TemporaryDirectory(prefix="kansho-backup-test-") as raw:
|
||||
base = Path(raw)
|
||||
db = base / "live" / "kansho.sqlite"
|
||||
media = base / "live" / "media"
|
||||
out = base / "archives"
|
||||
db.parent.mkdir(parents=True)
|
||||
media.mkdir(parents=True)
|
||||
write_db(db, "original-body")
|
||||
sample = media / "profile" / "shot.png"
|
||||
sample.parent.mkdir(parents=True)
|
||||
sample.write_bytes(b"png-bytes")
|
||||
secret = base / "live" / ".env"
|
||||
secret.write_text("KANSHO_PROVIDER_KEY=sk-secret\n", encoding="utf-8")
|
||||
|
||||
archive = out / "first.zip"
|
||||
created = create_backup(archive, db_path=db, media_root=media)
|
||||
expect(archive.is_file(), "backup file created")
|
||||
expect(created["manifest"]["kind"] == "kansho.local_backup", "manifest kind")
|
||||
expect("persönliche, unverschlüsselte" in created["manifest"]["warning"].lower() or "unverschl" in created["manifest"]["warning"], "personal-data warning")
|
||||
verified = verify_archive(archive)
|
||||
expect(verified["manifest"]["checksums"]["kansho.sqlite"], "sqlite checksum present")
|
||||
with zipfile.ZipFile(archive) as zf:
|
||||
names = zf.namelist()
|
||||
expect("kansho.sqlite" in names, "sqlite in archive")
|
||||
expect("media/profile/shot.png" in names, "media in archive")
|
||||
expect(".env" not in names, ".env excluded")
|
||||
expect(all("sk-secret" not in zf.read(name).decode("latin1", errors="ignore") for name in names if name.endswith(".json") or name.endswith(".txt")), "no secret in manifest/readme")
|
||||
|
||||
try:
|
||||
restore_backup(archive, db_path=db, media_root=media, confirm=False)
|
||||
expect(False, "restore without confirm must fail")
|
||||
except BackupError as exc:
|
||||
expect(exc.code == "confirm_required", "restore requires explicit confirm")
|
||||
|
||||
try:
|
||||
restore_backup(archive, db_path=db, media_root=media, confirm=True, allow_overwrite=False)
|
||||
expect(False, "restore must not silently overwrite")
|
||||
except BackupError as exc:
|
||||
expect(exc.code == "would_overwrite", "restore refuses existing data without replace")
|
||||
|
||||
target_db = base / "restore" / "kansho.sqlite"
|
||||
target_media = base / "restore" / "media"
|
||||
restored = restore_backup(
|
||||
archive,
|
||||
db_path=target_db,
|
||||
media_root=target_media,
|
||||
confirm=True,
|
||||
allow_overwrite=False,
|
||||
safety_dir=out,
|
||||
)
|
||||
expect(read_db(target_db) == "original-body", "restored sqlite content")
|
||||
expect((target_media / "profile" / "shot.png").read_bytes() == b"png-bytes", "restored media")
|
||||
expect(restored["safety_backup"] is None, "empty target needs no safety backup")
|
||||
|
||||
write_db(target_db, "changed-live")
|
||||
(target_media / "profile" / "extra.bin").write_bytes(b"extra")
|
||||
replaced = restore_backup(
|
||||
archive,
|
||||
db_path=target_db,
|
||||
media_root=target_media,
|
||||
confirm=True,
|
||||
allow_overwrite=True,
|
||||
safety_dir=out,
|
||||
)
|
||||
expect(read_db(target_db) == "original-body", "replace restores snapshot")
|
||||
expect(not (target_media / "profile" / "extra.bin").exists(), "extra live media not kept")
|
||||
expect(Path(replaced["safety_backup"]).is_file(), "safety backup written before replace")
|
||||
safety = verify_archive(Path(replaced["safety_backup"]))
|
||||
expect(safety["manifest"]["kind"] == "kansho.local_backup", "safety backup is a real archive")
|
||||
|
||||
broken = out / "broken.zip"
|
||||
with zipfile.ZipFile(archive) as src, zipfile.ZipFile(broken, "w") as dest:
|
||||
for info in src.infolist():
|
||||
data = src.read(info.filename)
|
||||
if info.filename == "kansho.sqlite":
|
||||
data = data + b"tamper"
|
||||
dest.writestr(info, data)
|
||||
before = read_db(target_db)
|
||||
try:
|
||||
restore_backup(broken, db_path=target_db, media_root=target_media, confirm=True, allow_overwrite=True)
|
||||
expect(False, "tampered archive must fail")
|
||||
except BackupError as exc:
|
||||
expect(exc.code == "checksum_mismatch", "tamper detected before restore")
|
||||
expect(read_db(target_db) == before, "failed restore leaves current db")
|
||||
|
||||
extra_zip = out / "extra.zip"
|
||||
with zipfile.ZipFile(archive) as src, zipfile.ZipFile(extra_zip, "w") as dest:
|
||||
for info in src.infolist():
|
||||
dest.writestr(info, src.read(info.filename))
|
||||
dest.writestr("notes.txt", "not in manifest")
|
||||
extra_verified = verify_archive(extra_zip)
|
||||
expect("notes.txt" in extra_verified["skipped_extra"], "extra archive files are skipped deterministically")
|
||||
|
||||
locked_db = base / "locked" / "kansho.sqlite"
|
||||
locked_media = base / "locked" / "media"
|
||||
locked_db.parent.mkdir()
|
||||
locked_media.mkdir()
|
||||
write_db(locked_db, "locked")
|
||||
holder = sqlite3.connect(str(locked_db), timeout=1)
|
||||
holder.execute("BEGIN EXCLUSIVE")
|
||||
try:
|
||||
restore_backup(
|
||||
archive,
|
||||
db_path=locked_db,
|
||||
media_root=locked_media,
|
||||
confirm=True,
|
||||
allow_overwrite=True,
|
||||
safety_dir=out,
|
||||
)
|
||||
expect(False, "in-use db must abort")
|
||||
except BackupError as exc:
|
||||
expect(exc.code in {"db_in_use", "db_unavailable", "backup_exists", "would_overwrite"} or "in_use" in exc.code or "unavailable" in exc.code, f"busy db fail-closed ({exc.code})")
|
||||
finally:
|
||||
holder.rollback()
|
||||
holder.close()
|
||||
|
||||
expect(True, "backup tests used only temporary directories")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,193 @@
|
||||
"""Network-free OpenRouter catalog tests. Run from backend/: python tests/test_model_catalog.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-model-catalog-test.sqlite")
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from model_catalog import (
|
||||
ModelWindow,
|
||||
catalog_ttl,
|
||||
reset_catalog,
|
||||
resolve_generate_metadata,
|
||||
)
|
||||
from prompt_budget import ERROR_MODEL_METADATA_UNKNOWN, JournalBudgetError
|
||||
from providers import ProviderConfig
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, payload, status_code: int = 200, text: str = ""):
|
||||
self._payload = payload
|
||||
self.status_code = status_code
|
||||
self.text = text
|
||||
|
||||
def json(self):
|
||||
if isinstance(self._payload, Exception):
|
||||
raise self._payload
|
||||
return self._payload
|
||||
|
||||
|
||||
def _openrouter_config(model: str = "openai/gpt-4o") -> ProviderConfig:
|
||||
return ProviderConfig(
|
||||
role="generate",
|
||||
name="openrouter",
|
||||
mode="http",
|
||||
url="https://openrouter.ai/api/v1/chat/completions",
|
||||
model=model,
|
||||
key="sk-test",
|
||||
local=False,
|
||||
zdr=True,
|
||||
no_train=True,
|
||||
)
|
||||
|
||||
|
||||
def _clear_env(*names: str) -> dict[str, str | None]:
|
||||
previous = {}
|
||||
for name in names:
|
||||
previous[name] = os.environ.pop(name, None)
|
||||
return previous
|
||||
|
||||
|
||||
def _restore_env(previous: dict[str, str | None]) -> None:
|
||||
for name, value in previous.items():
|
||||
if value is None:
|
||||
os.environ.pop(name, None)
|
||||
else:
|
||||
os.environ[name] = value
|
||||
|
||||
|
||||
def test_reads_context_and_completion() -> None:
|
||||
reset_catalog()
|
||||
previous = _clear_env("KANSHO_PROVIDER_CONTEXT_LENGTH", "KANSHO_PROVIDER_MAX_COMPLETION_TOKENS")
|
||||
payload = {
|
||||
"data": [
|
||||
{
|
||||
"id": "openai/gpt-4o",
|
||||
"context_length": 128000,
|
||||
"top_provider": {"max_completion_tokens": 16384},
|
||||
}
|
||||
]
|
||||
}
|
||||
with patch("model_catalog.httpx.get", lambda *args, **kwargs: FakeResponse(payload)):
|
||||
window = resolve_generate_metadata(_openrouter_config())
|
||||
expect(window.context_length == 128000, "catalog reads context_length")
|
||||
expect(window.max_completion_tokens == 16384, "catalog reads max completion")
|
||||
expect(window.source == "openrouter_models", "window is from the catalog")
|
||||
_restore_env(previous)
|
||||
|
||||
|
||||
def test_model_not_in_catalog() -> None:
|
||||
reset_catalog()
|
||||
previous = _clear_env("KANSHO_PROVIDER_CONTEXT_LENGTH", "KANSHO_PROVIDER_MAX_COMPLETION_TOKENS")
|
||||
payload = {"data": [{"id": "other/model", "context_length": 32000, "top_provider": {"max_completion_tokens": 4096}}]}
|
||||
with patch("model_catalog.httpx.get", lambda *args, **kwargs: FakeResponse(payload)):
|
||||
try:
|
||||
resolve_generate_metadata(_openrouter_config("missing/model"))
|
||||
raise SystemExit("FAIL: missing catalog model should fail closed")
|
||||
except JournalBudgetError as exc:
|
||||
expect(exc.code == ERROR_MODEL_METADATA_UNKNOWN, "model not in catalog is fail-closed")
|
||||
expect((exc.diagnostics or {}).get("reason") == "model_not_in_catalog", "reason is model_not_in_catalog")
|
||||
_restore_env(previous)
|
||||
|
||||
|
||||
def test_incomplete_metadata() -> None:
|
||||
reset_catalog()
|
||||
previous = _clear_env("KANSHO_PROVIDER_CONTEXT_LENGTH", "KANSHO_PROVIDER_MAX_COMPLETION_TOKENS")
|
||||
payload = {"data": [{"id": "openai/gpt-4o", "top_provider": {"max_completion_tokens": 4096}}]}
|
||||
with patch("model_catalog.httpx.get", lambda *args, **kwargs: FakeResponse(payload)):
|
||||
try:
|
||||
resolve_generate_metadata(_openrouter_config())
|
||||
raise SystemExit("FAIL: incomplete metadata should fail closed")
|
||||
except JournalBudgetError as exc:
|
||||
expect(exc.code == ERROR_MODEL_METADATA_UNKNOWN, "incomplete metadata is fail-closed")
|
||||
_restore_env(previous)
|
||||
|
||||
|
||||
def test_cache_hit_and_ttl_expiry() -> None:
|
||||
reset_catalog()
|
||||
previous = _clear_env("KANSHO_PROVIDER_CONTEXT_LENGTH", "KANSHO_PROVIDER_MAX_COMPLETION_TOKENS")
|
||||
calls = {"n": 0}
|
||||
|
||||
def fake_get(*args, **kwargs):
|
||||
calls["n"] += 1
|
||||
return FakeResponse(
|
||||
{
|
||||
"data": [
|
||||
{
|
||||
"id": "openai/gpt-4o",
|
||||
"context_length": 64000 + calls["n"],
|
||||
"top_provider": {"max_completion_tokens": 4096},
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
with patch("model_catalog.httpx.get", fake_get):
|
||||
first = resolve_generate_metadata(_openrouter_config(), now=1000.0)
|
||||
second = resolve_generate_metadata(_openrouter_config(), now=1000.0 + catalog_ttl() - 1)
|
||||
expect(calls["n"] == 1, "catalog is cached within TTL")
|
||||
expect(first.context_length == second.context_length, "cache hit returns the same window")
|
||||
expect(second.cached is True, "cached window is marked cached")
|
||||
third = resolve_generate_metadata(_openrouter_config(), now=1000.0 + catalog_ttl() + 1)
|
||||
expect(calls["n"] == 2, "catalog is refetched after TTL")
|
||||
expect(third.context_length != first.context_length, "refetch reads a new catalog row")
|
||||
_restore_env(previous)
|
||||
|
||||
|
||||
def test_env_fallback_and_fail_closed() -> None:
|
||||
reset_catalog()
|
||||
previous = _clear_env("KANSHO_PROVIDER_CONTEXT_LENGTH", "KANSHO_PROVIDER_MAX_COMPLETION_TOKENS")
|
||||
payload = {"data": [{"id": "other/model", "context_length": 32000, "top_provider": {"max_completion_tokens": 2048}}]}
|
||||
os.environ["KANSHO_PROVIDER_CONTEXT_LENGTH"] = "65536"
|
||||
os.environ["KANSHO_PROVIDER_MAX_COMPLETION_TOKENS"] = "8192"
|
||||
with patch("model_catalog.httpx.get", lambda *args, **kwargs: FakeResponse(payload)):
|
||||
window = resolve_generate_metadata(_openrouter_config("missing/model"))
|
||||
expect(window.context_length == 65536, "documented env fallback supplies the window")
|
||||
expect(window.max_completion_tokens == 8192, "env fallback supplies completion limit")
|
||||
expect(window.source == "env_fallback", "fallback is labeled as env_fallback")
|
||||
|
||||
reset_catalog()
|
||||
os.environ.pop("KANSHO_PROVIDER_CONTEXT_LENGTH", None)
|
||||
os.environ.pop("KANSHO_PROVIDER_MAX_COMPLETION_TOKENS", None)
|
||||
with patch("model_catalog.httpx.get", lambda *args, **kwargs: FakeResponse(payload)):
|
||||
try:
|
||||
resolve_generate_metadata(_openrouter_config("missing/model"))
|
||||
raise SystemExit("FAIL: no window should fail closed")
|
||||
except JournalBudgetError as exc:
|
||||
expect(exc.code == ERROR_MODEL_METADATA_UNKNOWN, "no window is fail-closed")
|
||||
|
||||
reset_catalog()
|
||||
with patch("model_catalog.httpx.get", lambda *args, **kwargs: FakeResponse({"nope": True})):
|
||||
try:
|
||||
resolve_generate_metadata(_openrouter_config())
|
||||
raise SystemExit("FAIL: bad catalog shape should fail closed")
|
||||
except JournalBudgetError as exc:
|
||||
expect(exc.code == ERROR_MODEL_METADATA_UNKNOWN, "bad metadata shape is fail-closed")
|
||||
_restore_env(previous)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
test_reads_context_and_completion()
|
||||
test_model_not_in_catalog()
|
||||
test_incomplete_metadata()
|
||||
test_cache_hit_and_ttl_expiry()
|
||||
test_env_fallback_and_fail_closed()
|
||||
print("model catalog tests passed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -10,9 +10,11 @@ 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_MEDIA_ROOT"] = str(Path(tempfile.gettempdir()) / "kansho-mvp-journal-media")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
Path(os.environ["KANSHO_MEDIA_ROOT"]).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
@@ -302,22 +304,29 @@ def main() -> None:
|
||||
|
||||
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")
|
||||
source_msgs = originals.json()["messages"]
|
||||
expect(sum(1 for item in source_msgs if item["role"] == "user") == 1, "A one user source")
|
||||
expect(any(item["role"] == "assistant" for item in source_msgs), "A assistant source present")
|
||||
|
||||
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]
|
||||
stages_a = {item.get("purpose"): item for item in (draft.json().get("trace") or {}).get("stages") or []}
|
||||
reconstruct_a = stages_a.get("journal_reconstruct")
|
||||
last_a = stages_a.get("journal_generate") or (draft.json().get("trace") or {})
|
||||
expect(reconstruct_a is not None, "A stage 1 reconstruct ran")
|
||||
reconstruct_intern = reconstruct_a.get("intern") or ""
|
||||
narrate_intern = last_a.get("intern") or ""
|
||||
expect("Kirschen" in reconstruct_intern or "Markt" in reconstruct_intern, "A reconstruct sees user words")
|
||||
expect("user:" in reconstruct_intern, "A reconstruct sees the day as dialogue")
|
||||
expect("assistant:" not in narrate_intern, "A stage 2 does not resend the day dialogue")
|
||||
expect("verified_artifact" in narrate_intern or "sources" in narrate_intern, "A stage 2 receives the reconstruction")
|
||||
expect("Stilquellen" in narrate_intern or "Dialog" in narrate_intern or "Erzählmerkmale" in narrate_intern or "Core:" in narrate_intern, "A style brief reaches generate")
|
||||
style_part = narrate_intern
|
||||
expect("Erzählmerkmale" in style_part, "A first generate still gets form hints")
|
||||
expect(
|
||||
"Heute war der Markt voll" not in style_part,
|
||||
"Heute war der Markt voll" not in style_part.split("Verified Artifact:")[0],
|
||||
"A source dialogue is not pasted a second time as style",
|
||||
)
|
||||
current_version = None
|
||||
@@ -387,11 +396,10 @@ def main() -> None:
|
||||
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")
|
||||
expect(turn_c is not None, "C gateway saw a request")
|
||||
intern_c = (turn_c.get("trace") or {}).get("intern") or ""
|
||||
expect("Hafen" in intern_c, "C prior plan in context")
|
||||
expect("nicht geschehen" in intern_c, "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."
|
||||
@@ -410,9 +418,8 @@ def main() -> None:
|
||||
"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")
|
||||
intern_e = (turn_e.get("trace") or {}).get("intern") or ""
|
||||
expect("Motivationen erfinden" in intern_e, "E no invented emotion rule")
|
||||
expect(turn_e["calls"] == 1, "E one follow-up")
|
||||
|
||||
day_f = client.post(
|
||||
@@ -498,9 +505,12 @@ def main() -> None:
|
||||
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")
|
||||
intern_i = ""
|
||||
for stage in (gen_i.json().get("trace") or {}).get("stages") or []:
|
||||
if stage.get("purpose") == "journal_generate":
|
||||
intern_i = stage.get("intern") or ""
|
||||
intern_i = intern_i or ((gen_i.json().get("trace") or {}).get("intern") or "")
|
||||
expect("knapp" in intern_i, "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")
|
||||
@@ -538,6 +548,50 @@ def main() -> None:
|
||||
(day_with_draft["current_draft"] or {}).get("body") != after_gen["body"],
|
||||
"J new draft is not the saved user text",
|
||||
)
|
||||
marker = "EXISTING_FASSUNG_XYZ_UNIQUE"
|
||||
marked = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"entry_id": saved.json()["id"],
|
||||
"title": "Markiert",
|
||||
"body": marker,
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(marked.status_code == 200, "include_existing setup save")
|
||||
marked_version = marked.json()["current_version_id"]
|
||||
without_flag = client.post(
|
||||
f"/api/journal/days/{day_id}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [conv["id"]]},
|
||||
)
|
||||
expect(without_flag.status_code == 200, f"generate default {without_flag.text}")
|
||||
without_intern = ""
|
||||
for stage in (without_flag.json().get("trace") or {}).get("stages") or []:
|
||||
if stage.get("purpose") == "journal_generate":
|
||||
without_intern = stage.get("intern") or ""
|
||||
expect(marker not in without_intern, "include_existing default does not send the saved body")
|
||||
after_without = client.get(f"/api/journal/entries/{saved.json()['id']}", headers=headers).json()
|
||||
expect(after_without["current_version_id"] == marked_version, "default generate leaves entry version")
|
||||
with_flag = client.post(
|
||||
f"/api/journal/days/{day_id}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [conv["id"]], "include_existing": True},
|
||||
)
|
||||
expect(with_flag.status_code == 200, f"generate include_existing {with_flag.text}")
|
||||
with_intern = ""
|
||||
for stage in (with_flag.json().get("trace") or {}).get("stages") or []:
|
||||
if stage.get("purpose") == "journal_generate":
|
||||
with_intern = stage.get("intern") or ""
|
||||
expect(marker in with_intern, "include_existing true sends the saved body only after explicit flag")
|
||||
existing_section = with_intern.split("Bestehender Text")[-1].split("Verified Artifact")[0]
|
||||
expect(marker in existing_section, "saved Fassung is the included existing text, not a leftover draft")
|
||||
after_with = client.get(f"/api/journal/entries/{saved.json()['id']}", headers=headers).json()
|
||||
expect(after_with["current_version_id"] == marked_version, "include_existing generate still does not overwrite")
|
||||
expect(after_with["body"] == marker, "saved user body remains")
|
||||
day_with_draft = client.get(f"/api/journal/days/{day_id}", headers=headers).json()
|
||||
n_before = len(client.get(f"/api/journal/entries/{saved.json()['id']}/versions", headers=headers).json())
|
||||
adopted = client.post(
|
||||
"/api/journal/entries",
|
||||
@@ -662,13 +716,10 @@ def main() -> None:
|
||||
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")
|
||||
blob = (turn_scratch.json().get("trace") or {}).get("intern") or ""
|
||||
intern = blob
|
||||
egress = (turn_scratch.json().get("trace") or {}).get("egress") or ""
|
||||
expect(scratch_mark not in blob and scratch_mark not in intern and scratch_mark not in egress, "scratch not in dialogue egress")
|
||||
reset_debug()
|
||||
gen_scratch = client.post(
|
||||
f"/api/journal/days/{day_id}/generate",
|
||||
@@ -676,12 +727,13 @@ def main() -> None:
|
||||
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_trace = gen_scratch.json().get("trace") or {}
|
||||
gen_blob = " ".join(
|
||||
str(last_scratch_gen.get(key) or "")
|
||||
for key in ("rendered", "masked", "mask_input", "raw", "reply")
|
||||
str(stage.get(key) or "")
|
||||
for stage in (gen_trace.get("stages") or [gen_trace])
|
||||
for key in ("intern", "egress", "mask_input", "raw", "reply")
|
||||
)
|
||||
gen_intern = (gen_scratch.json().get("trace") or {}).get("intern") or ""
|
||||
gen_intern = gen_trace.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
|
||||
|
||||
@@ -51,13 +51,12 @@ def main() -> None:
|
||||
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")
|
||||
trace = turn.json().get("trace") or {}
|
||||
expect(trace, "gateway returned request-scoped trace")
|
||||
expect(trace["detect_provider"] == "fake-detect", "detect provider is separate")
|
||||
expect("Anna" not in (trace.get("egress") or ""), "Anna masked before generate")
|
||||
expect("Hamburg" not in (trace.get("egress") or ""), "Hamburg masked before generate")
|
||||
expect("[[PERSON:" in (trace.get("egress") or "") or "[[PLACE:" in (trace.get("egress") or ""), "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}
|
||||
@@ -72,10 +71,10 @@ def main() -> None:
|
||||
json={"body": "Anna war ruhig."},
|
||||
)
|
||||
expect(again.status_code == 200, "second turn")
|
||||
from privacy_gateway import debug_last as last2
|
||||
last2 = again.json().get("trace") or {}
|
||||
|
||||
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")
|
||||
expect("nicht" in (last2.get("egress") or "").lower() or "Anna" not in (last2.get("egress") or ""), "dictionary remasks Anna without new invention")
|
||||
expect(last2.get("mapping_count", 0) >= 2, "dictionary reused")
|
||||
|
||||
from entity_detect import _pattern_entities
|
||||
from identity_store import is_maskable_label
|
||||
@@ -251,6 +250,25 @@ def main() -> None:
|
||||
except PrivacyGatewayError as exc:
|
||||
blocked = exc.code == "response_validation_failed"
|
||||
expect(blocked, "plaintext identity is blocked before demask")
|
||||
sushi_map = [{"local_label": "Sushi", "token": "PERSON:01"}]
|
||||
food_reply = _validate_response("Heute nur von Sushi essen erzählt.", sushi_map)
|
||||
expect("Sushi essen" in food_reply, "food homonym in a reply is not treated as a leak")
|
||||
mixed = mask_for_egress(
|
||||
"Belegte Vorhaben (nur user-Quellen):\n- Morgen wollen wir mit Anna fahren.\nuser: Heute Markt.",
|
||||
mapped,
|
||||
"dialogue_turn",
|
||||
)
|
||||
expect("Anna" not in mixed, "opening context names are masked for dialogue egress")
|
||||
expect("[[PERSON:01]]" in mixed, "opening context uses the person token")
|
||||
expect("Heute Markt" in mixed, "current user wording remains")
|
||||
excerpt = mask_for_egress(
|
||||
"Frühere Einträge im Space (nur Hinweise, keine Gewissheit):\n2026-08-01: Anna war am Hafen.\nuser: Heute windstill.",
|
||||
mapped,
|
||||
"dialogue_turn",
|
||||
)
|
||||
expect("Anna" not in excerpt, "space excerpts are masked for dialogue egress")
|
||||
expect("[[PERSON:01]]" in excerpt, "space excerpt uses the person token")
|
||||
|
||||
print("All privacy detect tests passed.")
|
||||
|
||||
|
||||
|
||||
@@ -183,11 +183,11 @@ def main() -> None:
|
||||
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
|
||||
intern = (turn.json().get("trace") or {}).get("intern") or ""
|
||||
|
||||
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")
|
||||
expect("Produktdefault" in intern, "hint reaches dialogue prompt")
|
||||
expect("Ein bis drei Sätze" in intern, "length default is in hint not anonymous")
|
||||
expect("anonyme Produktregel" in intern, "prompt labels the moved preference")
|
||||
|
||||
proposed = propose_observation(
|
||||
profile_id,
|
||||
@@ -223,10 +223,10 @@ def main() -> None:
|
||||
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
|
||||
intern = (later.json().get("trace") or {}).get("intern") or ""
|
||||
|
||||
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("Meist nur ein Satz" in intern, "personal preference reaches hint")
|
||||
expect("persönlich" in intern, "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")
|
||||
|
||||
|
||||
@@ -260,9 +260,9 @@ def main() -> None:
|
||||
json={"body": long_body},
|
||||
)
|
||||
expect(long_turn.status_code == 200, "long turn")
|
||||
from privacy_gateway import debug_last as last_long
|
||||
last_long = long_turn.json().get("trace") or {}
|
||||
|
||||
expect(last_long["purpose"] == "dialogue_turn", "long turn still has no review call")
|
||||
expect(last_long.get("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")
|
||||
|
||||
@@ -514,10 +514,10 @@ def main() -> None:
|
||||
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
|
||||
api_trace = api_review.json().get("trace") or {}
|
||||
|
||||
expect(api_trace["purpose"] == "profile_review", "api review uses dedicated purpose")
|
||||
expect(api_trace["layer"] == "profilreview", "trace is not a dialogue turn")
|
||||
expect(api_trace.get("purpose") == "profile_review", "api review uses dedicated purpose")
|
||||
expect(api_trace.get("layer") == "profilreview", "trace is not a dialogue turn")
|
||||
expect(api_review.json()["applied"] == 0, "api result is a proposal, not an auto-apply")
|
||||
expect(api_review.json()["suggested"] >= 1, "advising queues review as suggestion")
|
||||
expect(
|
||||
|
||||
@@ -0,0 +1,255 @@
|
||||
"""Intent-neutral provenance verification. Run from backend/: python tests/test_provenance.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ.setdefault(
|
||||
"KANSHO_DB_PATH",
|
||||
str(Path(tempfile.gettempdir()) / "kansho-provenance-test.sqlite"),
|
||||
)
|
||||
|
||||
from provenance import (
|
||||
COVERAGE_ALL_SELECTED_SOURCES,
|
||||
COVERAGE_SELECTED_EVIDENCE,
|
||||
EvidenceRef,
|
||||
ProvenanceError,
|
||||
SourceRecord,
|
||||
SourceRegistry,
|
||||
VerificationPolicy,
|
||||
verify,
|
||||
)
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def _expect_error(reason: str, **kwargs) -> None:
|
||||
try:
|
||||
verify(**kwargs)
|
||||
raise SystemExit(f"FAIL: expected {reason}")
|
||||
except ProvenanceError as exc:
|
||||
expect(exc.reason == reason, f"reason is {reason}, got {exc.reason}")
|
||||
|
||||
|
||||
CHANGELOG = (
|
||||
"Build 14.2 ersetzt den Hash-Algorithmus. "
|
||||
"Die Checksummen-Datei liegt unter /var/lib/app/SHA256SUMS. "
|
||||
"Der Paketname bleibt libcore."
|
||||
)
|
||||
NOTE = "Der Review-Kommentar betrifft nur die Signaturprüfung."
|
||||
|
||||
|
||||
def _registry() -> SourceRegistry:
|
||||
return SourceRegistry(
|
||||
[
|
||||
SourceRecord("s1", "primary", CHANGELOG),
|
||||
SourceRecord("s2", "note", NOTE),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def test_unknown_source() -> None:
|
||||
registry = _registry()
|
||||
policy = VerificationPolicy(allowed_roles=frozenset({"primary"}), selected_ids=("s9",))
|
||||
_expect_error("unknown_source", registry=registry, policy=policy)
|
||||
|
||||
|
||||
def test_disallowed_role() -> None:
|
||||
registry = _registry()
|
||||
policy = VerificationPolicy(allowed_roles=frozenset({"primary"}), selected_ids=("s2",))
|
||||
_expect_error("role_not_allowed", registry=registry, policy=policy)
|
||||
|
||||
|
||||
def test_wrong_source_excerpt() -> None:
|
||||
registry = _registry()
|
||||
policy = VerificationPolicy(allowed_roles=frozenset({"primary", "note"}), selected_ids=("s1", "s2"))
|
||||
_expect_error(
|
||||
"wrong_source",
|
||||
registry=registry,
|
||||
policy=policy,
|
||||
evidence=[EvidenceRef("s1", "Signaturprüfung")],
|
||||
)
|
||||
|
||||
|
||||
def test_invented_excerpt() -> None:
|
||||
registry = _registry()
|
||||
policy = VerificationPolicy(allowed_roles=frozenset({"primary"}), selected_ids=("s1",))
|
||||
_expect_error(
|
||||
"unverified_excerpt",
|
||||
registry=registry,
|
||||
policy=policy,
|
||||
evidence=[EvidenceRef("s1", "Die Lizenz ändert sich auf GPL.")],
|
||||
)
|
||||
|
||||
|
||||
def test_all_selected_rehydrates_full_text() -> None:
|
||||
registry = _registry()
|
||||
policy = VerificationPolicy(
|
||||
allowed_roles=frozenset({"primary"}),
|
||||
coverage=COVERAGE_ALL_SELECTED_SOURCES,
|
||||
selected_ids=("s1",),
|
||||
)
|
||||
artifact = verify(
|
||||
registry,
|
||||
policy=policy,
|
||||
evidence=[EvidenceRef("s1", "Build 14.2")],
|
||||
)
|
||||
expect(artifact.sources[0]["text"] == CHANGELOG, "full canonical changelog is rehydrated")
|
||||
expect("SHA256SUMS" in artifact.sources[0]["text"], "later changelog sentences stay")
|
||||
expect("libcore" in artifact.sources[0]["text"], "final changelog sentence stays")
|
||||
|
||||
|
||||
def test_excerpt_cannot_drop_remainder() -> None:
|
||||
registry = _registry()
|
||||
policy = VerificationPolicy(allowed_roles=frozenset({"primary"}), selected_ids=("s1",))
|
||||
artifact = verify(registry, policy=policy, evidence=[EvidenceRef("s1", "Build 14.2")])
|
||||
expect(artifact.sources[0]["text"] != "Build 14.2", "a short excerpt is not the source")
|
||||
expect(artifact.sources[0]["text"].startswith("Build 14.2 ersetzt"), "canonical prefix remains")
|
||||
expect("Paketname bleibt libcore" in artifact.sources[0]["text"], "unread remainder is not dropped")
|
||||
|
||||
|
||||
def test_model_cannot_overwrite_canonical() -> None:
|
||||
registry = _registry()
|
||||
policy = VerificationPolicy(allowed_roles=frozenset({"primary"}), selected_ids=("s1",))
|
||||
_expect_error(
|
||||
"canonical_overwrite",
|
||||
registry=registry,
|
||||
policy=policy,
|
||||
proposed_texts={"s1": "Build 14.2 ist ein Sicherheitsrelease ohne Dateipfade."},
|
||||
)
|
||||
artifact = verify(
|
||||
registry,
|
||||
policy=policy,
|
||||
proposed_texts={"s1": CHANGELOG},
|
||||
)
|
||||
expect(artifact.sources[0]["text"] == CHANGELOG, "identical proposed text is ignored as authority")
|
||||
|
||||
|
||||
def test_annotations_are_not_verified_content() -> None:
|
||||
registry = _registry()
|
||||
excluded = VerificationPolicy(
|
||||
allowed_roles=frozenset({"primary"}),
|
||||
selected_ids=("s1",),
|
||||
include_unverified_annotations=False,
|
||||
)
|
||||
artifact = verify(
|
||||
registry,
|
||||
policy=excluded,
|
||||
evidence=[EvidenceRef("s1", CHANGELOG)],
|
||||
annotations=[{"kind": "risk", "source_id": "s1", "excerpt": CHANGELOG}],
|
||||
)
|
||||
payload = artifact.to_payload()
|
||||
expect("annotations_unverified" not in payload, "excluded annotations do not appear")
|
||||
expect(all("kind" not in item for item in payload["sources"]), "source records have no semantic kind")
|
||||
dumped = str(payload)
|
||||
expect("risk" not in dumped, "unverified label is not in verified payload")
|
||||
|
||||
separated = VerificationPolicy(
|
||||
allowed_roles=frozenset({"primary"}),
|
||||
selected_ids=("s1",),
|
||||
include_unverified_annotations=True,
|
||||
)
|
||||
held = verify(
|
||||
registry,
|
||||
policy=separated,
|
||||
annotations=[{"kind": "risk", "source_id": "s1", "excerpt": CHANGELOG}],
|
||||
)
|
||||
expect(held.annotations_unverified[0]["kind"] == "risk", "unverified labels can be held separately")
|
||||
expect(held.sources[0]["text"] == CHANGELOG, "canonical text is independent of the label")
|
||||
|
||||
|
||||
def test_policies_differ_without_domain_logic() -> None:
|
||||
registry = _registry()
|
||||
primary_only = VerificationPolicy(allowed_roles=frozenset({"primary"}), selected_ids=("s2",))
|
||||
_expect_error("role_not_allowed", registry=registry, policy=primary_only)
|
||||
|
||||
with_notes = VerificationPolicy(
|
||||
allowed_roles=frozenset({"primary", "note"}),
|
||||
coverage=COVERAGE_ALL_SELECTED_SOURCES,
|
||||
selected_ids=("s2",),
|
||||
)
|
||||
accepted = verify(registry, policy=with_notes)
|
||||
expect(accepted.sources[0]["text"] == NOTE, "a second policy can allow another role")
|
||||
|
||||
selective = VerificationPolicy(
|
||||
allowed_roles=frozenset({"primary"}),
|
||||
coverage=COVERAGE_SELECTED_EVIDENCE,
|
||||
selected_ids=("s1",),
|
||||
)
|
||||
excerpt_only = verify(
|
||||
registry,
|
||||
policy=selective,
|
||||
evidence=[EvidenceRef("s1", "Build 14.2")],
|
||||
)
|
||||
expect(excerpt_only.sources[0]["text"] == "Build 14.2", "selected-evidence mode keeps only verified excerpts")
|
||||
expect("SHA256SUMS" not in excerpt_only.sources[0]["text"], "selected-evidence mode does not rehydrate the rest")
|
||||
|
||||
full = verify(
|
||||
registry,
|
||||
policy=VerificationPolicy(
|
||||
allowed_roles=frozenset({"primary"}),
|
||||
coverage=COVERAGE_ALL_SELECTED_SOURCES,
|
||||
selected_ids=("s1",),
|
||||
),
|
||||
evidence=[EvidenceRef("s1", "Build 14.2")],
|
||||
)
|
||||
expect("SHA256SUMS" in full.sources[0]["text"], "all-selected mode still rehydrates the same registry")
|
||||
|
||||
|
||||
def test_parallel_requests_are_isolated() -> None:
|
||||
registry_ok = SourceRegistry([SourceRecord("a1", "primary", "Alpha-Release 3 ist signiert.")])
|
||||
registry_bad = SourceRegistry([SourceRecord("b1", "primary", "Beta-Kanal bleibt intern.")])
|
||||
ok_policy = VerificationPolicy(allowed_roles=frozenset({"primary"}), selected_ids=("a1",))
|
||||
bad_policy = VerificationPolicy(allowed_roles=frozenset({"primary"}), selected_ids=("missing",))
|
||||
results: dict[str, object] = {}
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
def succeed() -> None:
|
||||
results["ok"] = verify(registry_ok, policy=ok_policy)
|
||||
|
||||
def fail() -> None:
|
||||
try:
|
||||
verify(registry_bad, policy=bad_policy)
|
||||
except ProvenanceError as exc:
|
||||
errors["bad"] = exc.reason
|
||||
|
||||
first = threading.Thread(target=fail)
|
||||
second = threading.Thread(target=succeed)
|
||||
first.start()
|
||||
second.start()
|
||||
first.join()
|
||||
second.join()
|
||||
expect(errors.get("bad") == "unknown_source", "failing request keeps its own reason")
|
||||
artifact = results.get("ok")
|
||||
expect(artifact is not None, "successful request still returns an artifact")
|
||||
expect(artifact.sources[0]["text"] == "Alpha-Release 3 ist signiert.", "parallel failure does not rewrite the other registry")
|
||||
later = verify(registry_ok, policy=ok_policy)
|
||||
expect(later.sources[0]["text"] == "Alpha-Release 3 ist signiert.", "later request is unaffected")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
test_unknown_source()
|
||||
test_disallowed_role()
|
||||
test_wrong_source_excerpt()
|
||||
test_invented_excerpt()
|
||||
test_all_selected_rehydrates_full_text()
|
||||
test_excerpt_cannot_drop_remainder()
|
||||
test_model_cannot_overwrite_canonical()
|
||||
test_annotations_are_not_verified_content()
|
||||
test_policies_differ_without_domain_logic()
|
||||
test_parallel_requests_are_isolated()
|
||||
print("provenance tests passed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user