codex/windows-native-pilot-shell-20261030
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 | """Synthetic read-only Mapping decision previews; NOT an approval workflow.""" |
| 2 | from dataclasses import replace |
| 3 | |
| 4 | import pytest |
| 5 | from sqlalchemy import select, update |
| 6 | from sqlalchemy.orm import Session |
| 7 | |
| 8 | from aram_backend.mapping_change_preview import ( |
| 9 | MappingChangePreview, preview_mapping_change, verify_mapping_change_preview_current, |
| 10 | ) |
| 11 | from aram_backend.mapping_service import MappingValidationError, save_mapping_rule |
| 12 | from aram_backend.models import ( |
| 13 | AccountVersion, Company, FiscalYear, FiscalYearStatus, MappingRule, NoteDefinition, |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | def setup_year(session, company_code="PREVIEW-A", year_number=1404): |
| 18 | company = Company(code=company_code, name="Synthetic accounting") |
| 19 | year = FiscalYear(company=company, year=year_number) |
| 20 | session.add(year) |
| 21 | session.flush() |
| 22 | old_note = NoteDefinition(year_id=year.id, note_number="1", title="Cash", version=1) |
| 23 | other_note = NoteDefinition(year_id=year.id, note_number="2", title="Receivables", version=1) |
| 24 | account = AccountVersion(year_id=year.id, account_code="001", account_name="Cash", version=1) |
| 25 | session.add_all([old_note, other_note, account]) |
| 26 | session.commit() |
| 27 | return year, old_note, other_note |
| 28 | |
| 29 | |
| 30 | def preview(session, year, note, **kw): |
| 31 | data = dict( |
| 32 | year_id=year.id, rule_key="cash", account_code="001", |
| 33 | target_note_id=note.id, mapping_type="direct", expected_base_version=0, |
| 34 | ) |
| 35 | data.update(kw) |
| 36 | return preview_mapping_change(session, **data) |
| 37 | |
| 38 | |
| 39 | def test_new_rule_preview_shows_version_one_without_persisting(session): |
| 40 | year, note, _ = setup_year(session) |
| 41 | before = session.scalars(select(MappingRule)).all() |
| 42 | result = preview(session, year, note) |
| 43 | |
| 44 | assert isinstance(result, MappingChangePreview) |
| 45 | assert result.before is None |
| 46 | assert result.expected_base_version == 0 |
| 47 | assert result.proposed.version == 1 |
| 48 | assert result.proposed.target_note_id == note.id |
| 49 | assert result.company_id == year.company_id |
| 50 | assert result.fiscal_year_number == year.year |
| 51 | assert len(result.fingerprint) == 64 |
| 52 | assert session.scalars(select(MappingRule)).all() == before == [] |
| 53 | verify_mapping_change_preview_current(session, result) |
| 54 | |
| 55 | |
| 56 | def test_change_preview_shows_explicit_old_new_and_is_deterministic(session): |
| 57 | year, old_note, new_note = setup_year(session, "PREVIEW-B") |
| 58 | saved = save_mapping_rule( |
| 59 | session, year_id=year.id, rule_key="cash", account_code="001", |
| 60 | target_note_id=old_note.id, mapping_type="direct", version=1, |
| 61 | ) |
| 62 | session.commit() |
| 63 | |
| 64 | result = preview( |
| 65 | session, year, new_note, mapping_type="manual", expected_base_version=1, |
| 66 | ) |
| 67 | again = preview( |
| 68 | session, year, new_note, mapping_type="manual", expected_base_version=1, |
| 69 | ) |
| 70 | assert result == again |
| 71 | assert result.before is not None |
| 72 | assert result.before.id == saved.id |
| 73 | assert result.before.version == 1 |
| 74 | assert result.before.target_note_id == old_note.id |
| 75 | assert result.proposed.version == 2 |
| 76 | assert result.proposed.target_note_id == new_note.id |
| 77 | assert result.proposed.mapping_type == "manual" |
| 78 | assert session.scalars(select(MappingRule)).all() == [saved] |
| 79 | verify_mapping_change_preview_current(session, result) |
| 80 | |
| 81 | |
| 82 | def test_reject_noop_reconfirmation_instead_of_faking_review(session): |
| 83 | year, note, _ = setup_year(session, "PREVIEW-C") |
| 84 | save_mapping_rule( |
| 85 | session, year_id=year.id, rule_key="cash", account_code="001", |
| 86 | target_note_id=note.id, mapping_type="direct", |
| 87 | ) |
| 88 | session.commit() |
| 89 | with pytest.raises(MappingValidationError, match="no mapping change"): |
| 90 | preview(session, year, note, expected_base_version=1) |
| 91 | |
| 92 | |
| 93 | def test_stale_precondition_rejected_even_if_target_is_valid(session): |
| 94 | year, note, other_note = setup_year(session, "PREVIEW-D") |
| 95 | save_mapping_rule( |
| 96 | session, year_id=year.id, rule_key="cash", account_code="001", |
| 97 | target_note_id=note.id, mapping_type="direct", |
| 98 | ) |
| 99 | session.commit() |
| 100 | with pytest.raises(MappingValidationError, match="stale base"): |
| 101 | preview(session, year, other_note, expected_base_version=0) |
| 102 | with pytest.raises(MappingValidationError, match="stale base"): |
| 103 | preview(session, year, other_note, expected_base_version=2) |
| 104 | |
| 105 | |
| 106 | def test_verify_rejects_later_mapping_version_without_mutation(session): |
| 107 | year, note, other = setup_year(session, "PREVIEW-E") |
| 108 | current = preview(session, year, note) |
| 109 | save_mapping_rule( |
| 110 | session, year_id=year.id, rule_key="cash", account_code="001", |
| 111 | target_note_id=other.id, mapping_type="manual", |
| 112 | ) |
| 113 | session.commit() |
| 114 | with pytest.raises(MappingValidationError, match="stale"): |
| 115 | verify_mapping_change_preview_current(session, current) |
| 116 | assert session.scalar(select(MappingRule)).target_note_id == other.id |
| 117 | |
| 118 | |
| 119 | def test_verify_rejects_later_note_version(session): |
| 120 | year, note, _ = setup_year(session, "PREVIEW-F") |
| 121 | current = preview(session, year, note) |
| 122 | session.add(NoteDefinition( |
| 123 | year_id=year.id, note_number="1", title="Revised", version=2, |
| 124 | )) |
| 125 | session.commit() |
| 126 | with pytest.raises(MappingValidationError, match="stale"): |
| 127 | verify_mapping_change_preview_current(session, current) |
| 128 | assert session.scalars(select(MappingRule)).all() == [] |
| 129 | |
| 130 | |
| 131 | def test_verify_rejects_updated_account_version_even_when_code_is_unchanged(session): |
| 132 | year, note, _ = setup_year(session, "PREVIEW-G") |
| 133 | current = preview(session, year, note) |
| 134 | session.add(AccountVersion( |
| 135 | year_id=year.id, account_code="001", account_name="Updated", version=2, |
| 136 | )) |
| 137 | session.commit() |
| 138 | with pytest.raises(MappingValidationError, match="stale"): |
| 139 | verify_mapping_change_preview_current(session, current) |
| 140 | assert session.scalars(select(MappingRule)).all() == [] |
| 141 | |
| 142 | |
| 143 | @pytest.mark.parametrize("changed", [ |
| 144 | ("mapping_type", "manual"), |
| 145 | ("fingerprint", "0" * 64), |
| 146 | ("company_id", 1000000), |
| 147 | ("expected_base_version", 1), |
| 148 | ]) |
| 149 | def test_verify_rejects_forged_or_modified_preview(session, changed): |
| 150 | year, note, _ = setup_year(session, "PREVIEW-FORGE") |
| 151 | original = preview(session, year, note) |
| 152 | key, value = changed |
| 153 | if key == "mapping_type": |
| 154 | altered = replace(original, proposed=replace(original.proposed, mapping_type=value)) |
| 155 | else: |
| 156 | altered = replace(original, **{key: value}) |
| 157 | with pytest.raises(MappingValidationError, match="stale|altered"): |
| 158 | verify_mapping_change_preview_current(session, altered) |
| 159 | |
| 160 | |
| 161 | def test_fiscal_year_protection_and_foreign_year_note(session): |
| 162 | year, note, _ = setup_year(session, "PREVIEW-H") |
| 163 | other_year, foreign_note, _ = setup_year(session, "PREVIEW-I") |
| 164 | with pytest.raises(MappingValidationError, match="same fiscal year"): |
| 165 | preview(session, year, foreign_note) |
| 166 | year.status = FiscalYearStatus.APPROVED |
| 167 | session.commit() |
| 168 | with pytest.raises(MappingValidationError, match="Draft"): |
| 169 | preview(session, year, note) |
| 170 | |
| 171 | |
| 172 | @pytest.mark.parametrize("override", [ |
| 173 | {"year_id": 0}, {"year_id": True}, {"rule_key": ""}, {"rule_key": "x "}, |
| 174 | {"account_code": 10}, {"account_code": "missing"}, |
| 175 | {"target_note_id": True}, {"target_note_id": -1}, |
| 176 | {"mapping_type": ""}, {"mapping_type": "x" * 33}, |
| 177 | {"expected_base_version": -1}, {"expected_base_version": False}, |
| 178 | ]) |
| 179 | def test_preview_rejects_invalid_input(session, override): |
| 180 | year, note, _ = setup_year(session, "PREVIEW-INPUT") |
| 181 | with pytest.raises(MappingValidationError): |
| 182 | preview(session, year, note, **override) |
| 183 | |
| 184 | |
| 185 | def test_pending_related_edits_fail_closed_without_autoflush(session): |
| 186 | year, note, _ = setup_year(session, "PREVIEW-PENDING") |
| 187 | pending = AccountVersion( |
| 188 | year_id=year.id, account_code="PENDING", account_name="Uncommitted", version=1, |
| 189 | ) |
| 190 | session.add(pending) |
| 191 | with pytest.raises(MappingValidationError, match="clean Session"): |
| 192 | preview(session, year, note) |
| 193 | assert pending in session.new |
| 194 | assert pending.id is None |
| 195 | |
| 196 | |
| 197 | def test_unrelated_pending_company_is_not_flushed(session): |
| 198 | year, note, _ = setup_year(session, "PREVIEW-UNRELATED") |
| 199 | unrelated = Company(code="UNCOMMITTED", name="Uncommitted company") |
| 200 | session.add(unrelated) |
| 201 | result = preview(session, year, note) |
| 202 | assert result.proposed.version == 1 |
| 203 | assert unrelated in session.new |
| 204 | assert unrelated.id is None |
| 205 | verify_mapping_change_preview_current(session, result) |
| 206 | |
| 207 | |
| 208 | def test_preview_never_commits_even_when_explicitly_verified(session): |
| 209 | year, note, _ = setup_year(session, "PREVIEW-READONLY") |
| 210 | original = preview(session, year, note) |
| 211 | verify_mapping_change_preview_current(session, original) |
| 212 | session.rollback() |
| 213 | assert session.scalars(select(MappingRule)).all() == [] |
| 214 | |
| 215 | |
| 216 | def test_parallel_update_of_same_rule_invalidates_cached_review_snapshot(session): |
| 217 | year, note, other = setup_year(session, "PREVIEW-PARALLEL-RULE") |
| 218 | saved = save_mapping_rule( |
| 219 | session, year_id=year.id, rule_key="cash", account_code="001", |
| 220 | target_note_id=note.id, mapping_type="direct", |
| 221 | ) |
| 222 | session.commit() |
| 223 | observed = preview(session, year, other, mapping_type="manual", expected_base_version=1) |
| 224 | assert observed.before is not None and observed.before.id == saved.id |
| 225 | |
| 226 | # A separate database writer updates a persisted rule without changing its |
| 227 | # version. An identity-map-cached ORM object must not mask that modification. |
| 228 | with Session(session.get_bind(), expire_on_commit=False) as other_session: |
| 229 | other_session.execute( |
| 230 | update(MappingRule).where(MappingRule.id == saved.id).values(mapping_type="external") |
| 231 | ) |
| 232 | other_session.commit() |
| 233 | |
| 234 | with pytest.raises(MappingValidationError, match="stale"): |
| 235 | verify_mapping_change_preview_current(session, observed) |
| 236 | |
| 237 | |
| 238 | def test_parallel_year_status_transition_invalidates_cached_review_snapshot(session): |
| 239 | year, note, _ = setup_year(session, "PREVIEW-PARALLEL-YEAR") |
| 240 | observed = preview(session, year, note) |
| 241 | |
| 242 | with Session(session.get_bind(), expire_on_commit=False) as other_session: |
| 243 | other_session.execute( |
| 244 | update(FiscalYear).where(FiscalYear.id == year.id) |
| 245 | .values(status=FiscalYearStatus.APPROVED) |
| 246 | ) |
| 247 | other_session.commit() |
| 248 | |
| 249 | with pytest.raises(MappingValidationError, match="stale"): |
| 250 | verify_mapping_change_preview_current(session, observed) |