codex/aram-mapping-source-parser-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 250 lines 9.92 KB
Raw
1 """End-to-end synthetic integration tests for all five ARAM statement engines."""
2 from __future__ import annotations
3
4 from dataclasses import asdict
5 from io import BytesIO
6 import json
7
8 import pytest
9 from openpyxl import Workbook
10
11 from aram_backend.balance_sheet_candidate import BalanceSheetLinePolicy
12 from aram_backend.cash_flow_direct_candidate import (
13 CashAccountPolicy, CashCounterpartPolicy, CashFlowEntry,
14 )
15 from aram_backend.coverage_preview import CoverageError
16 from aram_backend.cross_statement_reconciliation import CashBalanceNoteSelection
17 from aram_backend.equity_changes_candidate import EquityMovementPolicy
18 from aram_backend.income_statement_candidate import IncomeStatementLinePolicy
19 from aram_backend.pilot_workbook import SHEET_HEADERS
20 from aram_backend.reconciliation_review import NoteReference
21 from aram_backend.five_statement_pipeline import build_five_statement_pipeline
22 from aram_backend.five_statement_pipeline_cli import main as pipeline_cli_main
23 from aram_backend.synthetic_five_statement_demo import (
24 COMPANY, YEAR, cash_transactions, pipeline_manifest,
25 references, source_workbook,
26 )
27
28
29 def build_pipeline(
30 data: bytes | None = None, *, transactions=None, refs=None,
31 ):
32 source = source_workbook() if data is None else data
33 return build_five_statement_pipeline(
34 source, company_code=COMPANY, fiscal_year=YEAR,
35 references=references() if refs is None else refs,
36 balance_sheet_policies=(
37 BalanceSheetLinePolicy(
38 "Current assets", "C01", "", "ASSETS", 1,
39 "Cash and cash equivalents", "SYNTH-POL-BS-CASH",
40 ),
41 BalanceSheetLinePolicy(
42 "Current assets", "A01", "", "ASSETS", 1,
43 "Trade receivables", "SYNTH-POL-BS-AR",
44 ),
45 BalanceSheetLinePolicy(
46 "Current liabilities", "L01", "", "LIABILITIES", -1,
47 "Trade payables", "SYNTH-POL-BS-AP",
48 ),
49 BalanceSheetLinePolicy(
50 "Equity", "E01", "", "EQUITY", -1,
51 "Total equity", "SYNTH-POL-BS-EQ",
52 ),
53 ),
54 income_statement_policies=(
55 IncomeStatementLinePolicy(
56 "Revenue", "R01", "", "REVENUE", -1,
57 "Revenue", "SYNTH-POL-IS-REV",
58 ),
59 IncomeStatementLinePolicy(
60 "Cost of revenue", "X01", "", "COST_OF_REVENUE", 1,
61 "Cost of revenue", "SYNTH-POL-IS-COST",
62 ),
63 ),
64 equity_movement_policies=(
65 EquityMovementPolicy(
66 "Share capital", "E01", "", "SHARE_CAPITAL",
67 "CAPITAL_CONTRIBUTION", -1, "Share capital",
68 "SYNTH-POL-EQ-CAP",
69 ),
70 EquityMovementPolicy(
71 "Current year result", "P01", "", "RETAINED_EARNINGS",
72 "NET_PROFIT_LOSS", -1, "Current year profit",
73 "SYNTH-POL-EQ-PROFIT",
74 ),
75 ),
76 currency="IRR", unit="Rial",
77 cash_accounts=(CashAccountPolicy("CASH", "SYNTH-CASH-SCOPE"),),
78 counterpart_policies=(
79 CashCounterpartPolicy("REV", "OPERATING", "SYNTH-REV-CLASS"),
80 CashCounterpartPolicy("EXP", "OPERATING", "SYNTH-EXP-CLASS"),
81 ),
82 transactions=cash_transactions() if transactions is None else transactions,
83 cash_balance_notes=(
84 CashBalanceNoteSelection(
85 "Current assets", "C01", "", "SYNTH-CASH-NOTE-SELECTION",
86 ),
87 ),
88 )
89
90
91 def build_all(data: bytes | None = None, *, transactions=None):
92 pipeline = build_pipeline(data, transactions=transactions)
93 return (
94 pipeline.balance_sheet, pipeline.income_statement,
95 pipeline.equity_changes, pipeline.cash_flow,
96 pipeline.trial_balance, pipeline.reconciliation,
97 )
98
99
100
101 def test_all_five_real_builders_reconcile_from_one_immutable_workbook():
102 bs, income, equity, cash, trial, result = build_all()
103
104 assert bs.eligible_for_analyst_review and not bs.findings
105 assert income.eligible_for_analyst_review and not income.findings
106 assert equity.eligible_for_analyst_review and not equity.findings
107 assert cash.eligible_for_analyst_review and not cash.findings
108 assert trial.balanced_for_analyst_review and not trial.findings
109 assert len({item.source_sha256 for item in (bs, income, equity, cash, trial)}) == 1
110 assert len({
111 bs.note_references_sha256,
112 income.note_references_sha256,
113 equity.note_references_sha256,
114 }) == 1
115
116 assert bs.assets_total == "100"
117 assert bs.liabilities_total == "10"
118 assert bs.equity_total == "90"
119 assert income.net_result == "20"
120 assert equity.opening_equity_total == "70"
121 assert equity.net_profit_loss_movements == "20"
122 assert equity.closing_equity_total == "90"
123 assert cash.opening_cash_and_equivalents == "40"
124 assert cash.net_change_in_cash == "10"
125 assert cash.closing_cash_and_equivalents == "50"
126 assert trial.opening_debit_net_total == "0"
127 assert trial.debit_turnover_total == "60"
128 assert trial.credit_turnover_total == "60"
129 assert trial.closing_debit_net_total == "0"
130
131 assert result.eligible_for_analyst_review
132 assert result.findings == ()
133 assert result.balance_sheet_equity_difference == "0"
134 assert result.net_profit_equity_difference == "0"
135 assert result.balance_sheet_cash_difference == "0"
136 assert result.cash_accounts_match_trial_balance
137 assert result.cash_account_totals_valid
138 assert not result.source_ledger_completeness_verified
139 assert not result.accounting_policy_authorized
140 assert not result.independently_reviewed
141 assert not result.official_financial_statement
142 assert not result.export_authorized
143 assert not result.releasable
144
145
146 def test_missing_cash_transaction_blocks_real_five_statement_pipeline():
147 bs, income, equity, cash, trial, result = build_all(
148 transactions=cash_transactions()[:-1],
149 )
150 assert bs.eligible_for_analyst_review
151 assert income.eligible_for_analyst_review
152 assert equity.eligible_for_analyst_review
153 assert trial.balanced_for_analyst_review
154 assert not cash.eligible_for_analyst_review
155 assert not result.eligible_for_analyst_review
156 assert "CASH_FLOW_CANDIDATE_BLOCKED" in result.findings
157 assert "CASH_FLOW_BRIDGE_INVALID" in result.findings or "CASH_ACCOUNT_TURNOVER_OR_TOTALS_INVALID" in result.findings
158
159
160 def test_pipeline_result_is_nonofficial_and_freezes_reference_collection():
161 result = build_pipeline()
162 payload = result.to_dict()
163 assert result.eligible_for_analyst_review
164 assert payload["kind"] == "UNAPPROVED_FIVE_STATEMENT_PIPELINE_CANDIDATE"
165 assert payload["source_sha256"] == result.source_sha256
166 for flag in (
167 "source_ledger_completeness_verified", "accounting_policy_authorized",
168 "independently_reviewed", "trusted_approvals",
169 "official_financial_statement", "export_authorized", "releasable",
170 ):
171 assert payload[flag] is False
172
173 with pytest.raises(CoverageError) as error:
174 build_pipeline(refs=iter(references()))
175 assert error.value.code == "INVALID_FIVE_STATEMENT_NOTE_REFERENCES"
176
177
178 def test_offline_pipeline_cli_emits_json_without_creating_output_files(tmp_path, capsys):
179 workbook_path = tmp_path / "synthetic.xlsx"
180 manifest_path = tmp_path / "pipeline.json"
181 workbook_path.write_bytes(source_workbook())
182 manifest_path.write_text(
183 json.dumps(pipeline_manifest(), ensure_ascii=True),
184 encoding="utf-8",
185 )
186
187 exit_code = pipeline_cli_main([
188 "--file", str(workbook_path), "--manifest", str(manifest_path),
189 ])
190 captured = capsys.readouterr()
191 payload = json.loads(captured.out)
192 assert exit_code == 0
193 assert payload["eligible_for_analyst_review"] is True
194 assert payload["kind"] == "UNAPPROVED_FIVE_STATEMENT_PIPELINE_CANDIDATE"
195 assert payload["official_financial_statement"] is False
196 assert payload["releasable"] is False
197 assert {item.name for item in tmp_path.iterdir()} == {
198 "synthetic.xlsx", "pipeline.json",
199 }
200
201
202 def test_offline_pipeline_cli_returns_blocked_exit_for_incomplete_transactions(
203 tmp_path, capsys,
204 ):
205 workbook_path = tmp_path / "synthetic.xlsx"
206 manifest_path = tmp_path / "pipeline.json"
207 workbook_path.write_bytes(source_workbook())
208 manifest_path.write_text(
209 json.dumps(pipeline_manifest(transactions=cash_transactions()[:-1])),
210 encoding="utf-8",
211 )
212
213 exit_code = pipeline_cli_main([
214 "--file", str(workbook_path), "--manifest", str(manifest_path),
215 ])
216 payload = json.loads(capsys.readouterr().out)
217 assert exit_code == 3
218 assert payload["eligible_for_analyst_review"] is False
219 assert "CASH_FLOW_CANDIDATE_BLOCKED" in payload["findings"]
220
221
222 def test_pipeline_manifest_rejects_duplicate_keys_and_nonfinite_numbers():
223 from aram_backend.five_statement_pipeline_cli import parse_pipeline_manifest
224
225 with pytest.raises(CoverageError) as duplicate:
226 parse_pipeline_manifest(b'{"schema_version":1,"schema_version":1}')
227 assert duplicate.value.code == "DUPLICATE_MANIFEST_KEY"
228
229 document = pipeline_manifest()
230 document["fiscal_year"] = float("nan")
231 with pytest.raises(CoverageError) as nonfinite:
232 parse_pipeline_manifest(json.dumps(document).encode("utf-8"))
233 assert nonfinite.value.code == "PIPELINE_MANIFEST_NONFINITE_NUMBER"
234
235
236 def test_pipeline_manifest_rejects_unknown_fields_and_missing_cash_registry():
237 from aram_backend.five_statement_pipeline_cli import parse_pipeline_manifest
238
239 document = pipeline_manifest()
240 document["unexpected"] = True
241 with pytest.raises(CoverageError) as unknown:
242 parse_pipeline_manifest(json.dumps(document).encode("utf-8"))
243 assert unknown.value.code == "PIPELINE_MANIFEST_SCHEMA_INVALID"
244
245 document = pipeline_manifest()
246 document["cash_accounts"] = []
247 with pytest.raises(CoverageError) as missing:
248 parse_pipeline_manifest(json.dumps(document).encode("utf-8"))
249 assert missing.value.code == "PIPELINE_MANIFEST_CASH_ACCOUNTS_INVALID"
250