Skip to content

Commit edce2d7

Browse files
committed
test(doctor): kill the pairing check's surviving mutants (#729, slice 3)
The ADR-1122 diff mutation gate left 33 survivors in doctor_pairing: the tests checked neither the check's name, optional flag nor fix text, nor a version that is an empty string or not a string, nor a [project] that is not a table. The tests now pin each. The manifest is read as bytes (json decodes UTF-8 itself, RFC 8259 section 8.1) and pyproject through tomllib.load on a binary file, which removes the encoding argument whose mutants no test can tell apart. Refs #729 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018kyZYs18rVW23Ta85PHji4
1 parent 93b8e4f commit edce2d7

2 files changed

Lines changed: 87 additions & 37 deletions

File tree

‎mcp_server/doctor_pairing.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def manifest_version(plugin: Path) -> str:
5656
5757
Raises OSError when the manifest cannot be read, ValueError when it is not
5858
JSON or names no string version."""
59-
data = json.loads((plugin / MANIFEST).read_text(encoding="utf-8"))
59+
# json reads bytes as UTF-8 (RFC 8259 section 8.1), whatever the locale.
60+
data = json.loads((plugin / MANIFEST).read_bytes())
6061
version = data.get("version") if isinstance(data, dict) else None
6162
if not isinstance(version, str) or not version:
6263
raise ValueError(f"{plugin / MANIFEST} declares no version")
@@ -76,7 +77,8 @@ def runtime_version(root: Path) -> tuple[str, str]:
7677
if dist is not None and Path(str(dist.locate_file(PACKAGE))).resolve() == root:
7778
return dist.version, f"installed {DISTRIBUTION}"
7879
pyproject = root.parent / "pyproject.toml"
79-
project = tomllib.loads(pyproject.read_text(encoding="utf-8")).get("project", {})
80+
with pyproject.open("rb") as file: # tomllib reads binary files only
81+
project = tomllib.load(file).get("project")
8082
version = project.get("version") if isinstance(project, dict) else None
8183
if not isinstance(version, str) or not version:
8284
raise ValueError(f"{pyproject} declares no [project] version")
@@ -93,7 +95,7 @@ def plugin_pairing(
9395
set and the process imports ``mcp_server`` from elsewhere, when the
9496
manifest's version differs from the running code's, or when either cannot
9597
be read (the failure names it)."""
96-
plugin = environ.get(ROOT_ENV, "")
98+
plugin = environ.get(ROOT_ENV)
9799
if not plugin:
98100
detail = f"{ROOT_ENV} unset: not launched by the Claude plugin, no pairing"
99101
return check(NAME, True, detail, optional=True)

‎tests_py/test_doctor_pairing.py‎

Lines changed: 82 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@
99
from mcp_server import doctor, doctor_pairing
1010

1111
REPO = Path(__file__).resolve().parents[1]
12+
NAME = "Plugin/runtime pairing"
13+
REINSTALL = (
14+
"Update or reinstall the Claude plugin so its directory holds the code its "
15+
"manifest declares"
16+
)
1217

1318

14-
def _plugin(tmp_path, manifest="1.2.3", project="1.2.3"):
19+
def _plugin(tmp_path, manifest="1.2.3", project='version = "1.2.3"'):
1520
"""A Claude plugin directory: manifest, pyproject and an mcp_server package."""
1621
plugin = tmp_path / "plugin"
1722
(plugin / ".claude-plugin").mkdir(parents=True)
1823
(plugin / "mcp_server").mkdir()
1924
(plugin / ".claude-plugin" / "plugin.json").write_text(
2025
json.dumps({"name": "hypermnesia-mcp", "version": manifest})
2126
)
22-
(plugin / "pyproject.toml").write_text(f'[project]\nversion = "{project}"\n')
27+
(plugin / "pyproject.toml").write_text(f"[project]\n{project}\n")
2328
return plugin
2429

2530

@@ -34,71 +39,115 @@ def distribution(name):
3439
monkeypatch.setattr(doctor_pairing.metadata, "distribution", distribution)
3540

3641

