"""Tests for PackageService.""" from pathlib import Path import pytest from flow.core.config import AppConfig, FlowContext from flow.core.console import Console from flow.core.errors import FlowError from flow.core.platform import PlatformInfo from flow.core.runtime import SystemRuntime from flow.core import paths from flow.domain.packages.models import InstalledPackage, InstalledState from flow.services.packages import PackageService def _make_ctx(tmp_path, manifest=None): return FlowContext( config=AppConfig(), manifest=manifest or {}, platform=PlatformInfo(), console=Console(color=False), runtime=SystemRuntime(), ) class TestPackageService: def test_list_empty(self, tmp_path, monkeypatch, capsys): monkeypatch.setattr(paths, "INSTALLED_STATE", tmp_path / "installed.json") ctx = _make_ctx(tmp_path) svc = PackageService(ctx) svc.list_packages() assert "No packages" in capsys.readouterr().out def test_list_shows_installed(self, tmp_path, monkeypatch, capsys): state = InstalledState(packages={ "fd": InstalledPackage(name="fd", version="10.2", type="pkg"), }) state_path = tmp_path / "installed.json" import json state_path.parent.mkdir(parents=True, exist_ok=True) with open(state_path, "w") as f: json.dump(state.as_dict(), f) monkeypatch.setattr(paths, "INSTALLED_STATE", state_path) ctx = _make_ctx(tmp_path) svc = PackageService(ctx) svc.list_packages() output = capsys.readouterr().out assert "fd" in output assert "10.2" in output def test_remove_not_installed(self, tmp_path, monkeypatch, capsys): monkeypatch.setattr(paths, "INSTALLED_STATE", tmp_path / "installed.json") ctx = _make_ctx(tmp_path) svc = PackageService(ctx) svc.remove(["missing"]) assert "No matching" in capsys.readouterr().out def test_install_requires_args(self, tmp_path, monkeypatch): monkeypatch.setattr(paths, "INSTALLED_STATE", tmp_path / "installed.json") ctx = _make_ctx(tmp_path) svc = PackageService(ctx) with pytest.raises(FlowError, match="Specify"): svc.install()