test_runtime_resolution.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_supports_macos_x64(monkeypatch: pytest.MonkeyPatch) -> None:
  62. monkeypatch.setattr(runtime.sys, "platform", "darwin")
  63. monkeypatch.setattr(runtime.platform, "machine", lambda: "x86_64")
  64. assert runtime._current_platform_tag() == "macos-x64"
  65. def test_runtime_requires_ripgrep_sidecar(
  66. tmp_path: Path, monkeypatch: pytest.MonkeyPatch
  67. ) -> None:
  68. runtime_dir = tmp_path / "runtime"
  69. runtime_dir.mkdir()
  70. (runtime_dir / "deepseek-harness-sdk-runtime-linux-x64").touch()
  71. monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
  72. monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "linux-x64")
  73. with pytest.raises(FileNotFoundError, match="ripgrep sidecar"):
  74. runtime.bundled_runtime_path()
  75. def test_node_mode_runs_the_deployed_dsh_cli(
  76. tmp_path: Path, monkeypatch: pytest.MonkeyPatch
  77. ) -> None:
  78. bin_js = tmp_path / "runtime" / "node" / "node_modules" / "@deepseek-ai" / "dsh" / "lib" / "bin.js"
  79. bin_js.parent.mkdir(parents=True)
  80. bin_js.touch()
  81. monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
  82. monkeypatch.setattr(runtime.shutil, "which", lambda _name: "/node")
  83. assert resolve_bundled_launch_args("node") == ("/node", str(bin_js))
  84. def test_python_dsh_command_requires_explicit_home(
  85. monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
  86. ) -> None:
  87. monkeypatch.delenv("DSH_HOME", raising=False)
  88. with pytest.raises(SystemExit) as excinfo:
  89. main()
  90. assert excinfo.value.code == 2
  91. assert "explicit DSH_HOME" in capsys.readouterr().err
  92. def test_python_dsh_command_executes_the_bundled_cli(
  93. monkeypatch: pytest.MonkeyPatch
  94. ) -> None:
  95. called: dict[str, object] = {}
  96. monkeypatch.setenv("DSH_HOME", "/explicit/home")
  97. monkeypatch.setattr(runtime, "resolve_bundled_launch_args", lambda: ("/runtime",))
  98. monkeypatch.setattr(runtime.sys, "argv", ["dsh", "plugin", "--profile", "sdk", "list"])
  99. def execvpe(file: str, args: tuple[str, ...], env: dict[str, str]) -> None:
  100. called.update(file=file, args=args, home=env.get("DSH_HOME"))
  101. monkeypatch.setattr(runtime.os, "execvpe", execvpe)
  102. main()
  103. assert called == {
  104. "file": "/runtime",
  105. "args": ("/runtime", "plugin", "--profile", "sdk", "list"),
  106. "home": "/explicit/home",
  107. }