Repository navigation
Expand file tree
/
Copy pathtest_runtime_resolution.py
More file actions
430 lines (364 loc) · 20.2 KB
/
Copy pathtest_runtime_resolution.py
File metadata and controls
430 lines (364 loc) · 20.2 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
"""Keyless runtime-resolution tests; launch coverage lives in test_bundled_runtime.py."""
from __future__ import annotations
import json
import os
import subprocess
import shutil
import sys
import zipfile
from pathlib import Path
from types import SimpleNamespace
import deepseek_harness_runtime as runtime
import pytest
from deepseek_harness_runtime._resources import validate_resources
from deepseek_harness_runtime import (
RUNTIME_MODE_ENV_VAR,
bundled_package_dir,
main,
resolve_bundled_launch_args,
)
def _resource_sidecars(executable: Path, native_targets: tuple[str, ...] = ("darwin-arm64", "darwin-x64", "win32-x64")) -> Path:
office = executable.with_name(f"{executable.name.removesuffix('.exe')}-office")
tag = executable.name.removeprefix("deepseek-harness-sdk-runtime-").removesuffix(".exe")
native = tag.replace("win-", "win32-").replace("macos-", "darwin-")
engine = native if native in native_targets else "wasm"
for required in ("@deepseek-ai/libreoffice-kit/package.json", f"@deepseek-ai/libreoffice-kit-{engine}/prebuilds.json"):
path = office / "node_modules" / required
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("{}")
adapter = office / "node_modules/@deepseek-ai/libreoffice-kit/package.json"
adapter.write_text(json.dumps({"optionalDependencies": {
f"@deepseek-ai/libreoffice-kit-{target}": "0.0.1" for target in (*native_targets, "wasm")
}}), encoding="utf-8")
resources = executable.with_name(tag)
resources.mkdir(parents=True, exist_ok=True)
(resources / "downloads.json").write_text(json.dumps({
"target": tag.replace("macos-", "mac-"), "identity": "a" * 64,
"office": [{"name": "fixture"}], "pnpm": {},
}))
manifest = resources / "primary-runtime/runtime.json"
manifest.parent.mkdir(parents=True)
manifest.write_text(json.dumps({"platform": native.rsplit("-", 1)[0], "arch": tag.rsplit("-", 1)[1],
"python": "3.12.14", "pythonPackages": {"numpy": "2.3.5"}}))
python = resources / "primary-runtime/dependencies/python" / ("python.exe" if tag.startswith("win-") else "bin/python3")
python.parent.mkdir(parents=True)
python.touch()
python.chmod(0o755)
node = resources / "primary-runtime/dependencies/node/bin" / ("node.exe" if tag.startswith("win-") else "node")
node.parent.mkdir(parents=True)
node.touch()
node.chmod(0o755)
packages = python.parent if tag.startswith("win-") else python.parent.parent
(packages / ("Lib/site-packages" if tag.startswith("win-") else "lib/python3.12/site-packages")).mkdir(parents=True)
for file in (f"office-{kind}/SKILL.md" for kind in ("docx", "pptx", "xlsx")):
path = resources / "office-skills" / file
path.parent.mkdir(parents=True, exist_ok=True)
path.touch()
return office
def test_unknown_explicit_mode_fails_loud() -> None:
with pytest.raises(ValueError, match="expected 'exe' or 'node'"):
resolve_bundled_launch_args("bogus")
@pytest.mark.parametrize("target", ["linux-x64", "linux-arm64", "macos-arm64", "macos-x64", "win-x64"])
@pytest.mark.parametrize("invalid", [None, "manifest", "platform", "python", "skills", "packages", "mode", "node", "node-mode"])
def test_authoring_resources_validate_installed_and_wheel_payloads(tmp_path: Path, target: str, invalid: str | None) -> None:
executable = tmp_path / f"deepseek-harness-sdk-runtime-{target}"
_resource_sidecars(executable)
root = tmp_path / target
python = root / "primary-runtime/dependencies/python" / ("python.exe" if target == "win-x64" else "bin/python3")
node = root / "primary-runtime/dependencies/node/bin" / ("node.exe" if target == "win-x64" else "node")
if invalid in ("mode", "node-mode") and target == "win-x64":
pytest.skip("Windows executables do not require a POSIX executable bit")
if invalid == "manifest":
(root / "primary-runtime/runtime.json").unlink()
elif invalid == "platform":
manifest_path = root / "primary-runtime/runtime.json"
manifest = json.loads(manifest_path.read_text())
manifest["arch"] = "wrong"
manifest_path.write_text(json.dumps(manifest))
elif invalid == "node":
node.unlink()
elif invalid == "node-mode":
node.chmod(0o644)
elif invalid == "python":
python.unlink()
elif invalid == "skills":
(root / "office-skills/office-docx/SKILL.md").unlink()
elif invalid == "packages":
next(root.rglob("site-packages")).rmdir()
elif invalid == "mode":
python.chmod(0o644)
archive_path = tmp_path / "payload.zip"
with zipfile.ZipFile(archive_path, "w") as archive:
for path in root.rglob("*"):
archive.write(path, str(path.relative_to(root)).replace("\\", "/"))
with zipfile.ZipFile(archive_path) as archive:
for candidate in (root, zipfile.Path(archive)):
if invalid is None:
validate_resources(candidate, target)
else:
with pytest.raises((ValueError, FileNotFoundError)):
validate_resources(candidate, target)
def test_unknown_env_mode_fails_loud(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(RUNTIME_MODE_ENV_VAR, "bogus")
with pytest.raises(ValueError, match="expected 'exe' or 'node'"):
resolve_bundled_launch_args()
def test_explicit_mode_wins_over_env_mode(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(RUNTIME_MODE_ENV_VAR, "bogus")
try:
args = resolve_bundled_launch_args("exe")
except FileNotFoundError:
return # explicit 'exe' was honored; only the artifact is missing
assert args[0].endswith(("-x64", "-arm64"))
def test_runtime_requires_spawn_helper_only_on_macos(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
runtime_dir = tmp_path / "runtime"
runtime_dir.mkdir()
linux = runtime_dir / "deepseek-harness-sdk-runtime-linux-x64"
linux.touch()
Path(f"{linux}-rg").touch()
_resource_sidecars(linux)
macos = runtime_dir / "deepseek-harness-sdk-runtime-macos-arm64"
macos.touch()
Path(f"{macos}-rg").touch()
monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "macos-arm64")
with pytest.raises(FileNotFoundError, match="node-pty spawn helper"):
runtime.bundled_runtime_path()
monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "linux-x64")
assert runtime.bundled_runtime_path() == linux
def test_windows_runtime_uses_exe_payload_and_exe_sidecar(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
runtime_dir = tmp_path / "runtime"
runtime_dir.mkdir()
executable = runtime_dir / "deepseek-harness-sdk-runtime-win-x64.exe"
executable.touch()
(runtime_dir / "deepseek-harness-sdk-runtime-win-x64-rg.exe").touch()
_resource_sidecars(executable)
monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "win-x64")
assert runtime.bundled_runtime_path() == executable
def test_current_platform_supports_windows_x64_only(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(runtime.sys, "platform", "win32")
monkeypatch.setattr(runtime.platform, "machine", lambda: "AMD64")
assert runtime._current_platform_tag() == "win-x64"
monkeypatch.setattr(runtime.platform, "machine", lambda: "ARM64")
with pytest.raises(FileNotFoundError, match="Windows x64"):
runtime._current_platform_tag()
def test_current_platform_supports_macos_x64(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(runtime.sys, "platform", "darwin")
monkeypatch.setattr(runtime.platform, "machine", lambda: "x86_64")
assert runtime._current_platform_tag() == "macos-x64"
def test_runtime_requires_ripgrep_sidecar(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
runtime_dir = tmp_path / "runtime"
runtime_dir.mkdir()
(runtime_dir / "deepseek-harness-sdk-runtime-linux-x64").touch()
monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "linux-x64")
with pytest.raises(FileNotFoundError, match="ripgrep sidecar"):
runtime.bundled_runtime_path()
def test_runtime_requires_complete_resource_sidecars(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
executable = tmp_path / "runtime" / "deepseek-harness-sdk-runtime-linux-x64"
executable.parent.mkdir()
executable.touch()
Path(f"{executable}-rg").touch()
monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "linux-x64")
with pytest.raises(FileNotFoundError, match="downloads.json"):
runtime.bundled_runtime_path()
office = _resource_sidecars(executable)
assert runtime.bundled_runtime_path() == executable
(office / "node_modules/@deepseek-ai/libreoffice-kit-wasm/prebuilds.json").unlink()
assert runtime.bundled_runtime_path() == executable
shutil.rmtree(executable.with_name("linux-x64") / "primary-runtime")
assert runtime.bundled_runtime_path() == executable
def test_node_mode_runs_the_deployed_dsh_cli(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
bin_js = tmp_path / "runtime" / "node" / "runtime-bootstrap.mjs"
bin_js.parent.mkdir(parents=True)
bin_js.touch()
monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
monkeypatch.setattr(runtime.shutil, "which", lambda _name: "/node")
assert resolve_bundled_launch_args("node") == ("/node", str(bin_js))
def test_python_dsh_command_requires_explicit_home(
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
monkeypatch.delenv("DSH_HOME", raising=False)
with pytest.raises(SystemExit) as excinfo:
main()
assert excinfo.value.code == 2
assert "explicit DSH_HOME" in capsys.readouterr().err
def test_python_dsh_command_executes_the_bundled_cli(
monkeypatch: pytest.MonkeyPatch
) -> None:
called: dict[str, object] = {}
monkeypatch.setenv("DSH_HOME", "/explicit/home")
monkeypatch.setattr(runtime, "resolve_bundled_launch_args", lambda: ("/runtime",))
monkeypatch.setattr(runtime, "sys", SimpleNamespace(platform="linux", argv=["dsh", "plugin", "--profile", "sdk", "list"]))
def execvpe(file: str, args: tuple[str, ...], env: dict[str, str]) -> None:
called.update(file=file, args=args, home=env.get("DSH_HOME"))
monkeypatch.setattr(runtime.os, "execvpe", execvpe)
main()
assert called == {
"file": "/runtime",
"args": ("/runtime", "plugin", "--profile", "sdk", "list"),
"home": "/explicit/home",
}
@pytest.mark.parametrize("returncode", [0, 37, 513])
def test_windows_console_waits_and_forwards_runtime_status(monkeypatch: pytest.MonkeyPatch, returncode: int) -> None:
monkeypatch.setenv("DSH_HOME", "/explicit/home")
monkeypatch.setattr(runtime, "sys", SimpleNamespace(platform="win32", argv=["dsh", "plugin", "argument with spaces", "中文"]))
monkeypatch.setattr(runtime, "resolve_bundled_launch_args", lambda: ("runtime.exe",))
called = []
def run(args: tuple[str, ...], **kwargs: object) -> subprocess.CompletedProcess[str]:
called.append((args, kwargs))
return subprocess.CompletedProcess(args, returncode)
def forbidden_exec(*args: object) -> None:
pytest.fail("Windows console must wait instead of entering CRT exec")
monkeypatch.setattr(subprocess, "run", run)
monkeypatch.setattr(runtime.os, "execvpe", forbidden_exec)
with pytest.raises(SystemExit) as result:
main()
assert result.value.code == returncode
assert called == [(("runtime.exe", "plugin", "argument with spaces", "中文"), {"env": os.environ})]
@pytest.mark.parametrize("returncode", [0, 37, pytest.param(513, marks=pytest.mark.skipif(sys.platform != "win32", reason="POSIX truncates process exit codes to eight bits"))])
def test_windows_console_branch_preserves_real_child_io_and_completion(tmp_path: Path, returncode: int) -> None:
child = tmp_path / "child with spaces.py"
sentinel = tmp_path / "finished"
child.write_text(
"import pathlib,sys\n"
"assert sys.argv[1] == 'argument with spaces'\n"
"assert sys.argv[2] == '中文'\n"
"print('stdout-中文', flush=True)\n"
"print('stderr-中文', file=sys.stderr, flush=True)\n"
f"pathlib.Path({str(sentinel)!r}).write_text('done')\n"
f"raise SystemExit({returncode})\n", encoding="utf-8",
)
driver = (
"import deepseek_harness_runtime as runtime; from types import SimpleNamespace; "
f"runtime.sys = SimpleNamespace(platform='win32', argv=['dsh', 'argument with spaces', '中文']); "
f"runtime.resolve_bundled_launch_args = lambda: ({sys.executable!r}, {str(child)!r}); runtime.main()"
)
result = subprocess.run([sys.executable, "-c", driver], capture_output=True, text=True, encoding="utf-8",
env={**os.environ, "DSH_HOME": str(tmp_path), "PYTHONIOENCODING": "utf-8"}, timeout=15)
assert result.returncode == returncode, result.stderr
assert result.stdout == "stdout-中文\n"
assert result.stderr == "stderr-中文\n"
assert sentinel.read_text() == "done"
@pytest.mark.parametrize("target,native_targets", [
("linux-x64", ()), ("linux-arm64", ()),
("macos-arm64", ("darwin-arm64",)), ("macos-x64", ("darwin-x64",)), ("win-x64", ("win32-x64",)),
("linux-x64", ("linux-x64",)), ("macos-arm64", ()),
])
def test_runtime_starts_independently_of_office_engine(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, target: str, native_targets: tuple[str, ...],
) -> None:
extension = ".exe" if target.startswith("win-") else ""
executable = tmp_path / "runtime" / f"deepseek-harness-sdk-runtime-{target}{extension}"
executable.parent.mkdir()
executable.touch()
executable.with_name(f"{executable.stem}-rg{extension}").touch()
if target.startswith("macos-"):
Path(f"{executable}-spawn-helper").touch()
office = _resource_sidecars(executable, native_targets)
monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
monkeypatch.setattr(runtime, "_current_platform_tag", lambda: target)
assert runtime.bundled_runtime_path() == executable
engine = next(office.glob("node_modules/@deepseek-ai/libreoffice-kit-*/prebuilds.json"))
engine.unlink()
foreign = office / "node_modules/@deepseek-ai" / ("libreoffice-kit-darwin-arm64" if engine.parent.name == "libreoffice-kit-wasm" else "libreoffice-kit-wasm") / "prebuilds.json"
foreign.parent.mkdir(parents=True, exist_ok=True)
foreign.write_text("{}")
assert runtime.bundled_runtime_path() == executable
@pytest.mark.parametrize("function,resource", [
(runtime.download_office, "office"),
(runtime.download_primary_runtime, "primary"),
])
def test_explicit_download_uses_runtime_without_a_harness_home(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, function, resource: str,
) -> None:
root = tmp_path / "中文缓存"
root.mkdir()
_resource_sidecars(root / "deepseek-harness-sdk-runtime-linux-x64")
monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "linux-x64")
script = tmp_path / "download.py"
destination = root / "linux-x64/primary-runtime" if resource == "primary" else root
script.write_text("import json,os,sys\n" + f"assert os.environ['DSH_RUNTIME_DOWNLOAD'] == {resource!r}\n"
+ f"sys.stdout.buffer.write(json.dumps({str(destination)!r}, ensure_ascii=False).encode('utf-8'))\n", encoding="utf-8")
monkeypatch.delenv("DSH_HOME", raising=False)
monkeypatch.setattr(runtime, "resolve_bundled_launch_args", lambda: (sys.executable, str(script)))
assert function() == destination
script.write_text("import sys\nsys.stderr.buffer.write('下载失败'.encode('utf-8'))\nsys.exit(2)\n", encoding="utf-8")
with pytest.raises(RuntimeError, match="下载失败"):
function()
@pytest.mark.skipif(os.name == "nt", reason="LC_ALL=C is a POSIX locale fixture")
def test_download_decodes_utf8_in_an_ascii_locale(tmp_path: Path) -> None:
script = tmp_path / "download.py"
destination = tmp_path / "中文缓存"
script.write_text("import sys\nsys.stdout.buffer.write(" + json.dumps(str(destination), ensure_ascii=False).encode('utf-8').__repr__() + ")\n", encoding="utf-8")
code = ("import sys,locale,deepseek_harness_runtime as r; "
"assert locale.getpreferredencoding(False).lower() not in ('utf-8','utf8'); "
f"r.resolve_bundled_launch_args=lambda:(sys.executable,{str(script)!a}); "
f"assert str(r.download_office())=={str(destination)!a}")
result = subprocess.run([sys.executable, "-c", code], capture_output=True, encoding="utf-8", env={
**os.environ, "LC_ALL": "C", "PYTHONUTF8": "0", "PYTHONCOERCECLOCALE": "0",
"PYTHONPATH": str(Path(runtime.__file__).resolve().parents[1]),
}, timeout=30)
assert result.returncode == 0, result.stderr
@pytest.mark.parametrize("missing", ["primary-runtime/runtime.json", "primary-runtime/dependencies/python/bin/python3",
"primary-runtime/dependencies/node/bin/node", "office-skills/office-docx/SKILL.md"])
def test_primary_download_rejects_an_incomplete_payload(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, missing: str) -> None:
_resource_sidecars(tmp_path / "deepseek-harness-sdk-runtime-linux-x64")
root = tmp_path / "linux-x64"
(root / missing).unlink()
monkeypatch.setattr(runtime, "_download", lambda resource: root / "primary-runtime")
monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "linux-x64")
with pytest.raises(FileNotFoundError):
runtime.download_primary_runtime()
def test_office_resolution_requires_explicit_download(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
from deepseek_harness_runtime._resources import office_launch_args
executable = tmp_path / "deepseek-harness-sdk-runtime-linux-x64"
_resource_sidecars(executable)
cache = tmp_path / "cache"
monkeypatch.setenv("DSH_RESOURCE_CACHE", str(cache))
monkeypatch.delenv("DSH_OFFICE_SIDECAR", raising=False)
with pytest.raises(FileNotFoundError, match=r"download_office\(\)"):
office_launch_args(tmp_path / "linux-x64", "linux-x64")
office = cache / ("a" * 64) / "office"
node = office / "node/bin/node"
cli = office / "node_modules/@deepseek-ai/libreoffice-kit/lib/cli.js"
for path in (node, cli):
path.parent.mkdir(parents=True, exist_ok=True)
path.touch()
with pytest.raises(FileNotFoundError, match="incomplete"):
office_launch_args(tmp_path / "linux-x64", "linux-x64")
(office / "complete").write_text("wrong", encoding="utf-8")
with pytest.raises(ValueError, match="identity mismatch"):
office_launch_args(tmp_path / "linux-x64", "linux-x64")
(office / "complete").write_text("a" * 64, encoding="utf-8")
assert office_launch_args(tmp_path / "linux-x64", "linux-x64") == (str(node), str(cli))
(office / "complete").unlink()
monkeypatch.setenv("DSH_OFFICE_SIDECAR", str(office))
assert office_launch_args(tmp_path / "linux-x64", "linux-x64") == (str(node), str(cli))
@pytest.mark.parametrize("invalid", ["target", "identity", "office", "pnpm", "skills"])
def test_download_manifest_rejects_invalid_wheel_resources(tmp_path: Path, invalid: str) -> None:
from deepseek_harness_runtime._resources import validate_downloads
_resource_sidecars(tmp_path / "deepseek-harness-sdk-runtime-linux-x64")
root = tmp_path / "linux-x64"
path = root / "downloads.json"
manifest = json.loads(path.read_text(encoding="utf-8"))
if invalid == "skills":
(root / "office-skills/office-docx/SKILL.md").unlink()
with pytest.raises(FileNotFoundError, match="Office skill"):
validate_downloads(root, "linux-x64")
else:
manifest[invalid] = {"target": "win-x64", "identity": "z" * 64, "office": [], "pnpm": []}[invalid]
path.write_text(json.dumps(manifest), encoding="utf-8")
with pytest.raises(ValueError, match="invalid runtime download manifest"):
validate_downloads(root, "linux-x64")