@davoodmehraban89 / Aram / commits / 9909503
Fork 0

test(export): cover review pack content, literal strings and safe no-overwrite writes

davoodmehraban89 committed Oct 31, 2026 at 22:02 UTC 99095035dfd8d280af8e9c929075e3a4ffa16c40
1 file changed +126
backend/tests/test_analyst_review_workbook.py new
+126
@@ -0,0 +1,126 @@
1 +"""Safety and content tests for the nonofficial local analyst workbook."""
2 +from dataclasses import replace
3 +
4 +import pytest
5 +from openpyxl import load_workbook
6 +
7 +from aram_backend.analyst_review_workbook import (
8 + DISCLAIMER, build_review_workbook_bytes, write_review_workbook,
9 +)
10 +from aram_backend.coverage_preview import CoverageError
11 +from aram_backend.synthetic_five_statement_demo import cash_transactions
12 +from test_five_statement_pipeline_integration import build_pipeline
13 +
14 +
15 +def test_review_pack_contains_exact_strings_all_statements_and_disclaimers(tmp_path):
16 + result = build_pipeline()
17 + path = write_review_workbook(tmp_path / "review-only.xlsx", result)
18 + assert path.is_file()
19 + workbook = load_workbook(path, read_only=True, data_only=False)
20 + try:
21 + assert workbook.sheetnames == [
22 + "اطلاعات", "خلاصه مبالغ", "ترازنامه", "سود و زیان",
23 + "حقوق مالکانه", "جریان نقد", "تراز آزمایشی", "کنترل‌ها",
24 + ]
25 + for sheet in workbook.worksheets:
26 + assert DISCLAIMER in sheet["A1"].value
27 + summary = list(workbook["خلاصه مبالغ"].iter_rows(values_only=True))
28 + assert ("سود و زیان", "سود خالص", "20", "پیش‌نویس قابل بررسی؛ غیررسمی") in summary
29 + balance = list(workbook["ترازنامه"].iter_rows(values_only=True))
30 + assert any(row[1] == "Cash and cash equivalents" and row[8] == "50" for row in balance)
31 + trial = list(workbook["تراز آزمایشی"].iter_rows(values_only=True))
32 + assert len(trial) == len(result.trial_balance.accounts) + 4
33 + assert all(
34 + cell.data_type != "f"
35 + for sheet in workbook.worksheets
36 + for row in sheet.iter_rows()
37 + for cell in row
38 + )
39 + finally:
40 + workbook.close()
41 +
42 +
43 +def test_review_pack_never_overwrites_existing_files(tmp_path):
44 + target = tmp_path / "existing.xlsx"
45 + target.write_bytes(b"do not overwrite")
46 + with pytest.raises(CoverageError, match="TARGET_EXISTS"):
47 + write_review_workbook(target, build_pipeline())
48 + assert target.read_bytes() == b"do not overwrite"
49 +
50 +
51 +def test_review_pack_refuses_wrong_extension_and_missing_parent(tmp_path):
52 + result = build_pipeline()
53 + with pytest.raises(CoverageError, match="EXTENSION"):
54 + write_review_workbook(tmp_path / "review.xls", result)
55 + with pytest.raises(CoverageError, match="PARENT"):
56 + write_review_workbook(tmp_path / "missing" / "review.xlsx", result)
57 +
58 +
59 +def test_formula_like_user_text_is_written_as_a_literal_string(tmp_path):
60 + result = build_pipeline()
61 + line = replace(
62 + result.balance_sheet.lines[0],
63 + line_label='=HYPERLINK("https://example.invalid","open")',
64 + )
65 + changed = replace(
66 + result, balance_sheet=replace(
67 + result.balance_sheet, lines=(line,) + result.balance_sheet.lines[1:],
68 + ),
69 + )
70 + path = write_review_workbook(tmp_path / "literal.xlsx", changed)
71 + workbook = load_workbook(path, read_only=True, data_only=False)
72 + try:
73 + rows = list(workbook["ترازنامه"].iter_rows())
74 + matching = [
75 + cell for row in rows for cell in row
76 + if cell.value == '=HYPERLINK("https://example.invalid","open")'
77 + ]
78 + assert len(matching) == 1
79 + assert matching[0].data_type == "s"
80 + assert all(
81 + cell.data_type != "f"
82 + for sheet in workbook.worksheets
83 + for row in sheet.iter_rows()
84 + for cell in row
85 + )
86 + finally:
87 + workbook.close()
88 +
89 +
90 +def test_blocked_pipeline_pack_is_explicitly_diagnostic(tmp_path):
91 + result = build_pipeline(transactions=cash_transactions()[:-1])
92 + assert not result.eligible_for_analyst_review
93 + content = build_review_workbook_bytes(result)
94 + assert content.startswith(b"PK")
95 + path = write_review_workbook(tmp_path / "blocked.xlsx", result)
96 + workbook = load_workbook(path, read_only=True, data_only=True)
97 + try:
98 + summary = list(workbook["خلاصه مبالغ"].iter_rows(values_only=True))
99 + assert any("مسدود" in str(row[3]) for row in summary[4:])
100 + info = list(workbook["اطلاعات"].iter_rows(values_only=True))
101 + assert any(row[0] == "وضعیت کلی" and "مسدود" in row[1] for row in info)
102 + finally:
103 + workbook.close()
104 +
105 +
106 +def test_review_pack_rejects_bidi_and_control_text():
107 + result = build_pipeline()
108 + line = replace(result.income_statement.lines[0], line_label="Revenue\u202eforged")
109 + changed = replace(
110 + result, income_statement=replace(
111 + result.income_statement, lines=(line,) + result.income_statement.lines[1:],
112 + ),
113 + )
114 + with pytest.raises(CoverageError):
115 + build_review_workbook_bytes(changed)
116 +
117 +
118 +def test_review_pack_refuses_symlink_targets(tmp_path):
119 + target = tmp_path / "real.xlsx"
120 + link = tmp_path / "alias.xlsx"
121 + try:
122 + link.symlink_to(target)
123 + except (OSError, NotImplementedError):
124 + pytest.skip("Symlinks unavailable")
125 + with pytest.raises(CoverageError, match="TARGET_EXISTS"):
126 + write_review_workbook(link, build_pipeline())