Skip to content

Commit 28214a2

Browse files
test: cover manifest-backed artifact parity
Assisted-by: GitHub Copilot (model: GPT-5.6, autonomous) Co-authored-by: nicolehaugen <[email protected]>
1 parent 7d52a50 commit 28214a2

3 files changed

Lines changed: 116 additions & 96 deletions

File tree

‎tests/conftest.py‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
import shutil
66
import subprocess
77
import sys
8+
from pathlib import Path
89

910
import pytest
11+
import yaml
12+
13+
from specify_cli.presets import PresetRegistry
1014

1115
_ANSI_ESCAPE_RE = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]")
1216

@@ -63,6 +67,69 @@ def _has_working_bash() -> bool:
6367
)
6468

6569

70+
def install_preset(
71+
project_root: Path, pack_id: str, provides: dict, priority: int = 10
72+
) -> Path:
73+
"""Create a registered preset with a validated modern manifest."""
74+
pack_dir = project_root / ".specify" / "presets" / pack_id
75+
pack_dir.mkdir(parents=True)
76+
templates: list[dict[str, str]] = []
77+
78+
def _default_file(kind: str, name: str) -> str:
79+
if kind == "command":
80+
return f"commands/{name}.md"
81+
if kind == "script":
82+
return f"scripts/{name}.sh"
83+
return f"templates/{name}.md"
84+
85+
for entry in provides.get("templates", []):
86+
if not isinstance(entry, dict):
87+
continue
88+
entry_type = entry.get("type", "template")
89+
if not isinstance(entry_type, str) or entry_type not in (
90+
"command",
91+
"template",
92+
"script",
93+
):
94+
continue
95+
name = entry.get("name")
96+
if not isinstance(name, str):
97+
continue
98+
normalized = dict(entry)
99+
normalized["type"] = entry_type
100+
normalized.setdefault("file", _default_file(entry_type, name))
101+
templates.append(normalized)
102+
103+
for kind_key, entry_type in (("commands", "command"), ("scripts", "script")):
104+
for entry in provides.get(kind_key, []):
105+
if not isinstance(entry, dict):
106+
continue
107+
name = entry.get("name")
108+
if not isinstance(name, str):
109+
continue
110+
normalized = dict(entry)
111+
normalized["type"] = entry_type
112+
normalized.setdefault("file", _default_file(entry_type, name))
113+
templates.append(normalized)
114+
115+
manifest = {
116+
"schema_version": "1.0",
117+
"preset": {
118+
"id": pack_id,
119+
"name": f"Test preset {pack_id}",
120+
"version": "1.0.0",
121+
"description": f"Test preset {pack_id}",
122+
},
123+
"requires": {"speckit_version": ">=1.0.0"},
124+
"provides": {"templates": templates},
125+
}
126+
(pack_dir / "preset.yml").write_text(yaml.safe_dump(manifest), encoding="utf-8")
127+
PresetRegistry(project_root / ".specify" / "presets").add(
128+
pack_id, {"priority": priority, "version": "1.0.0"}
129+
)
130+
return pack_dir
131+
132+
66133
def strip_ansi(text: str) -> str:
67134
"""Remove ANSI escape codes from Rich-formatted CLI output."""
68135
return _ANSI_ESCAPE_RE.sub("", text)

‎tests/test_artifact_command.py‎

Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
_preset_display_name,
2929
)
3030
from specify_cli.extensions import ExtensionRegistry
31-
from specify_cli.presets import PresetResolver
31+
from specify_cli.presets import PresetRegistry, PresetResolver
32+
from tests.conftest import install_preset
3233

3334

