Skip to content

Commit a692b35

Browse files
macmac
authored andcommitted
test(extensions): cover skills-mode update lifecycle
Verify that extension updates match fresh installation, retain aliases, refresh Hermes global skills repeatedly, and restore generic skills after a missing-source failure. Assisted-by: OpenAI Codex (model: GPT-6, autonomous)
1 parent e210504 commit a692b35

2 files changed

Lines changed: 224 additions & 1 deletion

File tree

‎tests/integrations/test_integration_generic.py‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
from specify_cli.integrations.manifest import IntegrationManifest
1717
from specify_cli.integration_state import INTEGRATION_STATE_SCHEMA, write_integration_json
1818
from specify_cli.extensions import ExtensionCatalog, ExtensionError, ExtensionManager, HookExecutor
19-
from specify_cli import save_init_options
19+
from specify_cli import app, save_init_options
20+
from typer.testing import CliRunner
2021

2122

2223
@pytest.fixture
@@ -64,6 +65,59 @@ def generic_project(tmp_path, *, skills=False, commands_dir=".custom/commands"):
6465
return project
6566

6667

68+
def test_generic_skills_update_rolls_back_when_command_source_is_missing(
69+
tmp_path, generic_extension, monkeypatch,
70+
):
71+
"""Skills-mode updates retain source validation and restore old artifacts."""
72+
project = generic_project(tmp_path, skills=True)
73+
monkeypatch.chdir(project)
74+
manager = ExtensionManager(project)
75+
manager.install_from_directory(generic_extension, "1.0.0")
76+
artifact = project / ".custom/commands/speckit-sample-run/SKILL.md"
77+
original_artifact = artifact.read_bytes()
78+
original_registry = manager.registry.get("sample")
79+
80+
updated_source = tmp_path / "updated-source"
81+
shutil.copytree(generic_extension, updated_source)
82+
manifest_path = updated_source / "extension.yml"
83+
manifest = yaml.safe_load(manifest_path.read_text(encoding="utf-8"))
84+
manifest["extension"]["version"] = "2.0.0"
85+
manifest_path.write_text(yaml.safe_dump(manifest), encoding="utf-8")
86+
(updated_source / "commands" / "run.md").unlink()
87+
archive_path = tmp_path / "sample-update.zip"
88+
with zipfile.ZipFile(archive_path, "w") as archive:
89+
for path in updated_source.rglob("*"):
90+
if path.is_file():
91+
archive.write(path, path.relative_to(updated_source))
92+
93+
with (
94+
patch.object(
95+
ExtensionCatalog,
96+
"get_extension_info",
97+
return_value={
98+
"id": "sample",
99+
"name": "Sample",
100+
"version": "2.0.0",
101+
"_install_allowed": True,
102+
},
103+
),
104+
patch.object(
105+
ExtensionCatalog, "download_extension", return_value=archive_path
106+
),
107+
):
108+
result = CliRunner().invoke(
109+
app,
110+
["extension", "update", "sample"],
111+
input="y\n",
112+
catch_exceptions=True,
113+
)
114+
115+
assert result.exit_code == 1, result.output
116+
assert "missing source" in result.output.lower()
117+
assert ExtensionManager(project).registry.get("sample") == original_registry
118+
assert artifact.read_bytes() == original_artifact
119+
120+
67121
@pytest.mark.parametrize("skills", [False, True])
68122
def test_generic_extension_lifecycle(tmp_path, generic_extension, skills):
69123
project = generic_project(

‎tests/specify_cli/extensions/test_command_update.py‎

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import json
99
import shutil
10+
import zipfile
1011
from pathlib import Path
1112
from unittest.mock import patch
1213

@@ -696,6 +697,174 @@ def test_update_skips_catalog_entry_without_version(self, tmp_path):
696697
assert "test-ext: Missing catalog version (skipping)" in flat
697698
assert ExtensionManager(project_dir).registry.get("test-ext")["version"] == "1.0.0"
698699

700+
def test_update_preserves_skills_mode_registration(self, tmp_path, monkeypatch):
701+
"""Updating an extension must keep Claude skills in skills mode."""
702+
fake_home = tmp_path / "home"
703+
fake_home.mkdir()
704+
monkeypatch.setattr(Path, "home", lambda: fake_home)
705+
706+
project_dir = tmp_path / "project"
707+
(project_dir / ".specify").mkdir(parents=True)
708+
(project_dir / ".claude" / "skills").mkdir(parents=True)
709+
(project_dir / ".specify" / "init-options.json").write_text(
710+
json.dumps({"ai": "claude", "ai_skills": True, "script": "sh"}),
711+
encoding="utf-8",
712+
)
713+
714+
manager = ExtensionManager(project_dir)
715+
v1_dir = self._create_extension_source(tmp_path, "1.0.0")
716+
manifest_path = v1_dir / "extension.yml"
717+
manifest = yaml.safe_load(manifest_path.read_text(encoding="utf-8"))
718+
manifest["provides"]["commands"][0]["aliases"] = [
719+
"speckit.test-ext.alternate"
720+
]
721+
manifest_path.write_text(yaml.safe_dump(manifest), encoding="utf-8")
722+
manager.install_from_directory(v1_dir, "0.1.0")
723+
724+
skill_names = ["speckit-test-ext-hello", "speckit-test-ext-alternate"]
725+
command_names = [
726+
"speckit.test-ext.hello",
727+
"speckit.test-ext.alternate",
728+
]
729+
skill_files = [
730+
project_dir / ".claude" / "skills" / name / "SKILL.md"
731+
for name in skill_names
732+
]
733+
assert all(path.is_file() for path in skill_files)
734+
old_entry = manager.registry.get("test-ext")
735+
assert old_entry["registered_skills"] == []
736+
assert old_entry["registered_commands"] == {"claude": command_names}
737+
738+
v2_dir = self._create_extension_source(tmp_path, "2.0.0")
739+
manifest_path = v2_dir / "extension.yml"
740+
manifest = yaml.safe_load(manifest_path.read_text(encoding="utf-8"))
741+
manifest["provides"]["commands"][0]["aliases"] = [
742+
"speckit.test-ext.alternate"
743+
]
744+
manifest_path.write_text(yaml.safe_dump(manifest), encoding="utf-8")
745+
command_path = v2_dir / "commands" / "hello.md"
746+
command_path.write_text(
747+
command_path.read_text(encoding="utf-8") + "\nupdated command\n",
748+
encoding="utf-8",
749+
)
750+
zip_path = tmp_path / "test-ext-2.0.0.zip"
751+
with zipfile.ZipFile(zip_path, "w") as archive:
752+
for path in v2_dir.rglob("*"):
753+
if path.is_file():
754+
archive.write(path, path.relative_to(v2_dir))
755+
756+
runner = CliRunner()
757+
with (
758+
patch.object(Path, "cwd", return_value=project_dir),
759+
patch.object(
760+
ExtensionCatalog,
761+
"get_extension_info",
762+
return_value={
763+
"id": "test-ext",
764+
"name": "Test Extension",
765+
"version": "2.0.0",
766+
"_install_allowed": True,
767+
},
768+
),
769+
patch.object(
770+
ExtensionCatalog, "download_extension", return_value=zip_path
771+
),
772+
):
773+
result = runner.invoke(
774+
app,
775+
["extension", "update", "test-ext"],
776+
input="y\n",
777+
catch_exceptions=True,
778+
)
779+
780+
assert result.exit_code == 0, result.output
781+
updated = ExtensionManager(project_dir).registry.get("test-ext")
782+
assert updated["registered_skills"] == []
783+
assert updated["registered_commands"] == {"claude": command_names}
784+
fresh_project = tmp_path / "fresh-project"
785+
(fresh_project / ".specify").mkdir(parents=True)
786+
(fresh_project / ".claude" / "skills").mkdir(parents=True)
787+
(fresh_project / ".specify" / "init-options.json").write_text(
788+
json.dumps({"ai": "claude", "ai_skills": True, "script": "sh"}),
789+
encoding="utf-8",
790+
)
791+
ExtensionManager(fresh_project).install_from_directory(v2_dir, "0.1.0")
792+
for name, updated_path in zip(skill_names, skill_files, strict=True):
793+
fresh_path = fresh_project / ".claude" / "skills" / name / "SKILL.md"
794+
assert "updated command" in updated_path.read_text(encoding="utf-8")
795+
assert updated_path.read_text(encoding="utf-8") == fresh_path.read_text(
796+
encoding="utf-8"
797+
)
798+
799+
def test_consecutive_hermes_updates_refresh_global_skill(self, tmp_path, monkeypatch):
800+
"""Each update replaces the marker-owned skill in Hermes' home directory."""
801+
fake_home = tmp_path / "home"
802+
fake_home.mkdir()
803+
monkeypatch.setattr(Path, "home", lambda: fake_home)
804+
805+
project_dir = tmp_path / "hermes-project"
806+
(project_dir / ".specify").mkdir(parents=True)
807+
(project_dir / ".specify" / "init-options.json").write_text(
808+
json.dumps({"ai": "hermes", "ai_skills": True, "script": "sh"}),
809+
encoding="utf-8",
810+
)
811+
skill_file = (
812+
fake_home / ".hermes" / "skills" / "speckit-test-ext-hello" / "SKILL.md"
813+
)
814+
815+
def prepare_source(version):
816+
source = self._create_extension_source(tmp_path, version)
817+
command = source / "commands" / "hello.md"
818+
command.write_text(
819+
command.read_text(encoding="utf-8") + f"\nrelease {version}\n",
820+
encoding="utf-8",
821+
)
822+
return source
823+
824+
manager = ExtensionManager(project_dir)
825+
v1 = prepare_source("1.0.0")
826+
manager.install_from_directory(v1, "0.1.0")
827+
assert "release 1.0.0" in skill_file.read_text(encoding="utf-8")
828+
assert manager.registry.get("test-ext")["registered_commands"] == {
829+
"hermes": ["speckit.test-ext.hello"]
830+
}
831+
832+
runner = CliRunner()
833+
for version in ("2.0.0", "3.0.0"):
834+
source = prepare_source(version)
835+
archive_path = tmp_path / f"test-ext-{version}.zip"
836+
with zipfile.ZipFile(archive_path, "w") as archive:
837+
for path in source.rglob("*"):
838+
if path.is_file():
839+
archive.write(path, path.relative_to(source))
840+
with (
841+
patch.object(Path, "cwd", return_value=project_dir),
842+
patch.object(
843+
ExtensionCatalog,
844+
"get_extension_info",
845+
return_value={
846+
"id": "test-ext",
847+
"name": "Test Extension",
848+
"version": version,
849+
"_install_allowed": True,
850+
},
851+
),
852+
patch.object(
853+
ExtensionCatalog,
854+
"download_extension",
855+
return_value=archive_path,
856+
),
857+
):
858+
result = runner.invoke(
859+
app,
860+
["extension", "update", "test-ext"],
861+
input="y\n",
862+
catch_exceptions=True,
863+
)
864+
assert result.exit_code == 0, result.output
865+
assert f"release {version}" in skill_file.read_text(encoding="utf-8")
866+
assert ExtensionManager(project_dir).registry.get("test-ext")["version"] == version
867+
699868
def test_update_failure_rolls_back_registry_hooks_and_commands(self, tmp_path, monkeypatch):
700869
"""Failed update should restore original registry, hooks, and command files."""
701870
from specify_cli import app

0 commit comments

Comments
 (0)