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

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

Synced Dec 3, 22:00 UTC

codex/aram-multidimensional-mapping-20261101
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 544 lines 22 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 (
20 EquityAccountMovement, EquityChangesCandidate, EquityComponentTotal,
21 EquityNoteMovement,
22 )
23 from aram_backend.income_statement_candidate import (
24 CandidateIncomeStatementLine, IncomeStatementCandidate,
25 )
26 from aram_backend.trial_balance_diagnostic import TrialBalanceAccount, TrialBalanceDiagnostic
27
28
29 SOURCE = "a" * 64
30 REFS = "b" * 64
31 POLICY = "c" * 64
32 TXNS = "d" * 64
33
34
35 def bs(*, equity="100", cash="50", difference="0", eligible=True, lines=None):
36 rows = lines or (
37 CandidateBalanceSheetLine(
38 "Current assets", "CASH1", "", "ASSETS", 1, "Cash",
39 cash, cash, "REF-CASH", "POL-CASH",
40 ),
41 CandidateBalanceSheetLine(
42 "Current assets", "A02", "", "ASSETS", 1, "Receivables",
43 "150", "150", "REF-AR", "POL-AR",
44 ),
45 CandidateBalanceSheetLine(
46 "Current liabilities", "L01", "", "LIABILITIES", -1, "Payables",
47 "-100", "100", "REF-AP", "POL-AP",
48 ),
49 CandidateBalanceSheetLine(
50 "Equity", "E01", "", "EQUITY", -1, "Equity",
51 "-100", equity, "REF-EQ", "POL-EQ",
52 ),
53 )
54 return BalanceSheetCandidate(
55 company_code="001", fiscal_year=1404,
56 source_sha256=SOURCE, note_references_sha256=REFS,
57 presentation_policies_sha256=POLICY,
58 assets_total=str(Decimal(cash) + Decimal("150")),
59 liabilities_total="100", equity_total=equity,
60 balance_difference=difference, lines=rows, findings=(),
61 arithmetically_balanced=difference == "0",
62 eligible_for_analyst_review=eligible,
63 )
64
65
66 def income(*, net="40", gross="60", eligible=True):
67 return IncomeStatementCandidate(
68 company_code="001", fiscal_year=1404, source_sha256=SOURCE,
69 note_references_sha256=REFS, presentation_policies_sha256=POLICY,
70 revenue_total="100", cost_of_revenue_total="40",
71 gross_profit=gross, operating_expense_total="20",
72 operating_profit="40", other_income_total="5",
73 other_expense_total="2", profit_before_tax="43",
74 tax_expense_total="3", net_result=net,
75 lines=(
76 CandidateIncomeStatementLine(
77 "Revenue", "R01", "", "REVENUE", -1, "Revenue",
78 "-100", "100", "REF-REV", "POL-REV",
79 ),
80 CandidateIncomeStatementLine(
81 "Cost", "C01", "", "COST_OF_REVENUE", 1, "Cost of revenue",
82 "40", "40", "REF-COST", "POL-COST",
83 ),
84 CandidateIncomeStatementLine(
85 "Operating", "O01", "", "OPERATING_EXPENSE", 1, "Operating expense",
86 "20", "20", "REF-OPEX", "POL-OPEX",
87 ),
88 CandidateIncomeStatementLine(
89 "Other income", "I01", "", "OTHER_INCOME", -1, "Other income",
90 "-5", "5", "REF-OIN", "POL-OIN",
91 ),
92 CandidateIncomeStatementLine(
93 "Other expense", "X01", "", "OTHER_EXPENSE", 1, "Other expense",
94 "2", "2", "REF-OEX", "POL-OEX",
95 ),
96 CandidateIncomeStatementLine(
97 "Tax", "T01", "", "TAX_EXPENSE", 1, "Tax",
98 "3", "3", "REF-TAX", "POL-TAX",
99 ),
100 ), findings=(), eligible_for_analyst_review=eligible,
101 )
102
103
104 def equity(*, closing="100", profit="40", eligible=True):
105 closing_value = Decimal(closing)
106 profit_value = Decimal(profit)
107 rollforward = Decimal("60") + profit_value - closing_value
108 account = EquityAccountMovement(
109 account_code="EQ1", account_name="Equity", source_excel_row=3,
110 source_row=2, statement_section="Equity", note_number="E01",
111 subnote_code="", equity_component="RETAINED_EARNINGS",
112 movement_category="NET_PROFIT_LOSS", sign_multiplier=-1,
113 opening_balance="60", debit_turnover="0", credit_turnover=profit,
114 movement_amount=profit, closing_balance=closing,
115 policy_evidence_ref="POL-EQ",
116 )
117 note = EquityNoteMovement(
118 statement_section="Equity", note_number="E01", subnote_code="",
119 equity_component="RETAINED_EARNINGS", movement_category="NET_PROFIT_LOSS",
120 line_label="Retained earnings", opening_balance="60",
121 movement_amount=profit, closing_balance=closing,
122 source_closing_exposure=str(-closing_value),
123 note_reference_evidence="REF-EQ", policy_evidence_ref="POL-EQ",
124 )
125 component = EquityComponentTotal(
126 equity_component="RETAINED_EARNINGS", opening_balance="60",
127 movement_amount=profit, closing_balance=closing,
128 )
129 return EquityChangesCandidate(
130 company_code="001", fiscal_year=1404, source_sha256=SOURCE,
131 note_references_sha256=REFS, presentation_policies_sha256=POLICY,
132 opening_equity_total="60", capital_contributions="0",
133 net_profit_loss_movements=profit, dividends="0",
134 other_comprehensive_income="0", transfers="0",
135 prior_period_adjustments="0", other_movements="0",
136 total_movement=profit, closing_equity_total=closing,
137 rollforward_difference=str(rollforward), component_totals=(component,),
138 note_movements=(note,), account_movements=(account,), findings=(),
139 eligible_for_analyst_review=eligible,
140 )
141
142
143 def cash(*, closing="50", net="10", eligible=True):
144 return DirectCashFlowCandidate(
145 company_code="001", fiscal_year=1404, currency="IRR", unit="Rial",
146 source_sha256=SOURCE, transaction_ledger_sha256=TXNS,
147 classification_policies_sha256=POLICY,
148 cash_account_count=1, transaction_count=2,
149 opening_cash_and_equivalents="40",
150 operating_inflows="30", operating_outflows="20",
151 operating_net_cash_flow="10", investing_inflows="0",
152 investing_outflows="0", investing_net_cash_flow="0",
153 financing_inflows="0", financing_outflows="0",
154 financing_net_cash_flow="0", internal_transfer_inflows="0",
155 internal_transfer_outflows="0", net_change_in_cash=net,
156 closing_cash_and_equivalents=closing, rollforward_difference="0",
157 account_reconciliations=(CashAccountReconciliation(
158 account_code="CASH1", account_name="Cash", source_row=1,
159 opening_balance="40", source_debit_turnover="30",
160 source_credit_turnover="20", closing_balance="50",
161 transaction_debit_turnover="30", transaction_credit_turnover="20",
162 debit_difference="0", credit_difference="0",
163 rollforward_difference="0", policy_evidence_ref="CASH-POLICY",
164 ),), cash_flow_lines=(
165 CashFlowLine("L1", "V1", "1404-01-01", "CASH1", "REV", "OPERATING", "30", "0", "SRC1", "POL-REV"),
166 CashFlowLine("L2", "V2", "1404-01-02", "CASH1", "EXP", "OPERATING", "0", "20", "SRC2", "POL-EXP"),
167 ), findings=(),
168 eligible_for_analyst_review=eligible,
169 )
170
171
172 def trial(*, balanced=True):
173 accounts = (
174 TrialBalanceAccount("CASH1", "Cash", 2, 1, "40", "30", "20", "50", "10"),
175 TrialBalanceAccount("EQ1", "Equity", 3, 2, "-60", "0", "40", "-100", "-40"),
176 TrialBalanceAccount("REV", "Revenue", 4, 3, "0", "0", "30", "-30", "-30"),
177 TrialBalanceAccount("EXP", "Expense", 5, 4, "0", "20", "0", "20", "20"),
178 TrialBalanceAccount("OFFSET", "Offset", 6, 5, "20", "40", "0", "60", "40"),
179 )
180 return TrialBalanceDiagnostic(
181 company_code="001", fiscal_year=1404, source_sha256=SOURCE,
182 account_count=5, opening_debit_net_total="0",
183 debit_turnover_total="90", credit_turnover_total="90",
184 closing_debit_net_total="0", movement_difference="0",
185 opening_to_closing_difference="0", findings=(), accounts=accounts,
186 balanced_for_analyst_review=balanced,
187 )
188
189
190
191 def cash_with_transfer():
192 base = cash()
193 cash_one = replace(
194 base.account_reconciliations[0],
195 source_credit_turnover="30", closing_balance="40",
196 transaction_credit_turnover="30",
197 )
198 cash_two = CashAccountReconciliation(
199 account_code="CASH2", account_name="Cash two", source_row=6,
200 opening_balance="0", source_debit_turnover="10",
201 source_credit_turnover="0", closing_balance="10",
202 transaction_debit_turnover="10", transaction_credit_turnover="0",
203 debit_difference="0", credit_difference="0",
204 rollforward_difference="0", policy_evidence_ref="CASH2-POLICY",
205 )
206 transfer = CashFlowLine(
207 "L3", "V3", "1404-01-03", "CASH1", "CASH2",
208 "CASH_TRANSFER", "0", "10", "SRC3", "CASH2-POLICY",
209 )
210 return replace(
211 base, cash_account_count=2, transaction_count=3,
212 internal_transfer_inflows="10", internal_transfer_outflows="10",
213 account_reconciliations=(cash_one, cash_two),
214 cash_flow_lines=base.cash_flow_lines + (transfer,),
215 )
216
217
218 def trial_with_transfer():
219 base = trial()
220 rows = list(base.accounts)
221 rows[0] = replace(
222 rows[0], credit="30", closing="40", debit_net_movement="0",
223 )
224 rows.append(
225 TrialBalanceAccount("CASH2", "Cash two", 7, 6, "0", "10", "0", "10", "10")
226 )
227 return replace(
228 base, account_count=6, debit_turnover_total="100",
229 credit_turnover_total="100", accounts=tuple(rows),
230 )
231
232
233 def reconcile(**overrides):
234 inputs = {
235 "balance_sheet": bs(),
236 "income_statement": income(),
237 "equity_changes": equity(),
238 "cash_flow": cash(),
239 "trial_balance": trial(),
240 "cash_balance_notes": [
241 CashBalanceNoteSelection("Current assets", "CASH1", "", "FINANCE-POLICY-CASH")
242 ],
243 }
244 inputs.update(overrides)
245 return reconcile_cross_statements(**inputs)
246
247
248 def test_all_five_statements_tie_exactly_without_claiming_approval():
249 result = reconcile()
250 assert result.eligible_for_analyst_review, result.findings
251 assert not result.findings
252 assert result.same_source_snapshot
253 assert result.balance_sheet_difference == "0"
254 assert result.balance_sheet_equity_difference == "0"
255 assert result.net_profit_equity_difference == "0"
256 assert result.balance_sheet_cash_total == "50"
257 assert result.cash_flow_closing_cash == "50"
258 assert result.balance_sheet_cash_difference == "0"
259 assert result.income_statement_bridge_valid
260 assert result.cash_flow_bridge_valid
261 assert result.equity_rollforward_valid
262 assert result.trial_balance_arithmetic_valid
263 assert result.cash_accounts_match_trial_balance
264 assert result.cash_account_totals_valid
265 assert len(result.selected_cash_notes) == 1
266 assert not result.source_ledger_completeness_verified
267 assert not result.accounting_policy_authorized
268 assert not result.independently_reviewed
269 assert not result.trusted_approvals
270 assert not result.official_financial_statement
271 assert not result.export_authorized
272 assert not result.releasable
273
274
275 def test_different_source_snapshots_block_cross_statement_match():
276 result = reconcile(cash_flow=replace(cash(), source_sha256="e" * 64))
277 assert not result.eligible_for_analyst_review
278 assert not result.same_source_snapshot
279 assert "CROSS_STATEMENT_SOURCE_SNAPSHOT_MISMATCH" in result.findings
280
281
282 def test_company_or_year_mismatch_blocks():
283 result = reconcile(equity_changes=replace(equity(), company_code="002"))
284 assert "CROSS_STATEMENT_COMPANY_YEAR_MISMATCH" in result.findings
285 result = reconcile(trial_balance=replace(trial(), fiscal_year=1403))
286 assert "CROSS_STATEMENT_COMPANY_YEAR_MISMATCH" in result.findings
287
288
289 def test_balance_sheet_equity_must_equal_equity_statement_closing():
290 result = reconcile(equity_changes=equity(closing="99"))
291 assert result.balance_sheet_equity_difference == "1"
292 assert "BALANCE_SHEET_EQUITY_CLOSING_MISMATCH" in result.findings
293
294
295 def test_income_net_result_must_equal_equity_profit_movement():
296 result = reconcile(equity_changes=equity(profit="39"))
297 assert result.net_profit_equity_difference == "1"
298 assert "INCOME_RESULT_EQUITY_MOVEMENT_MISMATCH" in result.findings
299
300
301 def test_cash_flow_closing_must_equal_selected_balance_sheet_cash_notes():
302 result = reconcile(cash_flow=cash(closing="49"))
303 assert result.balance_sheet_cash_difference == "1"
304 assert "BALANCE_SHEET_CASH_FLOW_CLOSING_MISMATCH" in result.findings
305
306
307 def test_cash_note_selection_must_exist_and_be_an_asset():
308 result = reconcile(cash_balance_notes=[
309 CashBalanceNoteSelection("Missing", "C99", "", "POLICY")
310 ])
311 assert "CASH_BALANCE_NOTE_NOT_FOUND_IN_BALANCE_SHEET" in result.findings
312 assert "CASH_BALANCE_NOTE_SELECTION_INCOMPLETE" in result.findings
313 assert "BALANCE_SHEET_CASH_FLOW_CLOSING_MISMATCH" in result.findings
314
315 wrong = list(bs().lines)
316 wrong[0] = replace(wrong[0], category="LIABILITIES")
317 result = reconcile(balance_sheet=bs(lines=tuple(wrong)))
318 assert "CASH_BALANCE_NOTE_NOT_ASSET" in result.findings
319
320
321 def test_duplicate_or_empty_cash_note_selection_is_rejected():
322 selection = CashBalanceNoteSelection("Current assets", "CASH1", "", "POLICY")
323 with pytest.raises(CoverageError):
324 reconcile(cash_balance_notes=[selection, selection])
325 with pytest.raises(CoverageError):
326 reconcile(cash_balance_notes=[])
327
328
329 def test_blocked_upstream_candidates_block_cross_statement_gate():
330 result = reconcile(income_statement=income(eligible=False))
331 assert not result.eligible_for_analyst_review
332 assert "INCOME_STATEMENT_CANDIDATE_BLOCKED" in result.findings
333 result = reconcile(trial_balance=trial(balanced=False))
334 assert "TRIAL_BALANCE_CANDIDATE_BLOCKED" in result.findings
335 assert "TRIAL_BALANCE_ARITHMETIC_INVALID" in result.findings
336
337
338 def test_income_statement_internal_bridge_is_recomputed():
339 result = reconcile(income_statement=income(gross="61"))
340 assert not result.income_statement_bridge_valid
341 assert "INCOME_STATEMENT_BRIDGE_INVALID" in result.findings
342
343
344 def test_cash_flow_internal_bridge_is_recomputed():
345 result = reconcile(cash_flow=cash(net="9"))
346 assert not result.cash_flow_bridge_valid
347 assert "CASH_FLOW_BRIDGE_INVALID" in result.findings
348
349
350 def test_balance_sheet_equation_is_recomputed():
351 result = reconcile(balance_sheet=bs(difference="1"))
352 assert "BALANCE_SHEET_EQUATION_INVALID" in result.findings
353
354
355 def test_equity_rollforward_is_recomputed():
356 bad = replace(equity(), rollforward_difference="1")
357 result = reconcile(equity_changes=bad)
358 assert not result.equity_rollforward_valid
359 assert "EQUITY_STATEMENT_ROLLFORWARD_INVALID" in result.findings
360
361
362 def test_invalid_candidate_kind_or_object_is_rejected():
363 with pytest.raises(CoverageError):
364 reconcile(income_statement={"kind": "bad"})
365 with pytest.raises(CoverageError):
366 reconcile(balance_sheet=replace(bs(), kind="OFFICIAL"))
367
368
369 def test_approval_flags_cannot_be_smuggled_into_candidate_objects():
370 with pytest.raises(CoverageError):
371 reconcile(cash_flow=replace(cash(), official_financial_statement=True))
372 with pytest.raises(CoverageError):
373 reconcile(equity_changes=replace(equity(), releasable=True))
374
375
376 def test_invalid_source_hash_is_rejected():
377 with pytest.raises(CoverageError):
378 reconcile(balance_sheet=replace(bs(), source_sha256="not-a-hash"))
379
380
381 def test_cash_selection_hash_is_order_independent():
382 first = reconcile()
383 second = reconcile(cash_balance_notes=[
384 CashBalanceNoteSelection("Current assets", "CASH1", "", "FINANCE-POLICY-CASH")
385 ])
386 assert first.cash_note_selection_sha256 == second.cash_note_selection_sha256
387
388
389 def test_decimal_differences_are_not_rounded_or_tolerated():
390 result = reconcile(equity_changes=equity(closing="100.0000000001"))
391 assert result.balance_sheet_equity_difference == "-1E-10"
392 assert "BALANCE_SHEET_EQUITY_CLOSING_MISMATCH" in result.findings
393
394
395 def test_cash_account_source_balances_and_turnovers_must_match_trial_balance():
396 source = trial()
397 rows = list(source.accounts)
398 rows[0] = replace(rows[0], debit="31")
399 result = reconcile(trial_balance=replace(source, accounts=tuple(rows)))
400 assert not result.cash_accounts_match_trial_balance
401 assert "CASH_ACCOUNTS_TRIAL_BALANCE_MISMATCH" in result.findings
402
403
404 def test_cash_account_reconciliation_differences_block_cross_statement_gate():
405 source = cash()
406 row = replace(source.account_reconciliations[0], debit_difference="1")
407 result = reconcile(cash_flow=replace(source, account_reconciliations=(row,)))
408 assert not result.cash_account_totals_valid
409 assert "CASH_ACCOUNT_TURNOVER_OR_TOTALS_INVALID" in result.findings
410
411
412 def test_duplicate_balance_sheet_note_rows_are_not_silently_overwritten():
413 source = bs()
414 result = reconcile(balance_sheet=bs(lines=source.lines + (source.lines[0],)))
415 assert "DUPLICATE_BALANCE_SHEET_NOTE_LINE" in result.findings
416
417
418 def test_trial_balance_account_trace_count_must_be_complete():
419 source = trial()
420 result = reconcile(trial_balance=replace(source, account_count=3))
421 assert not result.cash_accounts_match_trial_balance
422 assert "CASH_ACCOUNTS_TRIAL_BALANCE_MISMATCH" in result.findings
423
424
425 def test_cash_flow_aggregates_must_match_individual_classified_lines():
426 source = cash()
427 lines = list(source.cash_flow_lines)
428 lines[0] = replace(lines[0], cash_inflow="29")
429 result = reconcile(cash_flow=replace(source, cash_flow_lines=tuple(lines)))
430 assert not result.cash_flow_bridge_valid
431 assert "CASH_FLOW_BRIDGE_INVALID" in result.findings
432
433
434 def test_unclassified_cash_flow_line_cannot_pass_cross_statement_gate():
435 source = cash()
436 lines = list(source.cash_flow_lines)
437 lines[0] = replace(lines[0], activity="UNCLASSIFIED")
438 result = reconcile(cash_flow=replace(source, cash_flow_lines=tuple(lines)))
439 assert not result.cash_flow_bridge_valid
440 assert "CASH_FLOW_BRIDGE_INVALID" in result.findings
441
442
443 def test_internal_cash_transfer_totals_must_be_derived_from_line_trace():
444 source = cash()
445 transfer = CashFlowLine(
446 "L3", "V3", "1404-01-03", "CASH1", "CASH2",
447 "CASH_TRANSFER", "10", "0", "SRC3", "CASH-POLICY",
448 )
449 bad = replace(
450 source, transaction_count=3,
451 cash_flow_lines=source.cash_flow_lines + (transfer,),
452 internal_transfer_inflows="9", internal_transfer_outflows="10",
453 )
454 result = reconcile(cash_flow=bad)
455 assert not result.cash_flow_bridge_valid
456 assert "CASH_FLOW_BRIDGE_INVALID" in result.findings
457
458
459 def test_balance_sheet_line_detail_must_support_summary_totals():
460 source = bs()
461 lines = list(source.lines)
462 lines[0] = replace(lines[0], presentation_amount="51")
463 result = reconcile(balance_sheet=replace(source, lines=tuple(lines)))
464 assert "BALANCE_SHEET_DETAIL_INVALID" in result.findings
465 assert not result.eligible_for_analyst_review
466
467
468 def test_income_statement_line_detail_must_support_summary_totals():
469 source = income()
470 lines = list(source.lines)
471 lines[0] = replace(lines[0], presentation_amount="101")
472 result = reconcile(income_statement=replace(source, lines=tuple(lines)))
473 assert not result.income_statement_bridge_valid
474 assert "INCOME_STATEMENT_BRIDGE_INVALID" in result.findings
475
476
477 def test_equity_account_detail_must_support_note_and_summary_movements():
478 source = equity()
479 account = replace(source.account_movements[0], credit_turnover="39")
480 result = reconcile(equity_changes=replace(
481 source, account_movements=(account,),
482 ))
483 assert not result.equity_rollforward_valid
484 assert "EQUITY_STATEMENT_ROLLFORWARD_INVALID" in result.findings
485
486
487 def test_trial_balance_row_rollforward_mismatch_cannot_net_off_globally():
488 source = trial()
489 rows = list(source.accounts)
490 rows[0] = replace(rows[0], closing="51")
491 rows[4] = replace(rows[4], closing="59")
492 assert sum(Decimal(row.closing) for row in rows) == 0
493 result = reconcile(trial_balance=replace(source, accounts=tuple(rows)))
494 assert not result.trial_balance_arithmetic_valid
495 assert "TRIAL_BALANCE_ARITHMETIC_INVALID" in result.findings
496
497
498 def test_cash_account_transaction_turnovers_must_match_cash_flow_line_trace():
499 source = cash()
500 account = replace(source.account_reconciliations[0], transaction_debit_turnover="29")
501 result = reconcile(cash_flow=replace(
502 source, account_reconciliations=(account,),
503 ))
504 assert not result.cash_account_totals_valid
505 assert "CASH_ACCOUNT_TURNOVER_OR_TOTALS_INVALID" in result.findings
506
507
508 def test_all_statement_candidates_must_share_one_note_reference_snapshot():
509 result = reconcile(income_statement=replace(
510 income(), note_references_sha256="e" * 64,
511 ))
512 assert "CROSS_STATEMENT_NOTE_REFERENCE_SNAPSHOT_MISMATCH" in result.findings
513 assert not result.eligible_for_analyst_review
514
515
516 def test_candidate_eligibility_must_be_a_real_boolean():
517 with pytest.raises(CoverageError):
518 reconcile(income_statement=replace(
519 income(), eligible_for_analyst_review="False",
520 ))
521
522
523 def test_cash_scope_and_ledger_completeness_flags_cannot_be_smuggled_in():
524 with pytest.raises(CoverageError):
525 reconcile(cash_flow=replace(
526 cash(), cash_account_scope_owner_approved=True,
527 ))
528
529
530 def test_equity_account_movement_must_match_trial_balance_source_row():
531 source = trial()
532 rows = list(source.accounts)
533 rows[1] = replace(rows[1], credit="39", closing="-99", debit_net_movement="-39")
534 result = reconcile(trial_balance=replace(source, accounts=tuple(rows)))
535 assert not result.equity_rollforward_valid
536 assert "EQUITY_STATEMENT_ROLLFORWARD_INVALID" in result.findings
537
538
539 def test_internal_cash_transfer_line_ties_both_cash_accounts_to_trial_balance():
540 result = reconcile(cash_flow=cash_with_transfer(), trial_balance=trial_with_transfer())
541 assert result.eligible_for_analyst_review, result.findings
542 assert result.cash_flow_bridge_valid
543 assert result.cash_accounts_match_trial_balance
544 assert result.cash_account_totals_valid