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

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

Synced Dec 3, 22:00 UTC

codex/aram-five-statement-integration-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 290 lines 11 KB
Raw
1 """Synthetic-only tests for unsigned income statement presentation."""
2 from __future__ import annotations
3
4 from dataclasses import replace
5 from io import BytesIO
6 from decimal import Decimal, localcontext
7 import json
8
9 import pytest
10 from openpyxl import Workbook
11
12 from aram_backend.coverage_preview import CoverageError
13 from aram_backend.income_statement_candidate import (
14 IncomeStatementLinePolicy,
15 build_income_statement_candidate,
16 )
17 from aram_backend.pilot_workbook import SHEET_HEADERS
18 from aram_backend.reconciliation_review import NoteReference
19
20
21 ITEMS = (
22 ("REV", "200", "Revenue", "R01", "REVENUE", -1),
23 ("COS", "110", "Cost", "C01", "COST_OF_REVENUE", 1),
24 ("OPE", "40", "Operating", "O01", "OPERATING_EXPENSE", 1),
25 ("OIN", "10", "Other income", "I01", "OTHER_INCOME", -1),
26 ("OEX", "5", "Other expense", "X01", "OTHER_EXPENSE", 1),
27 ("TAX", "10", "Taxes", "T01", "TAX_EXPENSE", 1),
28 )
29
30
31 def workbook(*, costs="110", missing_mapping=False, include_bs=False,
32 revenue="200") -> bytes:
33 book = Workbook()
34 book.remove(book.active)
35 for name, columns in SHEET_HEADERS.items():
36 book.create_sheet(name).append(list(columns))
37
38 def add(name, **values):
39 book[name].append([values.get(key) for key in SHEET_HEADERS[name]])
40
41 add("Company", company_code="001", company_name="Synthetic test only",
42 fiscal_year=1404, status="Draft")
43 items = tuple(
44 (code, revenue if code == "REV" else costs if code == "COS" else amount,
45 section, note, category, sign)
46 for code, amount, section, note, category, sign in ITEMS
47 )
48 if include_bs:
49 items += (("AST", "0", "Assets", "A01", "BS", 1),)
50 for index, (code, amount, section, note, category, sign) in enumerate(items, 2):
51 closing = str(-Decimal(amount)) if sign == -1 else amount
52 add("Accounts", company_code="001", fiscal_year=1404,
53 general_code=code, general_name=section, account_full_code=code,
54 account_name="Synthetic " + section, opening=closing, debit="0",
55 credit="0", closing=closing, closing_delta="0",
56 source_row=index, row_status="Draft")
57 statement = "BS" if category == "BS" else "IS"
58 add("Notes", company_code="001", fiscal_year=1404,
59 statement=statement, statement_section=section,
60 note_number=note, note_title=section)
61 if missing_mapping and code == "TAX":
62 continue
63 add("Mapping", mapping_id="M-" + code, company_code="001",
64 fiscal_year=1404, account_full_code=code, account_level="General",
65 general_code=code, target_statement=statement,
66 statement_section=section, note_number=note, mapping_version=1,
67 approval_status="Approved", reviewer="Synthetic reviewer",
68 review_date="2026-10-31", validation_status="Valid", source="Synthetic")
69 stream = BytesIO()
70 book.save(stream)
71 book.close()
72 return stream.getvalue()
73
74
75 def references(*, costs="110", include_bs=False, revenue="200"):
76 result = []
77 for code, amount, section, note, category, sign in ITEMS:
78 replacement = revenue if code == "REV" else costs if code == "COS" else amount
79 closing = str(-Decimal(replacement)) if sign == -1 else replacement
80 result.append(NoteReference("IS", section, note, "", closing, "SYNTH-" + code))
81 if include_bs:
82 result.append(NoteReference("BS", "Assets", "A01", "", "0", "SYNTH-BS"))
83 return result
84
85
86 def policies():
87 return [
88 IncomeStatementLinePolicy(section, note, "", category, sign,
89 "Synthetic " + section, "SYNTH-POL-" + code)
90 for code, _, section, note, category, sign in ITEMS
91 ]
92
93
94 def build(*, data=None, refs=None, pol=None):
95 return build_income_statement_candidate(
96 workbook_bytes=workbook() if data is None else data,
97 company_code="001", fiscal_year=1404,
98 references=references() if refs is None else refs,
99 policies=policies() if pol is None else pol,
100 )
101
102
103 def test_complete_profit_bridge_never_claims_approval():
104 result = build()
105 assert result.eligible_for_analyst_review
106 assert not result.findings
107 assert result.revenue_total == "200"
108 assert result.cost_of_revenue_total == "110"
109 assert result.gross_profit == "90"
110 assert result.operating_expense_total == "40"
111 assert result.operating_profit == "50"
112 assert result.other_income_total == "10"
113 assert result.other_expense_total == "5"
114 assert result.profit_before_tax == "55"
115 assert result.tax_expense_total == "10"
116 assert result.net_result == "45"
117 assert len(result.lines) == 6
118 assert result.kind == "UNAPPROVED_INCOME_STATEMENT_PRESENTATION_CANDIDATE"
119 assert not result.official_financial_statement
120 assert not result.accounting_policy_authorized
121 assert not result.independently_reviewed
122 assert not result.export_authorized
123 assert not result.releasable
124 assert all(line.policy_evidence_ref for line in result.lines)
125
126
127 def test_real_loss_is_not_false_positive_if_expense_exceeds_income():
128 result = build(data=workbook(costs="290"), refs=references(costs="290"))
129 assert result.eligible_for_analyst_review
130 assert result.net_result == "-135"
131 assert result.gross_profit == "-90"
132 assert not result.findings
133
134
135 def test_zero_cost_of_revenue_is_valid_with_explicit_reference():
136 # Source and reference keep the same input amount; no unseen tax estimate.
137 result = build(data=workbook(costs="0"), refs=references(costs="0"))
138 assert result.cost_of_revenue_total == "0"
139 assert result.net_result == "155"
140 assert result.eligible_for_analyst_review
141
142
143 def test_other_statement_reconciled_but_excluded_from_income_rows():
144 result = build(data=workbook(include_bs=True), refs=references(include_bs=True))
145 assert result.eligible_for_analyst_review
146 assert len(result.lines) == 6
147 assert all(l.note_number != "A01" for l in result.lines)
148
149
150 def test_other_statement_missing_reference_blocks_income_preview():
151 result = build(data=workbook(include_bs=True))
152 assert not result.eligible_for_analyst_review
153 assert "UPSTREAM_RECONCILIATION_BLOCKED" in result.findings
154
155
156 def test_unmapped_tax_account_blocks_even_with_other_matching_notes():
157 result = build(data=workbook(missing_mapping=True),
158 refs=references()[:-1])
159 assert not result.eligible_for_analyst_review
160 assert "UPSTREAM_RECONCILIATION_BLOCKED" in result.findings
161
162
163 def test_reference_mismatch_blocks_even_when_profit_bridge_can_be_computed():
164 refs = references()
165 refs[0] = replace(refs[0], expected_closing_exposure="-201")
166 result = build(refs=refs)
167 assert not result.eligible_for_analyst_review
168 assert "UPSTREAM_RECONCILIATION_BLOCKED" in result.findings
169
170
171 def test_missing_income_policy_blocks():
172 result = build(pol=policies()[:-1])
173 assert not result.eligible_for_analyst_review
174 assert "MISSING_INCOME_POLICY" in result.findings
175
176
177 def test_orphan_income_policy_blocks():
178 p = policies()
179 p.append(IncomeStatementLinePolicy("Extra", "Z1", "", "REVENUE", 1,
180 "Synthetic extra", "SYNTH-POL"))
181 result = build(pol=p)
182 assert "ORPHAN_INCOME_POLICY" in result.findings
183 assert not result.eligible_for_analyst_review
184
185
186 def test_negative_presentation_value_blocked_without_sign_guessing():
187 p = policies()
188 p[0] = replace(p[0], sign_multiplier=1)
189 result = build(pol=p)
190 assert "NEGATIVE_INCOME_PRESENTATION_AMOUNT" in result.findings
191 assert not result.eligible_for_analyst_review
192 assert result.revenue_total == "-200"
193
194
195 def test_policy_order_does_not_change_hash_or_rows():
196 p = policies()
197 first = build(pol=p)
198 second = build(pol=list(reversed(p)))
199 assert first.lines == second.lines
200 assert first.presentation_policies_sha256 == second.presentation_policies_sha256
201
202
203 def test_policy_change_changes_evidence_hash():
204 p = policies()
205 first = build(pol=p)
206 p[0] = replace(p[0], line_label="Synthetic revenues revised")
207 second = build(pol=p)
208 assert first.presentation_policies_sha256 != second.presentation_policies_sha256
209
210
211 @pytest.mark.parametrize("invalid", [
212 "invalid", [None], [object()], [policies()[0]] * 10001,
213 ])
214 def test_policy_collection_invalid(invalid):
215 with pytest.raises(CoverageError):
216 build(pol=invalid)
217
218
219 @pytest.mark.parametrize("invalid", [
220 IncomeStatementLinePolicy("Revenue", "R01", "", "REVENUE", True, "Revenue", "P"),
221 IncomeStatementLinePolicy("Revenue", "R01", "", "REVENUE", 0, "Revenue", "P"),
222 IncomeStatementLinePolicy("Revenue", "R01", "", "OTHER", 1, "Revenue", "P"),
223 IncomeStatementLinePolicy("Revenue", "", "", "REVENUE", -1, "Revenue", "P"),
224 IncomeStatementLinePolicy("Revenue", "R01", "", "REVENUE", -1, "", "P"),
225 IncomeStatementLinePolicy("Revenue", "R01", "", "REVENUE", -1, "Revenue", ""),
226 IncomeStatementLinePolicy("Revenue", "R01", "", "REVENUE", -1, "Bad" + chr(10) + "Label", "P"),
227 ])
228 def test_invalid_policy_blocked(invalid):
229 with pytest.raises(CoverageError):
230 build(pol=[invalid])
231
232
233 def test_duplicate_policy_is_never_deduplicated():
234 p = policies()
235 with pytest.raises(CoverageError) as error:
236 build(pol=p + [p[0]])
237 assert error.value.code == "DUPLICATE_INCOME_POLICY"
238
239
240 def test_missing_income_notes_is_nonreleasable():
241 from test_balance_sheet_candidate import workbook as bs_workbook
242 from test_balance_sheet_candidate import references as bs_references
243 result = build(data=bs_workbook(), refs=bs_references(), pol=[])
244 assert not result.eligible_for_analyst_review
245 assert "NO_INCOME_STATEMENT_NOTES" in result.findings
246 assert result.net_result == "0"
247
248
249 def test_source_bytes_preserved_and_results_json_safe():
250 content = workbook()
251 snapshot = bytes(content)
252 result = json.loads(json.dumps(build(data=content).to_dict()))
253 assert content == snapshot
254 assert result["releasable"] is False
255 assert isinstance(result["net_result"], str)
256 assert len(result["source_sha256"]) == 64
257 assert len(result["note_references_sha256"]) == 64
258 assert len(result["presentation_policies_sha256"]) == 64
259
260
261 def test_company_and_year_strict_scoping():
262 with pytest.raises(CoverageError):
263 build_income_statement_candidate(
264 workbook(), company_code="999", fiscal_year=1404,
265 references=references(), policies=policies(),
266 )
267 with pytest.raises(CoverageError):
268 build_income_statement_candidate(
269 workbook(), company_code="001", fiscal_year=1403,
270 references=references(), policies=policies(),
271 )
272
273
274 def test_invalid_xlsx_fails_closed():
275 with pytest.raises(CoverageError):
276 build(data=b"not XLSX")
277
278
279 def test_tiny_exact_decimal_is_not_rounded_away():
280 small = "0." + "0" * 30 + "1"
281 result = build(
282 data=workbook(costs=small),
283 refs=references(costs=small),
284 )
285 assert result.eligible_for_analyst_review
286 assert Decimal(result.cost_of_revenue_total) == Decimal(small)
287 with localcontext() as context:
288 context.prec = 400
289 expected = str(Decimal("155") - Decimal(small))
290 assert result.net_result == expected