@davoodmehraban89 / Aram-1 / commits / 0ba4952
Fork 0

test(review): cover exact note reconciliation, missing mappings, hostile inputs and provenance

davoodmehraban89 committed Oct 31, 2026 at 00:24 UTC 0ba49524974c12687ac5818ceca608659be21fb3
1 file changed +217
backend/tests/test_reconciliation_review.py new
+217
@@ -0,0 +1,217 @@
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