37-
def _check(env):
38-
return doctor_pairing.plugin_pairing(doctor.Check, env)
42+
def _check(env, *, ok, fix=""):
43+
"""Run the check; every result is optional, named, and carries ``fix``."""
44+
result = doctor_pairing.plugin_pairing(doctor.Check, env)
45+
assert (result.name, result.ok, result.optional) == (NAME, ok, True)
46+
assert result.fix == fix
47+
return result.detail
3948

4049

41-
def test_outside_the_claude_plugin_there_is_no_pairing_to_check():
42-
result = _check({})
43-
assert (result.ok, result.optional) == (True, True)
44-
assert "CLAUDE_PLUGIN_ROOT unset" in result.detail
50+
@pytest.mark.parametrize("env", [{}, {"CLAUDE_PLUGIN_ROOT": ""}])
51+
def test_outside_the_claude_plugin_there_is_no_pairing_to_check(env):
52+
detail = _check(env, ok=True)
53+
assert detail == (
54+
"CLAUDE_PLUGIN_ROOT unset: not launched by the Claude plugin, no pairing"
55+
)
4556

4657

4758
def test_the_plugin_running_its_own_source_tree_is_supported(tmp_path, monkeypatch):
4859
plugin = _plugin(tmp_path)
4960
_runs_from(monkeypatch, plugin / "mcp_server")
50-
result = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)})
51-
assert result.ok, result.detail
52-
assert "Claude plugin 1.2.3 runs its own mcp_server 1.2.3" in result.detail
53-
assert str(plugin / "pyproject.toml") in result.detail
61+
detail = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)}, ok=True)
62+
assert detail == (
63+
"Claude plugin 1.2.3 runs its own mcp_server 1.2.3 "
64+
f"({plugin.resolve() / 'pyproject.toml'})"
65+
)
5466

5567

5668
def test_code_imported_from_elsewhere_is_unsupported(tmp_path, monkeypatch):
5769
plugin = _plugin(tmp_path)
5870
other = tmp_path / "site" / "mcp_server"
5971
other.mkdir(parents=True)
6072
_runs_from(monkeypatch, other)
61-
result = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)})
62-
assert (result.ok, result.optional) == (False, True)
63-
assert f"imports mcp_server from {other.resolve()}" in result.detail
64-
assert "scripts/launcher.py mcp_server.doctor" in result.fix
73+
root = plugin.resolve()
74+
fix = (
75+
f"Run the doctor through the plugin launcher (`python3 {root}/scripts/"
76+
"launcher.py mcp_server.doctor`), which puts the plugin directory first "
77+
"on the import path, or unset CLAUDE_PLUGIN_ROOT outside the plugin"
78+
)
79+
detail = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)}, ok=False, fix=fix)
80+
assert detail == (
81+
f"Claude plugin 1.2.3 at {root}, but this process imports mcp_server "
82+
f"from {other.resolve()}: unsupported pairing"
83+
)
6584

6685

6786
def test_a_manifest_ahead_of_its_code_is_unsupported(tmp_path, monkeypatch):
68-
plugin = _plugin(tmp_path, manifest="1.2.4", project="1.2.3")
87+
plugin = _plugin(tmp_path, manifest="1.2.4")
6988
_runs_from(monkeypatch, plugin / "mcp_server")
70-
result = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)})
71-
assert not result.ok
72-
assert "declares 1.2.4, its mcp_server is 1.2.3" in result.detail
73-
assert result.fix == doctor_pairing._REINSTALL
89+
detail = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)}, ok=False, fix=REINSTALL)
90+
assert detail.startswith(
91+
"Claude plugin manifest declares 1.2.4, its mcp_server is 1.2.3 ("
92+
)
93+
assert detail.endswith("): unsupported pairing")
7494

7595

