test(statements): cover cash flow CLI manifests, scope, limits and safe reads
davoodmehraban89 committed
Oct 31, 2026 at 08:24 UTC
f1eaf5b0ce41cd9dd5365ae14eb676f9165abd24
1 file changed
+174
backend/tests/test_cash_flow_direct_candidate_cli.py
new
+174
@@ -0,0 +1,174 @@
1
+"""Synthetic CLI tests for direct cash flow manifests and local file safety."""
2
+from __future__ import annotations
3
+
4
+import json
5
+
6
+import pytest
7
+
8
+from aram_backend.cash_flow_direct_candidate_cli import (
9
+ MAX_CASH_MANIFEST_BYTES, main, parse_cash_manifest,
10
+)
11
+from aram_backend.coverage_preview import CoverageError
12
+from test_cash_flow_direct_candidate import (
13
+ cash_accounts, counterpart_policies, transactions, workbook,
14
+)
15
+
16
+
17
+def cash_manifest():
18
+ return {
19
+ "schema_version": 1,
20
+ "company_code": "001",
21
+ "fiscal_year": 1404,
22
+ "currency": "IRR",
23
+ "unit": "Rial",
24
+ "cash_accounts": [
25
+ {"account_code": p.account_code, "evidence_ref": p.evidence_ref}
26
+ for p in cash_accounts()
27
+ ],
28
+ "counterpart_policies": [
29
+ {"account_code": p.account_code, "activity": p.activity,
30
+ "evidence_ref": p.evidence_ref}
31
+ for p in counterpart_policies()
32
+ ],
33
+ "transactions": [
34
+ {
35
+ "company_code": t.company_code, "fiscal_year": t.fiscal_year,
36
+ "line_id": t.line_id, "transaction_id": t.transaction_id,
37
+ "posting_date": t.posting_date,
38
+ "cash_account_code": t.cash_account_code,
39
+ "counterpart_account_code": t.counterpart_account_code,
40
+ "cash_debit": t.cash_debit, "cash_credit": t.cash_credit,
41
+ "counterpart_debit": t.counterpart_debit,
42
+ "counterpart_credit": t.counterpart_credit,
43
+ "evidence_ref": t.evidence_ref,
44
+ }
45
+ for t in transactions()
46
+ ],
47
+ }
48
+
49
+
50
+def files(tmp_path, *, manifest=None, data=None):
51
+ data = workbook() if data is None else data
52
+ manifest = cash_manifest() if manifest is None else manifest
53
+ xlsx = tmp_path / "synthetic.xlsx"
54
+ json_file = tmp_path / "cash-manifest.json"
55
+ xlsx.write_bytes(data)
56
+ json_file.write_text(json.dumps(manifest), encoding="utf-8")
57
+ return xlsx, json_file, data
58
+
59
+
60
+def command(paths):
61
+ return ["--file", str(paths[0]), "--manifest", str(paths[1])]
62
+
63
+
64
+def test_cli_direct_cash_flow_json_and_source_are_unchanged(tmp_path, capsys):
65
+ paths = files(tmp_path)
66
+ original = paths[2]
67
+ assert main(command(paths)) == 0
68
+ output = capsys.readouterr()
69
+ result = json.loads(output.out)
70
+ assert result["operating_net_cash_flow"] == "10"
71
+ assert result["investing_net_cash_flow"] == "-10"
72
+ assert result["financing_net_cash_flow"] == "10"
73
+ assert result["net_change_in_cash"] == "10"
74
+ assert result["closing_cash_and_equivalents"] == "130"
75
+ assert result["eligible_for_analyst_review"]
76
+ assert not result["official_financial_statement"]
77
+ assert not result["releasable"]
78
+ assert output.err == ""
79
+ assert paths[0].read_bytes() == original
80
+ assert sorted(p.name for p in tmp_path.iterdir()) == [
81
+ "cash-manifest.json", "synthetic.xlsx"
82
+ ]
83
+
84
+
85
+def test_cli_blocked_transaction_manifest_returns_three(tmp_path, capsys):
86
+ manifest = cash_manifest()
87
+ manifest["transactions"].pop()
88
+ paths = files(tmp_path, manifest=manifest)
89
+ assert main(command(paths)) == 3
90
+ result = json.loads(capsys.readouterr().out)
91
+ assert not result["eligible_for_analyst_review"]
92
+ assert "CASH_ACCOUNT_CREDIT_TURNOVER_MISMATCH" in result["findings"]
93
+
94
+
95
+def test_cli_wrong_transaction_scope_returns_two_without_stdout(tmp_path, capsys):
96
+ manifest = cash_manifest()
97
+ manifest["transactions"][0]["company_code"] = "002"
98
+ paths = files(tmp_path, manifest=manifest)
99
+ assert main(command(paths)) == 2
100
+ output = capsys.readouterr()
101
+ assert output.out == ""
102
+ assert "CASH_FLOW_TRANSACTION_SCOPE_MISMATCH" in output.err
103
+
104
+
105
+@pytest.mark.parametrize("raw", [
106
+ b"null", b"[]", b"{}", b"not-json", b"\xff",
107
+ b'{"schema_version":1,"schema_version":1}',
108
+ json.dumps({**cash_manifest(), "extra": True}).encode("utf-8"),
109
+ json.dumps({**cash_manifest(), "schema_version": True}).encode("utf-8"),
110
+ json.dumps({**cash_manifest(), "company_code": " 001"}).encode("utf-8"),
111
+ json.dumps({**cash_manifest(), "fiscal_year": True}).encode("utf-8"),
112
+ json.dumps({**cash_manifest(), "cash_accounts": "bad"}).encode("utf-8"),
113
+ json.dumps({**cash_manifest(), "cash_accounts": [{}]}).encode("utf-8"),
114
+ json.dumps({**cash_manifest(), "counterpart_policies": [{}]}).encode("utf-8"),
115
+ json.dumps({**cash_manifest(), "transactions": [{}]}).encode("utf-8"),
116
+])
117
+def test_invalid_cash_manifest_schema_fails_closed(raw):
118
+ with pytest.raises(CoverageError):
119
+ parse_cash_manifest(raw)
120
+
121
+
122
+def test_duplicate_nested_transaction_key_fails_closed():
123
+ raw = (
124
+ '{"schema_version":1,"company_code":"001","fiscal_year":1404,'
125
+ '"currency":"IRR","unit":"Rial","cash_accounts":[],"counterpart_policies":[],'
126
+ '"transactions":[{"line_id":"A","line_id":"B"}]}'
127
+ ).encode("utf-8")
128
+ with pytest.raises(CoverageError) as error:
129
+ parse_cash_manifest(raw)
130
+ assert error.value.code == "DUPLICATE_MANIFEST_KEY"
131
+
132
+
133
+def test_manifest_transaction_limit_is_enforced():
134
+ manifest = cash_manifest()
135
+ manifest["transactions"] = manifest["transactions"] * 5001
136
+ with pytest.raises(CoverageError) as error:
137
+ parse_cash_manifest(json.dumps(manifest).encode("utf-8"))
138
+ assert error.value.code == "CASH_MANIFEST_TRANSACTIONS_INVALID"
139
+
140
+
141
+def test_manifest_size_limit_is_enforced():
142
+ with pytest.raises(CoverageError) as error:
143
+ parse_cash_manifest(b" " * (MAX_CASH_MANIFEST_BYTES + 1))
144
+ assert error.value.code == "CASH_MANIFEST_SIZE_INVALID"
145
+
146
+
147
+def test_symlink_manifest_rejected(tmp_path, capsys):
148
+ paths = files(tmp_path)
149
+ alias = tmp_path / "alias.json"
150
+ try:
151
+ alias.symlink_to(paths[1])
152
+ except (OSError, NotImplementedError):
153
+ pytest.skip("Symlinks unavailable")
154
+ args = command(paths)
155
+ args[-1] = str(alias)
156
+ assert main(args) == 2
157
+ assert capsys.readouterr().out == ""
158
+
159
+
160
+def test_missing_manifest_or_workbook_rejected(tmp_path, capsys):
161
+ paths = files(tmp_path)
162
+ paths[1].unlink()
163
+ assert main(command(paths)) == 2
164
+ assert capsys.readouterr().out == ""
165
+ paths = files(tmp_path)
166
+ paths[0].unlink()
167
+ assert main(command(paths)) == 2
168
+ assert capsys.readouterr().out == ""
169
+
170
+
171
+def test_invalid_workbook_rejected_before_json_success(tmp_path, capsys):
172
+ paths = files(tmp_path, data=b"broken")
173
+ assert main(command(paths)) == 2
174
+ assert capsys.readouterr().out == ""