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

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

Synced Dec 4, 01:45 UTC

codex/aram-cross-statement-controls-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 355 lines 14.4 KB
Raw
1 """Synthetic candidate-object tests for cross-statement integrity gate."""
2 from __future__ import annotations
3
4 from dataclasses import replace
5 from decimal import Decimal
6
7 import pytest
8
9 from aram_backend.balance_sheet_candidate import (
10 BalanceSheetCandidate, CandidateBalanceSheetLine,
11 )
12 from aram_backend.cash_flow_direct_candidate import (
13 CashAccountReconciliation, CashFlowLine, DirectCashFlowCandidate,
14 )
15 from aram_backend.coverage_preview import CoverageError
16 from aram_backend.cross_statement_reconciliation import (
17 CashBalanceNoteSelection, reconcile_cross_statements,
18 )
19 from aram_backend.equity_changes_candidate import EquityChangesCandidate
20 from aram_backend.income_statement_candidate import IncomeStatementCandidate
21 from aram_backend.trial_balance_diagnostic import TrialBalanceAccount, TrialBalanceDiagnostic
22
23
24 SOURCE = "a" * 64
25 REFS = "b" * 64
26 POLICY = "c" * 64
27 TXNS = "d" * 64
28
29
30 def bs(*, equity="100", cash="50", difference="0", eligible=True, lines=None):
31 rows = lines or (
32 CandidateBalanceSheetLine(
33 "Current assets", "CASH1", "", "ASSETS", 1, "Cash",
34 cash, cash, "REF-CASH", "POL-CASH",
35 ),
36 CandidateBalanceSheetLine(
37 "Current assets", "A02", "", "ASSETS", 1, "Receivables",
38 "150", "150", "REF-AR", "POL-AR",
39 ),
40 CandidateBalanceSheetLine(
41 "Current liabilities", "L01", "", "LIABILITIES", -1, "Payables",
42 "-100", "100", "REF-AP", "POL-AP",
43 ),
44 CandidateBalanceSheetLine(
45 "Equity", "E01", "", "EQUITY", -1, "Equity",
46 "-100", equity, "REF-EQ", "POL-EQ",
47 ),
48 )
49 return BalanceSheetCandidate(
50 company_code="001", fiscal_year=1404,
51 source_sha256=SOURCE, note_references_sha256=REFS,
52 presentation_policies_sha256=POLICY,
53 assets_total=str(Decimal(cash) + Decimal("150")),
54 liabilities_total="100", equity_total=equity,
55 balance_difference=difference, lines=rows, findings=(),
56 arithmetically_balanced=difference == "0",
57 eligible_for_analyst_review=eligible,
58 )
59
60
61 def income(*, net="40", gross="60", eligible=True):
62 return IncomeStatementCandidate(
63 company_code="001", fiscal_year=1404, source_sha256=SOURCE,
64 note_references_sha256=REFS, presentation_policies_sha256=POLICY,
65 revenue_total="100", cost_of_revenue_total="40",
66 gross_profit=gross, operating_expense_total="20",
67 operating_profit="40", other_income_total="5",
68 other_expense_total="2", profit_before_tax="43",
69 tax_expense_total="3", net_result=net,
70 lines=(), findings=(), eligible_for_analyst_review=eligible,
71 )
72
73
74 def equity(*, closing="100", profit="40", eligible=True):
75 return EquityChangesCandidate(
76 company_code="001", fiscal_year=1404, source_sha256=SOURCE,
77 note_references_sha256=REFS, presentation_policies_sha256=POLICY,
78 opening_equity_total="60", capital_contributions="0",
79 net_profit_loss_movements=profit, dividends="0",
80 other_comprehensive_income="0", transfers="0",
81 prior_period_adjustments="0", other_movements="0",
82 total_movement="40", closing_equity_total=closing,
83 rollforward_difference="0", component_totals=(),
84 note_movements=(), account_movements=(), findings=(),
85 eligible_for_analyst_review=eligible,
86 )
87
88
89 def cash(*, closing="50", net="10", eligible=True):
90 return DirectCashFlowCandidate(
91 company_code="001", fiscal_year=1404, currency="IRR", unit="Rial",
92 source_sha256=SOURCE, transaction_ledger_sha256=TXNS,
93 classification_policies_sha256=POLICY,
94 cash_account_count=1, transaction_count=2,
95 opening_cash_and_equivalents="40",
96 operating_inflows="30", operating_outflows="20",
97 operating_net_cash_flow="10", investing_inflows="0",
98 investing_outflows="0", investing_net_cash_flow="0",
99 financing_inflows="0", financing_outflows="0",
100 financing_net_cash_flow="0", internal_transfer_inflows="0",
101 internal_transfer_outflows="0", net_change_in_cash=net,
102 closing_cash_and_equivalents=closing, rollforward_difference="0",
103 account_reconciliations=(CashAccountReconciliation(
104 account_code="CASH1", account_name="Cash", source_row=1,
105 opening_balance="40", source_debit_turnover="30",
106 source_credit_turnover="20", closing_balance="50",
107 transaction_debit_turnover="30", transaction_credit_turnover="20",
108 debit_difference="0", credit_difference="0",
109 rollforward_difference="0", policy_evidence_ref="CASH-POLICY",
110 ),), cash_flow_lines=(
111 CashFlowLine("L1", "V1", "1404-01-01", "CASH1", "REV", "OPERATING", "30", "0", "SRC1", "POL-REV"),
112 CashFlowLine("L2", "V2", "1404-01-02", "CASH1", "EXP", "OPERATING", "0", "20", "SRC2", "POL-EXP"),
113 ), findings=(),
114 eligible_for_analyst_review=eligible,
115 )
116
117
118 def trial(*, balanced=True):
119 return TrialBalanceDiagnostic(
120 company_code="001", fiscal_year=1404, source_sha256=SOURCE,
121 account_count=2, opening_debit_net_total="0",
122 debit_turnover_total="100", credit_turnover_total="100",
123 closing_debit_net_total="0", movement_difference="0",
124 opening_to_closing_difference="0", findings=(), accounts=(
125 TrialBalanceAccount("CASH1", "Cash", 2, 1, "40", "30", "20", "50", "10"),
126 TrialBalanceAccount("OFFSET", "Offset", 3, 2, "-40", "70", "80", "-50", "-10"),
127 ),
128 balanced_for_analyst_review=balanced,
129 )
130
131
132 def reconcile(**overrides):
133 inputs = {
134 "balance_sheet": bs(),
135 "income_statement": income(),
136 "equity_changes": equity(),
137 "cash_flow": cash(),
138 "trial_balance": trial(),
139 "cash_balance_notes": [
140 CashBalanceNoteSelection("Current assets", "CASH1", "", "FINANCE-POLICY-CASH")
141 ],
142 }
143 inputs.update(overrides)
144 return reconcile_cross_statements(**inputs)
145
146
147 def test_all_five_statements_tie_exactly_without_claiming_approval():
148 result = reconcile()
149 assert result.eligible_for_analyst_review
150 assert not result.findings
151 assert result.same_source_snapshot
152 assert result.balance_sheet_difference == "0"
153 assert result.balance_sheet_equity_difference == "0"
154 assert result.net_profit_equity_difference == "0"
155 assert result.balance_sheet_cash_total == "50"
156 assert result.cash_flow_closing_cash == "50"
157 assert result.balance_sheet_cash_difference == "0"
158 assert result.income_statement_bridge_valid
159 assert result.cash_flow_bridge_valid
160 assert result.equity_rollforward_valid
161 assert result.trial_balance_arithmetic_valid
162 assert result.cash_accounts_match_trial_balance
163 assert result.cash_account_totals_valid
164 assert len(result.selected_cash_notes) == 1
165 assert not result.source_ledger_completeness_verified
166 assert not result.accounting_policy_authorized
167 assert not result.independently_reviewed
168 assert not result.trusted_approvals
169 assert not result.official_financial_statement
170 assert not result.export_authorized
171 assert not result.releasable
172
173
174 def test_different_source_snapshots_block_cross_statement_match():
175 result = reconcile(cash_flow=replace(cash(), source_sha256="e" * 64))
176 assert not result.eligible_for_analyst_review
177 assert not result.same_source_snapshot
178 assert "CROSS_STATEMENT_SOURCE_SNAPSHOT_MISMATCH" in result.findings
179
180
181 def test_company_or_year_mismatch_blocks():
182 result = reconcile(equity_changes=replace(equity(), company_code="002"))
183 assert "CROSS_STATEMENT_COMPANY_YEAR_MISMATCH" in result.findings
184 result = reconcile(trial_balance=replace(trial(), fiscal_year=1403))
185 assert "CROSS_STATEMENT_COMPANY_YEAR_MISMATCH" in result.findings
186
187
188 def test_balance_sheet_equity_must_equal_equity_statement_closing():
189 result = reconcile(equity_changes=equity(closing="99"))
190 assert result.balance_sheet_equity_difference == "1"
191 assert "BALANCE_SHEET_EQUITY_CLOSING_MISMATCH" in result.findings
192
193
194 def test_income_net_result_must_equal_equity_profit_movement():
195 result = reconcile(equity_changes=equity(profit="39"))
196 assert result.net_profit_equity_difference == "1"
197 assert "INCOME_RESULT_EQUITY_MOVEMENT_MISMATCH" in result.findings
198
199
200 def test_cash_flow_closing_must_equal_selected_balance_sheet_cash_notes():
201 result = reconcile(cash_flow=cash(closing="49"))
202 assert result.balance_sheet_cash_difference == "1"
203 assert "BALANCE_SHEET_CASH_FLOW_CLOSING_MISMATCH" in result.findings
204
205
206 def test_cash_note_selection_must_exist_and_be_an_asset():
207 result = reconcile(cash_balance_notes=[
208 CashBalanceNoteSelection("Missing", "C99", "", "POLICY")
209 ])
210 assert "CASH_BALANCE_NOTE_NOT_FOUND_IN_BALANCE_SHEET" in result.findings
211 assert "CASH_BALANCE_NOTE_SELECTION_INCOMPLETE" in result.findings
212 assert "BALANCE_SHEET_CASH_FLOW_CLOSING_MISMATCH" in result.findings
213
214 wrong = list(bs().lines)
215 wrong[0] = replace(wrong[0], category="LIABILITIES")
216 result = reconcile(balance_sheet=bs(lines=tuple(wrong)))
217 assert "CASH_BALANCE_NOTE_NOT_ASSET" in result.findings
218
219
220 def test_duplicate_or_empty_cash_note_selection_is_rejected():
221 selection = CashBalanceNoteSelection("Current assets", "CASH1", "", "POLICY")
222 with pytest.raises(CoverageError):
223 reconcile(cash_balance_notes=[selection, selection])
224 with pytest.raises(CoverageError):
225 reconcile(cash_balance_notes=[])
226
227
228 def test_blocked_upstream_candidates_block_cross_statement_gate():
229 result = reconcile(income_statement=income(eligible=False))
230 assert not result.eligible_for_analyst_review
231 assert "INCOME_STATEMENT_CANDIDATE_BLOCKED" in result.findings
232 result = reconcile(trial_balance=trial(balanced=False))
233 assert "TRIAL_BALANCE_CANDIDATE_BLOCKED" in result.findings
234 assert "TRIAL_BALANCE_ARITHMETIC_INVALID" in result.findings
235
236
237 def test_income_statement_internal_bridge_is_recomputed():
238 result = reconcile(income_statement=income(gross="61"))
239 assert not result.income_statement_bridge_valid
240 assert "INCOME_STATEMENT_BRIDGE_INVALID" in result.findings
241
242
243 def test_cash_flow_internal_bridge_is_recomputed():
244 result = reconcile(cash_flow=cash(net="9"))
245 assert not result.cash_flow_bridge_valid
246 assert "CASH_FLOW_BRIDGE_INVALID" in result.findings
247
248
249 def test_balance_sheet_equation_is_recomputed():
250 result = reconcile(balance_sheet=bs(difference="1"))
251 assert "BALANCE_SHEET_EQUATION_INVALID" in result.findings
252
253
254 def test_equity_rollforward_is_recomputed():
255 bad = replace(equity(), rollforward_difference="1")
256 result = reconcile(equity_changes=bad)
257 assert not result.equity_rollforward_valid
258 assert "EQUITY_STATEMENT_ROLLFORWARD_INVALID" in result.findings
259
260
261 def test_invalid_candidate_kind_or_object_is_rejected():
262 with pytest.raises(CoverageError):
263 reconcile(income_statement={"kind": "bad"})
264 with pytest.raises(CoverageError):
265 reconcile(balance_sheet=replace(bs(), kind="OFFICIAL"))
266
267
268 def test_approval_flags_cannot_be_smuggled_into_candidate_objects():
269 with pytest.raises(CoverageError):
270 reconcile(cash_flow=replace(cash(), official_financial_statement=True))
271 with pytest.raises(CoverageError):
272 reconcile(equity_changes=replace(equity(), releasable=True))
273
274
275 def test_invalid_source_hash_is_rejected():
276 with pytest.raises(CoverageError):
277 reconcile(balance_sheet=replace(bs(), source_sha256="not-a-hash"))
278
279
280 def test_cash_selection_hash_is_order_independent():
281 first = reconcile()
282 second = reconcile(cash_balance_notes=[
283 CashBalanceNoteSelection("Current assets", "CASH1", "", "FINANCE-POLICY-CASH")
284 ])
285 assert first.cash_note_selection_sha256 == second.cash_note_selection_sha256
286
287
288 def test_decimal_differences_are_not_rounded_or_tolerated():
289 result = reconcile(equity_changes=equity(closing="100.0000000001"))
290 assert result.balance_sheet_equity_difference == "-1E-10"
291 assert "BALANCE_SHEET_EQUITY_CLOSING_MISMATCH" in result.findings
292
293
294 def test_cash_account_source_balances_and_turnovers_must_match_trial_balance():
295 source = trial()
296 rows = list(source.accounts)
297 rows[0] = replace(rows[0], debit="31")
298 result = reconcile(trial_balance=replace(source, accounts=tuple(rows)))
299 assert not result.cash_accounts_match_trial_balance
300 assert "CASH_ACCOUNTS_TRIAL_BALANCE_MISMATCH" in result.findings
301
302
303 def test_cash_account_reconciliation_differences_block_cross_statement_gate():
304 source = cash()
305 row = replace(source.account_reconciliations[0], debit_difference="1")
306 result = reconcile(cash_flow=replace(source, account_reconciliations=(row,)))
307 assert not result.cash_account_totals_valid
308 assert "CASH_ACCOUNT_TURNOVER_OR_TOTALS_INVALID" in result.findings
309
310
311 def test_duplicate_balance_sheet_note_rows_are_not_silently_overwritten():
312 source = bs()
313 result = reconcile(balance_sheet=bs(lines=source.lines + (source.lines[0],)))
314 assert "DUPLICATE_BALANCE_SHEET_NOTE_LINE" in result.findings
315
316
317 def test_trial_balance_account_trace_count_must_be_complete():
318 source = trial()
319 result = reconcile(trial_balance=replace(source, account_count=3))
320 assert not result.cash_accounts_match_trial_balance
321 assert "CASH_ACCOUNTS_TRIAL_BALANCE_MISMATCH" in result.findings
322
323
324 def test_cash_flow_aggregates_must_match_individual_classified_lines():
325 source = cash()
326 lines = list(source.cash_flow_lines)
327 lines[0] = replace(lines[0], cash_inflow="29")
328 result = reconcile(cash_flow=replace(source, cash_flow_lines=tuple(lines)))
329 assert not result.cash_flow_bridge_valid
330 assert "CASH_FLOW_BRIDGE_INVALID" in result.findings
331
332
333 def test_unclassified_cash_flow_line_cannot_pass_cross_statement_gate():
334 source = cash()
335 lines = list(source.cash_flow_lines)
336 lines[0] = replace(lines[0], activity="UNCLASSIFIED")
337 result = reconcile(cash_flow=replace(source, cash_flow_lines=tuple(lines)))
338 assert not result.cash_flow_bridge_valid
339 assert "CASH_FLOW_BRIDGE_INVALID" in result.findings
340
341
342 def test_internal_cash_transfer_totals_must_be_derived_from_line_trace():
343 source = cash()
344 transfer = CashFlowLine(
345 "L3", "V3", "1404-01-03", "CASH1", "CASH2",
346 "CASH_TRANSFER", "10", "0", "SRC3", "CASH-POLICY",
347 )
348 bad = replace(
349 source, transaction_count=3,
350 cash_flow_lines=source.cash_flow_lines + (transfer,),
351 internal_transfer_inflows="9", internal_transfer_outflows="10",
352 )
353 result = reconcile(cash_flow=bad)
354 assert not result.cash_flow_bridge_valid
355 assert "CASH_FLOW_BRIDGE_INVALID" in result.findings