@davoodmehraban89 / Aram-1 / commits / aef9852
Fork 0

test(cli): reject invalid financial amounts, forged signs and mismatched transaction scope

davoodmehraban89 committed Oct 31, 2026 at 18:05 UTC aef9852735c1aebc0d4e66c9e334b025df6e3f97
1 file changed +122
backend/tests/test_five_statement_manifest_strictness.py new
+122
@@ -0,0 +1,122 @@
1 +"""Adversarial validation of the strictly typed five-statement JSON manifest.
2 +
3 +Synthetic data only. No financial documents or account information in fixtures.
4 +"""
5 +from __future__ import annotations
6 +
7 +import json
8 +from copy import deepcopy
9 +
10 +import pytest
11 +
12 +from aram_backend.coverage_preview import CoverageError
13 +from aram_backend.five_statement_pipeline_cli import parse_pipeline_manifest
14 +from test_five_statement_pipeline_integration import pipeline_manifest
15 +
16 +
17 +def parse(document):
18 + return parse_pipeline_manifest(json.dumps(document, ensure_ascii=True).encode("utf-8"))
19 +
20 +
21 +def test_synthetic_manifest_is_accepted_with_exact_money_strings_and_signs():
22 + document = pipeline_manifest()
23 + result = parse(document)
24 + assert result.company_code == "001"
25 + assert result.fiscal_year == 1404
26 + assert result.references[0].expected_closing_exposure == "50"
27 + assert result.balance_sheet_policies[0].sign_multiplier == 1
28 + assert result.transactions[0].cash_debit == "30"
29 +
30 +
31 +@pytest.mark.parametrize("section, index, field, bad", [
32 + ("references", 0, "expected_closing_exposure", 50),
33 + ("references", 0, "expected_closing_exposure", True),
34 + ("references", 0, "expected_closing_exposure", "NaN"),
35 + ("references", 0, "expected_closing_exposure", "Infinity"),
36 + ("references", 0, "expected_closing_exposure", "1e200"),
37 + ("references", 0, "expected_closing_exposure", " 50"),
38 + ("transactions", 0, "cash_debit", 30),
39 + ("transactions", 0, "cash_debit", False),
40 + ("transactions", 0, "cash_debit", "NaN"),
41 + ("transactions", 0, "cash_debit", "1e500"),
42 + ("transactions", 0, "cash_credit", 0.0),
43 + ("transactions", 0, "counterpart_credit", " 30"),
44 + ("balance_sheet_policies", 0, "sign_multiplier", True),
45 + ("balance_sheet_policies", 0, "sign_multiplier", 1.0),
46 + ("income_statement_policies", 0, "sign_multiplier", "1"),
47 + ("equity_movement_policies", 0, "sign_multiplier", 0),
48 + ("equity_movement_policies", 0, "sign_multiplier", False),
49 +])
50 +def test_invalid_amounts_or_signs_fail_before_engine_runs(section, index, field, bad):
51 + document = pipeline_manifest()
52 + document[section][index][field] = bad
53 + with pytest.raises(CoverageError) as exc:
54 + parse(document)
55 + assert exc.value.code == "PIPELINE_MANIFEST_" + section.upper() + "_ROW_INVALID"
56 +
57 +
58 +@pytest.mark.parametrize("section, field, bad", [
59 + ("references", "statement", None),
60 + ("references", "note_number", ""),
61 + ("references", "evidence_ref", "REF\nBAD"),
62 + ("balance_sheet_policies", "line_label", ""),
63 + ("balance_sheet_policies", "category", 3),
64 + ("income_statement_policies", "policy_evidence_ref", " "),
65 + ("equity_movement_policies", "movement_category", False),
66 + ("cash_accounts", "account_code", ""),
67 + ("cash_accounts", "evidence_ref", "REF\tBAD"),
68 + ("counterpart_policies", "activity", None),
69 + ("counterpart_policies", "evidence_ref", " SRC "),
70 + ("transactions", "line_id", 1),
71 + ("transactions", "posting_date", ""),
72 + ("transactions", "evidence_ref", "SRC\nBAD"),
73 + ("cash_balance_notes", "statement_section", None),
74 + ("cash_balance_notes", "note_number", ""),
75 + ("cash_balance_notes", "evidence_ref", " "),
76 +])
77 +def test_invalid_identity_and_evidence_fields_fail_closed(section, field, bad):
78 + document = pipeline_manifest()
79 + document[section][0][field] = bad
80 + with pytest.raises(CoverageError) as exc:
81 + parse(document)
82 + assert exc.value.code == "PIPELINE_MANIFEST_" + section.upper() + "_ROW_INVALID"
83 +
84 +
85 +@pytest.mark.parametrize("field, bad", [
86 + ("company_code", "002"),
87 + ("company_code", " 001 "),
88 + ("company_code", None),
89 + ("fiscal_year", 1403),
90 + ("fiscal_year", "1404"),
91 + ("fiscal_year", True),
92 +])
93 +def test_transaction_scope_mismatch_is_rejected(field, bad):
94 + document = pipeline_manifest()
95 + document["transactions"][0][field] = bad
96 + with pytest.raises(CoverageError) as exc:
97 + parse(document)
98 + assert exc.value.code == "PIPELINE_MANIFEST_TRANSACTION_SCOPE_MISMATCH"
99 +
100 +
101 +def test_zero_and_negative_reference_values_remain_exact_strings():
102 + document = pipeline_manifest()
103 + document["references"][0]["expected_closing_exposure"] = "-0.0001"
104 + document["transactions"][0]["cash_debit"] = "0.0000000001"
105 + parsed = parse(document)
106 + assert parsed.references[0].expected_closing_exposure == "-0.0001"
107 + assert parsed.transactions[0].cash_debit == "0.0000000001"
108 +
109 +
110 +def test_subnote_code_remains_optionally_blank_without_affecting_validation():
111 + document = pipeline_manifest()
112 + assert all(item["subnote_code"] == "" for item in document["references"])
113 + parsed = parse(document)
114 + assert len(parsed.references) == len(document["references"])
115 +
116 +
117 +def test_manifest_fields_are_not_mutated_during_parsing():
118 + original = pipeline_manifest()
119 + snapshot = deepcopy(original)
120 + parsed = parse(original)
121 + assert original == snapshot
122 + assert parsed.company_code == snapshot["company_code"]