codex/aram-cross-statement-controls-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 | """Synthetic-only tests for the unapproved statement of changes in equity.""" |
| 2 | from __future__ import annotations |
| 3 | |
| 4 | from dataclasses import replace |
| 5 | from io import BytesIO |
| 6 | import json |
| 7 | |
| 8 | import pytest |
| 9 | from openpyxl import Workbook, load_workbook |
| 10 | |
| 11 | from aram_backend.coverage_preview import CoverageError |
| 12 | from aram_backend.equity_changes_candidate import ( |
| 13 | EquityMovementPolicy, build_equity_changes_candidate, |
| 14 | ) |
| 15 | from aram_backend.pilot_workbook import SHEET_HEADERS |
| 16 | from aram_backend.reconciliation_review import NoteReference |
| 17 | |
| 18 | |
| 19 | ITEMS = ( |
| 20 | ("CAP", "Capital", "C01", "-100", "0", "20", "-120", |
| 21 | "SHARE_CAPITAL", "CAPITAL_CONTRIBUTION"), |
| 22 | ("RET", "Retained earnings", "R01", "-50", "0", "5", "-55", |
| 23 | "RETAINED_EARNINGS", "NET_PROFIT_LOSS"), |
| 24 | ("DIV", "Dividends", "D01", "0", "20", "0", "20", |
| 25 | "RETAINED_EARNINGS", "DIVIDENDS"), |
| 26 | ) |
| 27 | |
| 28 | |
| 29 | def workbook(*, include_eq=True, bad_delta=False, bad_mapping=False, |
| 30 | negative_turnover=False): |
| 31 | book = Workbook() |
| 32 | book.remove(book.active) |
| 33 | for name, headers in SHEET_HEADERS.items(): |
| 34 | book.create_sheet(name).append(list(headers)) |
| 35 | |
| 36 | def add(name, **values): |
| 37 | book[name].append([values.get(key) for key in SHEET_HEADERS[name]]) |
| 38 | |
| 39 | add("Company", company_code="001", company_name="Synthetic only", |
| 40 | fiscal_year=1404, currency="IRR", unit="Rial", status="Draft") |
| 41 | for index, item in enumerate(ITEMS, 2): |
| 42 | code, label, note, opening, debit, credit, closing, component, movement = item |
| 43 | if bad_delta and code == "CAP": |
| 44 | closing = "-119" |
| 45 | if negative_turnover and code == "DIV": |
| 46 | debit = "-20" |
| 47 | closing = "-20" |
| 48 | add("Accounts", company_code="001", fiscal_year=1404, |
| 49 | general_code=code, general_name=label, account_full_code=code, |
| 50 | account_name="Synthetic " + label, opening=opening, |
| 51 | debit=debit, credit=credit, closing=closing, |
| 52 | source_row=index + 10, row_status="Draft", |
| 53 | closing_delta="0" if not bad_delta else "1") |
| 54 | if include_eq: |
| 55 | add("Notes", company_code="001", fiscal_year=1404, |
| 56 | statement="EQ", statement_section=label, |
| 57 | note_number=note, note_title=label, subnote_code="", |
| 58 | subnote_title="") |
| 59 | if not (bad_mapping and code == "DIV"): |
| 60 | add("Mapping", mapping_id="M-" + code, company_code="001", |
| 61 | fiscal_year=1404, account_full_code=code, |
| 62 | account_level="General", general_code=code, |
| 63 | target_statement="EQ", statement_section=label, |
| 64 | note_number=note, subnote_code="", mapping_version=1, |
| 65 | approval_status="Approved", reviewer="Synthetic reviewer", |
| 66 | review_date="2026-10-31", source="Synthetic", |
| 67 | validation_status="Valid") |
| 68 | stream = BytesIO() |
| 69 | book.save(stream) |
| 70 | book.close() |
| 71 | return stream.getvalue() |
| 72 | |
| 73 | |
| 74 | def references(): |
| 75 | return [ |
| 76 | NoteReference("EQ", label, note, "", closing, "SYNTH-REF-" + code) |
| 77 | for code, label, note, opening, debit, credit, closing, component, movement in ITEMS |
| 78 | ] |
| 79 | |
| 80 | |
| 81 | def policies(): |
| 82 | return [ |
| 83 | EquityMovementPolicy( |
| 84 | label, note, "", component, movement, -1, |
| 85 | "Synthetic " + label, "SYNTH-POL-" + code, |
| 86 | ) |
| 87 | for code, label, note, opening, debit, credit, closing, component, movement in ITEMS |
| 88 | ] |
| 89 | |
| 90 | |
| 91 | def build(*, data=None, refs=None, pol=None, company="001", year=1404): |
| 92 | return build_equity_changes_candidate( |
| 93 | workbook() if data is None else data, |
| 94 | company_code=company, fiscal_year=year, |
| 95 | references=references() if refs is None else refs, |
| 96 | policies=policies() if pol is None else pol, |
| 97 | ) |
| 98 | |
| 99 | |
| 100 | def edit(data, change): |
| 101 | book = load_workbook(BytesIO(data)) |
| 102 | change(book) |
| 103 | out = BytesIO() |
| 104 | book.save(out) |
| 105 | book.close() |
| 106 | return out.getvalue() |
| 107 | |
| 108 | |
| 109 | def test_equity_statement_rollforward_and_category_totals_are_exact(): |
| 110 | result = build() |
| 111 | assert result.eligible_for_analyst_review |
| 112 | assert not result.findings |
| 113 | assert result.opening_equity_total == "150" |
| 114 | assert result.capital_contributions == "20" |
| 115 | assert result.net_profit_loss_movements == "5" |
| 116 | assert result.dividends == "-20" |
| 117 | assert result.other_comprehensive_income == "0" |
| 118 | assert result.transfers == "0" |
| 119 | assert result.prior_period_adjustments == "0" |
| 120 | assert result.other_movements == "0" |
| 121 | assert result.total_movement == "5" |
| 122 | assert result.closing_equity_total == "155" |
| 123 | assert result.rollforward_difference == "0" |
| 124 | assert len(result.note_movements) == 3 |
| 125 | assert result.note_movements[0].line_label.startswith("Synthetic ") |
| 126 | assert len(result.account_movements) == 3 |
| 127 | assert result.kind == "UNAPPROVED_EQUITY_CHANGES_PRESENTATION_CANDIDATE" |
| 128 | assert not result.official_financial_statement |
| 129 | assert not result.accounting_policy_authorized |
| 130 | assert not result.source_ledger_completeness_verified |
| 131 | assert not result.independently_reviewed |
| 132 | assert not result.export_authorized |
| 133 | assert not result.releasable |
| 134 | retained = next(x for x in result.component_totals |
| 135 | if x.equity_component == "RETAINED_EARNINGS") |
| 136 | assert retained.opening_balance == "50" |
| 137 | assert retained.movement_amount == "-15" |
| 138 | assert retained.closing_balance == "35" |
| 139 | |
| 140 | |
| 141 | def test_zero_movement_and_zero_balances_are_valid(): |
| 142 | def zero(book): |
| 143 | for row in book["Accounts"].iter_rows(min_row=2): |
| 144 | for column in ("opening", "debit", "credit", "closing"): |
| 145 | row[16 + ("opening", "debit", "credit", "closing").index(column)].value = "0" |
| 146 | data = edit(workbook(), zero) |
| 147 | refs = [replace(ref, expected_closing_exposure="0") for ref in references()] |
| 148 | result = build(data=data, refs=refs) |
| 149 | assert result.eligible_for_analyst_review |
| 150 | assert result.opening_equity_total == "0" |
| 151 | assert result.total_movement == "0" |
| 152 | assert result.closing_equity_total == "0" |
| 153 | |
| 154 | |
| 155 | def test_missing_policy_blocks_entire_equity_candidate(): |
| 156 | result = build(pol=policies()[:-1]) |
| 157 | assert not result.eligible_for_analyst_review |
| 158 | assert "MISSING_EQUITY_POLICY" in result.findings |
| 159 | |
| 160 | |
| 161 | def test_orphan_policy_blocks_candidate(): |
| 162 | p = policies() |
| 163 | p.append(EquityMovementPolicy( |
| 164 | "Unknown", "X01", "", "OTHER_EQUITY", "OTHER_MOVEMENT", -1, |
| 165 | "Synthetic extra", "SYNTH-EXTRA", |
| 166 | )) |
| 167 | result = build(pol=p) |
| 168 | assert not result.eligible_for_analyst_review |
| 169 | assert "ORPHAN_EQUITY_POLICY" in result.findings |
| 170 | |
| 171 | |
| 172 | def test_missing_mapping_blocks_even_when_other_equity_notes_match(): |
| 173 | result = build(data=workbook(bad_mapping=True)) |
| 174 | assert not result.eligible_for_analyst_review |
| 175 | assert "UPSTREAM_RECONCILIATION_BLOCKED" in result.findings |
| 176 | |
| 177 | |
| 178 | def test_reference_mismatch_blocks_candidate(): |
| 179 | refs = references() |
| 180 | refs[0] = replace(refs[0], expected_closing_exposure="-121") |
| 181 | result = build(refs=refs) |
| 182 | assert not result.eligible_for_analyst_review |
| 183 | assert "UPSTREAM_RECONCILIATION_BLOCKED" in result.findings |
| 184 | |
| 185 | |
| 186 | def test_invalid_source_rollforward_fails_closed(): |
| 187 | with pytest.raises(CoverageError): |
| 188 | build(data=workbook(bad_delta=True)) |
| 189 | |
| 190 | |
| 191 | def test_negative_turnover_fails_closed(): |
| 192 | refs = references() |
| 193 | refs[2] = replace(refs[2], expected_closing_exposure="-20") |
| 194 | with pytest.raises(CoverageError) as error: |
| 195 | build(data=workbook(negative_turnover=True), refs=refs) |
| 196 | assert error.value.code == "EQUITY_NEGATIVE_TURNOVER" |
| 197 | |
| 198 | |
| 199 | @pytest.mark.parametrize("invalid", [ |
| 200 | EquityMovementPolicy("Capital", "C01", "", "BAD", "CAPITAL_CONTRIBUTION", |
| 201 | -1, "Capital", "P"), |
| 202 | EquityMovementPolicy("Capital", "C01", "", "SHARE_CAPITAL", "BAD", |
| 203 | -1, "Capital", "P"), |
| 204 | EquityMovementPolicy("Capital", "C01", "", "SHARE_CAPITAL", "DIVIDENDS", |
| 205 | True, "Capital", "P"), |
| 206 | EquityMovementPolicy("Capital", "C01", "", "SHARE_CAPITAL", "DIVIDENDS", |
| 207 | 0, "Capital", "P"), |
| 208 | EquityMovementPolicy("Capital", "C01", "", "SHARE_CAPITAL", "DIVIDENDS", |
| 209 | -1, "", "P"), |
| 210 | EquityMovementPolicy("Capital", "C01", "", "SHARE_CAPITAL", "DIVIDENDS", |
| 211 | -1, "Capital", ""), |
| 212 | EquityMovementPolicy("Capital", "C01", "", "SHARE_CAPITAL", "DIVIDENDS", |
| 213 | -1, "Bad" + chr(10) + "label", "P"), |
| 214 | ]) |
| 215 | def test_invalid_equity_policy_fails_closed(invalid): |
| 216 | with pytest.raises(CoverageError): |
| 217 | build(pol=[invalid]) |
| 218 | |
| 219 | |
| 220 | def test_duplicate_policy_fails_closed(): |
| 221 | p = policies() |
| 222 | with pytest.raises(CoverageError) as error: |
| 223 | build(pol=p + [p[0]]) |
| 224 | assert error.value.code == "DUPLICATE_EQUITY_POLICY" |
| 225 | |
| 226 | |
| 227 | def test_wrong_company_and_year_are_not_guessed(): |
| 228 | with pytest.raises(CoverageError): |
| 229 | build(company="999") |
| 230 | with pytest.raises(CoverageError): |
| 231 | build(year=1403) |
| 232 | |
| 233 | |
| 234 | def test_missing_equity_notes_blocks(): |
| 235 | data = workbook(include_eq=False) |
| 236 | result = build(data=data, refs=[], pol=[]) |
| 237 | assert not result.eligible_for_analyst_review |
| 238 | assert "NO_EQUITY_NOTES" in result.findings |
| 239 | assert result.opening_equity_total == "0" |
| 240 | assert result.closing_equity_total == "0" |
| 241 | |
| 242 | |
| 243 | def test_source_unchanged_and_json_safe(): |
| 244 | data = workbook() |
| 245 | original = bytes(data) |
| 246 | result = json.loads(json.dumps(build(data=data).to_dict())) |
| 247 | assert data == original |
| 248 | assert result["releasable"] is False |
| 249 | assert len(result["source_sha256"]) == 64 |
| 250 | assert len(result["note_references_sha256"]) == 64 |
| 251 | assert len(result["presentation_policies_sha256"]) == 64 |
| 252 | assert all(isinstance(item["movement_amount"], str) |
| 253 | for item in result["note_movements"]) |
| 254 | |
| 255 | |
| 256 | def test_policy_order_does_not_change_hash_or_result(): |
| 257 | p = policies() |
| 258 | first = build(pol=p) |
| 259 | second = build(pol=list(reversed(p))) |
| 260 | assert first.note_movements == second.note_movements |
| 261 | assert first.presentation_policies_sha256 == second.presentation_policies_sha256 |
| 262 | |
| 263 | |
| 264 | def test_policy_change_changes_policy_hash(): |
| 265 | p = policies() |
| 266 | first = build(pol=p) |
| 267 | p[0] = replace(p[0], line_label="Revised synthetic capital") |
| 268 | second = build(pol=p) |
| 269 | assert first.presentation_policies_sha256 != second.presentation_policies_sha256 |
| 270 | |
| 271 | |
| 272 | def test_wrong_source_hash_cannot_be_silently_reused(): |
| 273 | # References are tied to note keys/amounts, while the source snapshot hash |
| 274 | # is recalculated for each build; a source mutation still needs exact |
| 275 | # reconciliation, so mutate a mapped account's closing without its control. |
| 276 | data = edit(workbook(), lambda book: setattr(book["Accounts"]["T2"], "value", "-121")) |
| 277 | with pytest.raises(CoverageError): |
| 278 | build(data=data) |