| 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_plugin_version.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( |
| 75 |
"bump_plugin_version", BUMP_SCRIPT_PATH |
| 76 |
) |
| 77 |
assert spec is not None and spec.loader is not None |
| 78 |
module = importlib.util.module_from_spec(spec) |
| 79 |
spec.loader.exec_module(module) |
| 80 |
return module |
| 81 |
|
| 82 |
|
| 83 |
@pytest.fixture(scope="module") |
| 84 |
def entry(marketplace: dict[str, Any]) -> dict[str, Any]: |
| 85 |
"""The one plugin the marketplace offers.""" |
| 86 |
plugins = marketplace["plugins"] |
| 87 |
assert len(plugins) == 1 |
| 88 |
offered: dict[str, Any] = plugins[0] |
| 89 |
return offered |
| 90 |
|
| 91 |
|
| 92 |
# --- the manifests ----------------------------------------------------------- |
| 93 |
|
| 94 |
|
| 95 |
def test_the_marketplace_entry_names_a_source_that_exists( |
| 96 |
entry: dict[str, Any], |
| 97 |
) -> None: |
| 98 |
"""The source is a path inside this repo, which is what saves a second one.""" |
| 99 |
source = (REPO_ROOT / entry["source"]).resolve() |
| 100 |
assert source == skill_mode.SKILL_DIR |
| 101 |
assert (source / skill_mode.FILENAME).is_file() |
| 102 |
|
| 103 |
|
| 104 |
def test_the_plugin_manifest_sits_inside_the_source_directory() -> None: |
| 105 |
"""`.claude-plugin/` beside `SKILL.md` is what makes the directory a plugin |
| 106 |
rather than a folder the marketplace points at.""" |
| 107 |
assert PLUGIN_PATH.parent.parent == skill_mode.SKILL_DIR |
| 108 |
assert PLUGIN_PATH.is_file() |
| 109 |
|
| 110 |
|
| 111 |
def test_one_name_names_the_plugin_everywhere( |
| 112 |
entry: dict[str, Any], plugin: dict[str, Any] |
| 113 |
) -> None: |
| 114 |
"""`/plugin install hsql@harlequin` is the entry's name and the manifest's, |
| 115 |
and the skill it installs answers to the same one.""" |
| 116 |
assert entry["name"] == plugin["name"] == skill_name() == "hsql" |
| 117 |
|
| 118 |
|
| 119 |
def test_the_plugin_version_is_the_version_it_ships_with( |
| 120 |
plugin: dict[str, Any], |
| 121 |
) -> None: |
| 122 |
"""The plugin is the skill for one release of hsql, so it carries that |
| 123 |
release's number -- and `release.yml` is what keeps this true.""" |
| 124 |
assert plugin["version"] == version("harlequin") |
| 125 |
|
| 126 |
|
| 127 |
def test_a_release_bumps_the_version_and_rewrites_nothing_else( |
| 128 |
bumper: ModuleType, tmp_path: Path |
| 129 |
) -> None: |
| 130 |
"""A manifest the release script would reformat is one nobody can read a |
| 131 |
version change out of.""" |
| 132 |
assert bumper.PLUGIN_PATH == PLUGIN_PATH |
| 133 |
committed = PLUGIN_PATH.read_bytes() |
| 134 |
manifest = tmp_path / "plugin.json" |
| 135 |
manifest.write_bytes(committed) |
| 136 |
bumper.write_version("9.9.9", manifest) |
| 137 |
assert manifest.read_bytes() == committed.replace( |
| 138 |
f'"version": "{version("harlequin")}"'.encode(), b'"version": "9.9.9"' |
| 139 |
) |
| 140 |
|
| 141 |
|
| 142 |
def test_the_plugin_manifest_ships_in_the_wheel_and_the_sdist() -> None: |
| 143 |
"""hatchling skips dot-directories, so the manifest is included by name in |
| 144 |
both targets. Losing either is silent: the build succeeds without it.""" |
| 145 |
targets = tomllib.loads((REPO_ROOT / "pyproject.toml").read_text(encoding="utf-8"))[ |
| 146 |
"tool" |
| 147 |
]["hatch"]["build"]["targets"] |
| 148 |
for target in ("wheel", "sdist"): |
| 149 |
assert PACKAGED_PLUGIN_MANIFEST in targets[target]["force-include"] |
| 150 |
|
| 151 |
|
| 152 |
# --- what the plugin tooling makes of them ----------------------------------- |
| 153 |
|
| 154 |
|
| 155 |
@pytest.mark.skipif( |
| 156 |
shutil.which("claude") is None, reason="the claude CLI is what reads these" |
| 157 |
) |
| 158 |
@pytest.mark.parametrize("target", [".", "src/harlequin/hsql/skill"]) |
| 159 |
def test_claude_plugin_validate_passes(target: str, tmp_path: Path) -> None: |
| 160 |
"""The marketplace and the plugin, checked by the tool that installs them. |
| 161 |
|
| 162 |
`--strict` because a warning here -- an unrecognized field, or metadata |
| 163 |
that is missing -- is a manifest some of the tooling reads differently. |
| 164 |
""" |
| 165 |
proc = subprocess.run( |
| 166 |
["claude", "plugin", "validate", target, "--strict"], |
| 167 |
cwd=REPO_ROOT, |
| 168 |
capture_output=True, |
| 169 |
text=True, |
| 170 |
env={**os.environ, "CLAUDE_CONFIG_DIR": str(tmp_path)}, |
| 171 |
) |
| 172 |
assert proc.returncode == 0, proc.stdout + proc.stderr |