test_bundled_runtime.py 3.8 KB

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