7696
def test_an_installed_wheel_in_the_plugin_gives_its_own_version(tmp_path, monkeypatch):
77-
plugin = _plugin(tmp_path, project="0.0.0")
97+
plugin = _plugin(tmp_path, project='version = "0.0.0"')
7898
info = plugin / "hypermnesia_mcp-1.2.3.dist-info"
7999
info.mkdir()
80100
(info / "METADATA").write_text("Name: hypermnesia-mcp\nVersion: 1.2.3\n")
81101
_runs_from(monkeypatch, plugin / "mcp_server", metadata.PathDistribution(info))
82-
result = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)})
83-
assert result.ok, result.detail
84-
assert "(installed hypermnesia-mcp)" in result.detail
102+
detail = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)}, ok=True)
103+
assert detail.endswith("mcp_server 1.2.3 (installed hypermnesia-mcp)")
104+
105+
106+
@pytest.mark.parametrize(
107+
"manifest", [b"{", b'{"name": "x"}', b"[]", b'{"version": ""}', b'{"version": 1}']
108+
)
109+
def test_a_manifest_without_a_version_fails(tmp_path, monkeypatch, manifest):
110+
plugin = _plugin(tmp_path)
111+
(plugin / ".claude-plugin" / "plugin.json").write_bytes(manifest)
112+
_runs_from(monkeypatch, plugin / "mcp_server")
113+
detail = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)}, ok=False, fix=REINSTALL)
114+
assert detail.startswith("cannot read the Claude plugin pairing: ")
115+
if manifest != b"{":
116+
assert detail.endswith("plugin.json declares no version")
117+
118+
119+
def test_a_non_ascii_manifest_is_read_as_utf8(tmp_path, monkeypatch):
120+
plugin = _plugin(tmp_path)
121+
manifest = {"description": "mémoire", "version": "1.2.3"}
122+
(plugin / ".claude-plugin" / "plugin.json").write_bytes(
123+
json.dumps(manifest, ensure_ascii=False).encode("utf-8")
124+
)
125+
_runs_from(monkeypatch, plugin / "mcp_server")
126+
_check({"CLAUDE_PLUGIN_ROOT": str(plugin)}, ok=True)
127+
128+
129+
@pytest.mark.parametrize("project", ['version = ""', "version = 1", 'name = "x"'])
130+
def test_a_pyproject_without_a_version_fails(tmp_path, monkeypatch, project):
131+
plugin = _plugin(tmp_path, project=project)
132+
_runs_from(monkeypatch, plugin / "mcp_server")
133+
detail = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)}, ok=False, fix=REINSTALL)
134+
assert detail.endswith("pyproject.toml declares no [project] version")
85135

86136

87-
@pytest.mark.parametrize("manifest", ["{", '{"name": "x"}', "[]"])
88-
def test_an_unreadable_manifest_fails_and_names_why(tmp_path, monkeypatch, manifest):
137+
def test_a_pyproject_whose_project_is_not_a_table_fails(tmp_path, monkeypatch):
89138
plugin = _plugin(tmp_path)
90-
(plugin / ".claude-plugin" / "plugin.json").write_text(manifest)
139+
(plugin / "pyproject.toml").write_text('project = "x"\n')
91140
_runs_from(monkeypatch, plugin / "mcp_server")
92-
result = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)})
93-
assert not result.ok
94-
assert result.detail.startswith("cannot read the Claude plugin pairing: ")
141+
detail = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)}, ok=False, fix=REINSTALL)
142+
assert detail.endswith("pyproject.toml declares no [project] version")
95143

96144

97145
def test_a_missing_manifest_fails(tmp_path, monkeypatch):
98146
plugin = _plugin(tmp_path)
99147
(plugin / ".claude-plugin" / "plugin.json").unlink()
100148
_runs_from(monkeypatch, plugin / "mcp_server")
101-
assert not _check({"CLAUDE_PLUGIN_ROOT": str(plugin)}).ok
149+
detail = _check({"CLAUDE_PLUGIN_ROOT": str(plugin)}, ok=False, fix=REINSTALL)
150+
assert "plugin.json" in detail
102151

103152

104153
def test_this_checkout_is_a_supported_pairing():
@@ -107,8 +156,7 @@ def test_this_checkout_is_a_supported_pairing():
107156
assert Path(doctor_pairing.mcp_server.__file__).resolve().parent == (
108157
REPO / "mcp_server"
109158
)
110-
result = _check({"CLAUDE_PLUGIN_ROOT": str(REPO)})
111-
assert result.ok, result.detail
159+
_check({"CLAUDE_PLUGIN_ROOT": str(REPO)}, ok=True)
112160

113161

114162
def test_both_check_lists_run_the_pairing_check():

0 commit comments

Comments
 (0)