codex/aram-multistatement-mapping-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
| 1 | """Read-only Mapping coverage preview, using synthetic Pilot XLSX inputs only.""" |
| 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.coverage_preview import CoverageError, preview_mapping_coverage |
| 10 | from aram_backend.pilot_workbook import SHEET_HEADERS |
| 11 | |
| 12 | |
| 13 | def sample_workbook(*, extra_mapping=False, approved=True): |
| 14 | book = Workbook() |
| 15 | book.remove(book.active) |
| 16 | sheets = {} |
| 17 | for name, headers in SHEET_HEADERS.items(): |
| 18 | sheet = book.create_sheet(name) |
| 19 | sheet.append(list(headers)) |
| 20 | sheets[name] = sheet |
| 21 | |
| 22 | def add(sheet_name, **fields): |
| 23 | headers = SHEET_HEADERS[sheet_name] |
| 24 | sheets[sheet_name].append([fields.get(header) for header in headers]) |
| 25 | |
| 26 | add("Company", company_code="001", company_name="Synthetic Company", |
| 27 | fiscal_year=1404, status="Draft") |
| 28 | for code, amount in (("0010", "110.25"), ("0020", "15.75")): |
| 29 | add("Accounts", company_code="001", fiscal_year=1404, |
| 30 | general_code=code, general_name="General", |
| 31 | account_full_code=code, account_name="Synthetic " + code, |
| 32 | opening=amount, debit="0", credit="0", closing=amount, |
| 33 | source_row=10, row_status="Draft", closing_delta="0") |
| 34 | add("Notes", company_code="001", fiscal_year=1404, statement="BS", |
| 35 | statement_section="Assets", note_number="N01", note_title="Cash") |
| 36 | options = { |
| 37 | "company_code": "001", "fiscal_year": 1404, |
| 38 | "account_full_code": "0010", "account_level": "General", |
| 39 | "general_code": "0010", "target_statement": "BS", |
| 40 | "statement_section": "Assets", "note_number": "N01", |
| 41 | "mapping_version": 1, "approval_status": "Approved" if approved else "Draft", |
| 42 | "source": "Manual", "validation_status": "Valid", |
| 43 | "reviewer": "Synthetic reviewer" if approved else None, |
| 44 | "review_date": "2026-10-31" if approved else None, |
| 45 | } |
| 46 | add("Mapping", mapping_id="M001", **options) |
| 47 | if extra_mapping: |
| 48 | add("Mapping", mapping_id="M002", **options) |
| 49 | stream = BytesIO() |
| 50 | book.save(stream) |
| 51 | return stream.getvalue() |
| 52 | |
| 53 | |
| 54 | def make_change(content, change): |
| 55 | book = __import__("openpyxl").load_workbook(BytesIO(content)) |
| 56 | change(book) |
| 57 | result = BytesIO() |
| 58 | book.save(result) |
| 59 | book.close() |
| 60 | return result.getvalue() |
| 61 | |
| 62 | |
| 63 | def preview(content): |
| 64 | return preview_mapping_coverage(content, company_code="001", fiscal_year=1404) |
| 65 | |
| 66 | |
| 67 | def test_includes_only_file_marked_approved_unique_mapping(): |
| 68 | result = preview(sample_workbook()) |
| 69 | assert result.account_count == 2 |
| 70 | assert result.candidate_mapped_accounts == 1 |
| 71 | assert result.blocked_accounts == 1 |
| 72 | assert result.source_closing_exposure == "126.00" |
| 73 | assert result.candidate_closing_exposure == "110.25" |
| 74 | assert result.blocked_closing_exposure == "15.75" |
| 75 | assert len(result.notes) == 1 |
| 76 | assert result.notes[0].note_number == "N01" |
| 77 | assert result.notes[0].account_codes == ("0010",) |
| 78 | assert [(issue.code, issue.account_code) for issue in result.exceptions] == [ |
| 79 | ("UNMAPPED_ACCOUNT", "0020") |
| 80 | ] |
| 81 | assert not result.financial_statement |
| 82 | assert not result.trusted_approvals |
| 83 | assert not result.releasable |
| 84 | |
| 85 | |
| 86 | def test_same_account_can_map_once_to_each_distinct_statement(): |
| 87 | def add_statement_mappings(book): |
| 88 | notes = book["Notes"] |
| 89 | notes.append([ |
| 90 | "001", 1404, "EQ", "Equity", "E01", "Equity note", "", "", |
| 91 | ]) |
| 92 | headers = SHEET_HEADERS["Mapping"] |
| 93 | base = { |
| 94 | header: book["Mapping"].cell(2, index + 1).value |
| 95 | for index, header in enumerate(headers) |
| 96 | } |
| 97 | eq_mapping = dict(base) |
| 98 | eq_mapping.update({ |
| 99 | "mapping_id": "M-EQ-001", "target_statement": "EQ", |
| 100 | "statement_section": "Equity", "note_number": "E01", |
| 101 | }) |
| 102 | book["Mapping"].append([eq_mapping.get(header) for header in headers]) |
| 103 | second_bs = dict(base) |
| 104 | second_bs.update({ |
| 105 | "mapping_id": "M-BS-002", "account_full_code": "0020", |
| 106 | "general_code": "0020", |
| 107 | }) |
| 108 | book["Mapping"].append([second_bs.get(header) for header in headers]) |
| 109 | |
| 110 | result = preview(make_change(sample_workbook(), add_statement_mappings)) |
| 111 | assert result.account_count == 2 |
| 112 | assert result.candidate_mapped_accounts == 2 |
| 113 | assert result.blocked_accounts == 0 |
| 114 | assert result.candidate_closing_exposure == "126.00" |
| 115 | assert len(result.accounts) == 2 |
| 116 | assert len(result.mappings) == 3 |
| 117 | account = next(item for item in result.accounts if item.account_code == "0010") |
| 118 | assert account.mapping_count == 2 |
| 119 | assert account.included |
| 120 | assert account.target_statement is None |
| 121 | by_statement = { |
| 122 | note.statement: note for note in result.notes |
| 123 | } |
| 124 | assert by_statement["BS"].closing_exposure == "126.00" |
| 125 | assert by_statement["BS"].account_codes == ("0010", "0020") |
| 126 | assert by_statement["EQ"].closing_exposure == "110.25" |
| 127 | assert by_statement["EQ"].account_codes == ("0010",) |
| 128 | assert all(item.included for item in result.mappings) |
| 129 | |
| 130 | |
| 131 | def test_multiple_mapping_rows_are_blocked_not_silently_deduplicated(): |
| 132 | result = preview(sample_workbook(extra_mapping=True)) |
| 133 | assert result.candidate_mapped_accounts == 0 |
| 134 | assert result.blocked_accounts == 2 |
| 135 | assert {issue.code for issue in result.exceptions} == { |
| 136 | "AMBIGUOUS_ACCOUNT_MAPPING", "UNMAPPED_ACCOUNT" |
| 137 | } |
| 138 | match = next(e for e in result.exceptions if e.code == "AMBIGUOUS_ACCOUNT_MAPPING") |
| 139 | assert match.mapping_excel_rows == (2, 3) |
| 140 | |
| 141 | |
| 142 | def test_unapproved_file_label_is_not_evidence_of_authorization(): |
| 143 | result = preview(sample_workbook(approved=False)) |
| 144 | assert result.candidate_mapped_accounts == 0 |
| 145 | assert result.notes == () |
| 146 | assert any(e.code == "MAPPING_NOT_FILE_MARKED_APPROVED" for e in result.exceptions) |
| 147 | |
| 148 | |
| 149 | @pytest.mark.parametrize("kwargs", [ |
| 150 | {"company_code": "", "fiscal_year": 1404}, |
| 151 | {"company_code": "001", "fiscal_year": 0}, |
| 152 | {"company_code": "001", "fiscal_year": True}, |
| 153 | {"company_code": "001", "fiscal_year": 1403}, |
| 154 | ]) |
| 155 | def test_scope_is_explicit_and_no_fallback(kwargs): |
| 156 | with pytest.raises(CoverageError): |
| 157 | preview_mapping_coverage(sample_workbook(), **kwargs) |
| 158 | |
| 159 | |
| 160 | def test_malformed_workbook_fails_closed(): |
| 161 | with pytest.raises(CoverageError) as exc: |
| 162 | preview(b"invalid file") |
| 163 | assert exc.value.code in {"WORKBOOK_INVALID", "WORKBOOK_ARCHIVE_INVALID"} |
| 164 | |
| 165 | |
| 166 | def test_wrong_headers_fail_closed(): |
| 167 | content = make_change(sample_workbook(), lambda book: setattr(book["Accounts"]["A1"], "value", "other")) |
| 168 | with pytest.raises(CoverageError) as exc: |
| 169 | preview(content) |
| 170 | assert exc.value.code == "WORKBOOK_INVALID" |
| 171 | |
| 172 | |
| 173 | @pytest.mark.parametrize("alter", [ |
| 174 | lambda book: setattr(book["Mapping"].row_dimensions[2], "hidden", True), |
| 175 | lambda book: setattr(book["Accounts"].column_dimensions["Q"], "hidden", True), |
| 176 | lambda book: book["Mapping"].merge_cells("C2:D2"), |
| 177 | lambda book: setattr(book["Notes"], "sheet_state", "hidden"), |
| 178 | ]) |
| 179 | def test_hidden_or_merged_inputs_fail_closed(alter): |
| 180 | content = make_change(sample_workbook(), alter) |
| 181 | with pytest.raises(CoverageError) as exc: |
| 182 | preview(content) |
| 183 | # The existing Pilot preflight may reject a merge before the stronger |
| 184 | # non-streaming visibility check. Both paths fail closed. |
| 185 | assert exc.value.code in {"HIDDEN_INPUT", "MERGED_INPUT", "WORKBOOK_INVALID"} |
| 186 | |
| 187 | |
| 188 | def test_original_bytes_never_mutated(): |
| 189 | source = sample_workbook() |
| 190 | before = bytes(source) |
| 191 | result = preview(source) |
| 192 | assert source == before |
| 193 | assert len(result.source_sha256) == 64 |
| 194 | |
| 195 | |
| 196 | def test_result_serializable_without_float_amounts(): |
| 197 | import json |
| 198 | result = preview(sample_workbook()) |
| 199 | encoded = json.dumps(result.to_dict(), sort_keys=True) |
| 200 | assert '"110.25"' in encoded |
| 201 | assert '"releasable": false' in encoded |
| 202 | |
| 203 | |
| 204 | def test_rejects_nonbytes_and_large_inputs(): |
| 205 | with pytest.raises(CoverageError): |
| 206 | preview_mapping_coverage("not-bytes", company_code="001", fiscal_year=1404) |
| 207 | with pytest.raises(CoverageError): |
| 208 | preview(b"x" * (20 * 1024 * 1024 + 1)) |
| 209 | |
| 210 | |
| 211 | def test_empty_scoped_accounts_fail_closed(): |
| 212 | content = make_change(sample_workbook(), lambda book: ( |
| 213 | book["Accounts"].delete_rows(2, 2), |
| 214 | book["Mapping"].delete_rows(2, 1), |
| 215 | )) |
| 216 | with pytest.raises(CoverageError) as exc: |
| 217 | preview(content) |
| 218 | assert exc.value.code == "NO_ACCOUNTS_IN_SCOPE" |
| 219 | |
| 220 | |
| 221 | def test_hidden_auxiliary_sheet_is_rejected_even_if_financial_tables_are_visible(): |
| 222 | content = make_change( |
| 223 | sample_workbook(), |
| 224 | lambda book: setattr(book["Checks"], "sheet_state", "hidden"), |
| 225 | ) |
| 226 | with pytest.raises(CoverageError) as exc: |
| 227 | preview(content) |
| 228 | assert exc.value.code == "HIDDEN_INPUT" |
| 229 | |
| 230 | |
| 231 | def test_hidden_unknown_extra_sheet_is_not_silently_ignored(): |
| 232 | def add_hidden_sheet(book): |
| 233 | book.create_sheet("UntrustedExtra").sheet_state = "hidden" |
| 234 | |
| 235 | content = make_change(sample_workbook(), add_hidden_sheet) |
| 236 | with pytest.raises(CoverageError) as exc: |
| 237 | preview(content) |
| 238 | assert exc.value.code == "HIDDEN_INPUT" |
| 239 | |
| 240 | |
| 241 | def test_all_zero_accounts_do_not_crash_decimal_aggregation(): |
| 242 | def zero_accounts(book): |
| 243 | for row in book["Accounts"].iter_rows(min_row=2): |
| 244 | row[16].value = "0" # opening |
| 245 | row[19].value = "0" # closing |
| 246 | |
| 247 | result = preview(make_change(sample_workbook(), zero_accounts)) |
| 248 | assert result.source_closing_exposure == "0" |
| 249 | assert result.candidate_closing_exposure == "0" |
| 250 | assert result.blocked_closing_exposure == "0" |
| 251 | assert result.blocked_accounts == 1 |
| 252 | assert not result.financial_statement |