Skip to content

Commit ab4a2b4

Browse files
committed
Add tests for guarded OOXML generation
1 parent 5c2ec0c commit ab4a2b4

1 file changed

Lines changed: 87 additions & 2 deletions

File tree

tests/test_office_cli.py

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77
import tempfile
88
import unittest
9+
import zipfile
910

1011
_REPO_ROOT = pathlib.Path(__file__).parent.parent
1112
sys.path.insert(0, str(_REPO_ROOT))
@@ -39,6 +40,13 @@ def _office_args(**overrides):
3940
return _Args(**values)
4041

4142

43+
def _assert_zip_contains(path, members):
44+
with zipfile.ZipFile(path, "r") as archive:
45+
names = set(archive.namelist())
46+
for member in members:
47+
assert member in names
48+
49+
4250
class TestOfficeCommands(unittest.TestCase):
4351
def test_office_doctor_direct(self):
4452
result = office.doctor(_Args())
@@ -91,12 +99,12 @@ def test_office_generate_execute_requires_policy_ok(self):
9199
)
92100
self.assertEqual(office.generate(args), 1)
93101

94-
def test_office_generate_execute_rejects_binary_formats(self):
102+
def test_office_generate_execute_rejects_unsupported_binary_formats(self):
95103
with tempfile.TemporaryDirectory() as tmpdir:
96104
args = _office_args(
97105
execute=True,
98106
policy_ok=True,
99-
format="docx",
107+
format="odt",
100108
output_root=tmpdir,
101109
template=None,
102110
prompt_ref=None,
@@ -130,6 +138,83 @@ def test_office_generate_execute_writes_markdown_and_evidence(self):
130138
self.assertEqual(evidence["kind"], "OfficeArtifactEvidence")
131139
self.assertEqual(evidence["operation"], "generate")
132140
self.assertEqual(evidence["status"], "requires-review")
141+
self.assertTrue(evidence["artifactHashes"][0]["sha256"].startswith("sha256:"))
142+
143+
def test_office_generate_execute_writes_docx_package_and_evidence(self):
144+
with tempfile.TemporaryDirectory() as tmpdir:
145+
evidence_path = os.path.join(tmpdir, "evidence", "docx.json")
146+
rc = main([
147+
"office",
148+
"generate",
149+
"--execute",
150+
"--policy-ok",
151+
"--artifact-type",
152+
"document",
153+
"--format",
154+
"docx",
155+
"--title",
156+
"Safe Doc",
157+
"--output-root",
158+
tmpdir,
159+
"--evidence-out",
160+
evidence_path,
161+
])
162+
self.assertEqual(rc, 0)
163+
output_path = os.path.join(tmpdir, "safe-doc.docx")
164+
self.assertTrue(os.path.exists(output_path))
165+
_assert_zip_contains(output_path, ["[Content_Types].xml", "_rels/.rels", "word/document.xml"])
166+
with open(evidence_path, "r", encoding="utf-8") as handle:
167+
evidence = json.load(handle)
168+
self.assertEqual(evidence["format"], "docx")
169+
self.assertEqual(evidence["artifactHashes"][0]["mimeType"], "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
170+
171+
def test_office_generate_execute_writes_xlsx_package(self):
172+
with tempfile.TemporaryDirectory() as tmpdir:
173+
rc = main([
174+
"office",
175+
"generate",
176+
"--execute",
177+
"--policy-ok",
178+
"--artifact-type",
179+
"spreadsheet",
180+
"--format",
181+
"xlsx",
182+
"--title",
183+
"Safe Sheet",
184+
"--output-root",
185+
tmpdir,
186+
])
187+
self.assertEqual(rc, 0)
188+
output_path = os.path.join(tmpdir, "safe-sheet.xlsx")
189+
self.assertTrue(os.path.exists(output_path))
190+
_assert_zip_contains(
191+
output_path,
192+
["[Content_Types].xml", "_rels/.rels", "xl/workbook.xml", "xl/worksheets/sheet1.xml"],
193+
)
194+
195+
def test_office_generate_execute_writes_pptx_package(self):
196+
with tempfile.TemporaryDirectory() as tmpdir:
197+
rc = main([
198+
"office",
199+
"generate",
200+
"--execute",
201+
"--policy-ok",
202+
"--artifact-type",
203+
"slide-deck",
204+
"--format",
205+
"pptx",
206+
"--title",
207+
"Safe Deck",
208+
"--output-root",
209+
tmpdir,
210+
])
211+
self.assertEqual(rc, 0)
212+
output_path = os.path.join(tmpdir, "safe-deck.pptx")
213+
self.assertTrue(os.path.exists(output_path))
214+
_assert_zip_contains(
215+
output_path,
216+
["[Content_Types].xml", "_rels/.rels", "ppt/presentation.xml", "ppt/slides/slide1.xml"],
217+
)
133218

134219
def test_office_generate_execute_rejects_whole_home_output_root(self):
135220
args = _office_args(

0 commit comments

Comments
 (0)