test(statements): exercise synthetic IS CLI, losses, file safety and hostile manifests
davoodmehraban89 committed
Oct 31, 2026 at 06:47 UTC
857b6ee268c1fcffb0d99e84447694a9b7b6fde9
1 file changed
+164
backend/tests/test_income_statement_candidate_cli.py
new
+164
@@ -0,0 +1,164 @@
1
+"""End-to-end synthetic CLI tests for unapproved IS preview and invalid inputs."""
2
+from __future__ import annotations
3
+
4
+import json
5
+
6
+import pytest
7
+
8
+from aram_backend.coverage_preview import CoverageError
9
+from aram_backend.income_statement_candidate_cli import main, parse_income_manifest
10
+from test_income_statement_candidate import workbook, references, policies
11
+
12
+
13
+def references_doc():
14
+ return {
15
+ "schema_version": 1, "company_code": "001", "fiscal_year": 1404,
16
+ "references": [
17
+ {
18
+ "statement": r.statement, "section": r.section,
19
+ "note_number": r.note_number, "subnote_code": r.subnote_code,
20
+ "expected_closing_exposure": r.expected_closing_exposure,
21
+ "evidence_ref": r.evidence_ref,
22
+ }
23
+ for r in references()
24
+ ],
25
+ }
26
+
27
+
28
+def policies_doc():
29
+ return {
30
+ "schema_version": 1, "company_code": "001", "fiscal_year": 1404,
31
+ "policies": [
32
+ {
33
+ "statement_section": p.statement_section,
34
+ "note_number": p.note_number, "subnote_code": p.subnote_code,
35
+ "category": p.category, "sign_multiplier": p.sign_multiplier,
36
+ "line_label": p.line_label, "policy_evidence_ref": p.policy_evidence_ref,
37
+ }
38
+ for p in policies()
39
+ ],
40
+ }
41
+
42
+
43
+def fixtures(tmp_path, *, refs=None, pols=None, data=None):
44
+ data = workbook() if data is None else data
45
+ ref = references_doc() if refs is None else refs
46
+ pol = policies_doc() if pols is None else pols
47
+ excel = tmp_path / "synthetic.xlsx"
48
+ control = tmp_path / "control.json"
49
+ policy = tmp_path / "policy.json"
50
+ excel.write_bytes(data)
51
+ control.write_text(json.dumps(ref), encoding="utf-8")
52
+ policy.write_text(json.dumps(pol), encoding="utf-8")
53
+ return excel, control, policy, data
54
+
55
+
56
+def argv(paths):
57
+ return [
58
+ "--file", str(paths[0]), "--references", str(paths[1]),
59
+ "--policies", str(paths[2]),
60
+ ]
61
+
62
+
63
+def test_cli_profit_is_unapproved_and_does_not_mutate_inputs(tmp_path, capsys):
64
+ original = workbook()
65
+ paths = fixtures(tmp_path, data=original)
66
+ assert main(argv(paths)) == 0
67
+ output = capsys.readouterr()
68
+ result = json.loads(output.out)
69
+ assert result["net_result"] == "45"
70
+ assert result["eligible_for_analyst_review"]
71
+ assert not result["official_financial_statement"]
72
+ assert not result["releasable"]
73
+ assert output.err == ""
74
+ assert paths[0].read_bytes() == original
75
+ assert sorted(item.name for item in tmp_path.iterdir()) == [
76
+ "control.json", "policy.json", "synthetic.xlsx",
77
+ ]
78
+
79
+
80
+def test_cli_loss_is_not_a_failure(tmp_path, capsys):
81
+ data = workbook(costs="290")
82
+ doc = references_doc()
83
+ next(r for r in doc["references"] if r["note_number"] == "C01")[
84
+ "expected_closing_exposure"
85
+ ] = "290"
86
+ paths = fixtures(tmp_path, refs=doc, data=data)
87
+ assert main(argv(paths)) == 0
88
+ assert json.loads(capsys.readouterr().out)["net_result"] == "-135"
89
+
90
+
91
+def test_cli_missing_policy_blocks_and_reports_findings(tmp_path, capsys):
92
+ doc = policies_doc()
93
+ doc["policies"].pop()
94
+ paths = fixtures(tmp_path, pols=doc)
95
+ assert main(argv(paths)) == 3
96
+ output = json.loads(capsys.readouterr().out)
97
+ assert "MISSING_INCOME_POLICY" in output["findings"]
98
+ assert not output["export_authorized"]
99
+
100
+
101
+def test_cli_company_mismatch_blocks_without_json_data(tmp_path, capsys):
102
+ doc = policies_doc()
103
+ doc["company_code"] = "002"
104
+ paths = fixtures(tmp_path, pols=doc)
105
+ assert main(argv(paths)) == 2
106
+ output = capsys.readouterr()
107
+ assert not output.out.strip()
108
+ assert "INCOME_SCOPE_MISMATCH" in output.err
109
+
110
+
111
+@pytest.mark.parametrize("raw", [
112
+ b"null", b"[]", b"{}", b"not-json", b"\xff",
113
+ b'{"schema_version":1,"schema_version":1}',
114
+ json.dumps({**policies_doc(), "unexpected": "value"}).encode("utf-8"),
115
+ json.dumps({**policies_doc(), "schema_version": True}).encode("utf-8"),
116
+ json.dumps({**policies_doc(), "company_code": " 001"}).encode("utf-8"),
117
+ json.dumps({**policies_doc(), "fiscal_year": True}).encode("utf-8"),
118
+ json.dumps({**policies_doc(), "policies": "not-list"}).encode("utf-8"),
119
+ json.dumps({**policies_doc(), "policies": [{"category": "REVENUE"}]}).encode("utf-8"),
120
+])
121
+def test_invalid_json_manifest_blocked(raw):
122
+ with pytest.raises(CoverageError):
123
+ parse_income_manifest(raw)
124
+
125
+
126
+def test_boolean_sign_cannot_pass_through_json(tmp_path, capsys):
127
+ doc = policies_doc()
128
+ doc["policies"][0]["sign_multiplier"] = True
129
+ paths = fixtures(tmp_path, pols=doc)
130
+ assert main(argv(paths)) == 2
131
+ assert capsys.readouterr().out == ""
132
+
133
+
134
+def test_symlink_to_policy_file_is_rejected(tmp_path, capsys):
135
+ paths = fixtures(tmp_path)
136
+ link = tmp_path / "alias.json"
137
+ try:
138
+ link.symlink_to(paths[2])
139
+ except (OSError, NotImplementedError):
140
+ pytest.skip("Symlinks unavailable")
141
+ params = argv(paths)
142
+ params[-1] = str(link)
143
+ assert main(params) == 2
144
+ assert capsys.readouterr().out == ""
145
+
146
+
147
+def test_invalid_excel_rejected_before_processing(tmp_path, capsys):
148
+ paths = fixtures(tmp_path, data=b"broken")
149
+ assert main(argv(paths)) == 2
150
+ assert capsys.readouterr().out == ""
151
+
152
+
153
+def test_missing_reference_file_is_rejected(tmp_path, capsys):
154
+ paths = fixtures(tmp_path)
155
+ paths[1].unlink()
156
+ assert main(argv(paths)) == 2
157
+ assert capsys.readouterr().out == ""
158
+
159
+
160
+def test_manifest_size_limit_applies_before_json_parsing():
161
+ from aram_backend.reconciliation_review_cli import MAX_MANIFEST_BYTES
162
+ with pytest.raises(CoverageError) as exc:
163
+ parse_income_manifest(b" " * (MAX_MANIFEST_BYTES + 1))
164
+ assert exc.value.code == "INCOME_MANIFEST_SIZE_INVALID"