-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pptx_tool.py
More file actions
161 lines (129 loc) · 4.88 KB
/
Copy pathtest_pptx_tool.py
File metadata and controls
161 lines (129 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""Smoke test for the ppt-llm integration (pptx_tool.py).
Exercises the generate/revise/list handlers end-to-end with a FAKE provider
client and a stubbed ToolResult, so it needs neither the live LLM nor the full
Gradio app — only the vendored pptx_engine. Run: python test_pptx_tool.py
"""
import json
import os
import sys
import tempfile
import types
# ── stub the app modules pptx_tool lazily imports, BEFORE importing it ────────
_tmp = tempfile.mkdtemp(prefix="pptx_smoke_")
os.environ["PPTLLM_DOWNLOAD_DIR"] = os.path.join(_tmp, "downloads")
cfg = types.ModuleType("config")
cfg.STORAGE_MOUNT_POINT = _tmp
sys.modules["config"] = cfg
class _ToolResult: # mimics tools.ToolResult
def __init__(self, text, ui=None, error=False):
self.text, self.ui, self.error = text, ui, error
te = types.ModuleType("tools")
te.ToolResult = _ToolResult
sys.modules["tools"] = te
class _Msg:
def __init__(self, content):
self.message = types.SimpleNamespace(content=content)
class _Resp:
def __init__(self, content):
self.choices = [_Msg(content)]
class _FakeCompletions:
def __init__(self, queue):
self._q = queue
def create(self, **kwargs):
return _Resp(self._q.pop(0))
class _FakeClient:
def __init__(self, queue):
self.chat = types.SimpleNamespace(completions=_FakeCompletions(queue))
PLAN = json.dumps(
{
"title": "Cycling to Work",
"subtitle": "A quick case",
"slides": [
{"layout": "title", "intent": "cover"},
{"layout": "bullets", "intent": "benefits"},
{"layout": "closing", "intent": "wrap"},
],
}
)
CONTENT = json.dumps(
{
"slides": [
{"layout": "title", "title": "Cycling to Work", "subtitle": "A quick case"},
{"layout": "bullets", "title": "Why", "body": ["Cheaper", "Healthier", "Greener"]},
{"layout": "closing", "title": "Thanks"},
]
}
)
REVISE = json.dumps(
{
"title": "Cycling to Work",
"theme": "aurora",
"slides": [
{"layout": "title", "title": "Cycling to Work"},
{"layout": "closing", "title": "Ride on"},
],
}
)
def main():
import provider_utils_stub # noqa (created below)
import pptx_tool
assert pptx_tool.HAS_PPTX_ENGINE, "engine not importable" # nosec B101
assert set(pptx_tool.PPTX_TOOL_NAMES) == { # nosec B101
"generate_presentation",
"revise_presentation",
"list_themes",
}
# patch get_client to return our fake (queues: generate=plan+content)
import provider_utils
queue = [PLAN, CONTENT]
provider_utils.get_client = lambda provider, key=None: _FakeClient(queue)
handlers = pptx_tool.make_pptx_tool_handlers(
{}, {"id": 42}, "Mistral", "mistral-large-latest", "k"
)
# list_themes
r = handlers["list_themes"]({})
assert "midnight" in r.text and not r.error, r.text # nosec B101
# generate
r = handlers["generate_presentation"](
{"topic": "Cycling to work", "num_slides": 3, "theme": "midnight"}
)
assert not r.error, r.text # nosec B101
assert "deck_id=" in r.text, r.text # nosec B101
deck_id = r.text.split("deck_id=")[1].split(".")[0].strip()
pptx = os.path.join(_tmp, "user_42", "presentations", f"{deck_id}.pptx")
assert os.path.exists(pptx) and os.path.getsize(pptx) > 5000, pptx # nosec B101
assert "/file=" in (r.ui or ""), f"no download link in ui: {r.ui}" # nosec B101
dl = os.path.join(_tmp, "downloads", deck_id)
assert os.path.isdir(dl) and os.listdir(dl), "download copy not published" # nosec B101
print(
f" generate OK -> {os.path.basename(pptx)} ({os.path.getsize(pptx)} bytes); "
f"download link + published copy OK"
)
# revise (new queue with the revised spec)
queue[:] = [REVISE]
r = handlers["revise_presentation"](
{"deck_id": deck_id, "instructions": "theme aurora; closing 'Ride on'"}
)
assert not r.error, r.text # nosec B101
spec = json.load(
open(os.path.join(_tmp, "user_42", "presentations", f"{deck_id}.json"), encoding="utf-8")
)
assert spec["theme"] == "aurora", spec["theme"] # nosec B101
print(f" revise OK -> theme now '{spec['theme']}'")
# error paths
assert handlers["generate_presentation"]({"topic": ""}).error # nosec B101
assert handlers["revise_presentation"](
{
"deck_id": "nope", # nosec B101
"instructions": "x",
}
).error
print(" error paths OK")
print("ALL SMOKE CHECKS PASSED")
# provider_utils stub module so `import provider_utils` resolves without app deps
_pu = types.ModuleType("provider_utils")
_pu.get_client = lambda provider, key=None: None
sys.modules["provider_utils"] = _pu
sys.modules["provider_utils_stub"] = types.ModuleType("provider_utils_stub")
if __name__ == "__main__":
main()