test_bundled_runtime.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """Keyless boot tests for the production exe and development node 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. from pathlib import Path
  7. import pytest
  8. from deepseek_harness import DeepSeekHarness, HarnessClient, HarnessConfig
  9. from deepseek_harness.errors import TransportClosedError
  10. from deepseek_harness_runtime import resolve_bundled_launch_args
  11. _MODES = ("exe", "node")
  12. # The config must include the JSON-RPC serving plugin.
  13. _CORDIS_YML = """\
  14. - id: jsonrpc
  15. name: '@deepseek-ai/dsh-jsonrpc'
  16. - id: agent-core
  17. name: '@deepseek-ai/dsh-agent-spine-demo'
  18. - id: sessions
  19. name: '@deepseek-ai/dsh-session-persistence-jsonl'
  20. config:
  21. root: './sessions'
  22. - id: bash
  23. name: '@deepseek-ai/dsh-bash-local'
  24. config:
  25. cwd: '.'
  26. - id: todo
  27. name: '@deepseek-ai/dsh-tool-todo'
  28. """
  29. def _launch_args(mode: str) -> tuple[str, ...]:
  30. try:
  31. return resolve_bundled_launch_args(mode)
  32. except FileNotFoundError as exc:
  33. pytest.skip(f"bundled {mode}-mode runtime unavailable on this machine: {exc}")
  34. def _client(tmp_path: Path, launch_args: tuple[str, ...]) -> HarnessClient:
  35. return HarnessClient(
  36. HarnessConfig(
  37. launch_args_override=launch_args,
  38. cwd=str(tmp_path),
  39. env={
  40. "DSH_CORDIS_CONFIG": "./cordis.yml",
  41. "DSH_SESSION_ROOT": str(tmp_path / "sessions"),
  42. "DSH_CWD": str(tmp_path),
  43. # The lazily mounted adapter requires a key even without a model call.
  44. "DEEPSEEK_API_KEY": "sk-dummy-for-boot",
  45. "DEEPSEEK_BASE_URL": "http://127.0.0.1:9",
  46. },
  47. request_timeout_seconds=120,
  48. )
  49. )
  50. @pytest.mark.parametrize("mode", _MODES)
  51. def test_bundled_runtime_boots_a_cordis_config(tmp_path: Path, mode: str) -> None:
  52. launch_args = _launch_args(mode)
  53. (tmp_path / "cordis.yml").write_text(_CORDIS_YML)
  54. with _client(tmp_path, launch_args) as client:
  55. init = client.initialize(cwd=str(tmp_path), model="deepseek-v4-pro")
  56. assert init.serverInfo is not None
  57. assert init.serverInfo.name == "deepseek-harness-sdk-runtime"
  58. @pytest.mark.parametrize("mode", _MODES)
  59. def test_bundled_runtime_surfaces_unbundled_plugin_failure(tmp_path: Path, mode: str) -> None:
  60. launch_args = _launch_args(mode)
  61. (tmp_path / "cordis.yml").write_text(
  62. "- id: missing\n name: '@deepseek-ai/dsh-does-not-exist'\n"
  63. )
  64. client = _client(tmp_path, launch_args)
  65. client.start()
  66. try:
  67. with pytest.raises((TransportClosedError, TimeoutError)) as excinfo:
  68. client.initialize(cwd=str(tmp_path), model="deepseek-v4-pro")
  69. finally:
  70. client.close()
  71. assert "@deepseek-ai/dsh-does-not-exist" in str(excinfo.value)
  72. @pytest.mark.parametrize("mode", _MODES)
  73. @pytest.mark.parametrize("ambient_config", [None, ""], ids=["unset", "empty-counts-as-absent"])
  74. def test_zero_config_run_injects_bundled_default_cordis_config(
  75. tmp_path: Path, mode: str, ambient_config: str | None, monkeypatch: pytest.MonkeyPatch
  76. ) -> None:
  77. _launch_args(mode) # skip early when this carrier is unavailable
  78. monkeypatch.setenv("DSH_RUNTIME_MODE", mode)
  79. if ambient_config is None:
  80. monkeypatch.delenv("DSH_CORDIS_CONFIG", raising=False)
  81. else:
  82. monkeypatch.setenv("DSH_CORDIS_CONFIG", ambient_config)
  83. harness = DeepSeekHarness(
  84. model="deepseek-v4-pro",
  85. cwd=str(tmp_path),
  86. session_root=str(tmp_path / "sessions"),
  87. api_key="sk-dummy-for-boot",
  88. base_url="http://127.0.0.1:9",
  89. request_timeout_seconds=120,
  90. )
  91. with harness:
  92. pass