99from mcp_server import doctor , doctor_pairing
1010
1111REPO = 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]\n version = " { 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
4758def 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
5668def 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
6786def 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
7696def 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\n Version: 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
97145def 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
104153def 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
114162def test_both_check_lists_run_the_pairing_check ():
0 commit comments