@davoodmehraban89 / Aram-1 / commits / 6c81e86
Fork 0

test(review): exercise CLI exit codes, reference JSON hostility, symlinks and nonmutation

davoodmehraban89 committed Oct 31, 2026 at 00:26 UTC 6c81e86943ba824add79aea234cef6fc2e1f8333
1 file changed +141
backend/tests/test_reconciliation_review_cli.py new
+141
@@ -0,0 +1,141 @@
1 +"""CLI tests use only ephemeral synthetic workbooks and control references."""
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.reconciliation_review_cli import (
10 + MAX_MANIFEST_BYTES, main, parse_manifest, safe_read_manifest,
11 +)
12 +from test_reconciliation_review import workbook
13 +
14 +
15 +def manifest(*, expected="126.00", company="001", year=1404):
16 + return {
17 + "schema_version": 1, "company_code": company, "fiscal_year": year,
18 + "references": [{
19 + "statement": "BS", "section": "Assets",
20 + "note_number": "N01", "subnote_code": "",
21 + "expected_closing_exposure": expected,
22 + "evidence_ref": "SYNTHETIC-REFERENCE-1",
23 + }],
24 + }
25 +
26 +
27 +def run(tmp_path, capsys, *, expected="126.00", company="001", data=None):
28 + xlsx = tmp_path / "synthetic.xlsx"
29 + controls = tmp_path / "synthetic.json"
30 + xlsx.write_bytes(workbook() if data is None else data)
31 + controls.write_text(json.dumps(manifest(expected=expected, company=company)),
32 + encoding="utf-8")
33 + code = main(["--file", str(xlsx), "--references", str(controls)])
34 + captured = capsys.readouterr()
35 + return code, captured, xlsx, controls
36 +
37 +
38 +def test_cli_matched_is_still_unapproved(tmp_path, capsys):
39 + code, captured, xlsx, controls = run(tmp_path, capsys)
40 + assert code == 0
41 + result = json.loads(captured.out)
42 + assert result["matched_for_analyst_review"]
43 + assert not result["approved"]
44 + assert not result["releasable"]
45 + assert len(result["reference_sha256"]) == 64
46 + assert captured.err == ""
47 + assert xlsx.read_bytes() == workbook()
48 + assert json.loads(controls.read_text()) == manifest()
49 +
50 +
51 +def test_cli_mismatch_returns_three_and_json_findings(tmp_path, capsys):
52 + code, captured, _, _ = run(tmp_path, capsys, expected="127.00")
53 + assert code == 3
54 + result = json.loads(captured.out)
55 + assert not result["matched_for_analyst_review"]
56 + assert any(f["code"] == "NOTE_EXPOSURE_MISMATCH" for f in result["findings"])
57 +
58 +
59 +def test_cli_wrong_scope_returns_two_without_leaking_workbook(tmp_path, capsys):
60 + code, captured, _, _ = run(tmp_path, capsys, company="999")
61 + assert code == 2
62 + assert captured.out == ""
63 + assert "COMPANY_SCOPE_NOT_FOUND" in captured.err
64 +
65 +
66 +def test_duplicate_json_keys_rejected():
67 + content = b'{"schema_version":1,"schema_version":1}'
68 + with pytest.raises(CoverageError) as error:
69 + parse_manifest(content)
70 + assert error.value.code == "DUPLICATE_MANIFEST_KEY"
71 +
72 +
73 +@pytest.mark.parametrize("content", [
74 + b"[]", b"null", b"{}", b"{not json", b"\xff",
75 + json.dumps({**manifest(), "extra": 1}).encode("utf-8"),
76 + json.dumps({**manifest(), "schema_version": True}).encode("utf-8"),
77 + json.dumps({**manifest(), "fiscal_year": True}).encode("utf-8"),
78 + json.dumps({**manifest(), "references": "not list"}).encode("utf-8"),
79 + json.dumps({**manifest(), "references": [{"note_number": "N01"}]}).encode("utf-8"),
80 +])
81 +def test_invalid_manifest_blocks(content):
82 + with pytest.raises(CoverageError):
83 + parse_manifest(content)
84 +
85 +
86 +def test_large_manifest_bytes_rejected():
87 + with pytest.raises(CoverageError) as error:
88 + parse_manifest(b" " * (MAX_MANIFEST_BYTES + 1))
89 + assert error.value.code == "MANIFEST_SIZE_INVALID"
90 +
91 +
92 +def test_cli_invalid_reference_type_blocks_safely(tmp_path, capsys):
93 + xlsx = tmp_path / "synthetic.xlsx"
94 + controls = tmp_path / "synthetic.json"
95 + xlsx.write_bytes(workbook())
96 + payload = manifest()
97 + payload["references"][0]["expected_closing_exposure"] = 126.0
98 + controls.write_text(json.dumps(payload), encoding="utf-8")
99 + assert main(["--file", str(xlsx), "--references", str(controls)]) == 2
100 + assert capsys.readouterr().out == ""
101 +
102 +
103 +def test_manifest_symlink_rejected(tmp_path):
104 + target = tmp_path / "reference.json"
105 + target.write_text(json.dumps(manifest()), encoding="utf-8")
106 + alias = tmp_path / "alias.json"
107 + try:
108 + alias.symlink_to(target)
109 + except (OSError, NotImplementedError):
110 + pytest.skip("No symlinks on this filesystem")
111 + with pytest.raises(CoverageError) as error:
112 + safe_read_manifest(str(alias))
113 + assert error.value.code == "MANIFEST_NOT_REGULAR_FILE"
114 +
115 +
116 +def test_manifest_directory_and_missing_path_are_rejected(tmp_path):
117 + folder = tmp_path / "folder.json"
118 + folder.mkdir()
119 + with pytest.raises(CoverageError):
120 + safe_read_manifest(str(folder))
121 + with pytest.raises(CoverageError):
122 + safe_read_manifest(str(tmp_path / "missing.json"))
123 +
124 +
125 +def test_manifest_size_checked_before_read(tmp_path):
126 + source = tmp_path / "large.json"
127 + with source.open("wb") as stream:
128 + stream.truncate(MAX_MANIFEST_BYTES + 1)
129 + with pytest.raises(CoverageError) as error:
130 + safe_read_manifest(str(source))
131 + assert error.value.code == "MANIFEST_SIZE_INVALID"
132 +
133 +
134 +def test_no_snapshot_written_or_input_edited_on_mismatch(tmp_path, capsys):
135 + code, _, xlsx, controls = run(tmp_path, capsys, expected="-100.00")
136 + assert code == 3
137 + assert sorted(path.name for path in tmp_path.iterdir()) == [
138 + "synthetic.json", "synthetic.xlsx",
139 + ]
140 + assert xlsx.read_bytes() == workbook()
141 + assert json.loads(controls.read_text()) == manifest(expected="-100.00")