test_bundled_runtime.py 3.9 KB

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