3435
ERROR_REGEX = re.compile(
@@ -61,67 +62,6 @@ def non_project(tmp_path: Path) -> Path:
6162
return root
6263

6364

64-
def _install_preset(project_root: Path, pack_id: str, provides: dict, priority: int = 10) -> Path:
65-
"""Drop a minimal preset onto disk and register it in the ``.registry`` file."""
66-
pack_dir = project_root / ".specify" / "presets" / pack_id
67-
pack_dir.mkdir(parents=True)
68-
templates: list[dict[str, str]] = []
69-
70-
def _default_file(kind: str, name: str) -> str:
71-
if kind == "command":
72-
return f"commands/{name}.md"
73-
if kind == "script":
74-
return f"scripts/{name}.sh"
75-
return f"templates/{name}.md"
76-
77-
for entry in provides.get("templates", []):
78-
if not isinstance(entry, dict):
79-
continue
80-
entry_type = entry.get("type", "template")
81-
if not isinstance(entry_type, str) or entry_type not in ("command", "template", "script"):
82-
continue
83-
name = entry.get("name")
84-
if not isinstance(name, str):
85-
continue
86-
normalized = dict(entry)
87-
normalized["type"] = entry_type
88-
normalized.setdefault("file", _default_file(entry_type, name))
89-
templates.append(normalized)
90-
91-
for kind_key, entry_type in (("commands", "command"), ("scripts", "script")):
92-
for entry in provides.get(kind_key, []):
93-
if not isinstance(entry, dict):
94-
continue
95-
name = entry.get("name")
96-
if not isinstance(name, str):
97-
continue
98-
normalized = dict(entry)
99-
normalized["type"] = entry_type
100-
normalized.setdefault("file", _default_file(entry_type, name))
101-
templates.append(normalized)
102-
103-
manifest = {
104-
"schema_version": "1.0",
105-
"preset": {
106-
"id": pack_id,
107-
"name": f"Test preset {pack_id}",
108-
"version": "1.0.0",
109-
"description": f"Test preset {pack_id}",
110-
},
111-
"requires": {"speckit_version": ">=1.0.0"},
112-
"provides": {"templates": templates},
113-
}
114-
(pack_dir / "preset.yml").write_text(yaml.safe_dump(manifest), encoding="utf-8")
115-
registry_path = project_root / ".specify" / "presets" / ".registry"
116-
if registry_path.is_file():
117-
registry = json.loads(registry_path.read_text(encoding="utf-8"))
118-
else:
119-
registry = {"schema_version": "1.0.0", "presets": {}}
120-
registry["presets"][pack_id] = {"priority": priority, "version": "1.0.0"}
121-
registry_path.write_text(json.dumps(registry), encoding="utf-8")
122-
return pack_dir
123-
124-
12565
# ---------------------------------------------------------------------------
12666
# Contract tests — matching artifact-list.schema.json
12767
# ---------------------------------------------------------------------------
@@ -307,7 +247,7 @@ def test_active_preset_description_overrides_hidden_core_description(
307247
"---\ndescription: Core description\n---\n", encoding="utf-8"
308248
)
309249

310-
pack = _install_preset(
250+
pack = install_preset(
311251
spec_kit_project,
312252
"override-preset",
313253
{
@@ -335,7 +275,7 @@ def test_higher_precedence_preset_description_wins(self, spec_kit_project: Path)
335275
not leak through just because it happens to be enumerated first
336276
alphabetically.
337277
"""
338-
pack_low = _install_preset(
278+
pack_low = install_preset(
339279
spec_kit_project,
340280
"aaa-low-priority-preset",
341281
{"templates": [{"name": "shared-artifact", "description": "Loser description"}]},
@@ -346,7 +286,7 @@ def test_higher_precedence_preset_description_wins(self, spec_kit_project: Path)
346286
"# Loser\n", encoding="utf-8"
347287
)
348288

349-
pack_high = _install_preset(
289+
pack_high = install_preset(
350290
spec_kit_project,
351291
"zzz-high-priority-preset",
352292
{"templates": [{"name": "shared-artifact", "description": "Winner description"}]},
@@ -477,7 +417,7 @@ def test_ambiguous_artifact_message(self, spec_kit_project: Path):
477417
# Register a preset that contributes 'shared-name' as both a
478418
# template and a script — the info lookup with no kind hint should
479419
# then be ambiguous.
480-
pack = _install_preset(
420+
pack = install_preset(
481421
spec_kit_project,
482422
"test-ambig",
483423
{
@@ -509,7 +449,7 @@ def test_info_rejects_corrupt_extension_registry(self, spec_kit_project: Path):
509449

510450
class TestKindHint:
511451
def test_kind_flag_disambiguates(self, spec_kit_project: Path):
512-
_install_preset(
452+
install_preset(
513453
spec_kit_project,
514454
"test-kind",
515455
{"templates": [{"name": "dup", "description": "t"}],
@@ -694,7 +634,7 @@ def test_output_is_utf8_without_bom(self, spec_kit_project: Path, monkeypatch: p
694634

695635
class TestStackComposition:
696636
def test_preset_command_uses_entry_type(self, spec_kit_project: Path):
697-
pack = _install_preset(
637+
pack = install_preset(
698638
spec_kit_project,
699639
"test-command",
700640
{
@@ -719,7 +659,7 @@ def test_preset_command_uses_entry_type(self, spec_kit_project: Path):
719659
def test_preset_single_segment_command_id_from_list_is_resolvable(
720660
self, spec_kit_project: Path
721661
):
722-
pack = _install_preset(
662+
pack = install_preset(
723663
spec_kit_project,
724664
"test-single-command",
725665
{"commands": [{"name": "specify", "description": "single segment"}]},
@@ -739,7 +679,7 @@ def test_preset_single_segment_command_id_from_list_is_resolvable(
739679

740680
def test_preset_replace_hides_core(self, spec_kit_project: Path):
741681
# Install a preset that replaces the constitution command.
742-
pack = _install_preset(
682+
pack = install_preset(
743683
spec_kit_project,
744684
"test-replace",
745685
{"commands": [{"name": "speckit.constitution", "description": "override"}]},
@@ -853,7 +793,11 @@ def test_malformed_dotted_override_is_not_forced_to_command(
853793
assert "command:speckit..local" not in ids
854794

855795
def test_unregistered_preset_template_without_manifest(self, spec_kit_project: Path):
856-
pack_dir = _install_preset(spec_kit_project, "legacy-preset", provides={"templates": []})
796+
pack_dir = spec_kit_project / ".specify" / "presets" / "legacy-preset"
797+
pack_dir.mkdir()
798+
PresetRegistry(pack_dir.parent).add(
799+
"legacy-preset", {"priority": 10, "version": "1.0.0"}
800+
)
857801
preset_templates_dir = pack_dir / "templates"
858802
preset_templates_dir.mkdir()
859803
(preset_templates_dir / "legacy-preset-template.md").write_text(

‎tests/test_artifact_command_parity.py‎

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,10 @@
1212
from pathlib import Path
1313

1414
import pytest
15-
import yaml
1615

1716
from specify_cli.artifacts import ArtifactCatalog
1817
from specify_cli.presets import PresetResolver
19-
20-
21-
def _install_preset(project_root: Path, pack_id: str, provides: dict, priority: int = 10) -> Path:
22-
pack_dir = project_root / ".specify" / "presets" / pack_id
23-
pack_dir.mkdir(parents=True)
24-
manifest = {
25-
"id": pack_id,
26-
"version": "1.0.0",
27-
"metadata": {"name": f"Test preset {pack_id}"},
28-
"provides": provides,
29-
}
30-
(pack_dir / "preset.yml").write_text(yaml.safe_dump(manifest), encoding="utf-8")
31-
registry_path = project_root / ".specify" / "presets" / ".registry"
32-
if registry_path.is_file():
33-
registry = json.loads(registry_path.read_text(encoding="utf-8"))
34-
else:
35-
registry = {"schema_version": "1.0.0", "presets": {}}
36-
registry["presets"][pack_id] = {"priority": priority, "version": "1.0.0"}
37-
registry_path.write_text(json.dumps(registry), encoding="utf-8")
38-
return pack_dir
18+
from tests.conftest import install_preset
3919

4020

4121
@pytest.fixture
@@ -53,7 +33,7 @@ class TestManifestPathIsPosix:
5333
"""The ``manifestPath`` field MUST use forward slashes on every OS."""
5434

5535
def test_no_backslashes(self, spec_kit_project: Path):
56-
pack = _install_preset(
36+
pack = install_preset(
5737
spec_kit_project,
5838
"test-posix",
5939
{"commands": [{"name": "speckit.constitution", "description": "d"}]},
@@ -70,7 +50,7 @@ def test_no_backslashes(self, spec_kit_project: Path):
7050
assert "\\" not in path, f"backslash leak: {path!r}"
7151

7252
def test_never_absolute(self, spec_kit_project: Path):
73-
pack = _install_preset(
53+
pack = install_preset(
7454
spec_kit_project,
7555
"test-rel",
7656
{"commands": [{"name": "speckit.constitution", "description": "d"}]},
@@ -93,7 +73,7 @@ class TestResolverParity:
9373
"""The ``active: true`` row must be what :meth:`resolve_content` would pick."""
9474

9575
def test_active_layer_matches_resolver(self, spec_kit_project: Path):
96-
pack = _install_preset(
76+
pack = install_preset(
9777
spec_kit_project,
9878
"test-parity",
9979
{"commands": [{"name": "speckit.constitution", "description": "override"}]},
@@ -115,6 +95,36 @@ def test_active_layer_matches_resolver(self, spec_kit_project: Path):
11595
assert "body-from-preset" in winner
11696
assert active["layer"] == "preset"
11797

98+
def test_manifest_declared_artifact_matches_resolver(self, spec_kit_project: Path):
99+
pack = install_preset(
100+
spec_kit_project,
101+
"test-manifest-parity",
102+
{
103+
"templates": [
104+
{
105+
"type": "command",
106+
"name": "speckit.manifest-declared",
107+
"file": "commands/differently-named.md",
108+
"description": "manifest contribution",
109+
}
110+
]
111+
},
112+
)
113+
(pack / "commands").mkdir()
114+
(pack / "commands" / "differently-named.md").write_text(
115+
"body-from-manifest", encoding="utf-8"
116+
)
117+
118+
catalog = ArtifactCatalog(spec_kit_project)
119+
info = catalog.get_artifact_info("speckit.manifest-declared")
120+
active = next(layer for layer in info["stack"] if layer["active"])
121+
winner = PresetResolver(spec_kit_project).resolve_content(
122+
"speckit.manifest-declared", template_type="command"
123+
)
124+
125+
assert winner == "body-from-manifest"
126+
assert active["lookupId"] == "preset:test-manifest-parity:command:speckit.manifest-declared"
127+
118128

119129
class TestJSONShape:
120130
"""Reasserts JSON-envelope invariants at the whole-payload level."""
@@ -136,4 +146,3 @@ def test_terminated_by_single_newline(self, spec_kit_project: Path):
136146

137147
def test_module_imports():
138148
_ = ArtifactCatalog
139-

0 commit comments

Comments
 (0)