test(desktop): cover native pilot lifecycle, stale-result invalidation and local-file security
davoodmehraban89 committed
Oct 31, 2026 at 18:31 UTC
f36a9a2c6c9c719abdfa1fcdb628aefe3bd49e0e
1 file changed
+188
backend/tests/test_desktop_five_statement.py
new
+188
@@ -0,0 +1,188 @@
1
+"""Synthetic-only safety and lifecycle checks for the native Windows pilot."""
2
+from __future__ import annotations
3
+
4
+from hashlib import sha256
5
+import json
6
+
7
+import pytest
8
+
9
+from aram_backend.desktop_five_statement_core import (
10
+ DesktopFiveInputError, DesktopFiveStatementController,
11
+ MAX_INPUT_BYTES, MAX_CASH_MANIFEST_BYTES,
12
+)
13
+from aram_backend.desktop_five_statement import ACTIONS, FiveStatementWindow, main, self_test
14
+from test_five_statement_pipeline_integration import (
15
+ cash_transactions, pipeline_manifest, source_workbook,
16
+)
17
+
18
+
19
+def fixtures(tmp_path, *, manifest=None):
20
+ xlsx = tmp_path / "synthetic.xlsx"
21
+ jsonfile = tmp_path / "synthetic.json"
22
+ xlsx.write_bytes(source_workbook())
23
+ jsonfile.write_text(
24
+ json.dumps(pipeline_manifest() if manifest is None else manifest),
25
+ encoding="utf-8",
26
+ )
27
+ return xlsx, jsonfile
28
+
29
+
30
+def controller_with_files(paths):
31
+ ctl = DesktopFiveStatementController()
32
+ ctl.select_workbook(str(paths[0]))
33
+ ctl.select_manifest(str(paths[1]))
34
+ return ctl
35
+
36
+
37
+def test_full_five_statement_preview_is_local_and_never_writes_source_files(tmp_path):
38
+ paths = fixtures(tmp_path)
39
+ workbook_digest = sha256(paths[0].read_bytes()).hexdigest()
40
+ manifest_digest = sha256(paths[1].read_bytes()).hexdigest()
41
+ ctl = controller_with_files(paths)
42
+ assert ctl.can_run
43
+ assert ctl.state == "SELECTED"
44
+ assert ctl.result is None
45
+ result = ctl.validate()
46
+ assert result.eligible_for_analyst_review
47
+ assert result.reconciliation.eligible_for_analyst_review
48
+ assert not result.releasable and not result.official_financial_statement
49
+ assert ctl.result is result
50
+ assert ctl.state == "ELIGIBLE"
51
+ assert sha256(paths[0].read_bytes()).hexdigest() == workbook_digest
52
+ assert sha256(paths[1].read_bytes()).hexdigest() == manifest_digest
53
+ assert {p.name for p in tmp_path.iterdir()} == {
54
+ "synthetic.xlsx", "synthetic.json",
55
+ }
56
+
57
+
58
+def test_missing_cash_transaction_blocks_without_creating_approved_state(tmp_path):
59
+ manifest = pipeline_manifest(transactions=cash_transactions()[:-1])
60
+ ctl = controller_with_files(fixtures(tmp_path, manifest=manifest))
61
+ result = ctl.validate()
62
+ assert not result.eligible_for_analyst_review
63
+ assert ctl.state == "BLOCKED"
64
+ assert "CASH_FLOW_CANDIDATE_BLOCKED" in result.findings
65
+
66
+
67
+def test_invalid_manifest_clears_previous_eligible_result(tmp_path):
68
+ paths = fixtures(tmp_path)
69
+ ctl = controller_with_files(paths)
70
+ assert ctl.validate().eligible_for_analyst_review
71
+ paths[1].write_text("not-json", encoding="utf-8")
72
+ with pytest.raises(DesktopFiveInputError):
73
+ ctl.validate()
74
+ assert ctl.state == "INPUT_ERROR"
75
+ assert ctl.result is None
76
+ assert ctl.error_code is not None
77
+
78
+
79
+def test_changing_selected_file_invalidates_previous_result(tmp_path):
80
+ paths = fixtures(tmp_path)
81
+ ctl = controller_with_files(paths)
82
+ assert ctl.validate().eligible_for_analyst_review
83
+ old_generation = ctl.generation
84
+ ctl.select_manifest(str(paths[1]))
85
+ assert ctl.generation > old_generation
86
+ assert ctl.result is None
87
+ assert ctl.state == "SELECTED"
88
+ ctl.select_workbook(str(paths[0]))
89
+ assert ctl.result is None
90
+
91
+
92
+def test_invalid_switch_does_not_leave_old_financial_preview_visible(tmp_path):
93
+ paths = fixtures(tmp_path)
94
+ ctl = controller_with_files(paths)
95
+ ctl.validate()
96
+ with pytest.raises(DesktopFiveInputError):
97
+ ctl.select_manifest(str(tmp_path / "missing.json"))
98
+ assert ctl.manifest_path is None
99
+ assert not ctl.can_run
100
+ assert ctl.result is None
101
+ assert ctl.state == "EMPTY"
102
+
103
+
104
+def test_removed_source_before_execution_refuses_stale_previously_eligible_file(tmp_path):
105
+ paths = fixtures(tmp_path)
106
+ ctl = controller_with_files(paths)
107
+ ctl.validate()
108
+ paths[0].unlink()
109
+ with pytest.raises(DesktopFiveInputError):
110
+ ctl.validate()
111
+ assert ctl.result is None
112
+ assert ctl.state == "INPUT_ERROR"
113
+
114
+
115
+@pytest.mark.parametrize("selection, name", [
116
+ ("workbook", "invalid.xls"),
117
+ ("workbook", "fake.xlsx.exe"),
118
+ ("manifest", "invalid.txt"),
119
+ ("manifest", "fake.json.bak"),
120
+])
121
+def test_selection_requires_expected_extension(tmp_path, selection, name):
122
+ target = tmp_path / name
123
+ target.write_bytes(b"bad")
124
+ ctl = DesktopFiveStatementController()
125
+ with pytest.raises(DesktopFiveInputError) as exc:
126
+ getattr(ctl, "select_" + selection)(str(target))
127
+ assert exc.value.args[0] == "FILE_EXTENSION_INVALID"
128
+ assert ctl.result is None
129
+
130
+
131
+@pytest.mark.parametrize("selection, extension", [
132
+ ("workbook", ".xlsx"), ("manifest", ".json"),
133
+])
134
+def test_selection_rejects_symlinks_and_directories(tmp_path, selection, extension):
135
+ target = tmp_path / ("target" + extension)
136
+ target.write_bytes(b"test")
137
+ link = tmp_path / ("alias" + extension)
138
+ try:
139
+ link.symlink_to(target)
140
+ except (OSError, NotImplementedError):
141
+ pytest.skip("No symlink support on test runner")
142
+ ctl = DesktopFiveStatementController()
143
+ with pytest.raises(DesktopFiveInputError):
144
+ getattr(ctl, "select_" + selection)(str(link))
145
+ folder = tmp_path / ("folder" + extension)
146
+ folder.mkdir()
147
+ with pytest.raises(DesktopFiveInputError):
148
+ getattr(ctl, "select_" + selection)(str(folder))
149
+
150
+
151
+@pytest.mark.parametrize("selection,extension,limit", [
152
+ ("workbook", ".xlsx", MAX_INPUT_BYTES),
153
+ ("manifest", ".json", MAX_CASH_MANIFEST_BYTES),
154
+])
155
+def test_selection_blocks_empty_and_oversized_files(tmp_path, selection, extension, limit):
156
+ target = tmp_path / ("oversize" + extension)
157
+ ctl = DesktopFiveStatementController()
158
+ target.write_bytes(b"")
159
+ with pytest.raises(DesktopFiveInputError):
160
+ getattr(ctl, "select_" + selection)(str(target))
161
+ with target.open("wb") as stream:
162
+ stream.truncate(limit + 1)
163
+ with pytest.raises(DesktopFiveInputError):
164
+ getattr(ctl, "select_" + selection)(str(target))
165
+
166
+
167
+def test_controller_clear_discards_both_files_and_all_prior_results(tmp_path):
168
+ ctl = controller_with_files(fixtures(tmp_path))
169
+ ctl.validate()
170
+ ctl.clear()
171
+ assert ctl.state == "EMPTY"
172
+ assert ctl.workbook_path is None and ctl.manifest_path is None
173
+ assert ctl.result is None and ctl.error_code is None
174
+ assert not ctl.can_run
175
+ with pytest.raises(DesktopFiveInputError):
176
+ ctl.validate()
177
+
178
+
179
+def test_headless_executable_smoke_and_all_enabled_actions_have_methods():
180
+ assert self_test() == 0
181
+ assert main(["--self-test"]) == 0
182
+ assert main(["--unexpected"]) == 2
183
+ assert set(ACTIONS) == {
184
+ "file.workbook", "file.manifest", "file.run",
185
+ "file.clear", "file.exit", "help.about",
186
+ }
187
+ for key, method in ACTIONS.items():
188
+ assert callable(getattr(FiveStatementWindow, method)), key