@davoodmehraban89 / Aram-2 Mirror · read-only
Fork 0

Mirrored from https://github.com/davoodmehraban89/Aram.git

Synced Dec 4, 14:15 UTC

dependabot/npm_and_yarn/frontend/globals-17.15.0
add-index-html-10623938925177053894 arena/094f314c-aram arena/2d4eda07-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 dependabot/github_actions/actions/checkout-7 dependabot/github_actions/actions/setup-node-6 dependabot/github_actions/actions/setup-python-7 dependabot/github_actions/actions/upload-artifact-7 dependabot/npm_and_yarn/frontend/eslint-plugin-react-hooks-7.1.1 dependabot/npm_and_yarn/frontend/globals-17.15.0 dependabot/npm_and_yarn/frontend/vite-8.4.0 dependabot/npm_and_yarn/frontend/vitest-5.1.2 dependabot/npm_and_yarn/frontend/vitest/coverage-v8-5.1.2 dependabot/pip/backend/mypy-gte-1.13-and-lt-3 dependabot/pip/backend/pytest-cov-gte-6.0-and-lt-8 dependabot/pip/backend/setuptools-gte-75-and-lt-85 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
py 276 lines 13.1 KB
Raw
1 from __future__ import annotations
2
3 from io import BytesIO
4
5 from openpyxl import load_workbook
6
7 from aram_backend.pilot_editing import (
8 InvalidAccountError,
9 InvalidCompanyError,
10 InvalidNoteError,
11 make_account,
12 make_company,
13 make_note,
14 )
15 from aram_backend.pilot_engine import build_report, export_working_workbook, parse_pilot_model
16 from aram_backend.pilot_store import PilotSessionStore
17 from test_pilot_engine import build_balanced_workbook
18
19
20 def _session():
21 store = PilotSessionStore()
22 model = parse_pilot_model(build_balanced_workbook())
23 return store, store.create(model, source="upload", filename="t.xlsx")
24
25
26 # ----------------------------------------------------------- domain validators
27 def test_make_account_derives_closing_and_never_invents():
28 account = make_account(
29 full_code="60", name="حساب جدید", general_code="60", general_name="کل",
30 opening="100", debit="50", credit="20", source_row=900001,
31 )
32 assert account.closing == 130 # 100 + 50 - 20, derived not guessed
33 assert account.opening + account.debit - account.credit == account.closing
34
35
36 def test_make_account_rejects_bad_values():
37 import pytest
38 with pytest.raises(InvalidAccountError):
39 make_account(full_code="", name="x", general_code="1", general_name="g",
40 opening=0, debit=0, credit=0, source_row=1)
41 with pytest.raises(InvalidAccountError):
42 make_account(full_code="1", name="x", general_code="1", general_name="g",
43 opening="NaN", debit=0, credit=0, source_row=1)
44 with pytest.raises(InvalidAccountError):
45 make_account(full_code="1", name="x", general_code="1", general_name="g",
46 opening=0, debit=0, credit=0, source_row=0)
47
48
49 def test_make_note_and_company_validation():
50 import pytest
51 note = make_note(statement="BS", section="Assets", note_number="07", note_title="یادداشت جدید")
52 assert note.key == ("BS", "Assets", "07", "")
53 with pytest.raises(InvalidNoteError):
54 make_note(statement="XX", section="Assets", note_number="1", note_title="t")
55 with pytest.raises(InvalidNoteError):
56 make_note(statement="BS", section="Assets", note_number="1", note_title="t",
57 subnote_code="A", subnote_title="")
58 company = make_company(code="C9", name="شرکت عمومی", fiscal_year=1405)
59 assert company.fiscal_year == 1405
60 with pytest.raises(InvalidCompanyError):
61 make_company(code="C9", name="x", fiscal_year=-1)
62
63
64 # ------------------------------------------------------------- account CRUD
65 def test_add_account_enters_exception_queue_then_maps():
66 store, session = _session()
67 store.add_account(session.id, full_code="60", name="هزینه جدید", general_code="60",
68 general_name="هزینه‌ها", opening=0, debit=5, credit=0, actor="tester")
69 report = build_report(session.working, session.mappings)
70 assert "60" in {e["account_code"] for e in report["exceptions"]}
71 assert report["status"]["label"] == "INCOMPLETE"
72 # map it to the expense note; equation stays closed because TB now has +5
73 store.set_mapping(session.id, "60", statement="IS", section="Expenses", note_number="05", actor="tester")
74 report = build_report(session.working, session.mappings)
75 assert report["totals"]["expenses"] == "15"
76 assert session.audit_trail[-2]["action"] == "add_account"
77
78
79 def test_add_duplicate_account_rejected():
80 import pytest
81
82 from aram_backend.pilot_editing import DuplicateAccountError
83 store, session = _session()
84 with pytest.raises(DuplicateAccountError):
85 store.add_account(session.id, full_code="10", name="تکراری", general_code="10",
86 general_name="کل", opening=0, debit=0, credit=0)
87
88
89 def test_rename_account_carries_mapping_forward():
90 store, session = _session()
91 store.rename_account(session.id, "10", "1001", actor="tester")
92 assert "10" not in session.working.accounts
93 assert "1001" in session.working.accounts
94 assert session.mappings["1001"].account_code == "1001"
95 assert session.mappings["1001"].note_number == "01"
96 report = build_report(session.working, session.mappings)
97 assert report["totals"]["assets"] == "100"
98 assert report["status"]["complete"]
99
100
101 def test_update_and_delete_account():
102 store, session = _session()
103 store.update_account(session.id, "50", name="هزینه ویرایش‌شده", general_code="50",
104 general_name="هزینه‌ها", opening=0, debit=12, credit=0, actor="tester")
105 assert session.working.accounts["50"].closing == 12
106 store.delete_account(session.id, "50", actor="tester")
107 assert "50" not in session.working.accounts
108 assert "50" not in session.mappings
109 report = build_report(session.working, session.mappings)
110 assert "50" not in {e["account_code"] for e in report["exceptions"]}
111
112
113 # --------------------------------------------------------------- note CRUD
114 def test_add_note_and_subnote_then_map_new_account_to_them():
115 store, session = _session()
116 store.add_note(session.id, make_note(statement="BS", section="Assets", note_number="06",
117 note_title="سرمایه‌گذاری جدید"), actor="tester")
118 store.add_note(session.id, make_note(statement="BS", section="Assets", note_number="06",
119 note_title="سرمایه‌گذاری جدید", subnote_code="01",
120 subnote_title="زیربخش الف"), actor="tester")
121 store.add_account(session.id, full_code="15", name="سرمایه‌گذاری", general_code="15",
122 general_name="دارایی", opening=0, debit=0, credit=0, actor="tester")
123 store.set_mapping(session.id, "15", statement="BS", section="Assets",
124 note_number="06", subnote_code="01", actor="tester")
125 report = build_report(session.working, session.mappings)
126 assets = report["statements"]["BS"]["sections"][0]
127 sub = [ln for ln in assets["lines"] if ln["note_number"] == "06" and ln["subnote_code"] == "01"]
128 assert sub and sub[0]["accounts"][0]["code"] == "15"
129
130
131 def test_delete_note_in_use_is_blocked():
132 import pytest
133
134 from aram_backend.pilot_editing import NoteInUseError
135 store, session = _session()
136 with pytest.raises(NoteInUseError):
137 store.delete_note(session.id, ("BS", "Assets", "01", ""), actor="tester")
138 # after clearing the mapping, deletion succeeds
139 store.clear_mapping(session.id, "10", actor="tester")
140 store.delete_note(session.id, ("BS", "Assets", "01", ""), actor="tester")
141 assert ("BS", "Assets", "01", "") not in session.working.notes
142
143
144 def test_update_note_title():
145 store, session = _session()
146 key = ("BS", "Assets", "01", "")
147 store.update_note(session.id, key, make_note(statement="BS", section="Assets",
148 note_number="01", note_title="عنوان تازه"), actor="tester")
149 assert session.working.notes[key].note_title == "عنوان تازه"
150
151
152 # ------------------------------------------------------------- company edit
153 def test_company_is_generic_and_editable():
154 store, session = _session()
155 store.update_company(session.id, make_company(code="GAS-01", name="شرکت انتقال گاز نمونه",
156 fiscal_year=1404, unit="میلیون ریال"), actor="tester")
157 assert session.working.company.name == "شرکت انتقال گاز نمونه"
158 report = build_report(session.working, session.mappings)
159 assert report["company"]["code"] == "GAS-01"
160 # and equally any other company
161 store.update_company(session.id, make_company(code="ANY-99", name="هر شرکت دیگر",
162 fiscal_year=1405), actor="tester")
163 assert session.working.company.code == "ANY-99"
164
165
166 # ------------------------------------------------- save / reload round-trip
167 def test_workspace_round_trip_is_lossless():
168 store, session = _session()
169 # make edits: add account+note, map, rename, edit company
170 store.add_note(session.id, make_note(statement="IS", section="Expenses", note_number="06",
171 note_title="هزینه تازه"), actor="tester")
172 store.add_account(session.id, full_code="55", name="هزینه تازه", general_code="55",
173 general_name="هزینه‌ها", opening=0, debit=0, credit=0, actor="tester")
174 store.set_mapping(session.id, "55", statement="IS", section="Expenses", note_number="06",
175 approval_status="Approved", reviewer="بازبین", actor="tester")
176 store.rename_account(session.id, "10", "1001", actor="tester")
177 store.update_company(session.id, make_company(code="NEW", name="شرکت بازطراحی‌شده",
178 fiscal_year=1406, unit="ریال"), actor="tester")
179
180 before = build_report(session.working, session.mappings)
181 saved = export_working_workbook(session.working)
182
183 # The saved workbook must pass preflight and rebuild the same state.
184 reloaded = parse_pilot_model(saved)
185 after = build_report(reloaded, reloaded.mappings)
186 assert after["totals"] == before["totals"]
187 assert after["status"] == before["status"]
188 assert after["company"]["code"] == "NEW"
189 assert after["company"]["fiscal_year"] == 1406
190 assert "1001" in reloaded.accounts
191 assert reloaded.mappings["55"].approval_status == "Approved"
192 assert ("IS", "Expenses", "06", "") in reloaded.notes
193
194
195 def test_workspace_export_is_preflight_valid():
196 from aram_backend.pilot_workbook import preflight_pilot_workbook
197 store, session = _session()
198 data = export_working_workbook(session.working)
199 result = preflight_pilot_workbook(data)
200 assert result.valid, result.issues
201 book = load_workbook(BytesIO(data))
202 assert "Company" in book.sheetnames and "Mapping" in book.sheetnames
203 book.close()
204
205
206 # ------------------------------------------------------------------ API CRUD
207 def test_api_account_note_company_crud_and_workspace(pilot_client):
208 client = pilot_client
209 sid = client.post("/api/v1/pilot/demo").json()["session"]["id"]
210 base = f"/api/v1/pilot/sessions/{sid}"
211
212 # add a note + subnote
213 assert client.post(f"{base}/notes", json={
214 "statement": "BS", "section": "Assets", "note_number": "20",
215 "note_title": "دارایی نامشهود", "actor": "tester",
216 }).status_code == 200
217 assert client.post(f"{base}/notes", json={
218 "statement": "BS", "section": "Assets", "note_number": "20",
219 "note_title": "دارایی نامشهود", "subnote_code": "01",
220 "subnote_title": "سرقفلی", "actor": "tester",
221 }).status_code == 200
222 # duplicate note -> 409
223 assert client.post(f"{base}/notes", json={
224 "statement": "BS", "section": "Assets", "note_number": "20", "note_title": "x",
225 }).status_code == 409
226
227 # add an account and map it to the subnote
228 assert client.post(f"{base}/accounts", json={
229 "full_code": "1201", "name": "سرقفلی", "general_code": "12",
230 "general_name": "دارایی نامشهود", "opening": 0, "debit": 0, "credit": 0,
231 "actor": "tester",
232 }).status_code == 200
233 assert client.put(f"{base}/mappings/1201", json={
234 "statement": "BS", "section": "Assets", "note_number": "20", "subnote_code": "01",
235 "actor": "tester",
236 }).status_code == 200
237 # duplicate account -> 409
238 assert client.post(f"{base}/accounts", json={
239 "full_code": "1201", "name": "x", "general_code": "12", "general_name": "g",
240 }).status_code == 409
241
242 # rename carries mapping
243 assert client.post(f"{base}/accounts/1201/rename", json={"new_code": "1299"}).status_code == 200
244 body = client.get(base).json()
245 acc = {a["code"]: a for a in body["accounts"]}
246 assert "1299" in acc and acc["1299"]["mapping"]["note_number"] == "20"
247
248 # edit company to a totally different generic company
249 assert client.put(f"{base}/company", json={
250 "code": "OTHER", "name": "شرکت دیگر", "fiscal_year": 1405, "unit": "ریال",
251 }).status_code == 200
252 assert client.get(base).json()["report"]["company"]["code"] == "OTHER"
253
254 # delete note in use -> 409 ; delete account then note succeeds
255 assert client.delete(f"{base}/notes", params={
256 "statement": "BS", "section": "Assets", "note_number": "20", "subnote_code": "01",
257 }).status_code == 409
258 assert client.delete(f"{base}/accounts/1299").status_code == 200
259 assert client.delete(f"{base}/notes", params={
260 "statement": "BS", "section": "Assets", "note_number": "20", "subnote_code": "01",
261 }).status_code == 200
262
263 # workspace export downloads and is a valid workbook
264 ws = client.get(f"{base}/workspace.xlsx")
265 assert ws.status_code == 200 and "spreadsheetml" in ws.headers["content-type"]
266 load_workbook(BytesIO(ws.content)).close()
267
268
269 def test_api_invalid_account_value_is_422(pilot_client):
270 client = pilot_client
271 sid = client.post("/api/v1/pilot/demo").json()["session"]["id"]
272 response = client.post(f"/api/v1/pilot/sessions/{sid}/accounts", json={
273 "full_code": "X1", "name": "بد", "general_code": "99", "general_name": "گ",
274 "opening": "not-a-number",
275 })
276 assert response.status_code == 422