"""CLI tests: list, filter, install, prompt, force, and update (SPEC sections 3, 6, 7).""" import json import shutil import pytest from phronesis import cli from phronesis import harnesses from phronesis import registry from phronesis import state @pytest.fixture def config(tmp_path, monkeypatch): """An isolated state file and an isolated vibe home.""" monkeypatch.setenv("PHRONESIS_CONFIG", str(tmp_path / "cfg")) harnesses.HARNESS_REGISTRY["vibe"].home = tmp_path / "home" yield tmp_path / "cfg" harnesses.HARNESS_REGISTRY["vibe"].home = None def run(capsys, *argv): code = cli.main(list(argv)) out = capsys.readouterr() return code, out.out, out.err def test_bare_call_lists_every_skill(config, capsys): code, out, err = run(capsys) assert code == 0 assert out.startswith("Phronesis Version:") assert "python-tdd" in out and "1.2.0" in out and "development" in out def test_list_flag_matches_bare_call(config, capsys): assert run(capsys, "--list")[0] == 0 def test_version_flag(config, capsys): code, out, _ = run(capsys, "--version") assert code == 0 assert out.startswith("phronesis ") def test_category_filters_the_table(config, capsys): code, out, _ = run(capsys, "development") assert code == 0 assert "python-tdd" in out def test_unknown_category_prompts_close_matches(config, capsys): code, out, err = run(capsys, "developmint") assert code == 2 assert "did you mean" in err def test_unknown_skill_suggests(config, capsys): code, _, err = run(capsys, "python-tb", "--install") assert code == 2 assert "python-tdd" in err def test_show_one_skill_prints_install_hint(config, capsys): code, out, _ = run(capsys, "python-tdd") assert code == 0 assert out.count("python-tdd") >= 2 assert "install: phronesis python-tdd --install --harness vibe" in out def test_install_and_project_reject_each_other(config, capsys): code, _, err = run(capsys, "python-tdd", "--install", "--project") assert code == 2 def test_unknown_harness_exits_two(config, capsys): code, _, err = run(capsys, "python-tdd", "--install", "--harness", "colossus") assert code == 2 assert "vibe" in err def test_install_needs_one_skill_word(config, capsys): code, _, err = run(capsys, "--install") assert code == 2 def test_install_writes_skill_and_state(config, capsys): code, out, _ = run(capsys, "python-tdd", "--install") assert code == 0 target = config.parent / "home" / ".vibe" / "skills" / "python-tdd" / "SKILL.md" assert target.is_file() assert str(target) in out entries = state.load(state.state_path()) assert [e["name"] for e in entries] == ["python-tdd"] assert entries[0]["version"] == "1.2.0" assert entries[0]["scope"] == "global" def test_project_install_lands_in_the_project(config, capsys, tmp_path, monkeypatch): work = tmp_path / "work" work.mkdir() monkeypatch.chdir(work) code, _, _ = run(capsys, "python-tdd", "--project") assert code == 0 target = tmp_path / "work" / ".vibe" / "skills" / "python-tdd" / "SKILL.md" assert target.is_file() assert state.load(state.state_path())[0]["scope"] == "project" def test_prompt_shown_when_installed(config, capsys, monkeypatch, tmp_path): answers = iter(["y"]) monkeypatch.setattr("builtins.input", lambda prompt: answers.__next__()) run(capsys, "python-tdd", "--install") asked = run(capsys, "python-tdd", "--install") assert asked[0] == 0 def test_prompt_no_leaves_target_untouched(config, capsys, monkeypatch): monkeypatch.setattr("builtins.input", lambda prompt: "n") run(capsys, "python-tdd", "--install") target = config.parent / "home" / ".vibe" / "skills" / "python-tdd" / "SKILL.md" before = target.read_text(encoding="utf-8") code, out, err = run(capsys, "python-tdd", "--install") assert code == 0 assert "skipped" in out assert target.read_text(encoding="utf-8") == before def test_prompt_accepts_default_no(config, capsys, monkeypatch): monkeypatch.setattr("builtins.input", lambda prompt: "") run(capsys, "python-tdd", "--install") code, out, _ = run(capsys, "python-tdd", "--install") assert code == 0 assert "skipped" in out def test_prompt_eof_on_stdin_means_no(config, capsys, monkeypatch): def eof(prompt): raise EOFError monkeypatch.setattr("builtins.input", eof) run(capsys, "python-tdd", "--install") code, out, _ = run(capsys, "python-tdd", "--install") assert code == 0 assert "skipped" in out def test_force_overwrites_without_prompt(config, capsys, monkeypatch): def fail(prompt): raise AssertionError("force must not prompt") monkeypatch.setattr("builtins.input", fail) run(capsys, "python-tdd", "--install") code, out, _ = run(capsys, "python-tdd", "--install", "--force") assert code == 0 assert "installed" in out def test_short_force_flag_matches_long(config, capsys): code, out, _ = run(capsys, "python-tdd", "--install", "-f") assert code == 0 def test_update_refreshes_installed_skills(config, capsys): path = state.state_path() state.save([{"name": "python-tdd", "version": "1.0.0", "harness": "vibe", "scope": "global", "path": str(config.parent / "home" / ".vibe" / "skills" / "python-tdd" / "SKILL.md"), "installed": "2026-01-01"}], path) code, out, _ = run(capsys, "--update") assert code == 0 assert "1.0.0 -> 1.2.0" in out assert state.load(path)[0]["version"] == "1.2.0" def test_update_nothing_installed(config, capsys): code, out, _ = run(capsys, "--update") assert code == 0 assert "nothing installed" in out def test_update_warns_for_unbundled_skill(config, capsys): path = state.state_path() state.save([{"name": "ghost", "version": "1.0.0", "harness": "vibe", "scope": "global", "path": "/tmp/ghost/SKILL.md", "installed": "2026-01-01"}], path) code, out, err = run(capsys, "--update") assert code == 0 assert "no longer bundled" in err def test_update_reinstalls_a_missing_target(config, capsys, tmp_path): path = state.state_path() state.save([{"name": "python-tdd", "version": "1.1.0", "harness": "vibe", "scope": "global", "path": str(config.parent / "home" / ".vibe" / "skills" / "python-tdd" / "SKILL.md"), "installed": "2026-01-01"}], path) code, out, _ = run(capsys, "--update") assert code == 0 assert "1.1.0 -> 1.2.0" in out def test_state_entries_unique_by_scope(config, capsys, tmp_path, monkeypatch): run(capsys, "python-tdd", "--install") work = tmp_path / "work2" work.mkdir() monkeypatch.chdir(work) run(capsys, "python-tdd", "--project") entries = state.load(state.state_path()) assert sorted(e["scope"] for e in entries) == ["global", "project"] def test_installed_json_is_plain_json(config, capsys): run(capsys, "python-tdd", "--install") data = json.loads(state.state_path().read_text(encoding="utf-8")) assert set(data) == {"skills"} assert set(data["skills"][0]) == {"name", "version", "harness", "scope", "path", "installed"} # --- uninstall (SPEC items 6.6, 6.7, 8.4) def test_uninstall_removes_skill_and_state(config, capsys): run(capsys, "python-tdd", "--install") target = config.parent / "home" / ".vibe" / "skills" / "python-tdd" code, out, _ = run(capsys, "python-tdd", "--uninstall") assert code == 0 assert "removed python-tdd 1.2.0 (vibe, global)" in out assert not target.exists() assert state.load(state.state_path()) == [] def test_uninstall_project_scoped(config, capsys, tmp_path, monkeypatch): work = tmp_path / "work" work.mkdir() monkeypatch.chdir(work) run(capsys, "python-tdd", "--project") code, _, _ = run(capsys, "python-tdd", "--uninstall", "--project") assert code == 0 assert not (work / ".vibe" / "skills" / "python-tdd").exists() assert state.load(state.state_path()) == [] def test_uninstall_needs_the_skill_installed(config, capsys): code, _, err = run(capsys, "python-tdd", "--uninstall") assert code == 2 assert "not installed" in err def test_uninstall_refuses_a_non_skill_directory(config, capsys): target = config.parent / "home" / ".vibe" / "skills" / "not-a-skill" target.mkdir(parents=True) (target / "SKILL.md").write_text("random notes, no front matter", encoding="utf-8") code, _, err = run(capsys, "not-a-skill", "--uninstall") assert code == 2 assert "refusing to remove" in err assert (target / "SKILL.md").is_file() def test_uninstall_refuses_a_name_mismatch(config, capsys): skill_md_run = run(capsys, "python-tdd", "--install") target_dir = config.parent / "home" / ".vibe" / "skills" / "python-tdd" other = target_dir.parent / "other-name" other.mkdir() shutil.copytree(target_dir, other, dirs_exist_ok=True) code, _, err = run(capsys, "other-name", "--uninstall") assert code == 2 assert "does not match the directory" in err assert (other / "SKILL.md").is_file() def test_uninstall_works_without_a_state_entry(config, capsys): target = config.parent / "home" / ".vibe" / "skills" / "python-tdd" target.mkdir(parents=True) body = registry.SKILLS["python-tdd"].skill_md.read_text(encoding="utf-8") (target / "SKILL.md").write_text(body, encoding="utf-8") code, out, _ = run(capsys, "python-tdd", "--uninstall") assert code == 0 assert "removed" in out assert not target.exists() def test_install_and_uninstall_are_exclusive(config, capsys): code, _, err = run(capsys, "python-tdd", "--install", "--uninstall") assert code == 2 assert "pick one of --install or --uninstall" in err def test_uninstall_of_unbundled_name_is_allowed(config, capsys): target = config.parent / "home" / ".vibe" / "skills" / "retired-skill" target.mkdir(parents=True) (target / "SKILL.md").write_text( "---\nname: retired-skill\nversion: 0.9.0\ndescription: gone\n" "category: misc\n---\n", encoding="utf-8") code, out, _ = run(capsys, "retired-skill", "--uninstall") assert code == 0 assert not target.exists()