@davoodmehraban89 / Aram-1 / commits / 50b4ec9
Fork 0

test(coverage): verify CLI file safety, nonrelease output and scope isolation

davoodmehraban89 committed Oct 31, 2026 at 00:19 UTC 50b4ec9746489b45b3638cd14b85689c99f7a527
1 file changed +125
backend/tests/test_coverage_preview_cli.py new
+125
@@ -0,0 +1,125 @@
1 +"""Safety and integration tests for the read-only CLI; synthetic files only."""
2 +from __future__ import annotations
3 +
4 +import json
5 +from io import BytesIO
6 +
7 +import pytest
8 +from openpyxl import Workbook
9 +
10 +from aram_backend.coverage_preview import CoverageError, MAX_INPUT_BYTES
11 +from aram_backend.coverage_preview_cli import main, safe_read_workbook
12 +from aram_backend.pilot_workbook import SHEET_HEADERS
13 +
14 +
15 +def synthetic_book() -> bytes:
16 + book = Workbook()
17 + book.remove(book.active)
18 + for name, columns in SHEET_HEADERS.items():
19 + book.create_sheet(name).append(list(columns))
20 +
21 + def add(name, **values):
22 + book[name].append([values.get(column) for column in SHEET_HEADERS[name]])
23 +
24 + add("Company", company_code="001", company_name="Synthetic",
25 + fiscal_year=1404, status="Draft")
26 + add("Accounts", company_code="001", fiscal_year=1404,
27 + general_code="01", general_name="Assets", account_full_code="01",
28 + account_name="Cash", opening="100", debit="0", credit="0",
29 + closing="100", source_row=2, row_status="Draft", closing_delta="0")
30 + add("Notes", company_code="001", fiscal_year=1404, statement="BS",
31 + statement_section="Assets", note_number="N1", note_title="Cash")
32 + add("Mapping", mapping_id="M1", company_code="001", fiscal_year=1404,
33 + account_full_code="01", account_level="General", general_code="01",
34 + target_statement="BS", statement_section="Assets",
35 + note_number="N1", mapping_version=1, approval_status="Approved",
36 + reviewer="Synthetic reviewer", review_date="2026-10-31",
37 + validation_status="Valid", source="Manual")
38 + buffer = BytesIO()
39 + book.save(buffer)
40 + book.close()
41 + return buffer.getvalue()
42 +
43 +
44 +def test_safe_read_preserves_bytes(tmp_path):
45 + data = synthetic_book()
46 + file = tmp_path / "pilot.xlsx"
47 + file.write_bytes(data)
48 + assert safe_read_workbook(str(file)) == data
49 + assert file.read_bytes() == data
50 +
51 +
52 +def test_cli_outputs_explicitly_unapproved_json(tmp_path, capsys):
53 + file = tmp_path / "pilot.xlsx"
54 + file.write_bytes(synthetic_book())
55 + assert main(["--file", str(file), "--company-code", "001",
56 + "--fiscal-year", "1404"]) == 0
57 + result = json.loads(capsys.readouterr().out)
58 + assert result["candidate_mapped_accounts"] == 1
59 + assert result["source_closing_exposure"] == "100"
60 + assert result["releasable"] is False
61 + assert result["trusted_approvals"] is False
62 + assert result["financial_statement"] is False
63 +
64 +
65 +@pytest.mark.parametrize("name,body", [
66 + ("pilot.csv", b"content"),
67 + ("bad.xlsx.exe", b"content"),
68 + ("empty.xlsx", b""),
69 +])
70 +def test_rejects_disallowed_or_empty_file(tmp_path, name, body):
71 + file = tmp_path / name
72 + file.write_bytes(body)
73 + with pytest.raises(CoverageError):
74 + safe_read_workbook(str(file))
75 +
76 +
77 +def test_rejects_missing_path_and_directory(tmp_path):
78 + with pytest.raises(CoverageError):
79 + safe_read_workbook(str(tmp_path / "absent.xlsx"))
80 + folder = tmp_path / "folder.xlsx"
81 + folder.mkdir()
82 + with pytest.raises(CoverageError):
83 + safe_read_workbook(str(folder))
84 +
85 +
86 +def test_rejects_file_beyond_limit_without_full_read(tmp_path):
87 + file = tmp_path / "huge.xlsx"
88 + with file.open("wb") as stream:
89 + stream.truncate(MAX_INPUT_BYTES + 1)
90 + with pytest.raises(CoverageError) as exc:
91 + safe_read_workbook(str(file))
92 + assert exc.value.code == "WORKBOOK_SIZE_INVALID"
93 +
94 +
95 +def test_symlink_never_followed(tmp_path):
96 + target = tmp_path / "good.xlsx"
97 + target.write_bytes(synthetic_book())
98 + link = tmp_path / "alias.xlsx"
99 + try:
100 + link.symlink_to(target)
101 + except (NotImplementedError, OSError):
102 + pytest.skip("Symlink creation unavailable")
103 + with pytest.raises(CoverageError) as exc:
104 + safe_read_workbook(str(link))
105 + assert exc.value.code == "NOT_REGULAR_FILE"
106 +
107 +
108 +def test_cli_invalid_workbook_has_no_stdout_success(tmp_path, capsys):
109 + file = tmp_path / "invalid.xlsx"
110 + file.write_bytes(b"not a workbook")
111 + assert main(["--file", str(file), "--company-code", "001",
112 + "--fiscal-year", "1404"]) == 2
113 + result = capsys.readouterr()
114 + assert not result.out.strip()
115 + assert "blocked" in result.err
116 +
117 +
118 +def test_wrong_company_never_falls_back(tmp_path, capsys):
119 + file = tmp_path / "pilot.xlsx"
120 + file.write_bytes(synthetic_book())
121 + assert main(["--file", str(file), "--company-code", "002",
122 + "--fiscal-year", "1404"]) == 2
123 + captured = capsys.readouterr()
124 + assert not captured.out.strip()
125 + assert "COMPANY_SCOPE_NOT_FOUND" in captured.err