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

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

Synced Dec 3, 20:45 UTC

test(control): cover five-statement tie-outs, provenance and adversarial mismatch cases

davoodmehraban89 committed Oct 31, 2026 at 08:33 UTC 9b444fba994f0cbeae6945bf8cfed955a45201d9
1 file changed +273
backend/tests/test_cross_statement_reconciliation.py new
+273
@@ -0,0 +1,273 @@
1 +"""Synthetic candidate-object tests for cross-statement integrity gate."""
2 +from __future__ import annotations
3 +
4 +from dataclasses import replace
5 +
6 +import pytest
7 +
8 +from aram_backend.balance_sheet_candidate import (
9 + BalanceSheetCandidate, CandidateBalanceSheetLine,
10 +)
11 +from aram_backend.cash_flow_direct_candidate import DirectCashFlowCandidate
12 +from aram_backend.coverage_preview import CoverageError
13 +from aram_backend.cross_statement_reconciliation import (
14 + CashBalanceNoteSelection, reconcile_cross_statements,
15 +)
16 +from aram_backend.equity_changes_candidate import EquityChangesCandidate
17 +from aram_backend.income_statement_candidate import IncomeStatementCandidate
18 +from aram_backend.trial_balance_diagnostic import TrialBalanceDiagnostic
19 +
20 +
21 +SOURCE = "a" * 64
22 +REFS = "b" * 64
23 +POLICY = "c" * 64
24 +TXNS = "d" * 64
25 +
26 +
27 +def bs(*, equity="100", cash="50", difference="0", eligible=True, lines=None):
28 + rows = lines or (
29 + CandidateBalanceSheetLine(
30 + "Current assets", "CASH1", "", "ASSETS", 1, "Cash",
31 + cash, cash, "REF-CASH", "POL-CASH",
32 + ),
33 + CandidateBalanceSheetLine(
34 + "Current assets", "A02", "", "ASSETS", 1, "Receivables",
35 + "150", "150", "REF-AR", "POL-AR",
36 + ),
37 + CandidateBalanceSheetLine(
38 + "Current liabilities", "L01", "", "LIABILITIES", -1, "Payables",
39 + "-100", "100", "REF-AP", "POL-AP",
40 + ),
41 + CandidateBalanceSheetLine(
42 + "Equity", "E01", "", "EQUITY", -1, "Equity",
43 + "-100", equity, "REF-EQ", "POL-EQ",
44 + ),
45 + )
46 + return BalanceSheetCandidate(
47 + company_code="001", fiscal_year=1404,
48 + source_sha256=SOURCE, note_references_sha256=REFS,
49 + presentation_policies_sha256=POLICY,
50 + assets_total=str(Decimal(cash) + Decimal("150")),
51 + liabilities_total="100", equity_total=equity,
52 + balance_difference=difference, lines=rows, findings=(),
53 + arithmetically_balanced=difference == "0",
54 + eligible_for_analyst_review=eligible,
55 + )
56 +
57 +
58 +def income(*, net="40", gross="60", eligible=True):
59 + return IncomeStatementCandidate(
60 + company_code="001", fiscal_year=1404, source_sha256=SOURCE,
61 + note_references_sha256=REFS, presentation_policies_sha256=POLICY,
62 + revenue_total="100", cost_of_revenue_total="40",
63 + gross_profit=gross, operating_expense_total="20",
64 + operating_profit="40", other_income_total="5",
65 + other_expense_total="2", profit_before_tax="43",
66 + tax_expense_total="3", net_result=net,
67 + lines=(), findings=(), eligible_for_analyst_review=eligible,
68 + )
69 +
70 +
71 +def equity(*, closing="100", profit="40", eligible=True):
72 + return EquityChangesCandidate(
73 + company_code="001", fiscal_year=1404, source_sha256=SOURCE,
74 + note_references_sha256=REFS, presentation_policies_sha256=POLICY,
75 + opening_equity_total="60", capital_contributions="0",
76 + net_profit_loss_movements=profit, dividends="0",
77 + other_comprehensive_income="0", transfers="0",
78 + prior_period_adjustments="0", other_movements="0",
79 + total_movement="40", closing_equity_total=closing,
80 + rollforward_difference="0", component_totals=(),
81 + note_movements=(), account_movements=(), findings=(),
82 + eligible_for_analyst_review=eligible,
83 + )
84 +
85 +
86 +def cash(*, closing="50", net="10", eligible=True):
87 + return DirectCashFlowCandidate(
88 + company_code="001", fiscal_year=1404, currency="IRR", unit="Rial",
89 + source_sha256=SOURCE, transaction_ledger_sha256=TXNS,
90 + classification_policies_sha256=POLICY,
91 + cash_account_count=1, transaction_count=2,
92 + opening_cash_and_equivalents="40",
93 + operating_inflows="30", operating_outflows="20",
94 + operating_net_cash_flow="10", investing_inflows="0",
95 + investing_outflows="0", investing_net_cash_flow="0",
96 + financing_inflows="0", financing_outflows="0",
97 + financing_net_cash_flow="0", internal_transfer_inflows="0",
98 + internal_transfer_outflows="0", net_change_in_cash=net,
99 + closing_cash_and_equivalents=closing, rollforward_difference="0",
100 + account_reconciliations=(), cash_flow_lines=(), findings=(),
101 + eligible_for_analyst_review=eligible,
102 + )
103 +
104 +
105 +def trial(*, balanced=True):
106 + return TrialBalanceDiagnostic(
107 + company_code="001", fiscal_year=1404, source_sha256=SOURCE,
108 + account_count=4, opening_debit_net_total="0",
109 + debit_turnover_total="100", credit_turnover_total="100",
110 + closing_debit_net_total="0", movement_difference="0",
111 + opening_to_closing_difference="0", findings=(), accounts=(),
112 + balanced_for_analyst_review=balanced,
113 + )
114 +
115 +
116 +def reconcile(**overrides):
117 + inputs = {
118 + "balance_sheet": bs(),
119 + "income_statement": income(),
120 + "equity_changes": equity(),
121 + "cash_flow": cash(),
122 + "trial_balance": trial(),
123 + "cash_balance_notes": [
124 + CashBalanceNoteSelection("Current assets", "CASH1", "", "FINANCE-POLICY-CASH")
125 + ],
126 + }
127 + inputs.update(overrides)
128 + return reconcile_cross_statements(**inputs)
129 +
130 +
131 +def test_all_five_statements_tie_exactly_without_claiming_approval():
132 + result = reconcile()
133 + assert result.eligible_for_analyst_review
134 + assert not result.findings
135 + assert result.same_source_snapshot
136 + assert result.balance_sheet_difference == "0"
137 + assert result.balance_sheet_equity_difference == "0"
138 + assert result.net_profit_equity_difference == "0"
139 + assert result.balance_sheet_cash_total == "50"
140 + assert result.cash_flow_closing_cash == "50"
141 + assert result.balance_sheet_cash_difference == "0"
142 + assert result.income_statement_bridge_valid
143 + assert result.cash_flow_bridge_valid
144 + assert result.equity_rollforward_valid
145 + assert result.trial_balance_arithmetic_valid
146 + assert len(result.selected_cash_notes) == 1
147 + assert not result.source_ledger_completeness_verified
148 + assert not result.accounting_policy_authorized
149 + assert not result.independently_reviewed
150 + assert not result.trusted_approvals
151 + assert not result.official_financial_statement
152 + assert not result.export_authorized
153 + assert not result.releasable
154 +
155 +
156 +def test_different_source_snapshots_block_cross_statement_match():
157 + result = reconcile(cash_flow=replace(cash(), source_sha256="e" * 64))
158 + assert not result.eligible_for_analyst_review
159 + assert not result.same_source_snapshot
160 + assert "CROSS_STATEMENT_SOURCE_SNAPSHOT_MISMATCH" in result.findings
161 +
162 +
163 +def test_company_or_year_mismatch_blocks():
164 + result = reconcile(equity_changes=replace(equity(), company_code="002"))
165 + assert "CROSS_STATEMENT_COMPANY_YEAR_MISMATCH" in result.findings
166 + result = reconcile(trial_balance=replace(trial(), fiscal_year=1403))
167 + assert "CROSS_STATEMENT_COMPANY_YEAR_MISMATCH" in result.findings
168 +
169 +
170 +def test_balance_sheet_equity_must_equal_equity_statement_closing():
171 + result = reconcile(equity_changes=equity(closing="99"))
172 + assert result.balance_sheet_equity_difference == "1"
173 + assert "BALANCE_SHEET_EQUITY_CLOSING_MISMATCH" in result.findings
174 +
175 +
176 +def test_income_net_result_must_equal_equity_profit_movement():
177 + result = reconcile(equity_changes=equity(profit="39"))
178 + assert result.net_profit_equity_difference == "1"
179 + assert "INCOME_RESULT_EQUITY_MOVEMENT_MISMATCH" in result.findings
180 +
181 +
182 +def test_cash_flow_closing_must_equal_selected_balance_sheet_cash_notes():
183 + result = reconcile(cash_flow=cash(closing="49"))
184 + assert result.balance_sheet_cash_difference == "1"
185 + assert "BALANCE_SHEET_CASH_FLOW_CLOSING_MISMATCH" in result.findings
186 +
187 +
188 +def test_cash_note_selection_must_exist_and_be_an_asset():
189 + result = reconcile(cash_balance_notes=[
190 + CashBalanceNoteSelection("Missing", "C99", "", "POLICY")
191 + ])
192 + assert "CASH_BALANCE_NOTE_NOT_FOUND_IN_BALANCE_SHEET" in result.findings
193 + assert "CASH_BALANCE_NOTE_SELECTION_INCOMPLETE" in result.findings
194 + assert "BALANCE_SHEET_CASH_FLOW_CLOSING_MISMATCH" in result.findings
195 +
196 + wrong = list(bs().lines)
197 + wrong[0] = replace(wrong[0], category="LIABILITIES")
198 + result = reconcile(balance_sheet=bs(lines=tuple(wrong)))
199 + assert "CASH_BALANCE_NOTE_NOT_ASSET" in result.findings
200 +
201 +
202 +def test_duplicate_or_empty_cash_note_selection_is_rejected():
203 + selection = CashBalanceNoteSelection("Current assets", "CASH1", "", "POLICY")
204 + with pytest.raises(CoverageError):
205 + reconcile(cash_balance_notes=[selection, selection])
206 + with pytest.raises(CoverageError):
207 + reconcile(cash_balance_notes=[])
208 +
209 +
210 +def test_blocked_upstream_candidates_block_cross_statement_gate():
211 + result = reconcile(income_statement=income(eligible=False))
212 + assert not result.eligible_for_analyst_review
213 + assert "INCOME_STATEMENT_CANDIDATE_BLOCKED" in result.findings
214 + result = reconcile(trial_balance=trial(balanced=False))
215 + assert "TRIAL_BALANCE_CANDIDATE_BLOCKED" in result.findings
216 + assert "TRIAL_BALANCE_ARITHMETIC_INVALID" in result.findings
217 +
218 +
219 +def test_income_statement_internal_bridge_is_recomputed():
220 + result = reconcile(income_statement=income(gross="61"))
221 + assert not result.income_statement_bridge_valid
222 + assert "INCOME_STATEMENT_BRIDGE_INVALID" in result.findings
223 +
224 +
225 +def test_cash_flow_internal_bridge_is_recomputed():
226 + result = reconcile(cash_flow=cash(net="9"))
227 + assert not result.cash_flow_bridge_valid
228 + assert "CASH_FLOW_BRIDGE_INVALID" in result.findings
229 +
230 +
231 +def test_balance_sheet_equation_is_recomputed():
232 + result = reconcile(balance_sheet=bs(difference="1"))
233 + assert "BALANCE_SHEET_EQUATION_INVALID" in result.findings
234 +
235 +
236 +def test_equity_rollforward_is_recomputed():
237 + bad = replace(equity(), rollforward_difference="1")
238 + result = reconcile(equity_changes=bad)
239 + assert not result.equity_rollforward_valid
240 + assert "EQUITY_STATEMENT_ROLLFORWARD_INVALID" in result.findings
241 +
242 +
243 +def test_invalid_candidate_kind_or_object_is_rejected():
244 + with pytest.raises(CoverageError):
245 + reconcile(income_statement={"kind": "bad"})
246 + with pytest.raises(CoverageError):
247 + reconcile(balance_sheet=replace(bs(), kind="OFFICIAL"))
248 +
249 +
250 +def test_approval_flags_cannot_be_smuggled_into_candidate_objects():
251 + with pytest.raises(CoverageError):
252 + reconcile(cash_flow=replace(cash(), official_financial_statement=True))
253 + with pytest.raises(CoverageError):
254 + reconcile(equity_changes=replace(equity(), releasable=True))
255 +
256 +
257 +def test_invalid_source_hash_is_rejected():
258 + with pytest.raises(CoverageError):
259 + reconcile(balance_sheet=replace(bs(), source_sha256="not-a-hash"))
260 +
261 +
262 +def test_cash_selection_hash_is_order_independent():
263 + first = reconcile()
264 + second = reconcile(cash_balance_notes=[
265 + CashBalanceNoteSelection("Current assets", "CASH1", "", "FINANCE-POLICY-CASH")
266 + ])
267 + assert first.cash_note_selection_sha256 == second.cash_note_selection_sha256
268 +
269 +
270 +def test_decimal_differences_are_not_rounded_or_tolerated():
271 + result = reconcile(equity_changes=equity(closing="100.0000000001"))
272 + assert result.balance_sheet_equity_difference == "-1E-10"
273 + assert "BALANCE_SHEET_EQUITY_CLOSING_MISMATCH" in result.findings