test(desktop): verify full drill-down, traceability and adversarial provenance rejection
davoodmehraban89 committed
Oct 31, 2026 at 21:37 UTC
2c77779ffc7cc28bd84d7ebf406685de5bae11ee
1 file changed
+138
backend/tests/test_desktop_five_statement_details.py
new
+138
@@ -0,0 +1,138 @@
1
+"""Adversarial tests for desktop statement drill-down and provenance."""
2
+from dataclasses import replace
3
+
4
+import pytest
5
+
6
+from aram_backend.coverage_preview import CoverageError
7
+from aram_backend.desktop_five_statement_details import (
8
+ build_preview_details,
9
+)
10
+from aram_backend.desktop_five_statement_integrity import (
11
+ validate_desktop_pipeline_result,
12
+)
13
+from test_five_statement_pipeline_integration import build_pipeline
14
+from aram_backend.synthetic_five_statement_demo import cash_transactions
15
+
16
+
17
+def test_details_cover_every_candidate_line_and_account_row():
18
+ result = build_pipeline()
19
+ rows = build_preview_details(result)
20
+ expected = (
21
+ len(result.balance_sheet.lines)
22
+ + len(result.income_statement.lines)
23
+ + len(result.equity_changes.note_movements)
24
+ + len(result.equity_changes.account_movements)
25
+ + len(result.cash_flow.cash_flow_lines)
26
+ + len(result.cash_flow.account_reconciliations)
27
+ + len(result.trial_balance.accounts)
28
+ )
29
+ assert len(rows) == expected
30
+ assert {row.statement for row in rows} == {
31
+ "ترازنامه", "سود و زیان", "تغییرات حقوق مالکانه",
32
+ "جریان وجوه نقد", "تراز آزمایشی",
33
+ }
34
+ cash = next(row for row in rows if row.item == "Cash and cash equivalents")
35
+ assert cash.opening_or_source == "50"
36
+ assert cash.closing_or_presentation == "50"
37
+ profit = next(row for row in rows if row.item == "Revenue")
38
+ assert profit.closing_or_presentation == "40"
39
+ assert all(row.state == "قابل بررسی؛ غیررسمی" for row in rows)
40
+ assert all(len(row.values()) == 11 for row in rows)
41
+
42
+
43
+def test_cash_transactions_and_trial_balance_accounts_are_individually_traceable():
44
+ result = build_pipeline()
45
+ rows = build_preview_details(result)
46
+ cash_rows = [row for row in rows if row.statement == "جریان وجوه نقد"]
47
+ tb_rows = [row for row in rows if row.statement == "تراز آزمایشی"]
48
+ assert len(cash_rows) == (
49
+ len(result.cash_flow.cash_flow_lines)
50
+ + len(result.cash_flow.account_reconciliations)
51
+ )
52
+ assert any("سند" in row.reference for row in cash_rows)
53
+ assert len(tb_rows) == len(result.trial_balance.accounts)
54
+ assert all("کد حساب" in row.reference for row in tb_rows)
55
+
56
+
57
+def test_blocked_pipeline_details_are_explicitly_diagnostic():
58
+ result = build_pipeline(transactions=cash_transactions()[:-1])
59
+ assert not result.eligible_for_analyst_review
60
+ rows = build_preview_details(result)
61
+ assert rows
62
+ assert all(row.state == "مسدود؛ صرفاً تشخیصی" for row in rows)
63
+
64
+
65
+@pytest.mark.parametrize("bad", ["NaN", "Infinity", "1e500", "", "not-a-number"])
66
+def test_invalid_detail_amount_fails_closed(bad):
67
+ result = build_pipeline()
68
+ line = replace(result.balance_sheet.lines[0], presentation_amount=bad)
69
+ changed = replace(
70
+ result, balance_sheet=replace(
71
+ result.balance_sheet, lines=(line,) + result.balance_sheet.lines[1:],
72
+ ),
73
+ )
74
+ with pytest.raises(CoverageError):
75
+ build_preview_details(changed)
76
+
77
+
78
+def test_invalid_detail_text_with_control_character_fails_closed():
79
+ result = build_pipeline()
80
+ line = replace(result.income_statement.lines[0], line_label="Revenue\nforged")
81
+ changed = replace(
82
+ result, income_statement=replace(
83
+ result.income_statement, lines=(line,) + result.income_statement.lines[1:],
84
+ ),
85
+ )
86
+ with pytest.raises(CoverageError):
87
+ build_preview_details(changed)
88
+
89
+
90
+def test_row_cap_refuses_truncation_instead_of_silently_hiding_detail(monkeypatch):
91
+ import aram_backend.desktop_five_statement_details as module
92
+ monkeypatch.setattr(module, "MAX_DETAIL_ROWS", 1)
93
+ with pytest.raises(CoverageError, match="ROW_LIMIT"):
94
+ build_preview_details(build_pipeline())
95
+
96
+
97
+def test_integrity_gate_rejects_forged_approval_flags():
98
+ result = build_pipeline()
99
+ forged = replace(
100
+ result, balance_sheet=replace(
101
+ result.balance_sheet, official_financial_statement=True,
102
+ ),
103
+ )
104
+ with pytest.raises(CoverageError, match="PROVENANCE"):
105
+ validate_desktop_pipeline_result(forged)
106
+
107
+
108
+def test_integrity_gate_rejects_cross_statement_source_hash_mismatch():
109
+ result = build_pipeline()
110
+ changed = replace(
111
+ result, trial_balance=replace(
112
+ result.trial_balance, source_sha256="0" * 64,
113
+ ),
114
+ )
115
+ with pytest.raises(CoverageError, match="SOURCE_SNAPSHOT"):
116
+ validate_desktop_pipeline_result(changed)
117
+
118
+
119
+def test_integrity_gate_rejects_tampered_summary_cross_tie():
120
+ result = build_pipeline()
121
+ changed = replace(
122
+ result, balance_sheet=replace(
123
+ result.balance_sheet, equity_total="123",
124
+ ),
125
+ )
126
+ with pytest.raises(CoverageError, match="RECONCILIATION"):
127
+ validate_desktop_pipeline_result(changed)
128
+
129
+
130
+def test_integrity_gate_rejects_truthy_non_boolean_approval_flag():
131
+ result = build_pipeline()
132
+ forged = replace(
133
+ result, cash_flow=replace(
134
+ result.cash_flow, transaction_ledger_completeness_verified=1,
135
+ ),
136
+ )
137
+ with pytest.raises(CoverageError, match="PROVENANCE"):
138
+ validate_desktop_pipeline_result(forged)