codex/aram-five-statement-orchestrator-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 | import pytest |
| 2 | from sqlalchemy.exc import IntegrityError |
| 3 | |
| 4 | from aram_backend.models import ( |
| 5 | AccountVersion, |
| 6 | Company, |
| 7 | FinancialStatement, |
| 8 | FiscalYear, |
| 9 | FiscalYearStatus, |
| 10 | MappingRule, |
| 11 | NoteDefinition, |
| 12 | NoteRow, |
| 13 | SourceDocument, |
| 14 | StatementLine, |
| 15 | StatementType, |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | def make_year(session, year=1404): |
| 20 | company = Company(code=f"CO-{year}", name="Test Company") |
| 21 | fiscal_year = FiscalYear(company=company, year=year) |
| 22 | session.add(company) |
| 23 | session.commit() |
| 24 | return fiscal_year |
| 25 | |
| 26 | |
| 27 | def test_model_relationships(session): |
| 28 | year = make_year(session) |
| 29 | note = NoteDefinition(year_id=year.id, note_number="10", title="Assets", version=1) |
| 30 | note.rows.append(NoteRow(row_order=0, label="Tangible assets")) |
| 31 | statement = FinancialStatement(year_id=year.id, statement_type=StatementType.BALANCE_SHEET) |
| 32 | statement.lines.append(StatementLine(line_order=0, line_label="Assets", target_note=note)) |
| 33 | session.add_all([note, statement]) |
| 34 | session.commit() |
| 35 | assert note.rows[0].note_id == note.id |
| 36 | assert statement.lines[0].target_note_id == note.id |
| 37 | |
| 38 | |
| 39 | def test_version_uniqueness(session): |
| 40 | year = make_year(session) |
| 41 | session.add(AccountVersion(year_id=year.id, account_code="1000", account_name="Cash", version=1)) |
| 42 | session.commit() |
| 43 | session.add(AccountVersion(year_id=year.id, account_code="1000", account_name="Cash duplicate", version=1)) |
| 44 | with pytest.raises(IntegrityError): |
| 45 | session.commit() |
| 46 | |
| 47 | |
| 48 | def test_source_document_hash_unique_within_year(session): |
| 49 | year = make_year(session) |
| 50 | session.add(SourceDocument(year_id=year.id, filename="a.xlsx", file_hash="abc", document_type="ledger")) |
| 51 | session.commit() |
| 52 | session.add(SourceDocument(year_id=year.id, filename="b.xlsx", file_hash="abc", document_type="ledger")) |
| 53 | with pytest.raises(IntegrityError): |
| 54 | session.commit() |
| 55 | |
| 56 | |
| 57 | def test_fiscal_year_isolation_for_mapping(session): |
| 58 | first = make_year(session, 1403) |
| 59 | second = make_year(session, 1404) |
| 60 | note = NoteDefinition(year_id=first.id, note_number="10", title="Assets") |
| 61 | session.add(note) |
| 62 | session.commit() |
| 63 | session.add(MappingRule( |
| 64 | year_id=second.id, rule_key="rule-1", account_code="1000", |
| 65 | target_note=note, mapping_type="O" |
| 66 | )) |
| 67 | with pytest.raises(ValueError, match="same fiscal year"): |
| 68 | session.commit() |
| 69 | |
| 70 | |
| 71 | def test_fiscal_year_isolation_cannot_be_bypassed_with_raw_ids(session): |
| 72 | first = make_year(session, 1403) |
| 73 | second = make_year(session, 1404) |
| 74 | note = NoteDefinition(year_id=first.id, note_number="10", title="Assets") |
| 75 | session.add(note) |
| 76 | session.commit() |
| 77 | session.add(MappingRule( |
| 78 | year_id=second.id, rule_key="raw-id-rule", account_code="1000", |
| 79 | target_note_id=note.id, mapping_type="O" |
| 80 | )) |
| 81 | with pytest.raises(ValueError, match="same fiscal year"): |
| 82 | session.commit() |
| 83 | |
| 84 | |
| 85 | def test_statement_line_isolation_cannot_be_bypassed_with_raw_ids(session): |
| 86 | first = make_year(session, 1403) |
| 87 | second = make_year(session, 1404) |
| 88 | note = NoteDefinition(year_id=first.id, note_number="10", title="Assets") |
| 89 | statement = FinancialStatement( |
| 90 | year_id=second.id, statement_type=StatementType.BALANCE_SHEET |
| 91 | ) |
| 92 | session.add_all([note, statement]) |
| 93 | session.commit() |
| 94 | session.add(StatementLine( |
| 95 | statement_id=statement.id, line_order=0, line_label="Assets", |
| 96 | target_note_id=note.id |
| 97 | )) |
| 98 | with pytest.raises(ValueError, match="same fiscal year"): |
| 99 | session.commit() |
| 100 | |
| 101 | |
| 102 | def test_locked_year_blocks_mutation(session): |
| 103 | year = make_year(session) |
| 104 | year.status = FiscalYearStatus.APPROVED |
| 105 | session.commit() |
| 106 | year.status = FiscalYearStatus.LOCKED |
| 107 | session.commit() |
| 108 | session.add(AccountVersion(year_id=year.id, account_code="1000", account_name="Cash")) |
| 109 | with pytest.raises(ValueError, match="locked"): |
| 110 | session.commit() |
| 111 | |
| 112 | |
| 113 | def test_locked_year_protection_cannot_be_bypassed_with_raw_parent_id(session): |
| 114 | year = make_year(session) |
| 115 | statement = FinancialStatement( |
| 116 | year_id=year.id, statement_type=StatementType.BALANCE_SHEET |
| 117 | ) |
| 118 | session.add(statement) |
| 119 | session.commit() |
| 120 | year.status = FiscalYearStatus.APPROVED |
| 121 | session.commit() |
| 122 | year.status = FiscalYearStatus.LOCKED |
| 123 | session.commit() |
| 124 | session.add(StatementLine( |
| 125 | statement_id=statement.id, line_order=0, line_label="Blocked" |
| 126 | )) |
| 127 | with pytest.raises(ValueError, match="locked"): |
| 128 | session.commit() |
| 129 | |
| 130 | |
| 131 | def test_approved_year_blocks_silent_overwrite(session): |
| 132 | year = make_year(session) |
| 133 | account = AccountVersion( |
| 134 | year_id=year.id, account_code="1000", account_name="Cash" |
| 135 | ) |
| 136 | session.add(account) |
| 137 | session.commit() |
| 138 | year.status = FiscalYearStatus.APPROVED |
| 139 | session.commit() |
| 140 | account.account_name = "Silently changed cash" |
| 141 | with pytest.raises(ValueError, match="approved"): |
| 142 | session.commit() |
| 143 | |
| 144 | |
| 145 | def test_locked_year_cannot_be_reopened(session): |
| 146 | year = make_year(session) |
| 147 | year.status = FiscalYearStatus.APPROVED |
| 148 | session.commit() |
| 149 | year.status = FiscalYearStatus.LOCKED |
| 150 | session.commit() |
| 151 | year.status = FiscalYearStatus.DRAFT |
| 152 | with pytest.raises(ValueError, match="Invalid fiscal-year transition"): |
| 153 | session.commit() |
| 154 | |
| 155 | |
| 156 | def test_invalid_status_skip_is_rejected(session): |
| 157 | year = make_year(session) |
| 158 | year.status = FiscalYearStatus.LOCKED |
| 159 | with pytest.raises(ValueError, match="Invalid fiscal-year transition"): |
| 160 | session.commit() |
| 161 | |
| 162 | |
| 163 | # These adversarial cases reproduce the V1 audit bypasses; keep them as release gates. |
| 164 | @pytest.mark.parametrize("status", [FiscalYearStatus.APPROVED, FiscalYearStatus.LOCKED]) |
| 165 | def test_protected_year_identity_cannot_be_mutated(session, status): |
| 166 | year = make_year(session) |
| 167 | year.status = FiscalYearStatus.APPROVED |
| 168 | session.commit() |
| 169 | if status is FiscalYearStatus.LOCKED: |
| 170 | year.status = FiscalYearStatus.LOCKED |
| 171 | session.commit() |
| 172 | |
| 173 | year.year = 1405 |
| 174 | with pytest.raises(ValueError, match="identity cannot be mutated"): |
| 175 | session.commit() |
| 176 | |
| 177 | |
| 178 | @pytest.mark.parametrize("status", [FiscalYearStatus.APPROVED, FiscalYearStatus.LOCKED]) |
| 179 | def test_protected_year_cannot_be_deleted(session, status): |
| 180 | year = make_year(session) |
| 181 | year.status = FiscalYearStatus.APPROVED |
| 182 | session.commit() |
| 183 | if status is FiscalYearStatus.LOCKED: |
| 184 | year.status = FiscalYearStatus.LOCKED |
| 185 | session.commit() |
| 186 | |
| 187 | session.delete(year) |
| 188 | with pytest.raises(ValueError, match="cannot be deleted"): |
| 189 | session.commit() |
| 190 | |
| 191 | |
| 192 | def test_company_with_protected_year_cannot_be_mutated(session): |
| 193 | year = make_year(session) |
| 194 | year.status = FiscalYearStatus.APPROVED |
| 195 | session.commit() |
| 196 | |
| 197 | year.company.name = "Changed after approval" |
| 198 | with pytest.raises(ValueError, match="cannot be mutated"): |
| 199 | session.commit() |
| 200 | |
| 201 | |
| 202 | def test_company_with_protected_year_cannot_be_deleted(session): |
| 203 | year = make_year(session) |
| 204 | year.status = FiscalYearStatus.APPROVED |
| 205 | session.commit() |
| 206 | |
| 207 | session.delete(year.company) |
| 208 | with pytest.raises(ValueError, match="cannot be deleted"): |
| 209 | session.commit() |
| 210 | |
| 211 | |
| 212 | def test_protected_year_cannot_be_reparented_through_relationship(session): |
| 213 | year = make_year(session) |
| 214 | replacement = Company(code="REPLACEMENT", name="Replacement Company") |
| 215 | session.add(replacement) |
| 216 | session.commit() |
| 217 | year.status = FiscalYearStatus.APPROVED |
| 218 | session.commit() |
| 219 | |
| 220 | year.company = replacement |
| 221 | with pytest.raises(ValueError, match="cannot be mutated"): |
| 222 | session.commit() |
| 223 | |
| 224 | |
| 225 | def test_draft_year_identity_and_deletion_remain_mutable(session): |
| 226 | year = make_year(session) |
| 227 | year.year = 1405 |
| 228 | session.commit() |
| 229 | assert year.year == 1405 |
| 230 | |
| 231 | session.delete(year) |
| 232 | session.commit() |
| 233 | assert session.get(FiscalYear, year.id) is None |