"""Check a point-in-time supplier confirmation rule on declared fixtures.""" from dataclasses import replace from datetime import datetime, timedelta, timezone import json from pathlib import Path import sys sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "draft-0.4" / "treasury")) from replay import Evidence, snapshot UTC = timezone.utc BASE = datetime(2026, 11, 19, 11, tzinfo=UTC) def test_cases(): request = Evidence("order", "org-a", "partner-a", "request", BASE, BASE, "r5", "RUB", 24_000_000) confirmation = Evidence("confirm", "org-a", "partner-a", "confirmation", BASE, BASE, "r5") stock = Evidence( "earlier", "org-a", "partner-a", "settled", BASE - timedelta(days=5), BASE - timedelta(days=4), "r4", "RUB", 9_000_000, ) alert = Evidence("alert", "org-a", "partner-a", "security_alert", BASE, BASE) other_partner = replace(confirmation, event_id="other-partner", supplier="partner-b") other_org = replace(confirmation, event_id="other-org", organization="org-b") other_revision = replace(confirmation, event_id="previous-revision", revision="r4") late = replace(confirmation, available_at=BASE + timedelta(minutes=8)) second_request = replace(request, supplier="partner-z", currency="EUR") second_confirmation = replace(confirmation, supplier="partner-z") return ( ("Сверка отсутствует", request, [], BASE, True), ("Нужная версия подтверждена", request, [confirmation], BASE, False), ("Другой поставщик", request, [other_partner], BASE, True), ("Другая организация", request, [other_org], BASE, True), ("Предыдущая версия", request, [other_revision], BASE, True), ("Сверка ещё не поступила", request, [late], BASE, True), ("Та же сверка после поступления", request, [late], BASE + timedelta(minutes=8), False), ("Повтор допустимой сверки", request, [confirmation, confirmation], BASE, False), ("Оплата по прежней версии", request, [stock], BASE, True), ("Сигнал ИБ без сверки", request, [alert], BASE, True), ("Допустимая сверка при двух чужих", request, [other_revision, confirmation, other_org], BASE, False), ("Другой поставщик и другая валюта", second_request, [second_confirmation, stock], BASE, False), ) def check_matrix(actual, predicted): counts = [[0, 0], [0, 0]] for need, sent_to_review in zip(actual, predicted): counts[int(need)][int(sent_to_review)] += 1 return counts def evaluate(cases): output = [] for name, request, events, timestamp, expected in cases: naive = not any(event.kind == "confirmation" and event.available_at <= timestamp for event in events) decision = snapshot(request, events, timestamp) scoped = decision.state == "verification_pending" output.append({ "case": name, "expected_second_check": expected, "batch_flag_second_check": naive, "point_in_time_second_check": scoped, }) actual = [row["expected_second_check"] for row in output] return { "scope": "Declared synthetic fixtures for deterministic review routing, no business labels", "matrix_axes": ["Expected second check [No, Yes]", "Sent for second check [No, Yes]"], "scenarios": output, "batch_flag": check_matrix(actual, [r["batch_flag_second_check"] for r in output]), "point_in_time": check_matrix(actual, [r["point_in_time_second_check"] for r in output]), } def main(): cases = test_cases() report = evaluate(cases) assert report["point_in_time"] == [[5, 0], [0, 7]] assert report == evaluate([(name, request, list(reversed(events)), at, expected) for name, request, events, at, expected in cases]) request, valid = cases[1][1:3] try: snapshot(request, [valid[0], replace(valid[0], revision="conflicting")], BASE) except ValueError: pass else: raise AssertionError("Conflicting evidence must be rejected") assert snapshot(request, valid, BASE - timedelta(seconds=1)).state == "request_not_available" assert snapshot(request, [], BASE).payments_in_30d is None Path(__file__).with_name("decision_audit.json").write_text( json.dumps(report, ensure_ascii=False, indent=2, allow_nan=False) + "\n" ) if __name__ == "__main__": main()