codex/aram-analyst-review-workbook-20261031
add-index-html-10623938925177053894 arena/094f314c-aram chatgpt-phase1b2-autopilot codex/adr-future-connectors-pi-v1 codex/aram-account-note-source-trace-20261031 codex/aram-actions-node24-maintenance-20261031 codex/aram-analyst-review-workbook-20261031 codex/aram-bs-candidate-statement-20261031 codex/aram-cash-flow-direct-candidate-20261031 codex/aram-clean-reference-year-integrity-20261030 codex/aram-cross-statement-controls-20261031 codex/aram-equity-changes-candidate-20261031 codex/aram-five-statement-cli-20261031 codex/aram-five-statement-integration-20261031 codex/aram-five-statement-orchestrator-20261031 codex/aram-gate3-coverage-target-guard-20261030 codex/aram-gate3-hidden-xlsx-guard-20261030 codex/aram-gate3-integration-validation-20261030 codex/aram-gate3-mapping-change-preview-20261030 codex/aram-gate3-stale-note-guard-20261030 codex/aram-income-statement-candidate-20261031 codex/aram-manifest-referential-integrity-20261031 codex/aram-mapping-source-parser-20261101 codex/aram-multidimensional-mapping-20261101 codex/aram-multistatement-mapping-20261031 codex/aram-pipeline-manifest-schema-20261031 codex/aram-readonly-mapping-coverage-20261031 codex/aram-reconciliation-review-gate-20261031 codex/aram-same-flush-fiscal-lock-20261030 codex/aram-synthetic-windows-demo-20261031 codex/aram-trial-balance-diagnostic-20261031 codex/aram-windows-background-detail-search-20261031 codex/aram-windows-detail-review-20261031 codex/aram-windows-five-statement-pilot-20261031 codex/aram-windows-statement-figures-20261031 codex/gate-2a-pilot-workbook codex/gate-2a-pilot-workbook-6864754244772568979 codex/gate3-authenticated-principal-boundary-20261030 codex/gate3-authored-draft-staging-20261030 codex/gate3-carry-forward-preview-20261023 codex/gate3-draft-staging-audit-20261030 codex/gate3-mapping-batch-freshness-20261030 codex/gate3-mapping-batch-review-20261030 codex/gate3-mapping-core codex/gate3-mapping-coverage-20261023 codex/gate3-mapping-excel-20261023 codex/gate3-note-mapping-review-20261023 codex/gate3-signed-year-scope-20261030 codex/gate3-unapproved-proposal-storage-20261030 codex/payvast-mapping-discovery-20261023 codex/phase-2a-backend-foundation codex/phase-2b-post-merge-record codex/phase-2b-source-registration codex/phase-2b-source-registration-12955190277775369594 codex/post-gate2a-control-reconciliation codex/post-pr18-acceptance-record codex/project-control-foundation codex/t1-repo-hygiene-20261023 codex/t2-ci-hardening-20261023 codex/t3-explicit-migrations-20261023 codex/t6a-readonly-diagnostics-20261023 codex/v1-01-safety-gate-hardening codex/v1-year-isolation-followup-20261023 codex/windows-native-pilot-shell-20261030 docs/aram-master-control-plan-20261111 docs/aram-roadmap-dashboard-20261030 docs/financial-statement-reconciliation-1402-1404-20261110 feat/gate3-governed-mapping-20261101 feat/gate3-package9-1-security-20261103 feat/gate3-package9-isolated-20261103 fix/password-unicode-validation-20261111 governance/aram-5-1-3-controls-20261030 governance/role-based-operating-model jules-1168663913887856558-73b8a885 jules-gate-2b-mapping-workflow-foundation-4137724387867065330 phase-1b1-audit-report-7365389436990903239 phase-2a-backend-foundation-10237597358138521261 phase1b2-postmerge-metadata-fix refactor/drg-parent-year-id-15088443704684163304 source-data-cleanup
py 198 lines 7.28 KB
Raw
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.source_detail == "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)
139
140
141 def test_invalid_source_exposure_amount_fails_closed():
142 result = build_pipeline()
143 line = replace(result.balance_sheet.lines[0], source_closing_exposure="NaN")
144 changed = replace(
145 result, balance_sheet=replace(
146 result.balance_sheet, lines=(line,) + result.balance_sheet.lines[1:],
147 ),
148 )
149 with pytest.raises(CoverageError, match="SOURCE_INVALID"):
150 build_preview_details(changed)
151
152
153 @pytest.mark.parametrize("hidden", ["Revenue\u202eforged", "Revenue\x85forged", "Revenue\ufeffforged"])
154 def test_detail_labels_reject_bidi_and_invisible_control_characters(hidden):
155 result = build_pipeline()
156 line = replace(result.balance_sheet.lines[0], line_label=hidden)
157 changed = replace(
158 result, balance_sheet=replace(
159 result.balance_sheet, lines=(line,) + result.balance_sheet.lines[1:],
160 ),
161 )
162 with pytest.raises(CoverageError, match="ITEM_INVALID"):
163 build_preview_details(changed)
164
165
166 def test_persian_zero_width_non_joiner_is_preserved_in_detail_label():
167 result = build_pipeline()
168 label = "سود\u200cخالص"
169 line = replace(result.income_statement.lines[0], line_label=label)
170 changed = replace(
171 result, income_statement=replace(
172 result.income_statement, lines=(line,) + result.income_statement.lines[1:],
173 ),
174 )
175 rows = build_preview_details(changed)
176 assert any(row.item == label for row in rows)
177
178
179 def test_integrity_gate_rejects_malformed_policy_digest():
180 result = build_pipeline()
181 changed = replace(
182 result, income_statement=replace(
183 result.income_statement, presentation_policies_sha256="bad-digest",
184 ),
185 )
186 with pytest.raises(CoverageError, match="PROVENANCE_HASH"):
187 validate_desktop_pipeline_result(changed)
188
189
190 def test_integrity_gate_rejects_blocked_state_without_explanation():
191 result = build_pipeline()
192 changed = replace(
193 result, reconciliation=replace(
194 result.reconciliation, eligible_for_analyst_review=False,
195 ),
196 )
197 with pytest.raises(CoverageError, match="BLOCKED_WITHOUT_FINDINGS"):
198 validate_desktop_pipeline_result(changed)