mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 12:05:54 +00:00
Follow-up to the Box-only refactor. The previous commit removed the
local-fallback BRANCHES from every public method; this one removes the
HELPERS those branches called, which are now unreachable.
SkillService (service/skill.py): 787 → 449 lines
Removed: scan_directory (sync), _read_skill_package, _write_skill_md,
_resolve_create_field, _managed_skill_path,
_managed_install_root_for_package, _normalize_package_root,
_resolve_skill_path, _find_skill_entry, _discover_skill_directories,
_safe_extract_zip, _extract_uploaded_skill_to_temp,
_download_github_skill_to_temp, _resolve_github_source_root,
_build_preview_target_dir, _preview_skill_candidates,
_select_preview_candidates, _install_preview_candidates,
_preview_source_root, _resolve_installed_skills, plus the
module-level _FRONTMATTER_FIELDS and _build_skill_md.
Kept (still needed by the surviving GitHub-import path):
_download_github_asset, _download_github_skill_directory_as_zip,
_find_github_skill_archive_entry, _copy_github_skill_directory_to_zip,
_is_github_skill_md_url, _parse_github_skill_md_url,
_resolve_github_skill_md_package_name, _validate_github_asset_url,
_uploaded_skill_target_stem, _validate_skill_name.
Imports dropped: shutil, tempfile, yaml, ....utils.paths.
SkillManager (skill/manager.py): 187 → 88 lines
Removed: get_managed_skills_root, _discover_skill_directories,
_find_skill_entry, _load_skill_file, _normalize_package_root.
Imports dropped: datetime, parse_frontmatter, paths.
Tests:
- test_skill_service.py: drop the 3 sync scan_directory tests +
skill_service fixture + _create_skill_file helper
- test_skill_tools.py: drop test_load_skill_file_success; rename
TestSkillManagerPackageLoading → TestSkillManagerCache
Full unit suite: 277 passed, 1 skipped. ``ruff check`` clean.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
90 lines
3.9 KiB
Python
90 lines
3.9 KiB
Python
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
|
|
from langbot.pkg.api.http.service.skill import SkillService
|
|
|
|
|
|
class TestRequireBoxForWrite:
|
|
"""Box is the only source of truth for skills — there is no local
|
|
filesystem fallback. Every write and (most) read methods refuse cleanly
|
|
when the Box runtime is disabled, unreachable, or simply not installed."""
|
|
|
|
def _ap_with_disabled_box(self):
|
|
return SimpleNamespace(
|
|
skill_mgr=SimpleNamespace(reload_skills=AsyncMock()),
|
|
box_service=SimpleNamespace(
|
|
available=False,
|
|
enabled=False,
|
|
_connector_error='Box runtime is disabled in config (box.enabled = false)',
|
|
),
|
|
)
|
|
|
|
def _ap_with_failed_box(self):
|
|
return SimpleNamespace(
|
|
skill_mgr=SimpleNamespace(reload_skills=AsyncMock()),
|
|
box_service=SimpleNamespace(
|
|
available=False,
|
|
enabled=True,
|
|
_connector_error='docker daemon not running',
|
|
),
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_skill_refused_when_box_disabled(self):
|
|
service = SkillService(self._ap_with_disabled_box())
|
|
with pytest.raises(ValueError, match='disabled in config'):
|
|
await service.create_skill({'name': 'x'})
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_skill_refused_when_box_failed(self):
|
|
service = SkillService(self._ap_with_failed_box())
|
|
with pytest.raises(ValueError, match='docker daemon not running'):
|
|
await service.create_skill({'name': 'x'})
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_skill_refused_when_box_disabled(self):
|
|
service = SkillService(self._ap_with_disabled_box())
|
|
with pytest.raises(ValueError, match='Editing a skill requires the Box runtime'):
|
|
await service.update_skill('x', {})
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_write_skill_file_refused_when_box_disabled(self):
|
|
service = SkillService(self._ap_with_disabled_box())
|
|
with pytest.raises(ValueError, match='Editing skill files requires the Box runtime'):
|
|
await service.write_skill_file('x', 'a.txt', 'hi')
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_install_from_github_refused_when_box_disabled(self):
|
|
service = SkillService(self._ap_with_disabled_box())
|
|
with pytest.raises(ValueError, match='Installing a skill from GitHub'):
|
|
await service.install_from_github({'owner': 'o', 'repo': 'r', 'asset_url': 'https://example/x.zip'})
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_install_from_zip_upload_refused_when_box_disabled(self):
|
|
service = SkillService(self._ap_with_disabled_box())
|
|
with pytest.raises(ValueError, match='Installing a skill from upload'):
|
|
await service.install_from_zip_upload(file_bytes=b'', filename='x.zip')
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_skill_refused_when_box_service_missing_entirely(self):
|
|
"""No ap.box_service attribute at all (truly minimal setup):
|
|
Box is the only source of truth, so creation must still refuse."""
|
|
service = SkillService(SimpleNamespace(skill_mgr=SimpleNamespace(reload_skills=AsyncMock())))
|
|
with pytest.raises(ValueError, match='not initialised'):
|
|
await service.create_skill({'name': 'x'})
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_skills_returns_empty_when_box_unavailable(self):
|
|
"""list_skills should render an empty surface (not crash) so the
|
|
skills page can show a banner instead of a broken state."""
|
|
service = SkillService(self._ap_with_disabled_box())
|
|
assert await service.list_skills() == []
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_read_skill_file_refused_when_box_unavailable(self):
|
|
service = SkillService(self._ap_with_disabled_box())
|
|
with pytest.raises(ValueError, match='Reading a skill file'):
|
|
await service.read_skill_file('x', 'a.txt')
|