test(cli): cover strict manifest parsing and local five-statement CLI outcomes
davoodmehraban89 committed
Oct 31, 2026 at 11:45 UTC
e4e5322c942a0e29707feae43b7940d69c728b15
1 file changed
+157
backend/tests/test_five_statement_pipeline_integration.py
+157
@@ -1,7 +1,9 @@
1
"""End-to-end synthetic integration tests for all five ARAM statement engines."""
2
from __future__ import annotations
3
4
+from dataclasses import asdict
5
from io import BytesIO
6
+import json
7
8
import pytest
9
from openpyxl import Workbook
@@ -17,6 +19,7 @@ from aram_backend.income_statement_candidate import IncomeStatementLinePolicy
19
from aram_backend.pilot_workbook import SHEET_HEADERS
20
from aram_backend.reconciliation_review import NoteReference
21
from aram_backend.five_statement_pipeline import build_five_statement_pipeline
22
+from aram_backend.five_statement_pipeline_cli import main as pipeline_cli_main
23
24
25
COMPANY = "001"
@@ -209,6 +212,86 @@ def build_all(data: bytes | None = None, *, transactions=None):
212
)
213
214
215
+
216
+def pipeline_manifest(*, transactions=None):
217
+ return {
218
+ "schema_version": 1,
219
+ "company_code": COMPANY,
220
+ "fiscal_year": YEAR,
221
+ "currency": "IRR",
222
+ "unit": "Rial",
223
+ "references": [asdict(item) for item in references()],
224
+ "balance_sheet_policies": [
225
+ {
226
+ "statement_section": "Current assets", "note_number": "C01",
227
+ "subnote_code": "", "category": "ASSETS", "sign_multiplier": 1,
228
+ "line_label": "Cash and cash equivalents",
229
+ "policy_evidence_ref": "SYNTH-POL-BS-CASH",
230
+ },
231
+ {
232
+ "statement_section": "Current assets", "note_number": "A01",
233
+ "subnote_code": "", "category": "ASSETS", "sign_multiplier": 1,
234
+ "line_label": "Trade receivables", "policy_evidence_ref": "SYNTH-POL-BS-AR",
235
+ },
236
+ {
237
+ "statement_section": "Current liabilities", "note_number": "L01",
238
+ "subnote_code": "", "category": "LIABILITIES", "sign_multiplier": -1,
239
+ "line_label": "Trade payables", "policy_evidence_ref": "SYNTH-POL-BS-AP",
240
+ },
241
+ {
242
+ "statement_section": "Equity", "note_number": "E01",
243
+ "subnote_code": "", "category": "EQUITY", "sign_multiplier": -1,
244
+ "line_label": "Total equity", "policy_evidence_ref": "SYNTH-POL-BS-EQ",
245
+ },
246
+ ],
247
+ "income_statement_policies": [
248
+ {
249
+ "statement_section": "Revenue", "note_number": "R01",
250
+ "subnote_code": "", "category": "REVENUE", "sign_multiplier": -1,
251
+ "line_label": "Revenue", "policy_evidence_ref": "SYNTH-POL-IS-REV",
252
+ },
253
+ {
254
+ "statement_section": "Cost of revenue", "note_number": "X01",
255
+ "subnote_code": "", "category": "COST_OF_REVENUE", "sign_multiplier": 1,
256
+ "line_label": "Cost of revenue", "policy_evidence_ref": "SYNTH-POL-IS-COST",
257
+ },
258
+ ],
259
+ "equity_movement_policies": [
260
+ {
261
+ "statement_section": "Share capital", "note_number": "E01",
262
+ "subnote_code": "", "equity_component": "SHARE_CAPITAL",
263
+ "movement_category": "CAPITAL_CONTRIBUTION", "sign_multiplier": -1,
264
+ "line_label": "Share capital", "policy_evidence_ref": "SYNTH-POL-EQ-CAP",
265
+ },
266
+ {
267
+ "statement_section": "Current year result", "note_number": "P01",
268
+ "subnote_code": "", "equity_component": "RETAINED_EARNINGS",
269
+ "movement_category": "NET_PROFIT_LOSS", "sign_multiplier": -1,
270
+ "line_label": "Current year profit", "policy_evidence_ref": "SYNTH-POL-EQ-PROFIT",
271
+ },
272
+ ],
273
+ "cash_accounts": [
274
+ {"account_code": "CASH", "evidence_ref": "SYNTH-CASH-SCOPE"},
275
+ ],
276
+ "counterpart_policies": [
277
+ {"account_code": "REV", "activity": "OPERATING", "evidence_ref": "SYNTH-REV-CLASS"},
278
+ {"account_code": "EXP", "activity": "OPERATING", "evidence_ref": "SYNTH-EXP-CLASS"},
279
+ ],
280
+ "transactions": [
281
+ asdict(item) for item in (
282
+ cash_transactions() if transactions is None else transactions
283
+ )
284
+ ],
285
+ "cash_balance_notes": [
286
+ {
287
+ "statement_section": "Current assets", "note_number": "C01",
288
+ "subnote_code": "", "evidence_ref": "SYNTH-CASH-NOTE-SELECTION",
289
+ },
290
+ ],
291
+ }
292
+
293
+
294
+
295
def test_all_five_real_builders_reconcile_from_one_immutable_workbook():
296
bs, income, equity, cash, trial, result = build_all()
297
@@ -285,3 +368,77 @@ def test_pipeline_result_is_nonofficial_and_freezes_reference_collection():
368
build_pipeline(refs=iter(references()))
369
assert error.value.code == "INVALID_FIVE_STATEMENT_NOTE_REFERENCES"
370
371
+
372
+def test_offline_pipeline_cli_emits_json_without_creating_output_files(tmp_path, capsys):
373
+ workbook_path = tmp_path / "synthetic.xlsx"
374
+ manifest_path = tmp_path / "pipeline.json"
375
+ workbook_path.write_bytes(source_workbook())
376
+ manifest_path.write_text(
377
+ json.dumps(pipeline_manifest(), ensure_ascii=True),
378
+ encoding="utf-8",
379
+ )
380
+
381
+ exit_code = pipeline_cli_main([
382
+ "--file", str(workbook_path), "--manifest", str(manifest_path),
383
+ ])
384
+ captured = capsys.readouterr()
385
+ payload = json.loads(captured.out)
386
+ assert exit_code == 0
387
+ assert payload["eligible_for_analyst_review"] is True
388
+ assert payload["kind"] == "UNAPPROVED_FIVE_STATEMENT_PIPELINE_CANDIDATE"
389
+ assert payload["official_financial_statement"] is False
390
+ assert payload["releasable"] is False
391
+ assert {item.name for item in tmp_path.iterdir()} == {
392
+ "synthetic.xlsx", "pipeline.json",
393
+ }
394
+
395
+
396
+def test_offline_pipeline_cli_returns_blocked_exit_for_incomplete_transactions(
397
+ tmp_path, capsys,
398
+):
399
+ workbook_path = tmp_path / "synthetic.xlsx"
400
+ manifest_path = tmp_path / "pipeline.json"
401
+ workbook_path.write_bytes(source_workbook())
402
+ manifest_path.write_text(
403
+ json.dumps(pipeline_manifest(transactions=cash_transactions()[:-1])),
404
+ encoding="utf-8",
405
+ )
406
+
407
+ exit_code = pipeline_cli_main([
408
+ "--file", str(workbook_path), "--manifest", str(manifest_path),
409
+ ])
410
+ payload = json.loads(capsys.readouterr().out)
411
+ assert exit_code == 3
412
+ assert payload["eligible_for_analyst_review"] is False
413
+ assert "CASH_FLOW_CANDIDATE_BLOCKED" in payload["findings"]
414
+
415
+
416
+def test_pipeline_manifest_rejects_duplicate_keys_and_nonfinite_numbers():
417
+ from aram_backend.five_statement_pipeline_cli import parse_pipeline_manifest
418
+
419
+ with pytest.raises(CoverageError) as duplicate:
420
+ parse_pipeline_manifest(b'{"schema_version":1,"schema_version":1}')
421
+ assert duplicate.value.code == "DUPLICATE_MANIFEST_KEY"
422
+
423
+ document = pipeline_manifest()
424
+ document["fiscal_year"] = float("nan")
425
+ with pytest.raises(CoverageError) as nonfinite:
426
+ parse_pipeline_manifest(json.dumps(document).encode("utf-8"))
427
+ assert nonfinite.value.code == "PIPELINE_MANIFEST_NONFINITE_NUMBER"
428
+
429
+
430
+def test_pipeline_manifest_rejects_unknown_fields_and_missing_cash_registry():
431
+ from aram_backend.five_statement_pipeline_cli import parse_pipeline_manifest
432
+
433
+ document = pipeline_manifest()
434
+ document["unexpected"] = True
435
+ with pytest.raises(CoverageError) as unknown:
436
+ parse_pipeline_manifest(json.dumps(document).encode("utf-8"))
437
+ assert unknown.value.code == "PIPELINE_MANIFEST_SCHEMA_INVALID"
438
+
439
+ document = pipeline_manifest()
440
+ document["cash_accounts"] = []
441
+ with pytest.raises(CoverageError) as missing:
442
+ parse_pipeline_manifest(json.dumps(document).encode("utf-8"))
443
+ assert missing.value.code == "PIPELINE_MANIFEST_CASH_ACCOUNTS_INVALID"
444
+