test_builtin_packs.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. tests/test_builtin_packs.py — Unit tests for builtin_packs.ensure_builtin_packs_installed().
  3. Verifies:
  4. - All 3 research packs are installed into a fresh directory.
  5. - Second call is idempotent (no re-install, no error).
  6. - Missing source dir is handled gracefully (no exception).
  7. - Per-pack failures don't abort the remaining installs.
  8. """
  9. from __future__ import annotations
  10. import os
  11. from pathlib import Path
  12. from unittest.mock import patch
  13. import pytest
  14. from agentpaas.engine.builtin_packs import (
  15. BUILTIN_PACK_IDS,
  16. ensure_builtin_packs_installed,
  17. )
  18. from agentpaas.engine.agentpack_store import get_installed, list_installed
  19. # ── helpers ──────────────────────────────────────────────────────────────────
  20. def _packs_src() -> Path | None:
  21. """Locate agentexample/agentpacks/ relative to repo root."""
  22. here = Path(__file__).resolve().parent.parent # repo root
  23. candidate = here / "agentexample" / "agentpacks"
  24. return candidate if candidate.is_dir() else None
  25. # ── tests ─────────────────────────────────────────────────────────────────────
  26. @pytest.mark.skipif(
  27. _packs_src() is None,
  28. reason="agentexample/agentpacks/ not present (running outside repo)",
  29. )
  30. def test_ensure_installs_all_builtin_packs(tmp_path):
  31. """Fresh packs_dir → all 3 built-in packs installed."""
  32. packs_dir = str(tmp_path / "packs")
  33. ensure_builtin_packs_installed(packs_dir)
  34. installed_ids = {p.id for p in list_installed(packs_dir)}
  35. for pack_id in BUILTIN_PACK_IDS:
  36. assert pack_id in installed_ids, f"pack {pack_id} was not installed"
  37. @pytest.mark.skipif(
  38. _packs_src() is None,
  39. reason="agentexample/agentpacks/ not present",
  40. )
  41. def test_ensure_idempotent(tmp_path):
  42. """Calling twice does not raise and does not corrupt the installs."""
  43. packs_dir = str(tmp_path / "packs")
  44. ensure_builtin_packs_installed(packs_dir)
  45. first_count = len(list_installed(packs_dir))
  46. ensure_builtin_packs_installed(packs_dir) # second call
  47. second_count = len(list_installed(packs_dir))
  48. assert first_count == second_count == len(BUILTIN_PACK_IDS)
  49. def test_ensure_graceful_when_no_source_dir(tmp_path):
  50. """If agentexample/agentpacks/ cannot be found, no exception is raised."""
  51. packs_dir = str(tmp_path / "packs")
  52. with patch(
  53. "agentpaas.engine.builtin_packs._find_packs_src",
  54. return_value=None,
  55. ):
  56. # Must not raise
  57. ensure_builtin_packs_installed(packs_dir)
  58. # Nothing installed (source was unavailable)
  59. assert list_installed(packs_dir) == []
  60. @pytest.mark.skipif(
  61. _packs_src() is None,
  62. reason="agentexample/agentpacks/ not present",
  63. )
  64. def test_ensure_continues_after_per_pack_failure(tmp_path, monkeypatch):
  65. """A failure in the first pack does not block the remaining two."""
  66. packs_dir = str(tmp_path / "packs")
  67. first_pack = BUILTIN_PACK_IDS[0]
  68. rest = BUILTIN_PACK_IDS[1:]
  69. import agentpaas.engine.agentpack_store as _store
  70. original_get = _store.get_installed
  71. call_count = {"n": 0}
  72. def patched_get(d, pid, v=None):
  73. call_count["n"] += 1
  74. if pid == first_pack and call_count["n"] == 1:
  75. raise RuntimeError("simulated failure for first pack")
  76. return original_get(d, pid, v)
  77. # Patch the function on the source module so the local import inside
  78. # ensure_builtin_packs_installed() picks up the patched version.
  79. monkeypatch.setattr(_store, "get_installed", patched_get)
  80. # Must not raise even though first pack fails
  81. ensure_builtin_packs_installed(packs_dir)
  82. installed_ids = {p.id for p in list_installed(packs_dir)}
  83. for pack_id in rest:
  84. assert pack_id in installed_ids, (
  85. f"pack {pack_id} should have been installed despite earlier failure"
  86. )