Debian Codegraph @

tests/unit_tests/test_plugin.py

raw
1 """The marketplace entry, and the plugin it makes of the skill directory.
2
3 `hsql --skill -o …` is the install that needs no network; this is the one that
4 needs no hsql. `.claude-plugin/marketplace.json` at the repo root names
5 `src/harlequin/hsql/skill` as a plugin, and the `.claude-plugin/plugin.json`
6 inside that directory is what makes it one -- no second repo, no copied file,
7 no build step.
8
9 What that costs is three manifests that have to go on agreeing, and none of
10 them fails loudly: a source path left behind by a move, or a version left
11 behind by a release, is a broken install for everyone who has already added
12 the marketplace.
13 """
14
15 from __future__ import annotations
16
17 import importlib.util
18 import json
19 import os
20 import re
21 import shutil
22 import subprocess
23 import sys
24 from importlib.metadata import version
25 from pathlib import Path
26 from types import ModuleType
27 from typing import Any
28
29 import pytest
30 import yaml
31
32 from harlequin.hsql.modes import skill as skill_mode
33
34 if sys.version_info >= (3, 11):
35 import tomllib
36 else:
37 import tomli as tomllib
38
39 REPO_ROOT = Path(__file__).resolve().parents[2]
40 MARKETPLACE_PATH = REPO_ROOT / ".claude-plugin" / "marketplace.json"
41 PLUGIN_PATH = skill_mode.SKILL_DIR / ".claude-plugin" / "plugin.json"
42
43 BUMP_SCRIPT_PATH = REPO_ROOT / "scripts" / "bump_versions.py"
44
45 PACKAGED_PLUGIN_MANIFEST = "src/harlequin/hsql/skill/.claude-plugin/plugin.json"
46 """The manifest's path from the repo root, as the build targets spell it."""
47
48
49 def skill_name() -> str:
50 """The name in `SKILL.md`'s frontmatter, which is the skill's own."""
51 match = re.match(
52 r"^---\n(.*?)\n---\n", skill_mode.text().decode("utf-8"), re.DOTALL
53 )
54 assert match is not None
55 name: str = yaml.safe_load(match.group(1))["name"]
56 return name
57
58
59 @pytest.fixture(scope="module")
60 def marketplace() -> dict[str, Any]:
61 data: dict[str, Any] = json.loads(MARKETPLACE_PATH.read_text(encoding="utf-8"))
62 return data
63
64
65 @pytest.fixture(scope="module")
66 def plugin() -> dict[str, Any]:
67 data: dict[str, Any] = json.loads(PLUGIN_PATH.read_text(encoding="utf-8"))
68 return data
69
70
71 @pytest.fixture(scope="module")
72 def bumper() -> ModuleType:
73 """The release script, loaded from `scripts/`, which is not a package."""
74 spec = importlib.util.spec_from_file_location("bump_versions", BUMP_SCRIPT_PATH)
75 assert spec is not None and spec.loader is not None
76 module = importlib.util.module_from_spec(spec)
77 spec.loader.exec_module(module)
78 return module
79
80
81 @pytest.fixture(scope="module")
82 def entry(marketplace: dict[str, Any]) -> dict[str, Any]:
83 """The one plugin the marketplace offers."""
84 plugins = marketplace["plugins"]
85 assert len(plugins) == 1
86 offered: dict[str, Any] = plugins[0]
87 return offered
88
89
90 # --- the manifests -----------------------------------------------------------
91
92
93 def test_the_marketplace_entry_names_a_source_that_exists(
94 entry: dict[str, Any],
95 ) -> None:
96 """The source is a path inside this repo, which is what saves a second one."""
97 source = (REPO_ROOT / entry["source"]).resolve()
98 assert source == skill_mode.SKILL_DIR
99 assert (source / skill_mode.FILENAME).is_file()
100
101
102 def test_the_plugin_manifest_sits_inside_the_source_directory() -> None:
103 """`.claude-plugin/` beside `SKILL.md` is what makes the directory a plugin
104 rather than a folder the marketplace points at."""
105 assert PLUGIN_PATH.parent.parent == skill_mode.SKILL_DIR
106 assert PLUGIN_PATH.is_file()
107
108
109 def test_one_name_names_the_plugin_everywhere(
110 entry: dict[str, Any], plugin: dict[str, Any]
111 ) -> None:
112 """`/plugin install hsql@harlequin` is the entry's name and the manifest's,
113 and the skill it installs answers to the same one."""
114 assert entry["name"] == plugin["name"] == skill_name() == "hsql"
115
116
117 def test_the_plugin_version_is_the_version_it_ships_with(
118 plugin: dict[str, Any],
119 ) -> None:
120 """The plugin is the skill for one release of hsql, so it carries that
121 release's number -- and `release.yml` is what keeps this true."""
122 assert plugin["version"] == version("harlequin")
123
124
125 def test_a_release_bumps_the_version_and_rewrites_nothing_else(
126 bumper: ModuleType, tmp_path: Path
127 ) -> None:
128 """A manifest the release script would reformat is one nobody can read a
129 version change out of."""
130 assert bumper.PLUGIN_PATH == PLUGIN_PATH
131 committed = PLUGIN_PATH.read_bytes()
132 manifest = tmp_path / "plugin.json"
133 manifest.write_bytes(committed)
134 bumper.write_plugin_version("9.9.9", manifest)
135 assert manifest.read_bytes() == committed.replace(
136 f'"version": "{version("harlequin")}"'.encode(), b'"version": "9.9.9"'
137 )
138
139
140 def test_the_plugin_manifest_ships_in_the_wheel_and_the_sdist() -> None:
141 """hatchling skips dot-directories, so the manifest is included by name in
142 both targets. Losing either is silent: the build succeeds without it."""
143 targets = tomllib.loads((REPO_ROOT / "pyproject.toml").read_text(encoding="utf-8"))[
144 "tool"
145 ]["hatch"]["build"]["targets"]
146 for target in ("wheel", "sdist"):
147 assert PACKAGED_PLUGIN_MANIFEST in targets[target]["force-include"]
148
149
150 # --- what the plugin tooling makes of them -----------------------------------
151
152
153 @pytest.mark.skipif(
154 shutil.which("claude") is None, reason="the claude CLI is what reads these"
155 )
156 @pytest.mark.parametrize("target", [".", "src/harlequin/hsql/skill"])
157 def test_claude_plugin_validate_passes(target: str, tmp_path: Path) -> None:
158 """The marketplace and the plugin, checked by the tool that installs them.
159
160 `--strict` because a warning here -- an unrecognized field, or metadata
161 that is missing -- is a manifest some of the tooling reads differently.
162 """
163 proc = subprocess.run(
164 ["claude", "plugin", "validate", target, "--strict"],
165 cwd=REPO_ROOT,
166 capture_output=True,
167 text=True,
168 env={**os.environ, "CLAUDE_CONFIG_DIR": str(tmp_path)},
169 )
170 assert proc.returncode == 0, proc.stdout + proc.stderr