Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 3, 20:45 UTC
test(statements): exercise BS CLI, malicious manifests, scope, sign and nonmutation
davoodmehraban89 committed
Oct 31, 2026 at 00:38 UTC
fb021a5acee810bdf2d0d4c98b173ec1eb4570b0
1 file changed
+167
backend/tests/test_balance_sheet_candidate_cli.py
new
+167
@@ -0,0 +1,167 @@
1
+"""Synthetic end-to-end tests of the non-official BS CLI."""
2
+from __future__ import annotations
3
+
4
+import json
5
+
6
+import pytest
7
+
8
+from aram_backend.balance_sheet_candidate_cli import (
9
+ main, parse_presentation_manifest,
10
+)
11
+from aram_backend.coverage_preview import CoverageError
12
+from test_balance_sheet_candidate import workbook, policies, references
13
+
14
+
15
+def reference_document():
16
+ return {
17
+ "schema_version": 1, "company_code": "001", "fiscal_year": 1404,
18
+ "references": [
19
+ {
20
+ "statement": r.statement, "section": r.section,
21
+ "note_number": r.note_number, "subnote_code": r.subnote_code,
22
+ "expected_closing_exposure": r.expected_closing_exposure,
23
+ "evidence_ref": r.evidence_ref,
24
+ }
25
+ for r in references()
26
+ ],
27
+ }
28
+
29
+
30
+def policy_document():
31
+ return {
32
+ "schema_version": 1, "company_code": "001", "fiscal_year": 1404,
33
+ "policies": [
34
+ {
35
+ "statement_section": p.statement_section,
36
+ "note_number": p.note_number,
37
+ "subnote_code": p.subnote_code, "category": p.category,
38
+ "sign_multiplier": p.sign_multiplier, "line_label": p.line_label,
39
+ "policy_evidence_ref": p.policy_evidence_ref,
40
+ }
41
+ for p in policies()
42
+ ],
43
+ }
44
+
45
+
46
+def files(tmp_path, *, reference=None, presentation=None, source=None):
47
+ source_bytes = workbook() if source is None else source
48
+ ref_doc = reference_document() if reference is None else reference
49
+ policy_doc = policy_document() if presentation is None else presentation
50
+ file = tmp_path / "sample.xlsx"
51
+ reference_file = tmp_path / "controls.json"
52
+ policy_file = tmp_path / "presentation.json"
53
+ file.write_bytes(source_bytes)
54
+ reference_file.write_text(json.dumps(ref_doc), encoding="utf-8")
55
+ policy_file.write_text(json.dumps(policy_doc), encoding="utf-8")
56
+ return file, reference_file, policy_file, source_bytes
57
+
58
+
59
+def command(files):
60
+ file, refs, pol, _ = files
61
+ return [
62
+ "--file", str(file), "--references", str(refs),
63
+ "--policies", str(pol),
64
+ ]
65
+
66
+
67
+def test_cli_presents_three_sections_but_no_official_document(tmp_path, capsys):
68
+ input_files = files(tmp_path)
69
+ initial = input_files[3]
70
+ assert main(command(input_files)) == 0
71
+ output = capsys.readouterr()
72
+ data = json.loads(output.out)
73
+ assert data["assets_total"] == "100"
74
+ assert data["liabilities_total"] == "60"
75
+ assert data["equity_total"] == "40"
76
+ assert data["eligible_for_analyst_review"]
77
+ assert not data["releasable"]
78
+ assert not data["official_financial_statement"]
79
+ assert output.err == ""
80
+ assert input_files[0].read_bytes() == initial
81
+ assert sorted(p.name for p in tmp_path.iterdir()) == [
82
+ "controls.json", "presentation.json", "sample.xlsx"
83
+ ]
84
+
85
+
86
+def test_cli_unbalanced_returns_code_three_with_findings(tmp_path, capsys):
87
+ xlsx = workbook(liability="-59")
88
+ controls = reference_document()
89
+ controls["references"][1]["expected_closing_exposure"] = "-59"
90
+ inputs = files(tmp_path, source=xlsx, reference=controls)
91
+ assert main(command(inputs)) == 3
92
+ result = json.loads(capsys.readouterr().out)
93
+ assert result["balance_difference"] == "1"
94
+ assert "BALANCE_SHEET_DOES_NOT_BALANCE" in result["findings"]
95
+ assert not result["export_authorized"]
96
+
97
+
98
+def test_cli_policy_scope_mismatch_fails_without_stdout(tmp_path, capsys):
99
+ doc = policy_document()
100
+ doc["fiscal_year"] = 1403
101
+ inputs = files(tmp_path, presentation=doc)
102
+ assert main(command(inputs)) == 2
103
+ output = capsys.readouterr()
104
+ assert not output.out.strip()
105
+ assert "PRESENTATION_SCOPE_MISMATCH" in output.err
106
+
107
+
108
+@pytest.mark.parametrize("content", [
109
+ b"[]", b"{}", b"not json", b"null", b"\xff",
110
+ b'{"schema_version":1,"schema_version":1}',
111
+ json.dumps({**policy_document(), "extra": "oops"}).encode("utf-8"),
112
+ json.dumps({**policy_document(), "schema_version": True}).encode("utf-8"),
113
+ json.dumps({**policy_document(), "company_code": " 001"}).encode("utf-8"),
114
+ json.dumps({**policy_document(), "fiscal_year": True}).encode("utf-8"),
115
+ json.dumps({**policy_document(), "policies": "bad"}).encode("utf-8"),
116
+ json.dumps({**policy_document(), "policies": [{}]}).encode("utf-8"),
117
+])
118
+def test_bad_policy_manifest_is_rejected(content):
119
+ with pytest.raises(CoverageError):
120
+ parse_presentation_manifest(content)
121
+
122
+
123
+def test_presentation_sign_boolean_rejected_by_engine(tmp_path, capsys):
124
+ doc = policy_document()
125
+ doc["policies"][0]["sign_multiplier"] = True
126
+ inputs = files(tmp_path, presentation=doc)
127
+ assert main(command(inputs)) == 2
128
+ output = capsys.readouterr()
129
+ assert not output.out.strip()
130
+ assert "INVALID_PRESENTATION_SIGN" in output.err
131
+
132
+
133
+def test_extra_policy_and_missing_policy_block_not_silently_ignored(tmp_path, capsys):
134
+ doc = policy_document()
135
+ doc["policies"].pop()
136
+ inputs = files(tmp_path, presentation=doc)
137
+ assert main(command(inputs)) == 3
138
+ result = json.loads(capsys.readouterr().out)
139
+ assert "MISSING_EXPLICIT_PRESENTATION_POLICY" in result["findings"]
140
+
141
+
142
+def test_missing_xlsx_blocks_safely(tmp_path, capsys):
143
+ inputs = files(tmp_path)
144
+ inputs[0].unlink()
145
+ assert main(command(inputs)) == 2
146
+ assert capsys.readouterr().out == ""
147
+
148
+
149
+def test_policy_symlink_is_rejected(tmp_path, capsys):
150
+ inputs = files(tmp_path)
151
+ link = tmp_path / "policy_link.json"
152
+ try:
153
+ link.symlink_to(inputs[2])
154
+ except (OSError, NotImplementedError):
155
+ pytest.skip("Symlink creation unsupported")
156
+ args = command(inputs)
157
+ args[-1] = str(link)
158
+ assert main(args) == 2
159
+ assert capsys.readouterr().out == ""
160
+
161
+
162
+def test_reference_file_scope_mismatch_no_fallback(tmp_path, capsys):
163
+ controls = reference_document()
164
+ controls["company_code"] = "999"
165
+ inputs = files(tmp_path, reference=controls)
166
+ assert main(command(inputs)) == 2
167
+ assert capsys.readouterr().out == ""