test_bundled_runtime.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """Keyless boot tests for the production exe and development dsh carrier.
  2. Each carrier skips independently when absent. The dummy API key only satisfies
  3. adapter loading; initialize and shutdown do not call a model.
  4. """
  5. from __future__ import annotations
  6. import json
  7. from pathlib import Path
  8. import pytest
  9. from deepseek_harness import DeepSeekHarness, HarnessClient, HarnessConfig
  10. from deepseek_harness.errors import JsonRpcError, TransportClosedError
  11. from deepseek_harness_runtime import RUNTIME_MODE_ENV_VAR, resolve_bundled_launch_args
  12. _MODES = ("exe", "node")
  13. def _select_mode(mode: str, monkeypatch: pytest.MonkeyPatch) -> None:
  14. try:
  15. resolve_bundled_launch_args(mode)
  16. except FileNotFoundError as exc:
  17. pytest.skip(f"bundled {mode}-mode runtime unavailable on this machine: {exc}")
  18. monkeypatch.setenv(RUNTIME_MODE_ENV_VAR, mode)
  19. def _client(tmp_path: Path, mode: str, monkeypatch: pytest.MonkeyPatch, *patches: Path) -> HarnessClient:
  20. _select_mode(mode, monkeypatch)
  21. return HarnessClient(
  22. HarnessConfig(
  23. dsh_home=str(tmp_path / "home"),
  24. patches=tuple(str(patch) for patch in patches),
  25. cwd=str(tmp_path),
  26. env={
  27. # The lazily mounted adapter requires a key even without a model call.
  28. "DEEPSEEK_API_KEY": "sk-dummy-for-boot",
  29. "DEEPSEEK_BASE_URL": "http://127.0.0.1:9",
  30. "DSH_PERMISSION_MODE": "danger-full-access",
  31. "DSH_TELEMETRY_DISABLED": "1",
  32. },
  33. request_timeout_seconds=120,
  34. )
  35. )
  36. @pytest.mark.parametrize("mode", _MODES)
  37. def test_bundled_runtime_boots_the_sdk_profile(
  38. tmp_path: Path, mode: str, monkeypatch: pytest.MonkeyPatch
  39. ) -> None:
  40. with _client(tmp_path, mode, monkeypatch) as client:
  41. init = client.initialize(provider="deepseek-official", cwd=str(tmp_path), model="deepseek-v4-pro")
  42. assert init.serverInfo is not None
  43. assert init.serverInfo.name == "deepseek-harness-sdk-runtime"
  44. profile = json.loads((tmp_path / "home" / "profiles" / "sdk" / "package.json").read_text())
  45. assert profile["dsh"]["profile"]["bundles"] == [
  46. "@deepseek-ai/dsh-base",
  47. "@deepseek-ai/dsh-sdk-app",
  48. ]
  49. @pytest.mark.parametrize("mode", _MODES)
  50. def test_python_sdk_applies_an_ordered_profile_patch(
  51. tmp_path: Path, mode: str, monkeypatch: pytest.MonkeyPatch
  52. ) -> None:
  53. _select_mode(mode, monkeypatch)
  54. patch = tmp_path / "persona.patch.yml"
  55. patch.write_text(json.dumps([{
  56. "id": "system-prompt",
  57. "config": {"persona": "Python SDK ordered patch marker."},
  58. }]))
  59. harness = DeepSeekHarness(
  60. model="deepseek-v4-pro",
  61. cwd=str(tmp_path),
  62. dsh_home=str(tmp_path / "home"),
  63. patches=(str(patch),),
  64. env={"DSH_PERMISSION_MODE": "danger-full-access"},
  65. api_key="sk-dummy-for-boot",
  66. base_url="http://127.0.0.1:9",
  67. request_timeout_seconds=120,
  68. )
  69. with harness:
  70. pass
  71. @pytest.mark.parametrize("mode", _MODES)
  72. def test_bundled_runtime_surfaces_unbundled_plugin_failure(
  73. tmp_path: Path, mode: str, monkeypatch: pytest.MonkeyPatch
  74. ) -> None:
  75. patch = tmp_path / "missing.patch.yml"
  76. patch.write_text(json.dumps([{
  77. "insert": [{"id": "missing", "name": "@deepseek-ai/dsh-does-not-exist"}],
  78. }]))
  79. client = _client(tmp_path, mode, monkeypatch, patch)
  80. client.start()
  81. try:
  82. with pytest.raises((JsonRpcError, TransportClosedError, TimeoutError)) as excinfo:
  83. client.initialize(provider="deepseek-official", cwd=str(tmp_path), model="deepseek-v4-pro")
  84. finally:
  85. client.close()
  86. assert "@deepseek-ai/dsh-does-not-exist" in str(excinfo.value)