codex/aram-five-statement-cli-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 337 lines 13.1 KB
Raw
1 """Synthetic-only direct cash flow candidate tests; no customer data."""
2 from __future__ import annotations
3
4 from dataclasses import replace
5 from io import BytesIO
6 import json
7
8 import pytest
9 from openpyxl import Workbook, load_workbook
10
11 from aram_backend.cash_flow_direct_candidate import (
12 CashAccountPolicy, CashCounterpartPolicy, CashFlowEntry,
13 build_direct_cash_flow_candidate,
14 )
15 from aram_backend.coverage_preview import CoverageError
16 from aram_backend.pilot_workbook import SHEET_HEADERS
17
18
19 ACCOUNT_ROWS = (
20 ("CASH1", "Cash account one", "100", "40", "40", "100"),
21 ("CASH2", "Cash account two", "20", "20", "10", "30"),
22 ("REV", "Sales counterpart", "0", "0", "40", "-40"),
23 ("EXP1", "Operating expense", "0", "20", "0", "20"),
24 ("ASSET", "Equipment", "0", "10", "0", "10"),
25 ("CAP", "Capital", "0", "0", "10", "-10"),
26 ("EXP2", "Other operating expense", "0", "10", "0", "10"),
27 )
28
29
30 def workbook(*, mutate=None):
31 book = Workbook()
32 book.remove(book.active)
33 for name, headers in SHEET_HEADERS.items():
34 book.create_sheet(name).append(list(headers))
35
36 def add(name, **values):
37 book[name].append([values.get(key) for key in SHEET_HEADERS[name]])
38
39 add("Company", company_code="001", company_name="Synthetic only",
40 fiscal_year=1404, currency="IRR", unit="Rial", status="Draft")
41 for index, (code, name, opening, debit, credit, closing) in enumerate(ACCOUNT_ROWS, 2):
42 add("Accounts", company_code="001", fiscal_year=1404,
43 general_code=code, general_name=name, account_full_code=code,
44 account_name=name, opening=opening, debit=debit, credit=credit,
45 closing=closing, source_row=index + 20, row_status="Draft",
46 closing_delta="0")
47 if mutate:
48 mutate(book)
49 stream = BytesIO()
50 book.save(stream)
51 book.close()
52 return stream.getvalue()
53
54
55 def cash_accounts():
56 return [
57 CashAccountPolicy("CASH1", "SYNTH-CASH-POLICY-1"),
58 CashAccountPolicy("CASH2", "SYNTH-CASH-POLICY-2"),
59 ]
60
61
62 def counterpart_policies():
63 return [
64 CashCounterpartPolicy("REV", "OPERATING", "SYNTH-REV"),
65 CashCounterpartPolicy("EXP1", "OPERATING", "SYNTH-EXP1"),
66 CashCounterpartPolicy("ASSET", "INVESTING", "SYNTH-ASSET"),
67 CashCounterpartPolicy("CAP", "FINANCING", "SYNTH-CAP"),
68 CashCounterpartPolicy("EXP2", "OPERATING", "SYNTH-EXP2"),
69 ]
70
71
72 def transactions():
73 common = {"company_code": "001", "fiscal_year": 1404}
74 return [
75 CashFlowEntry(**common, line_id="L1", transaction_id="V1",
76 posting_date="1404-01-01", cash_account_code="CASH1",
77 counterpart_account_code="REV", cash_debit="40", cash_credit="0",
78 counterpart_debit="0", counterpart_credit="40", evidence_ref="SYNTH-T1"),
79 CashFlowEntry(**common, line_id="L2", transaction_id="V2",
80 posting_date="1404-01-02", cash_account_code="CASH1",
81 counterpart_account_code="EXP1", cash_debit="0", cash_credit="20",
82 counterpart_debit="20", counterpart_credit="0", evidence_ref="SYNTH-T2"),
83 CashFlowEntry(**common, line_id="L3", transaction_id="V3",
84 posting_date="1404-01-03", cash_account_code="CASH1",
85 counterpart_account_code="ASSET", cash_debit="0", cash_credit="10",
86 counterpart_debit="10", counterpart_credit="0", evidence_ref="SYNTH-T3"),
87 CashFlowEntry(**common, line_id="L4", transaction_id="V4",
88 posting_date="1404-01-04", cash_account_code="CASH1",
89 counterpart_account_code="CASH2", cash_debit="0", cash_credit="10",
90 counterpart_debit="10", counterpart_credit="0", evidence_ref="SYNTH-T4"),
91 CashFlowEntry(**common, line_id="L5", transaction_id="V5",
92 posting_date="1404-01-05", cash_account_code="CASH2",
93 counterpart_account_code="CAP", cash_debit="10", cash_credit="0",
94 counterpart_debit="0", counterpart_credit="10", evidence_ref="SYNTH-T5"),
95 CashFlowEntry(**common, line_id="L6", transaction_id="V6",
96 posting_date="1404-01-06", cash_account_code="CASH2",
97 counterpart_account_code="EXP2", cash_debit="0", cash_credit="10",
98 counterpart_debit="10", counterpart_credit="0", evidence_ref="SYNTH-T6"),
99 ]
100
101
102 def build(*, data=None, cash=None, policies=None, entries=None,
103 company="001", year=1404, currency="IRR", unit="Rial"):
104 return build_direct_cash_flow_candidate(
105 workbook() if data is None else data,
106 company_code=company, fiscal_year=year, currency=currency, unit=unit,
107 cash_accounts=cash_accounts() if cash is None else cash,
108 counterpart_policies=counterpart_policies() if policies is None else policies,
109 transactions=transactions() if entries is None else entries,
110 )
111
112
113 def edit(data, change):
114 book = load_workbook(BytesIO(data))
115 change(book)
116 stream = BytesIO()
117 book.save(stream)
118 book.close()
119 return stream.getvalue()
120
121
122 def test_direct_cash_flow_classification_and_cash_rollforward():
123 result = build()
124 assert result.eligible_for_analyst_review
125 assert not result.findings
126 assert result.opening_cash_and_equivalents == "120"
127 assert result.operating_inflows == "40"
128 assert result.operating_outflows == "30"
129 assert result.operating_net_cash_flow == "10"
130 assert result.investing_inflows == "0"
131 assert result.investing_outflows == "10"
132 assert result.investing_net_cash_flow == "-10"
133 assert result.financing_inflows == "10"
134 assert result.financing_outflows == "0"
135 assert result.financing_net_cash_flow == "10"
136 assert result.internal_transfer_inflows == "10"
137 assert result.internal_transfer_outflows == "10"
138 assert result.net_change_in_cash == "10"
139 assert result.closing_cash_and_equivalents == "130"
140 assert result.rollforward_difference == "0"
141 assert result.cash_account_count == 2
142 assert result.transaction_count == 6
143 assert len(result.cash_flow_lines) == 6
144 assert all(a.debit_difference == "0" and a.credit_difference == "0"
145 for a in result.account_reconciliations)
146 assert result.kind == "UNAPPROVED_DIRECT_CASH_FLOW_CANDIDATE"
147 assert not result.cash_account_scope_owner_approved
148 assert not result.transaction_ledger_completeness_verified
149 assert not result.accounting_policy_authorized
150 assert not result.official_financial_statement
151 assert not result.independently_reviewed
152 assert not result.export_authorized
153 assert not result.releasable
154
155
156 def test_missing_transaction_line_blocks_turnover_reconciliation():
157 result = build(entries=transactions()[:-1])
158 assert not result.eligible_for_analyst_review
159 assert "CASH_ACCOUNT_CREDIT_TURNOVER_MISMATCH" in result.findings
160
161
162 def test_missing_counterpart_classification_blocks_candidate():
163 policies = counterpart_policies()[:-1]
164 result = build(policies=policies)
165 assert not result.eligible_for_analyst_review
166 assert "MISSING_COUNTERPART_ACTIVITY_POLICY" in result.findings
167 assert "CASH_FLOW_CASH_BALANCE_ROLLFORWARD_MISMATCH" in result.findings
168
169
170 def test_orphan_counterpart_policy_blocks_candidate():
171 policies = counterpart_policies() + [
172 CashCounterpartPolicy("UNUSED", "OPERATING", "SYNTH-UNUSED")
173 ]
174 result = build(policies=policies)
175 assert not result.eligible_for_analyst_review
176 assert "ORPHAN_COUNTERPART_ACTIVITY_POLICY" in result.findings
177
178
179 def test_internal_cash_transfer_is_excluded_from_activity_categories():
180 result = build()
181 assert result.internal_transfer_inflows == result.internal_transfer_outflows
182 assert result.operating_net_cash_flow == "10"
183 assert result.investing_net_cash_flow == "-10"
184 assert result.financing_net_cash_flow == "10"
185
186
187 def test_unbalanced_cash_counterpart_pair_rejected():
188 entries = transactions()
189 entries[0] = replace(entries[0], counterpart_credit="39")
190 with pytest.raises(CoverageError) as error:
191 build(entries=entries)
192 assert error.value.code == "CASH_FLOW_COUNTERPART_NOT_BALANCED"
193
194
195 def test_duplicate_line_id_rejected():
196 entries = transactions()
197 entries[1] = replace(entries[1], line_id="L1")
198 with pytest.raises(CoverageError) as error:
199 build(entries=entries)
200 assert error.value.code == "DUPLICATE_CASH_FLOW_LINE_ID"
201
202
203 def test_wrong_company_or_year_transaction_rejected():
204 entries = transactions()
205 entries[0] = replace(entries[0], company_code="002")
206 with pytest.raises(CoverageError) as error:
207 build(entries=entries)
208 assert error.value.code == "CASH_FLOW_TRANSACTION_SCOPE_MISMATCH"
209 entries = transactions()
210 entries[0] = replace(entries[0], fiscal_year=1403)
211 with pytest.raises(CoverageError):
212 build(entries=entries)
213
214
215 def test_invalid_posting_date_rejected():
216 entries = transactions()
217 entries[0] = replace(entries[0], posting_date="1404-13-01")
218 with pytest.raises(CoverageError) as error:
219 build(entries=entries)
220 assert error.value.code == "INVALID_CASH_FLOW_POSTING_DATE"
221
222
223 def test_currency_or_unit_mismatch_rejected():
224 with pytest.raises(CoverageError) as error:
225 build(currency="USD")
226 assert error.value.code == "CASH_FLOW_CURRENCY_UNIT_MISMATCH"
227 with pytest.raises(CoverageError):
228 build(unit="Thousand Rial")
229
230
231 def test_cash_account_missing_from_source_rejected():
232 with pytest.raises(CoverageError) as error:
233 build(cash=[CashAccountPolicy("MISSING", "SYNTH")])
234 assert error.value.code == "CASH_ACCOUNT_NOT_FOUND_IN_SOURCE"
235
236
237 def test_transaction_unknown_cash_or_counterpart_account_rejected():
238 entries = transactions()
239 entries[0] = replace(entries[0], cash_account_code="UNKNOWN")
240 with pytest.raises(CoverageError):
241 build(entries=entries)
242 entries = transactions()
243 entries[0] = replace(entries[0], counterpart_account_code="UNKNOWN")
244 with pytest.raises(CoverageError):
245 build(entries=entries)
246
247
248 def test_negative_or_two_sided_cash_posting_rejected():
249 entries = transactions()
250 entries[0] = replace(entries[0], cash_debit="-40")
251 with pytest.raises(CoverageError):
252 build(entries=entries)
253 entries = transactions()
254 entries[0] = replace(entries[0], cash_credit="1")
255 with pytest.raises(CoverageError):
256 build(entries=entries)
257
258
259 def test_duplicate_cash_or_counterpart_policies_rejected():
260 with pytest.raises(CoverageError):
261 build(cash=cash_accounts() + [cash_accounts()[0]])
262 with pytest.raises(CoverageError):
263 build(policies=counterpart_policies() + [counterpart_policies()[0]])
264
265
266 def test_invalid_activity_or_cash_counterpart_policy_rejected():
267 bad_type = counterpart_policies()
268 bad_type[0] = replace(bad_type[0], activity=[])
269 with pytest.raises(CoverageError):
270 build(policies=bad_type)
271 bad = counterpart_policies()
272 bad[0] = replace(bad[0], activity="CASH_TRANSFER")
273 with pytest.raises(CoverageError):
274 build(policies=bad)
275 bad = counterpart_policies() + [
276 CashCounterpartPolicy("CASH1", "OPERATING", "SYNTH-BAD")
277 ]
278 with pytest.raises(CoverageError):
279 build(policies=bad)
280
281
282 def test_all_zero_cash_balances_and_no_transactions_are_valid():
283 def zero(book):
284 for row in book["Accounts"].iter_rows(min_row=2):
285 for col in (16, 17, 18, 19):
286 row[col].value = "0"
287 data = edit(workbook(), zero)
288 result = build(data=data, entries=[], policies=[])
289 assert result.eligible_for_analyst_review
290 assert result.opening_cash_and_equivalents == "0"
291 assert result.net_change_in_cash == "0"
292 assert result.closing_cash_and_equivalents == "0"
293
294
295 def test_source_cash_rollforward_invalid_rejected():
296 data = edit(workbook(), lambda book: setattr(book["Accounts"]["T2"], "value", "101"))
297 with pytest.raises(CoverageError):
298 build(data=data)
299
300
301 def test_wrong_company_year_and_currency_source_scope_rejected():
302 with pytest.raises(CoverageError):
303 build(company="999")
304 with pytest.raises(CoverageError):
305 build(year=1403)
306 data = edit(workbook(), lambda book: setattr(book["Company"]["D2"], "value", "USD"))
307 with pytest.raises(CoverageError):
308 build(data=data)
309
310
311 def test_policy_and_transaction_hashes_are_stable_and_source_unchanged():
312 data = workbook()
313 original = bytes(data)
314 first = build(data=data)
315 second = build(data=data, entries=list(reversed(transactions())))
316 assert data == original
317 assert first.transaction_ledger_sha256 == second.transaction_ledger_sha256
318 assert first.classification_policies_sha256 == second.classification_policies_sha256
319 assert len(first.source_sha256) == 64
320 assert not first.releasable
321
322
323 def test_classification_policy_change_changes_policy_hash_without_rounding_cash():
324 first = build()
325 changed = counterpart_policies()
326 changed[0] = replace(changed[0], activity="INVESTING")
327 second = build(policies=changed)
328 assert first.classification_policies_sha256 != second.classification_policies_sha256
329 assert first.net_change_in_cash == second.net_change_in_cash
330 assert second.eligible_for_analyst_review
331
332
333 def test_input_size_and_transaction_count_are_bounded():
334 with pytest.raises(CoverageError):
335 build(data=b"x" * (20 * 1024 * 1024 + 1))
336 with pytest.raises(CoverageError):
337 build(entries=transactions() * 5001)