test_runtime_resolution.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_runtime_requires_ripgrep_sidecar(
  44. tmp_path: Path, monkeypatch: pytest.MonkeyPatch
  45. ) -> None:
  46. runtime_dir = tmp_path / "runtime"
  47. runtime_dir.mkdir()
  48. (runtime_dir / "deepseek-harness-sdk-runtime-linux-x64").touch()
  49. monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
  50. monkeypatch.setattr(runtime, "_current_platform_tag", lambda: "linux-x64")
  51. with pytest.raises(FileNotFoundError, match="ripgrep sidecar"):
  52. runtime.bundled_runtime_path()
  53. def test_node_mode_runs_the_deployed_dsh_cli(
  54. tmp_path: Path, monkeypatch: pytest.MonkeyPatch
  55. ) -> None:
  56. bin_js = tmp_path / "runtime" / "node" / "node_modules" / "@deepseek-ai" / "dsh" / "lib" / "bin.js"
  57. bin_js.parent.mkdir(parents=True)
  58. bin_js.touch()
  59. monkeypatch.setattr(runtime, "bundled_package_dir", lambda: tmp_path)
  60. monkeypatch.setattr(runtime.shutil, "which", lambda _name: "/node")
  61. assert resolve_bundled_launch_args("node") == ("/node", str(bin_js))
  62. def test_python_dsh_command_requires_explicit_home(
  63. monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
  64. ) -> None:
  65. monkeypatch.delenv("DSH_HOME", raising=False)
  66. with pytest.raises(SystemExit) as excinfo:
  67. main()
  68. assert excinfo.value.code == 2
  69. assert "explicit DSH_HOME" in capsys.readouterr().err
  70. def test_python_dsh_command_executes_the_bundled_cli(
  71. monkeypatch: pytest.MonkeyPatch
  72. ) -> None:
  73. called: dict[str, object] = {}
  74. monkeypatch.setenv("DSH_HOME", "/explicit/home")
  75. monkeypatch.setattr(runtime, "resolve_bundled_launch_args", lambda: ("/runtime",))
  76. monkeypatch.setattr(runtime.sys, "argv", ["dsh", "plugin", "--profile", "sdk", "list"])
  77. def execvpe(file: str, args: tuple[str, ...], env: dict[str, str]) -> None:
  78. called.update(file=file, args=args, home=env.get("DSH_HOME"))
  79. monkeypatch.setattr(runtime.os, "execvpe", execvpe)
  80. main()
  81. assert called == {
  82. "file": "/runtime",
  83. "args": ("/runtime", "plugin", "--profile", "sdk", "list"),
  84. "home": "/explicit/home",
  85. }