@davoodmehraban89 / Aram-2 Mirror · read-only
Fork 0

Mirrored from https://github.com/davoodmehraban89/Aram.git

Synced Dec 3, 20:45 UTC

codex/aram-account-note-source-trace-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 260 lines 10.3 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 + len(result.mapping_coverage.mappings)
29 )
30 assert len(rows) == expected
31 assert {row.statement for row in rows} == {
32 "ترازنامه", "سود و زیان", "تغییرات حقوق مالکانه",
33 "جریان وجوه نقد", "تراز آزمایشی",
34 }
35 cash = next(row for row in rows if row.item == "Cash and cash equivalents")
36 assert cash.source_detail == "50"
37 assert cash.closing_or_presentation == "50"
38 profit = next(row for row in rows if row.item == "Revenue")
39 assert profit.closing_or_presentation == "40"
40 assert all(row.state == "قابل بررسی؛ غیررسمی" for row in rows)
41 assert all(len(row.values()) == 11 for row in rows)
42
43
44 def test_cash_transactions_and_trial_balance_accounts_are_individually_traceable():
45 result = build_pipeline()
46 rows = build_preview_details(result)
47 cash_rows = [row for row in rows if row.statement == "جریان وجوه نقد"]
48 tb_rows = [row for row in rows if row.statement == "تراز آزمایشی"]
49 assert len(cash_rows) == (
50 len(result.cash_flow.cash_flow_lines)
51 + len(result.cash_flow.account_reconciliations)
52 )
53 assert any("سند" in row.reference for row in cash_rows)
54 assert len(tb_rows) == len(result.trial_balance.accounts)
55 assert all("کد حساب" in row.reference for row in tb_rows)
56
57
58 def test_blocked_pipeline_details_are_explicitly_diagnostic():
59 result = build_pipeline(transactions=cash_transactions()[:-1])
60 assert not result.eligible_for_analyst_review
61 rows = build_preview_details(result)
62 assert rows
63 assert all(row.state == "مسدود؛ صرفاً تشخیصی" for row in rows)
64
65
66 @pytest.mark.parametrize("bad", ["NaN", "Infinity", "1e500", "", "not-a-number"])
67 def test_invalid_detail_amount_fails_closed(bad):
68 result = build_pipeline()
69 line = replace(result.balance_sheet.lines[0], presentation_amount=bad)
70 changed = replace(
71 result, balance_sheet=replace(
72 result.balance_sheet, lines=(line,) + result.balance_sheet.lines[1:],
73 ),
74 )
75 with pytest.raises(CoverageError):
76 build_preview_details(changed)
77
78
79 def test_invalid_detail_text_with_control_character_fails_closed():
80 result = build_pipeline()
81 line = replace(result.income_statement.lines[0], line_label="Revenue\nforged")
82 changed = replace(
83 result, income_statement=replace(
84 result.income_statement, lines=(line,) + result.income_statement.lines[1:],
85 ),
86 )
87 with pytest.raises(CoverageError):
88 build_preview_details(changed)
89
90
91 def test_row_cap_refuses_truncation_instead_of_silently_hiding_detail(monkeypatch):
92 import aram_backend.desktop_five_statement_details as module
93 monkeypatch.setattr(module, "MAX_DETAIL_ROWS", 1)
94 with pytest.raises(CoverageError, match="ROW_LIMIT"):
95 build_preview_details(build_pipeline())
96
97
98 def test_integrity_gate_rejects_forged_approval_flags():
99 result = build_pipeline()
100 forged = replace(
101 result, balance_sheet=replace(
102 result.balance_sheet, official_financial_statement=True,
103 ),
104 )
105 with pytest.raises(CoverageError, match="PROVENANCE"):
106 validate_desktop_pipeline_result(forged)
107
108
109 def test_integrity_gate_rejects_cross_statement_source_hash_mismatch():
110 result = build_pipeline()
111 changed = replace(
112 result, trial_balance=replace(
113 result.trial_balance, source_sha256="0" * 64,
114 ),
115 )
116 with pytest.raises(CoverageError, match="SOURCE_SNAPSHOT"):
117 validate_desktop_pipeline_result(changed)
118
119
120 def test_integrity_gate_rejects_tampered_summary_cross_tie():
121 result = build_pipeline()
122 changed = replace(
123 result, balance_sheet=replace(
124 result.balance_sheet, equity_total="123",
125 ),
126 )
127 with pytest.raises(CoverageError, match="RECONCILIATION"):
128 validate_desktop_pipeline_result(changed)
129
130
131 def test_integrity_gate_rejects_truthy_non_boolean_approval_flag():
132 result = build_pipeline()
133 forged = replace(
134 result, cash_flow=replace(
135 result.cash_flow, transaction_ledger_completeness_verified=1,
136 ),
137 )
138 with pytest.raises(CoverageError, match="PROVENANCE"):
139 validate_desktop_pipeline_result(forged)
140
141
142 def test_invalid_source_exposure_amount_fails_closed():
143 result = build_pipeline()
144 line = replace(result.balance_sheet.lines[0], source_closing_exposure="NaN")
145 changed = replace(
146 result, balance_sheet=replace(
147 result.balance_sheet, lines=(line,) + result.balance_sheet.lines[1:],
148 ),
149 )
150 with pytest.raises(CoverageError, match="SOURCE_INVALID"):
151 build_preview_details(changed)
152
153
154 @pytest.mark.parametrize("hidden", ["Revenue\u202eforged", "Revenue\x85forged", "Revenue\ufeffforged"])
155 def test_detail_labels_reject_bidi_and_invisible_control_characters(hidden):
156 result = build_pipeline()
157 line = replace(result.balance_sheet.lines[0], line_label=hidden)
158 changed = replace(
159 result, balance_sheet=replace(
160 result.balance_sheet, lines=(line,) + result.balance_sheet.lines[1:],
161 ),
162 )
163 with pytest.raises(CoverageError, match="ITEM_INVALID"):
164 build_preview_details(changed)
165
166
167 def test_persian_zero_width_non_joiner_is_preserved_in_detail_label():
168 result = build_pipeline()
169 label = "سود\u200cخالص"
170 line = replace(result.income_statement.lines[0], line_label=label)
171 changed = replace(
172 result, income_statement=replace(
173 result.income_statement, lines=(line,) + result.income_statement.lines[1:],
174 ),
175 )
176 rows = build_preview_details(changed)
177 assert any(row.item == label for row in rows)
178
179
180 def test_integrity_gate_rejects_malformed_policy_digest():
181 result = build_pipeline()
182 changed = replace(
183 result, income_statement=replace(
184 result.income_statement, presentation_policies_sha256="bad-digest",
185 ),
186 )
187 with pytest.raises(CoverageError, match="PROVENANCE_HASH"):
188 validate_desktop_pipeline_result(changed)
189
190
191 def test_integrity_gate_rejects_blocked_state_without_explanation():
192 result = build_pipeline()
193 changed = replace(
194 result, reconciliation=replace(
195 result.reconciliation, eligible_for_analyst_review=False,
196 ),
197 )
198 with pytest.raises(CoverageError, match="BLOCKED_WITHOUT_FINDINGS"):
199 validate_desktop_pipeline_result(changed)
200
201
202 def test_detail_filter_supports_statement_and_cross_column_search():
203 from aram_backend.desktop_five_statement_details import filter_preview_details
204 rows = build_preview_details(build_pipeline())
205 all_rows = filter_preview_details(rows, statement="همه", query="")
206 assert all_rows == rows
207 income = filter_preview_details(rows, statement="سود و زیان", query="")
208 assert income and all(row.statement == "سود و زیان" for row in income)
209 revenue = filter_preview_details(rows, statement="همه", query="Revenue")
210 assert any(row.item == "Revenue" for row in revenue)
211 note = filter_preview_details(rows, statement="سود و زیان", query="یادداشت R01")
212 assert len(note) == 2
213 assert {row.item for row in note} == {"Revenue", "حساب REV / Revenue"}
214
215
216 def test_detail_filter_is_casefolded_and_handles_no_match():
217 from aram_backend.desktop_five_statement_details import filter_preview_details
218 rows = build_preview_details(build_pipeline())
219 assert filter_preview_details(rows, statement="همه", query="revenue") == (
220 filter_preview_details(rows, statement="همه", query="Revenue")
221 )
222 assert filter_preview_details(rows, statement="همه", query="no-such-account") == ()
223
224
225 @pytest.mark.parametrize("query", ["x" * 257, "bad\nquery", "bad\u202equery"])
226 def test_detail_filter_rejects_oversized_and_unsafe_queries(query):
227 from aram_backend.desktop_five_statement_details import filter_preview_details
228 rows = build_preview_details(build_pipeline())
229 with pytest.raises(CoverageError, match="SEARCH_QUERY"):
230 filter_preview_details(rows, statement="همه", query=query)
231
232
233 def test_detail_filter_rejects_invalid_statement_or_mutated_row():
234 from dataclasses import replace
235 from aram_backend.desktop_five_statement_details import (
236 PreviewDetail, filter_preview_details,
237 )
238 rows = build_preview_details(build_pipeline())
239 with pytest.raises(CoverageError, match="SEARCH_STATEMENT"):
240 filter_preview_details(rows, statement="all", query="")
241 forged = (replace(rows[0], trace="hidden\ncontrol"),) + rows[1:]
242 with pytest.raises(CoverageError, match="SEARCH_ROW"):
243 filter_preview_details(forged, statement="همه", query="")
244 with pytest.raises(CoverageError, match="SEARCH_ROWS"):
245 filter_preview_details(list(rows), statement="همه", query="")
246 assert PreviewDetail is type(rows[0])
247
248
249 def test_detail_filter_handles_large_bounded_row_collection_without_truncation():
250 from aram_backend.desktop_five_statement_details import (
251 MAX_DETAIL_ROWS, filter_preview_details,
252 )
253 rows = build_preview_details(build_pipeline())
254 repeats, remainder = divmod(MAX_DETAIL_ROWS, len(rows))
255 expanded = rows * repeats + rows[:remainder]
256 assert len(expanded) == MAX_DETAIL_ROWS
257 matches = filter_preview_details(expanded, statement="تراز آزمایشی", query="")
258 assert len(matches) == sum(
259 1 for row in expanded if row.statement == "تراز آزمایشی"
260 )