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

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

Synced Dec 4, 15:30 UTC

codex/aram-income-statement-candidate-20261031
add-index-html-10623938925177053894 arena/094f314c-aram arena/2d4eda07-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 dependabot/github_actions/actions/checkout-7 dependabot/github_actions/actions/setup-node-6 dependabot/github_actions/actions/setup-python-7 dependabot/github_actions/actions/upload-artifact-7 dependabot/npm_and_yarn/frontend/eslint-plugin-react-hooks-7.1.1 dependabot/npm_and_yarn/frontend/globals-17.15.0 dependabot/npm_and_yarn/frontend/vite-8.4.0 dependabot/npm_and_yarn/frontend/vitest-5.1.2 dependabot/npm_and_yarn/frontend/vitest/coverage-v8-5.1.2 dependabot/pip/backend/mypy-gte-1.13-and-lt-3 dependabot/pip/backend/pytest-cov-gte-6.0-and-lt-8 dependabot/pip/backend/setuptools-gte-75-and-lt-85 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 233 lines 9.07 KB
Raw
1 """Synthetic tests for independent reference reconciliation; no real finance data."""
2 from __future__ import annotations
3
4 from io import BytesIO
5 import json
6
7 import pytest
8 from openpyxl import Workbook
9
10 from aram_backend.coverage_preview import CoverageError
11 from aram_backend.pilot_workbook import SHEET_HEADERS
12 from aram_backend.reconciliation_review import (
13 NoteReference, reconcile_workbook_for_review,
14 )
15
16
17 def workbook(*, include_second_mapping=True, second_draft=False,
18 split_notes=False, second_amount="15.75") -> bytes:
19 book = Workbook()
20 book.remove(book.active)
21 for sheet_name, columns in SHEET_HEADERS.items():
22 book.create_sheet(sheet_name).append(list(columns))
23
24 def add(name, **values):
25 book[name].append([values.get(key) for key in SHEET_HEADERS[name]])
26
27 add("Company", company_code="001", company_name="Synthetic",
28 fiscal_year=1404, status="Draft")
29 for code, amount in [("0010", "110.25"), ("0020", second_amount)]:
30 add("Accounts", company_code="001", fiscal_year=1404,
31 general_code=code, general_name="General", account_full_code=code,
32 account_name="Synthetic " + code, opening=amount,
33 debit="0", credit="0", closing=amount, source_row=7,
34 row_status="Draft", closing_delta="0")
35 add("Notes", company_code="001", fiscal_year=1404,
36 statement="BS", statement_section="Assets",
37 note_number="N01", note_title="Cash")
38 if split_notes:
39 add("Notes", company_code="001", fiscal_year=1404,
40 statement="BS", statement_section="Assets",
41 note_number="N02", note_title="Other")
42
43 for index, code in enumerate(["0010", "0020"]):
44 if index == 1 and not include_second_mapping:
45 continue
46 add("Mapping", mapping_id=f"M{index + 1}", company_code="001",
47 fiscal_year=1404, account_full_code=code, account_level="General",
48 general_code=code, target_statement="BS",
49 statement_section="Assets", note_number="N02" if split_notes
50 and index == 1 else "N01", mapping_version=1,
51 approval_status="Draft" if index == 1 and second_draft else "Approved",
52 reviewer="Synthetic reviewer" if index == 0 or not second_draft else None,
53 review_date="2026-10-31" if index == 0 or not second_draft else None,
54 validation_status="Valid", source="Synthetic")
55
56 output = BytesIO()
57 book.save(output)
58 book.close()
59 return output.getvalue()
60
61
62 def ref(amount="126.00", note="N01", statement="BS",
63 section="Assets", evidence="SYNTHETIC-TB-1") -> NoteReference:
64 return NoteReference(
65 statement=statement, section=section, note_number=note, subnote_code="",
66 expected_closing_exposure=amount, evidence_ref=evidence,
67 )
68
69
70 def evaluate(content=None, references=None, company="001", year=1404):
71 return reconcile_workbook_for_review(
72 content if content is not None else workbook(),
73 company_code=company, fiscal_year=year,
74 references=[ref()] if references is None else references,
75 )
76
77
78 def test_fully_reconciled_is_still_not_financial_approval():
79 reviewed = evaluate()
80 assert reviewed.matched_for_analyst_review
81 assert not reviewed.findings
82 assert reviewed.account_count == reviewed.candidate_mapped_accounts == 2
83 assert reviewed.blocked_accounts == 0
84 assert len(reviewed.notes) == 1
85 assert reviewed.notes[0].difference == "0.00"
86 assert reviewed.notes[0].candidate_closing_exposure == "126.00"
87 assert not reviewed.financial_statement
88 assert not reviewed.trusted_approvals
89 assert not reviewed.approved
90 assert not reviewed.releasable
91
92
93 def test_mismatch_blocks_without_tolerance_or_rounding():
94 reviewed = evaluate(references=[ref("126.01")])
95 assert not reviewed.matched_for_analyst_review
96 assert reviewed.notes[0].difference == "-0.01"
97 assert reviewed.findings[0].code == "NOTE_EXPOSURE_MISMATCH"
98
99
100 def test_missing_account_mapping_blocks_even_with_matching_reference():
101 reviewed = evaluate(content=workbook(include_second_mapping=False),
102 references=[ref("110.25")])
103 assert not reviewed.matched_for_analyst_review
104 assert reviewed.blocked_accounts == 1
105 assert any(e.code == "BLOCKED_ACCOUNT_UNMAPPED_ACCOUNT"
106 and e.account_code == "0020" for e in reviewed.findings)
107
108
109 def test_workbook_marked_draft_is_not_silently_approved():
110 reviewed = evaluate(content=workbook(second_draft=True),
111 references=[ref("110.25")])
112 assert not reviewed.matched_for_analyst_review
113 assert any("NOT_FILE_MARKED_APPROVED" in e.code for e in reviewed.findings)
114
115
116 def test_missing_reference_blocks_and_does_not_invent_a_total():
117 reviewed = evaluate(references=[])
118 assert not reviewed.matched_for_analyst_review
119 assert reviewed.notes[0].reference_closing_exposure is None
120 assert reviewed.notes[0].difference is None
121 assert reviewed.findings[0].code == "MISSING_NOTE_REFERENCE"
122
123
124 def test_extra_reference_blocks_without_creating_a_note():
125 reviewed = evaluate(references=[ref(), ref("1", note="N99")])
126 assert not reviewed.matched_for_analyst_review
127 extra = next(note for note in reviewed.notes if note.note_number == "N99")
128 assert extra.candidate_closing_exposure is None
129 assert extra.reference_closing_exposure == "1"
130 assert any(f.code == "UNKNOWN_REFERENCE_TARGET" for f in reviewed.findings)
131
132
133 def test_separate_notes_reference_order_is_irrelevant():
134 data = workbook(split_notes=True)
135 left = evaluate(content=data, references=[ref("110.25"), ref("15.75", note="N02")])
136 right = evaluate(content=data, references=[ref("15.75", note="N02"), ref("110.25")])
137 assert left.matched_for_analyst_review and right.matched_for_analyst_review
138 assert left.reference_sha256 == right.reference_sha256
139 assert left.notes == right.notes
140
141
142 def test_duplicate_reference_rejected_not_deduplicated():
143 with pytest.raises(CoverageError) as err:
144 evaluate(references=[ref(), ref("126.00", evidence="ANOTHER")])
145 assert str(err.value) == "DUPLICATE_NOTE_REFERENCE"
146
147
148 @pytest.mark.parametrize("refs", [
149 "not a list", (item for item in []), [object()],
150 [ref()] * 10_001,
151 ])
152 def test_reference_collection_fails_closed(refs):
153 with pytest.raises(CoverageError):
154 evaluate(references=refs)
155
156
157 @pytest.mark.parametrize("bad", [
158 ref(statement="OTHER"), ref(note=""), ref(note=" N01"),
159 ref(section="Assets\nInjected"), ref(evidence=""),
160 ref(amount="NaN"), ref(amount="Infinity"), ref(amount="1E+999"),
161 ref(amount="0E-99999999"), ref(amount="nonsense"),
162 ref(amount="1" * 151),
163 ])
164 def test_invalid_reference_rejected(bad):
165 with pytest.raises(CoverageError):
166 evaluate(references=[bad])
167
168
169 def test_reference_amount_must_be_text_not_float_or_bool():
170 with pytest.raises(CoverageError):
171 evaluate(references=[ref(126.0)])
172 with pytest.raises(CoverageError):
173 evaluate(references=[ref(True)])
174
175
176 def test_wrong_company_or_year_has_no_fallback():
177 with pytest.raises(CoverageError):
178 evaluate(company="002")
179 with pytest.raises(CoverageError):
180 evaluate(year=1403)
181
182
183 def test_provenance_hashes_bind_input_and_reference():
184 source = workbook()
185 original = bytes(source)
186 left = evaluate(content=source, references=[ref()])
187 right = evaluate(content=source, references=[ref("126.00", evidence="SYNTHETIC-TB-2")])
188 assert left.source_sha256 == right.source_sha256
189 assert left.reference_sha256 != right.reference_sha256
190 assert len(left.source_sha256) == len(left.reference_sha256) == 64
191 assert source == original
192
193
194 def test_signed_negative_amount_is_compared_without_faking_statement_signs():
195 reviewed = evaluate(content=workbook(second_amount="-15.75"),
196 references=[ref("94.50")])
197 assert reviewed.matched_for_analyst_review
198 assert reviewed.source_closing_exposure == "94.50"
199
200
201 def test_read_only_packet_is_json_safe_and_explicitly_provisional():
202 result = evaluate()
203 data = json.loads(json.dumps(result.to_dict(), sort_keys=True))
204 assert data["kind"] == "UNAPPROVED_NOTE_RECONCILIATION_REVIEW"
205 assert data["matched_for_analyst_review"]
206 assert data["releasable"] is False
207
208
209 def test_untrusted_invalid_excel_fails_before_comparison():
210 with pytest.raises(CoverageError):
211 evaluate(content=b"invalid xlsx")
212
213
214 def test_unmatched_cents_do_not_disappear_in_decimal_context():
215 reviewed = evaluate(references=[ref("126.000000000000001")])
216 assert reviewed.notes[0].difference == "-1E-15"
217 assert not reviewed.matched_for_analyst_review
218
219
220 def test_all_zero_note_reference_does_not_crash_exact_decimal_difference():
221 from openpyxl import load_workbook
222
223 book = load_workbook(BytesIO(workbook()))
224 for row in book["Accounts"].iter_rows(min_row=2):
225 row[16].value = "0" # opening
226 row[19].value = "0" # closing
227 stream = BytesIO()
228 book.save(stream)
229 book.close()
230 reviewed = evaluate(content=stream.getvalue(), references=[ref("0")])
231 assert reviewed.matched_for_analyst_review
232 assert reviewed.notes[0].difference == "0"
233 assert reviewed.candidate_closing_exposure == "0"