test_bundled_runtime.py 3.8 KB

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