1
0

test_runtime_resolution.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. """Keyless runtime-resolution tests; launch coverage lives in test_bundled_runtime.py."""
  2. from __future__ import annotations
  3. from pathlib import Path
  4. import deepseek_harness_runtime as runtime
  5. import pytest
  6. from deepseek_harness_runtime import (
  7. RUNTIME_MODE_ENV_VAR,
  8. bundled_package_dir,
  9. main,
  10. resolve_bundled_launch_args,
  11. )
  12. def test_unknown_explicit_mode_fails_loud() -> None:
  13. with pytest.raises(ValueError, match="expected 'exe' or 'node'"):
  14. resolve_bundled_launch_args("bogus")
  15. def test_unknown_env_mode_fails_loud(monkeypatch: pytest.MonkeyPatch) -> None:
  16. monkeypatch.setenv(RUNTIME_MODE_ENV_VAR, "bogus")
  17. with pytest.raises(ValueError, match="expected 'exe' or 'node'"):
  18. resolve_bundled_launch_args()
  19. def test_explicit_mode_wins_over_env_mode(monkeypatch: pytest.MonkeyPatch) -> None:
  20. monkeypatch.setenv(RUNTIME_MODE_ENV_VAR, "bogus")
  21. try:
  22. args = resolve_bundled_launch_args("exe")
  23. except FileNotFoundError:
  24. return # explicit 'exe' was honored; only the artifact is missing
  25. assert args[0].endswith(("-x64", "-arm64"))
  26. def test_runtime_requires_spawn_helper_only_on_macos(
  27. tmp_path: Path, monkeypatch: pytest.MonkeyPatch
  28. ) -> None:
  29. runtime_dir = tmp_path / "runtime"
  30. runtime_dir.mkdir()
  31. linux = runtime_dir / "deepseek-harness-sdk-runtime-linux-x64"
  32. linux.touch()
  33. Path(f"{linux}-rg").touch()
  34. macos = runtime_dir / "deepseek-harness-sdk-runtime-macos-arm64"
  35. macos.touch()
  36. Path(f"{macos}-rg").touch()
  37. monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
  38. monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "macos-arm64")
  39. with pytest.raises(FileNotFoundError, match="node-pty spawn helper"):
  40. runtime.bundled_runtime_path()
  41. monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "linux-x64")
  42. assert runtime.bundled_runtime_path() == linux
  43. def test_windows_runtime_uses_exe_payload_and_exe_sidecar(
  44. tmp_path: Path, monkeypatch: pytest.MonkeyPatch
  45. ) -> None:
  46. runtime_dir = tmp_path / "runtime"
  47. runtime_dir.mkdir()
  48. executable = runtime_dir / "deepseek-harness-sdk-runtime-win-x64.exe"
  49. executable.touch()
  50. (runtime_dir / "deepseek-harness-sdk-runtime-win-x64-rg.exe").touch()
  51. monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
  52. monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "win-x64")
  53. assert runtime.bundled_runtime_path() == executable
  54. def test_current_platform_supports_windows_x64_only(monkeypatch: pytest.MonkeyPatch) -> None:
  55. monkeypatch.setattr(runtime.sys, "platform", "win32")
  56. monkeypatch.setattr(runtime.platform, "machine", lambda: "AMD64")
  57. assert runtime._current_platform_tag() == "win-x64"
  58. monkeypatch.setattr(runtime.platform, "machine", lambda: "ARM64")
  59. with pytest.raises(FileNotFoundError, match="Windows x64"):
  60. runtime._current_platform_tag()
  61. def test_current_platform_rejects_macos_x64(monkeypatch: pytest.MonkeyPatch) -> None:
  62. monkeypatch.setattr(runtime.sys, "platform", "darwin")
  63. monkeypatch.setattr(runtime.platform, "machine", lambda: "x86_64")
  64. with pytest.raises(FileNotFoundError, match="macOS arm64"):
  65. runtime._current_platform_tag()
  66. def test_runtime_requires_ripgrep_sidecar(
  67. tmp_path: Path, monkeypatch: pytest.MonkeyPatch
  68. ) -> None:
  69. runtime_dir = tmp_path / "runtime"
  70. runtime_dir.mkdir()
  71. (runtime_dir / "deepseek-harness-sdk-runtime-linux-x64").touch()
  72. monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
  73. monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "linux-x64")
  74. with pytest.raises(FileNotFoundError, match="ripgrep sidecar"):
  75. runtime.bundled_runtime_path()
  76. def test_node_mode_runs_the_deployed_dsh_cli(
  77. tmp_path: Path, monkeypatch: pytest.MonkeyPatch
  78. ) -> None:
  79. bin_js = tmp_path / "runtime" / "node" / "node_modules" / "@deepseek-ai" / "dsh" / "lib" / "bin.js"
  80. bin_js.parent.mkdir(parents=True)
  81. bin_js.touch()
  82. monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
  83. monkeypatch.setattr(runtime.shutil, "which", lambda _name: "/node")
  84. assert resolve_bundled_launch_args("node") == ("/node", str(bin_js))
  85. def test_python_dsh_command_requires_explicit_home(
  86. monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
  87. ) -> None:
  88. monkeypatch.delenv("DSH_HOME", raising=False)
  89. with pytest.raises(SystemExit) as excinfo:
  90. main()
  91. assert excinfo.value.code == 2
  92. assert "explicit DSH_HOME" in capsys.readouterr().err
  93. def test_python_dsh_command_executes_the_bundled_cli(
  94. monkeypatch: pytest.MonkeyPatch
  95. ) -> None:
  96. called: dict[str, object] = {}
  97. monkeypatch.setenv("DSH_HOME", "/explicit/home")
  98. monkeypatch.setattr(runtime, "resolve_bundled_launch_args", lambda: ("/runtime",))
  99. monkeypatch.setattr(runtime.sys, "argv", ["dsh", "plugin", "--profile", "sdk", "list"])
  100. def execvpe(file: str, args: tuple[str, ...], env: dict[str, str]) -> None:
  101. called.update(file=file, args=args, home=env.get("DSH_HOME"))
  102. monkeypatch.setattr(runtime.os, "execvpe", execvpe)
  103. main()
  104. assert called == {
  105. "file": "/runtime",
  106. "args": ("/runtime", "plugin", "--profile", "sdk", "list"),
  107. "home": "/explicit/home",
  108. }