142 lines
5.1 KiB
Python
142 lines
5.1 KiB
Python
"""Tests for scripts/assemble_telegram_visible_direct_claim_capture_receipt.py."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(REPO_ROOT / "scripts"))
|
|
|
|
import assemble_telegram_visible_direct_claim_capture_receipt as receipt # noqa: E402
|
|
|
|
|
|
def load_packet() -> dict:
|
|
return json.loads(receipt.DEFAULT_PACKET.read_text(encoding="utf-8"))
|
|
|
|
|
|
def handler_replies() -> dict[str, str]:
|
|
data = json.loads((REPO_ROOT / "docs/reports/leo-working-state-20260709/telegram-handler-direct-claim-suite-current.json").read_text(encoding="utf-8"))
|
|
return {item["prompt_id"]: item["reply"] for item in data["results"]}
|
|
|
|
|
|
def good_capture(packet: dict) -> dict:
|
|
replies = handler_replies()
|
|
messages = []
|
|
for index, item in enumerate(packet["exact_messages"], start=1):
|
|
messages.append(
|
|
{
|
|
"prompt_id": item["prompt_id"],
|
|
"sent_text": item["message"],
|
|
"reply": replies[item["prompt_id"]],
|
|
"sent_at_utc": f"2026-07-10T03:0{index}:00Z",
|
|
"reply_at_utc": f"2026-07-10T03:0{index}:30Z",
|
|
"telegram_message_id": f"sent-{index}",
|
|
"reply_message_id": f"reply-{index}",
|
|
}
|
|
)
|
|
return {
|
|
"operator_authorization_text": packet["explicit_operator_authorization_text"],
|
|
"capture_source": {
|
|
"platform": "telegram",
|
|
"chat_label": "Leo",
|
|
"chat_id": "-5146042086",
|
|
"captured_at_utc": "2026-07-10T03:08:00Z",
|
|
"captured_by": "codex",
|
|
"final_screenshot": "outputs/telegram-visible-direct-claim-suite-20260710/final.png",
|
|
"final_accessibility": "outputs/telegram-visible-direct-claim-suite-20260710/final.txt",
|
|
},
|
|
"db_before_after": {
|
|
"before": {"public.claims": 1837},
|
|
"after": {"public.claims": 1837},
|
|
"db_counts_changed": False,
|
|
},
|
|
"service_readback": {
|
|
"before": {"ActiveState": "active", "SubState": "running"},
|
|
"after": {"ActiveState": "active", "SubState": "running"},
|
|
},
|
|
"messages": messages,
|
|
}
|
|
|
|
|
|
def test_receipt_without_capture_emits_awaiting_capture_template():
|
|
packet = load_packet()
|
|
|
|
data = receipt.build_receipt(packet, None, source_capture_path=None)
|
|
|
|
assert data["status"] == "awaiting_capture"
|
|
assert data["receipt_ready"] is False
|
|
assert data["capture_template"]["messages"][0]["prompt_id"] == "DC-01"
|
|
assert data["checks"]["missing_prompt_ids"] == ["DC-01", "DC-02", "DC-03", "DC-04", "DC-05", "DC-06"]
|
|
|
|
|
|
def test_good_capture_scores_and_receipt_passes():
|
|
packet = load_packet()
|
|
|
|
data = receipt.build_receipt(packet, good_capture(packet), source_capture_path=Path("capture.json"))
|
|
|
|
assert data["status"] == "pass"
|
|
assert data["receipt_ready"] is True
|
|
assert data["score"]["pass"] is True
|
|
assert data["score"]["passes"] == data["score"]["expected_prompt_count"] == 6
|
|
assert len(data["results"]) == 6
|
|
assert data["results"][0]["evidence_tier"] == "telegram_visible_capture"
|
|
|
|
|
|
def test_capture_with_message_text_mismatch_fails():
|
|
packet = load_packet()
|
|
capture = good_capture(packet)
|
|
capture["messages"][0]["sent_text"] = "wrong"
|
|
|
|
data = receipt.build_receipt(packet, capture, source_capture_path=Path("capture.json"))
|
|
|
|
assert data["status"] == "fail"
|
|
assert data["receipt_ready"] is False
|
|
assert data["checks"]["sent_text_exact"] is False
|
|
assert data["checks"]["text_mismatch_prompt_ids"] == ["DC-01"]
|
|
|
|
|
|
def test_capture_with_empty_reply_fails():
|
|
packet = load_packet()
|
|
capture = good_capture(packet)
|
|
capture["messages"][1]["reply"] = ""
|
|
|
|
data = receipt.build_receipt(packet, capture, source_capture_path=Path("capture.json"))
|
|
|
|
assert data["status"] == "fail"
|
|
assert data["receipt_ready"] is False
|
|
assert data["checks"]["replies_nonempty"] is False
|
|
assert data["checks"]["empty_reply_prompt_ids"] == ["DC-02"]
|
|
|
|
|
|
def test_main_writes_awaiting_capture_artifacts(tmp_path: Path):
|
|
out = tmp_path / "receipt.json"
|
|
md = tmp_path / "receipt.md"
|
|
results = tmp_path / "results.json"
|
|
|
|
code = receipt.main(["--out", str(out), "--markdown-out", str(md), "--results-out", str(results)])
|
|
|
|
assert code == 0
|
|
assert json.loads(out.read_text(encoding="utf-8"))["status"] == "awaiting_capture"
|
|
assert "Telegram-Visible Direct-Claim Capture Receipt" in md.read_text(encoding="utf-8")
|
|
assert not results.exists()
|
|
|
|
|
|
def test_receipt_records_cli_override_paths(tmp_path: Path):
|
|
packet_path = tmp_path / "packet.json"
|
|
results = tmp_path / "results.json"
|
|
packet = load_packet()
|
|
|
|
packet_path.write_text(json.dumps(packet), encoding="utf-8")
|
|
data = receipt.build_receipt(
|
|
packet,
|
|
good_capture(packet),
|
|
source_capture_path=tmp_path / "capture.json",
|
|
packet_path=packet_path,
|
|
results_out=results,
|
|
)
|
|
|
|
assert data["packet_path"] == str(packet_path)
|
|
assert data["results_out"] == str(results)
|