Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 3, 22:00 UTC
codex/gate3-unapproved-proposal-storage-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 DB-backed *unreviewed* proposal persistence; not approval or apply.""" |
| 2 | import pytest |
| 3 | from sqlalchemy import select, update, insert, inspect |
| 4 | from sqlalchemy.exc import IntegrityError |
| 5 | |
| 6 | from aram_backend.mapping_batch_review import ProposedMappingRow, preview_mapping_batch |
| 7 | from aram_backend.mapping_draft_store import ( |
| 8 | mapping_review_drafts, save_unreviewed_batch, |
| 9 | get_unreviewed_batch, validate_unreviewed_batch_current, |
| 10 | ) |
| 11 | from aram_backend.mapping_service import MappingValidationError, save_mapping_rule |
| 12 | from aram_backend.models import ( |
| 13 | Company, FiscalYear, FiscalYearStatus, NoteDefinition, AccountVersion, MappingRule, |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | @pytest.fixture |
| 18 | def setup(session): |
| 19 | # Deliberately NOT part of Base.metadata. Migration creates this table |
| 20 | # after initial metadata-driven migration to prevent double-create. |
| 21 | mapping_review_drafts.create(session.get_bind()) |
| 22 | company = Company(code="DRAFT-STORE", name="Synthetic") |
| 23 | year = FiscalYear(company=company, year=1404) |
| 24 | session.add(year) |
| 25 | session.flush() |
| 26 | note1 = NoteDefinition(year_id=year.id, note_number="1", title="Cash", version=1) |
| 27 | note2 = NoteDefinition(year_id=year.id, note_number="2", title="Receivables", version=1) |
| 28 | account = AccountVersion( |
| 29 | year_id=year.id, account_code="001", account_name="Cash", version=1, |
| 30 | ) |
| 31 | session.add_all([note1, note2, account]) |
| 32 | session.commit() |
| 33 | try: |
| 34 | yield session, year, note1, note2 |
| 35 | finally: |
| 36 | # This table deliberately lives outside Base.metadata. It MUST be |
| 37 | # removed before the outer session fixture drops FiscalYear tables. |
| 38 | session.rollback() |
| 39 | mapping_review_drafts.drop(session.get_bind()) |
| 40 | |
| 41 | |
| 42 | def proposal(session, year, note, key="cash"): |
| 43 | return preview_mapping_batch(session, year_id=year.id, rows=( |
| 44 | ProposedMappingRow( |
| 45 | source_locator="Mapping!2", rule_key=key, account_code="001", |
| 46 | target_note_id=note.id, mapping_type="manual", expected_base_version=0, |
| 47 | ), |
| 48 | )) |
| 49 | |
| 50 | |
| 51 | def test_save_unreviewed_draft_persists_exact_original_without_mapping_write(setup): |
| 52 | session, year, note, _ = setup |
| 53 | draft = save_unreviewed_batch( |
| 54 | session, proposal(session, year, note), request_key="request-0001", |
| 55 | ) |
| 56 | assert draft.id > 0 |
| 57 | assert draft.year_id == year.id |
| 58 | assert draft.state == "Unreviewed" |
| 59 | assert draft.checksum |
| 60 | assert session.scalars(select(MappingRule)).all() == [] |
| 61 | session.commit() |
| 62 | other = get_unreviewed_batch(session, draft_id=draft.id, year_id=year.id) |
| 63 | assert other == draft |
| 64 | assert validate_unreviewed_batch_current( |
| 65 | session, draft_id=draft.id, year_id=year.id |
| 66 | ) is None |
| 67 | assert session.scalars(select(MappingRule)).all() == [] |
| 68 | |
| 69 | |
| 70 | def test_exact_idempotent_retry_never_creates_second_draft(setup): |
| 71 | session, year, note, _ = setup |
| 72 | snapshot = proposal(session, year, note) |
| 73 | first = save_unreviewed_batch(session, snapshot, request_key="request-0002") |
| 74 | session.commit() |
| 75 | again = save_unreviewed_batch(session, snapshot, request_key="request-0002") |
| 76 | session.commit() |
| 77 | assert first == again |
| 78 | assert len(session.execute(select(mapping_review_drafts)).all()) == 1 |
| 79 | |
| 80 | |
| 81 | def test_idempotency_key_cannot_represent_different_proposal(setup): |
| 82 | session, year, cash, other = setup |
| 83 | save_unreviewed_batch(session, proposal(session, year, cash), request_key="request-0003") |
| 84 | session.commit() |
| 85 | with pytest.raises(MappingValidationError, match="idempotency|request key"): |
| 86 | save_unreviewed_batch(session, proposal(session, year, other), request_key="request-0003") |
| 87 | assert len(session.execute(select(mapping_review_drafts)).all()) == 1 |
| 88 | |
| 89 | |
| 90 | def test_rollback_removes_staged_draft_without_touching_mapping(setup): |
| 91 | session, year, note, _ = setup |
| 92 | draft = save_unreviewed_batch(session, proposal(session, year, note), request_key="request-0004") |
| 93 | assert draft.id > 0 |
| 94 | session.rollback() |
| 95 | assert session.execute(select(mapping_review_drafts)).all() == [] |
| 96 | assert session.scalars(select(MappingRule)).all() == [] |
| 97 | |
| 98 | |
| 99 | def test_original_draft_is_stale_after_newer_note_version(setup): |
| 100 | session, year, note, _ = setup |
| 101 | draft = save_unreviewed_batch(session, proposal(session, year, note), request_key="request-0005") |
| 102 | session.commit() |
| 103 | session.add(NoteDefinition(year_id=year.id, note_number="1", title="Revised", version=2)) |
| 104 | session.commit() |
| 105 | with pytest.raises(MappingValidationError, match="stale"): |
| 106 | validate_unreviewed_batch_current(session, draft_id=draft.id, year_id=year.id) |
| 107 | assert get_unreviewed_batch(session, draft_id=draft.id, year_id=year.id).state == "Unreviewed" |
| 108 | |
| 109 | |
| 110 | def test_original_draft_stale_after_rule_change(setup): |
| 111 | session, year, note, other = setup |
| 112 | draft = save_unreviewed_batch(session, proposal(session, year, note), request_key="request-0006") |
| 113 | session.commit() |
| 114 | save_mapping_rule( |
| 115 | session, year_id=year.id, rule_key="cash", account_code="001", |
| 116 | target_note_id=other.id, mapping_type="manual", version=1, |
| 117 | ) |
| 118 | session.commit() |
| 119 | with pytest.raises(MappingValidationError, match="stale"): |
| 120 | validate_unreviewed_batch_current(session, draft_id=draft.id, year_id=year.id) |
| 121 | |
| 122 | |
| 123 | def test_staged_draft_is_invalid_after_approved_year(setup): |
| 124 | session, year, note, _ = setup |
| 125 | draft = save_unreviewed_batch(session, proposal(session, year, note), request_key="request-0007") |
| 126 | session.commit() |
| 127 | year.status = FiscalYearStatus.APPROVED |
| 128 | session.commit() |
| 129 | with pytest.raises(MappingValidationError, match="stale"): |
| 130 | validate_unreviewed_batch_current(session, draft_id=draft.id, year_id=year.id) |
| 131 | |
| 132 | |
| 133 | def test_wrong_year_cannot_access_draft_even_with_known_id(setup): |
| 134 | session, year, note, _ = setup |
| 135 | draft = save_unreviewed_batch(session, proposal(session, year, note), request_key="request-0008") |
| 136 | session.commit() |
| 137 | with pytest.raises(MappingValidationError, match="not found"): |
| 138 | get_unreviewed_batch(session, draft_id=draft.id, year_id=year.id + 1) |
| 139 | with pytest.raises(MappingValidationError, match="not found"): |
| 140 | validate_unreviewed_batch_current(session, draft_id=draft.id, year_id=year.id + 1) |
| 141 | |
| 142 | |
| 143 | @pytest.mark.parametrize("bad", ["", " spaces ", "a" * 65, "\n", "contains\x00nul", False, 123]) |
| 144 | def test_invalid_request_keys_are_rejected_without_sql_inserts(setup, bad): |
| 145 | session, year, note, _ = setup |
| 146 | with pytest.raises(MappingValidationError, match="request key"): |
| 147 | save_unreviewed_batch(session, proposal(session, year, note), request_key=bad) |
| 148 | assert session.execute(select(mapping_review_drafts)).all() == [] |
| 149 | |
| 150 | |
| 151 | def test_conflicted_batch_cannot_be_stored(setup): |
| 152 | session, year, cash, other = setup |
| 153 | first = ProposedMappingRow("M!2", "cash", "001", cash.id, "manual", 0) |
| 154 | second = ProposedMappingRow("M!3", "cash", "001", other.id, "manual", 0) |
| 155 | rejected = preview_mapping_batch(session, year_id=year.id, rows=[first, second]) |
| 156 | assert not rejected.ready_for_review |
| 157 | with pytest.raises(MappingValidationError, match="not ready"): |
| 158 | save_unreviewed_batch(session, rejected, request_key="request-0009") |
| 159 | assert session.execute(select(mapping_review_drafts)).all() == [] |
| 160 | |
| 161 | |
| 162 | def test_modified_snapshot_cannot_be_stored(setup): |
| 163 | from dataclasses import replace |
| 164 | session, year, note, _ = setup |
| 165 | original = proposal(session, year, note) |
| 166 | tampered = replace(original, fingerprint="a" * 64) |
| 167 | with pytest.raises(MappingValidationError, match="stale"): |
| 168 | save_unreviewed_batch(session, tampered, request_key="request-0010") |
| 169 | assert session.execute(select(mapping_review_drafts)).all() == [] |
| 170 | |
| 171 | |
| 172 | def test_db_check_constraint_rejects_synthetic_approved_state(setup): |
| 173 | session, year, note, _ = setup |
| 174 | draft = save_unreviewed_batch(session, proposal(session, year, note), request_key="request-0011") |
| 175 | session.commit() |
| 176 | with pytest.raises(IntegrityError): |
| 177 | session.execute( |
| 178 | update(mapping_review_drafts) |
| 179 | .where(mapping_review_drafts.c.id == draft.id) |
| 180 | .values(state="Approved") |
| 181 | ) |
| 182 | session.commit() |
| 183 | session.rollback() |
| 184 | assert get_unreviewed_batch(session, draft_id=draft.id, year_id=year.id).state == "Unreviewed" |
| 185 | |
| 186 | |
| 187 | def test_cannot_store_draft_with_pending_related_financial_changes(setup): |
| 188 | session, year, note, _ = setup |
| 189 | snap = proposal(session, year, note) |
| 190 | session.add(AccountVersion( |
| 191 | year_id=year.id, account_code="SHADOW", account_name="Pending", version=1, |
| 192 | )) |
| 193 | with pytest.raises(MappingValidationError, match="clean Session"): |
| 194 | save_unreviewed_batch(session, snap, request_key="request-0012") |
| 195 | assert session.execute(select(mapping_review_drafts)).all() == [] |
| 196 | |
| 197 | |
| 198 | def test_draft_state_is_unreviewed_not_authenticated_approval(setup): |
| 199 | session, year, note, _ = setup |
| 200 | draft = save_unreviewed_batch(session, proposal(session, year, note), request_key="request-0013") |
| 201 | assert not hasattr(draft, "approver_id") |
| 202 | assert not hasattr(draft, "approved_at") |
| 203 | assert not hasattr(draft, "apply") |
| 204 | assert draft.state == "Unreviewed" |
| 205 | |
| 206 | |
| 207 | def test_stored_proposal_checksum_detects_direct_sql_payload_change(setup): |
| 208 | session, year, note, _ = setup |
| 209 | saved = save_unreviewed_batch( |
| 210 | session, proposal(session, year, note), request_key="request-0014", |
| 211 | ) |
| 212 | session.commit() |
| 213 | session.execute( |
| 214 | update(mapping_review_drafts) |
| 215 | .where(mapping_review_drafts.c.id == saved.id) |
| 216 | .values(payload_json={"batch": {"source_rows": []}}) |
| 217 | ) |
| 218 | session.commit() |
| 219 | with pytest.raises(MappingValidationError, match="corrupted"): |
| 220 | get_unreviewed_batch(session, draft_id=saved.id, year_id=year.id) |
| 221 | with pytest.raises(MappingValidationError, match="corrupted"): |
| 222 | validate_unreviewed_batch_current(session, draft_id=saved.id, year_id=year.id) |
| 223 | |
| 224 | |
| 225 | def test_database_unique_constraint_rejects_duplicate_request_identity(setup): |
| 226 | session, year, note, _ = setup |
| 227 | first = save_unreviewed_batch( |
| 228 | session, proposal(session, year, note), request_key="request-0015", |
| 229 | ) |
| 230 | session.commit() |
| 231 | existing = session.execute( |
| 232 | select(mapping_review_drafts).where(mapping_review_drafts.c.id == first.id) |
| 233 | ).mappings().one() |
| 234 | with pytest.raises(IntegrityError): |
| 235 | session.execute( |
| 236 | insert(mapping_review_drafts).values( |
| 237 | year_id=year.id, request_key=first.request_key, |
| 238 | payload_json=existing["payload_json"], |
| 239 | payload_hash=existing["payload_hash"], state="Unreviewed", |
| 240 | ) |
| 241 | ) |
| 242 | session.commit() |
| 243 | session.rollback() |
| 244 | assert len(session.execute(select(mapping_review_drafts)).all()) == 1 |
| 245 | |
| 246 | |
| 247 | def test_proposal_table_has_independent_metadata_and_fk(setup): |
| 248 | from aram_backend.database import Base |
| 249 | session, year, note, _ = setup |
| 250 | assert "mapping_review_drafts" not in Base.metadata.tables |
| 251 | assert "mapping_review_drafts" in inspect(session.get_bind()).get_table_names() |
| 252 | names = {fk["referred_table"] for fk in inspect(session.get_bind()).get_foreign_keys("mapping_review_drafts")} |
| 253 | assert "fiscal_years" in names |