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

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

Synced Dec 3, 18:15 UTC

codex/aram-five-statement-orchestrator-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 287 lines 11.7 KB
Raw
1 """End-to-end synthetic integration tests for all five ARAM statement engines."""
2 from __future__ import annotations
3
4 from io import BytesIO
5
6 import pytest
7 from openpyxl import Workbook
8
9 from aram_backend.balance_sheet_candidate import BalanceSheetLinePolicy
10 from aram_backend.cash_flow_direct_candidate import (
11 CashAccountPolicy, CashCounterpartPolicy, CashFlowEntry,
12 )
13 from aram_backend.coverage_preview import CoverageError
14 from aram_backend.cross_statement_reconciliation import CashBalanceNoteSelection
15 from aram_backend.equity_changes_candidate import EquityMovementPolicy
16 from aram_backend.income_statement_candidate import IncomeStatementLinePolicy
17 from aram_backend.pilot_workbook import SHEET_HEADERS
18 from aram_backend.reconciliation_review import NoteReference
19 from aram_backend.five_statement_pipeline import build_five_statement_pipeline
20
21
22 COMPANY = "001"
23 YEAR = 1404
24
25
26 def source_workbook() -> bytes:
27 """One debit-net balanced ledger shared by every candidate builder."""
28 book = Workbook()
29 book.remove(book.active)
30 for name, headers in SHEET_HEADERS.items():
31 book.create_sheet(name).append(list(headers))
32
33 def add(sheet: str, **values: object) -> None:
34 book[sheet].append([values.get(column) for column in SHEET_HEADERS[sheet]])
35
36 add(
37 "Company", company_code=COMPANY, company_name="Synthetic integration only",
38 fiscal_year=YEAR, currency="IRR", unit="Rial", prepared_by="Test fixture",
39 status="Draft",
40 )
41 ledger = (
42 # code, name, opening, debit, credit, closing
43 ("CASH", "Cash", "40", "30", "20", "50"),
44 ("AR", "Receivables", "50", "0", "0", "50"),
45 ("AP", "Payables", "-20", "10", "0", "-10"),
46 ("CAP", "Share capital", "-70", "0", "0", "-70"),
47 ("REV", "Revenue", "0", "0", "40", "-40"),
48 ("EXP", "Cost of revenue", "0", "20", "0", "20"),
49 )
50 for index, (code, name, opening, debit, credit, closing) in enumerate(ledger, 101):
51 add(
52 "Accounts", company_code=COMPANY, fiscal_year=YEAR,
53 general_code=code, general_name=name, account_full_code=code,
54 account_name=name, opening=opening, debit=debit, credit=credit,
55 closing=closing, source_row=index, row_status="Draft",
56 closing_delta="0",
57 )
58
59 notes = (
60 ("BS", "Current assets", "C01", "Cash"),
61 ("BS", "Current assets", "A01", "Receivables"),
62 ("BS", "Current liabilities", "L01", "Payables"),
63 ("BS", "Equity", "E01", "Equity"),
64 ("IS", "Revenue", "R01", "Revenue"),
65 ("IS", "Cost of revenue", "X01", "Cost of revenue"),
66 ("EQ", "Share capital", "E01", "Share capital"),
67 ("EQ", "Current year result", "P01", "Current year result"),
68 )
69 for statement, section, note, title in notes:
70 add(
71 "Notes", company_code=COMPANY, fiscal_year=YEAR,
72 statement=statement, statement_section=section,
73 note_number=note, note_title=title, subnote_code="",
74 subnote_title="",
75 )
76
77 mappings = (
78 ("CASH", "BS", "Current assets", "C01"),
79 ("AR", "BS", "Current assets", "A01"),
80 ("AP", "BS", "Current liabilities", "L01"),
81 ("CAP", "BS", "Equity", "E01"),
82 ("CAP", "EQ", "Share capital", "E01"),
83 ("REV", "IS", "Revenue", "R01"),
84 ("REV", "BS", "Equity", "E01"),
85 ("REV", "EQ", "Current year result", "P01"),
86 ("EXP", "IS", "Cost of revenue", "X01"),
87 ("EXP", "BS", "Equity", "E01"),
88 ("EXP", "EQ", "Current year result", "P01"),
89 )
90 for index, (account, statement, section, note) in enumerate(mappings, 1):
91 add(
92 "Mapping", mapping_id=f"SYNTH-M-{index:02d}",
93 company_code=COMPANY, fiscal_year=YEAR,
94 account_full_code=account, account_level="General",
95 general_code=account, target_statement=statement,
96 statement_section=section, note_number=note, subnote_code="",
97 mapping_version=1, approval_status="Approved",
98 reviewer="Synthetic fixture only", review_date="2026-10-31",
99 source="Synthetic test", notes="Not an authenticated approval",
100 validation_status="Valid",
101 )
102
103 output = BytesIO()
104 book.save(output)
105 book.close()
106 return output.getvalue()
107
108
109 def references() -> tuple[NoteReference, ...]:
110 return (
111 NoteReference("BS", "Current assets", "C01", "", "50", "SYNTH-BS-CASH"),
112 NoteReference("BS", "Current assets", "A01", "", "50", "SYNTH-BS-AR"),
113 NoteReference("BS", "Current liabilities", "L01", "", "-10", "SYNTH-BS-AP"),
114 NoteReference("BS", "Equity", "E01", "", "-90", "SYNTH-BS-EQ"),
115 NoteReference("IS", "Revenue", "R01", "", "-40", "SYNTH-IS-REV"),
116 NoteReference("IS", "Cost of revenue", "X01", "", "20", "SYNTH-IS-COST"),
117 NoteReference("EQ", "Share capital", "E01", "", "-70", "SYNTH-EQ-CAP"),
118 NoteReference("EQ", "Current year result", "P01", "", "-20", "SYNTH-EQ-PROFIT"),
119 )
120
121
122 def cash_transactions() -> tuple[CashFlowEntry, ...]:
123 return (
124 CashFlowEntry(
125 company_code=COMPANY, fiscal_year=YEAR, line_id="CF-L1",
126 transaction_id="CF-V1", posting_date="1404-01-01",
127 cash_account_code="CASH", counterpart_account_code="REV",
128 cash_debit="30", cash_credit="0", counterpart_debit="0",
129 counterpart_credit="30", evidence_ref="SYNTH-TXN-1",
130 ),
131 CashFlowEntry(
132 company_code=COMPANY, fiscal_year=YEAR, line_id="CF-L2",
133 transaction_id="CF-V2", posting_date="1404-01-02",
134 cash_account_code="CASH", counterpart_account_code="EXP",
135 cash_debit="0", cash_credit="20", counterpart_debit="20",
136 counterpart_credit="0", evidence_ref="SYNTH-TXN-2",
137 ),
138 )
139
140
141 def build_pipeline(
142 data: bytes | None = None, *, transactions=None, refs=None,
143 ):
144 source = source_workbook() if data is None else data
145 return build_five_statement_pipeline(
146 source, company_code=COMPANY, fiscal_year=YEAR,
147 references=references() if refs is None else refs,
148 balance_sheet_policies=(
149 BalanceSheetLinePolicy(
150 "Current assets", "C01", "", "ASSETS", 1,
151 "Cash and cash equivalents", "SYNTH-POL-BS-CASH",
152 ),
153 BalanceSheetLinePolicy(
154 "Current assets", "A01", "", "ASSETS", 1,
155 "Trade receivables", "SYNTH-POL-BS-AR",
156 ),
157 BalanceSheetLinePolicy(
158 "Current liabilities", "L01", "", "LIABILITIES", -1,
159 "Trade payables", "SYNTH-POL-BS-AP",
160 ),
161 BalanceSheetLinePolicy(
162 "Equity", "E01", "", "EQUITY", -1,
163 "Total equity", "SYNTH-POL-BS-EQ",
164 ),
165 ),
166 income_statement_policies=(
167 IncomeStatementLinePolicy(
168 "Revenue", "R01", "", "REVENUE", -1,
169 "Revenue", "SYNTH-POL-IS-REV",
170 ),
171 IncomeStatementLinePolicy(
172 "Cost of revenue", "X01", "", "COST_OF_REVENUE", 1,
173 "Cost of revenue", "SYNTH-POL-IS-COST",
174 ),
175 ),
176 equity_movement_policies=(
177 EquityMovementPolicy(
178 "Share capital", "E01", "", "SHARE_CAPITAL",
179 "CAPITAL_CONTRIBUTION", -1, "Share capital",
180 "SYNTH-POL-EQ-CAP",
181 ),
182 EquityMovementPolicy(
183 "Current year result", "P01", "", "RETAINED_EARNINGS",
184 "NET_PROFIT_LOSS", -1, "Current year profit",
185 "SYNTH-POL-EQ-PROFIT",
186 ),
187 ),
188 currency="IRR", unit="Rial",
189 cash_accounts=(CashAccountPolicy("CASH", "SYNTH-CASH-SCOPE"),),
190 counterpart_policies=(
191 CashCounterpartPolicy("REV", "OPERATING", "SYNTH-REV-CLASS"),
192 CashCounterpartPolicy("EXP", "OPERATING", "SYNTH-EXP-CLASS"),
193 ),
194 transactions=cash_transactions() if transactions is None else transactions,
195 cash_balance_notes=(
196 CashBalanceNoteSelection(
197 "Current assets", "C01", "", "SYNTH-CASH-NOTE-SELECTION",
198 ),
199 ),
200 )
201
202
203 def build_all(data: bytes | None = None, *, transactions=None):
204 pipeline = build_pipeline(data, transactions=transactions)
205 return (
206 pipeline.balance_sheet, pipeline.income_statement,
207 pipeline.equity_changes, pipeline.cash_flow,
208 pipeline.trial_balance, pipeline.reconciliation,
209 )
210
211
212 def test_all_five_real_builders_reconcile_from_one_immutable_workbook():
213 bs, income, equity, cash, trial, result = build_all()
214
215 assert bs.eligible_for_analyst_review and not bs.findings
216 assert income.eligible_for_analyst_review and not income.findings
217 assert equity.eligible_for_analyst_review and not equity.findings
218 assert cash.eligible_for_analyst_review and not cash.findings
219 assert trial.balanced_for_analyst_review and not trial.findings
220 assert len({item.source_sha256 for item in (bs, income, equity, cash, trial)}) == 1
221 assert len({
222 bs.note_references_sha256,
223 income.note_references_sha256,
224 equity.note_references_sha256,
225 }) == 1
226
227 assert bs.assets_total == "100"
228 assert bs.liabilities_total == "10"
229 assert bs.equity_total == "90"
230 assert income.net_result == "20"
231 assert equity.opening_equity_total == "70"
232 assert equity.net_profit_loss_movements == "20"
233 assert equity.closing_equity_total == "90"
234 assert cash.opening_cash_and_equivalents == "40"
235 assert cash.net_change_in_cash == "10"
236 assert cash.closing_cash_and_equivalents == "50"
237 assert trial.opening_debit_net_total == "0"
238 assert trial.debit_turnover_total == "60"
239 assert trial.credit_turnover_total == "60"
240 assert trial.closing_debit_net_total == "0"
241
242 assert result.eligible_for_analyst_review
243 assert result.findings == ()
244 assert result.balance_sheet_equity_difference == "0"
245 assert result.net_profit_equity_difference == "0"
246 assert result.balance_sheet_cash_difference == "0"
247 assert result.cash_accounts_match_trial_balance
248 assert result.cash_account_totals_valid
249 assert not result.source_ledger_completeness_verified
250 assert not result.accounting_policy_authorized
251 assert not result.independently_reviewed
252 assert not result.official_financial_statement
253 assert not result.export_authorized
254 assert not result.releasable
255
256
257 def test_missing_cash_transaction_blocks_real_five_statement_pipeline():
258 bs, income, equity, cash, trial, result = build_all(
259 transactions=cash_transactions()[:-1],
260 )
261 assert bs.eligible_for_analyst_review
262 assert income.eligible_for_analyst_review
263 assert equity.eligible_for_analyst_review
264 assert trial.balanced_for_analyst_review
265 assert not cash.eligible_for_analyst_review
266 assert not result.eligible_for_analyst_review
267 assert "CASH_FLOW_CANDIDATE_BLOCKED" in result.findings
268 assert "CASH_FLOW_BRIDGE_INVALID" in result.findings or "CASH_ACCOUNT_TURNOVER_OR_TOTALS_INVALID" in result.findings
269
270
271 def test_pipeline_result_is_nonofficial_and_freezes_reference_collection():
272 result = build_pipeline()
273 payload = result.to_dict()
274 assert result.eligible_for_analyst_review
275 assert payload["kind"] == "UNAPPROVED_FIVE_STATEMENT_PIPELINE_CANDIDATE"
276 assert payload["source_sha256"] == result.source_sha256
277 for flag in (
278 "source_ledger_completeness_verified", "accounting_policy_authorized",
279 "independently_reviewed", "trusted_approvals",
280 "official_financial_statement", "export_authorized", "releasable",
281 ):
282 assert payload[flag] is False
283
284 with pytest.raises(CoverageError) as error:
285 build_pipeline(refs=iter(references()))
286 assert error.value.code == "INVALID_FIVE_STATEMENT_NOTE_REFERENCES"
287