claude-dev-env 8.108.2 → 8.109.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,295 @@
1
+ import json
2
+ import os
3
+ import subprocess
4
+ import sys
5
+ from pathlib import Path
6
+
7
+ import pytest
8
+
9
+ _SESSION_DIR = Path(__file__).resolve().parent
10
+ _HOOKS_ROOT = _SESSION_DIR.parent
11
+ for each_sys_path_entry in (str(_SESSION_DIR), str(_HOOKS_ROOT)):
12
+ if each_sys_path_entry not in sys.path:
13
+ sys.path.insert(0, each_sys_path_entry)
14
+
15
+ import npx_pele_guidance
16
+ from hooks_constants.npx_pele_guidance_constants import (
17
+ PACKAGE_MANIFEST_NAME,
18
+ PELE_PACKAGE_NAME,
19
+ )
20
+
21
+ HOOK_SCRIPT = _SESSION_DIR / "npx_pele_guidance.py"
22
+ CLAUDE_CONFIG_DIRECTORY_VARIABLE = "CLAUDE_CONFIG_DIR"
23
+ JS_FENCE_OPEN = "```js\n"
24
+ JS_FENCE_CLOSE = "\n```"
25
+ CHAIN_TEXT = "flowchart LR\n A --> B\n"
26
+
27
+
28
+ def _write_installed_pele(claude_root: Path, version: str) -> Path:
29
+ package_directory = npx_pele_guidance.pele_package_directory_under(claude_root)
30
+ (package_directory / "dist").mkdir(parents=True)
31
+ (package_directory / PACKAGE_MANIFEST_NAME).write_text(
32
+ json.dumps({"name": PELE_PACKAGE_NAME, "version": version, "type": "module"}),
33
+ encoding="utf-8",
34
+ )
35
+ (package_directory / "dist" / "pele.js").write_text(
36
+ "export function render(text) {\n return { svg: `<svg>${text.length}</svg>` };\n}\n",
37
+ encoding="utf-8",
38
+ )
39
+ return package_directory
40
+
41
+
42
+ def _payload(command_text: str) -> dict[str, object]:
43
+ return {
44
+ "hook_event_name": "PreToolUse",
45
+ "tool_name": "Bash",
46
+ "tool_input": {"command": command_text},
47
+ }
48
+
49
+
50
+ def _run_hook(payload_text: str, claude_root: Path) -> subprocess.CompletedProcess[str]:
51
+ environment = dict(os.environ)
52
+ environment[CLAUDE_CONFIG_DIRECTORY_VARIABLE] = str(claude_root)
53
+ return subprocess.run(
54
+ [sys.executable, str(HOOK_SCRIPT)],
55
+ input=payload_text,
56
+ capture_output=True,
57
+ text=True,
58
+ check=True,
59
+ env=environment,
60
+ )
61
+
62
+
63
+ @pytest.mark.parametrize(
64
+ "command_text",
65
+ [
66
+ "npx pele",
67
+ "npx -y pele chain.mmd",
68
+ "npx --yes pele@1.12.0 chain.mmd",
69
+ "npx --cache ./c pele chain.mmd",
70
+ "npx --registry https://r.example pele chain.mmd",
71
+ "npx --timeout 5000 pele chain.mmd",
72
+ "npx -p pele mmdc -i a.mmd",
73
+ "npx --package=pele mmdc chain.mmd",
74
+ "npx -c 'pele a.mmd'",
75
+ "npm.cmd exec pele a.mmd",
76
+ "npx.ps1 pele a.mmd",
77
+ "npm.ps1 exec pele a.mmd",
78
+ "npm --prefix exec pele chain.mmd",
79
+ "cd diagrams && npx pele chain.mmd",
80
+ "npm exec pele chain.mmd",
81
+ "npm --silent exec pele chain.mmd",
82
+ "npm --prefix ./p exec pele chain.mmd",
83
+ "npm --globalconfig ./g exec pele chain.mmd",
84
+ "npm --otp 123 exec pele chain.mmd",
85
+ "npx.cmd pele chain.mmd",
86
+ "CI=1 npx pele chain.mmd",
87
+ "NODE_ENV=prod CI=1 npx -y pele",
88
+ "env CI=1 npx pele chain.mmd",
89
+ "sudo npx pele chain.mmd",
90
+ "time npx pele chain.mmd",
91
+ "timeout 60 npx pele chain.mmd",
92
+ "if [ -f x ]; then npx pele chain.mmd; fi",
93
+ "timeout --foreground 60 npx pele chain.mmd",
94
+ 'bash -c "npx pele chain.mmd"',
95
+ "env -i npx pele a.mmd",
96
+ 'npm exec "pele" chain.mmd',
97
+ "echo `npx pele chain.mmd`",
98
+ "echo \\'; npx pele a.mmd; echo \\'",
99
+ ],
100
+ )
101
+ def test_npx_pele_commands_match(command_text: str) -> None:
102
+ assert npx_pele_guidance.is_npx_pele_command({"command": command_text})
103
+
104
+
105
+ @pytest.mark.parametrize(
106
+ "command_text",
107
+ [
108
+ "npx prettier --check .",
109
+ "npm install pele",
110
+ "npx -y claude-dev-env@latest",
111
+ "npx pelee",
112
+ "npx -y create-pele-app my-app",
113
+ "CI=1 npx prettier --check .",
114
+ 'git commit -m "fix; npx pele docs"',
115
+ "cat > notes.md <<'EOF'\nnpx pele chain.mmd\nEOF",
116
+ "cat > notes.md <<'EOF'\nnpx pele chain.mmd",
117
+ "npx @scope/pele chain.mmd",
118
+ "echo pele",
119
+ ],
120
+ )
121
+ def test_other_commands_do_not_match(command_text: str) -> None:
122
+ assert not npx_pele_guidance.is_npx_pele_command({"command": command_text})
123
+
124
+
125
+ @pytest.mark.parametrize("tool_input", [None, "npx pele", {}, {"command": 7}])
126
+ def test_input_without_a_command_string_does_not_match(tool_input: object) -> None:
127
+ assert not npx_pele_guidance.is_npx_pele_command(tool_input)
128
+
129
+
130
+ def test_installed_version_reads_the_manifest(tmp_path: Path) -> None:
131
+ package_directory = _write_installed_pele(tmp_path, "1.12.0")
132
+ manifest = npx_pele_guidance.installed_pele_manifest(package_directory)
133
+ assert npx_pele_guidance.installed_pele_version(manifest) == "1.12.0"
134
+
135
+
136
+ def test_installed_version_is_none_without_a_manifest(tmp_path: Path) -> None:
137
+ manifest = npx_pele_guidance.installed_pele_manifest(npx_pele_guidance.pele_package_directory_under(tmp_path))
138
+ assert npx_pele_guidance.installed_pele_version(manifest) is None
139
+
140
+
141
+ @pytest.mark.parametrize("manifest_text", ["not json", '["list"]', '{"version": 1}', '{"version": ""}'])
142
+ def test_unreadable_manifest_reads_as_no_version(tmp_path: Path, manifest_text: str) -> None:
143
+ package_directory = npx_pele_guidance.pele_package_directory_under(tmp_path)
144
+ package_directory.mkdir(parents=True)
145
+ (package_directory / PACKAGE_MANIFEST_NAME).write_text(manifest_text, encoding="utf-8")
146
+ manifest = npx_pele_guidance.installed_pele_manifest(package_directory)
147
+ assert npx_pele_guidance.installed_pele_version(manifest) is None
148
+
149
+
150
+ def test_package_directory_follows_the_claude_config_directory(
151
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
152
+ ) -> None:
153
+ monkeypatch.setenv(CLAUDE_CONFIG_DIRECTORY_VARIABLE, str(tmp_path))
154
+ package_directory = npx_pele_guidance.pele_package_directory_under(npx_pele_guidance.claude_home())
155
+ assert package_directory == npx_pele_guidance.pele_package_directory_under(tmp_path)
156
+
157
+
158
+ def test_installed_copy_gets_its_version_path_and_snippet(
159
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
160
+ ) -> None:
161
+ monkeypatch.setenv(CLAUDE_CONFIG_DIRECTORY_VARIABLE, str(tmp_path))
162
+ package_directory = _write_installed_pele(tmp_path, "1.12.0")
163
+ hook_output = npx_pele_guidance.decide_hook_output(_payload("npx pele chain.mmd"))
164
+ assert hook_output is not None
165
+ specific_output = hook_output["hookSpecificOutput"]
166
+ assert specific_output["hookEventName"] == "PreToolUse"
167
+ assert "permissionDecision" not in specific_output
168
+ context_text = specific_output["additionalContext"]
169
+ assert "could not determine executable to run" in context_text
170
+ assert f"Pele 1.12.0 is installed at {package_directory}" in context_text
171
+ assert json.dumps(str(package_directory / "dist" / "pele.js")) in context_text
172
+ assert "pele.run" not in context_text
173
+
174
+
175
+ def test_missing_copy_points_to_the_playground(
176
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
177
+ ) -> None:
178
+ monkeypatch.setenv(CLAUDE_CONFIG_DIRECTORY_VARIABLE, str(tmp_path))
179
+ hook_output = npx_pele_guidance.decide_hook_output(_payload("npx -y pele chain.mmd"))
180
+ assert hook_output is not None
181
+ context_text = hook_output["hookSpecificOutput"]["additionalContext"]
182
+ assert "could not determine executable to run" in context_text
183
+ assert "No usable Pele package was found at" in context_text
184
+ assert "checks only the Claude home" in context_text
185
+ assert "playground at pele.run" in context_text
186
+ assert "pathToFileURL" not in context_text
187
+
188
+
189
+ def test_manifest_without_an_entry_file_points_to_the_playground(
190
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
191
+ ) -> None:
192
+ monkeypatch.setenv(CLAUDE_CONFIG_DIRECTORY_VARIABLE, str(tmp_path))
193
+ package_directory = npx_pele_guidance.pele_package_directory_under(tmp_path)
194
+ package_directory.mkdir(parents=True)
195
+ (package_directory / PACKAGE_MANIFEST_NAME).write_text(
196
+ json.dumps({"name": PELE_PACKAGE_NAME, "version": "1.12.0"}),
197
+ encoding="utf-8",
198
+ )
199
+ hook_output = npx_pele_guidance.decide_hook_output(_payload("npx pele chain.mmd"))
200
+ assert hook_output is not None
201
+ context_text = hook_output["hookSpecificOutput"]["additionalContext"]
202
+ assert "playground at pele.run" in context_text
203
+ assert "pathToFileURL" not in context_text
204
+
205
+
206
+ def test_installed_snippet_renders_an_svg_file(
207
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
208
+ ) -> None:
209
+ claude_root = tmp_path / "claude"
210
+ _write_installed_pele(claude_root, "1.12.0")
211
+ monkeypatch.setenv(CLAUDE_CONFIG_DIRECTORY_VARIABLE, str(claude_root))
212
+ work_directory = tmp_path / "work"
213
+ work_directory.mkdir()
214
+ (work_directory / "diagram.mmd").write_text(CHAIN_TEXT, encoding="utf-8")
215
+ context_text = npx_pele_guidance.guidance_text()
216
+ snippet = context_text.split(JS_FENCE_OPEN, 1)[1].split(JS_FENCE_CLOSE, 1)[0]
217
+ (work_directory / "pele-render.mjs").write_text(snippet, encoding="utf-8")
218
+ completed = subprocess.run(
219
+ ["node", "pele-render.mjs", "diagram.mmd"],
220
+ cwd=work_directory,
221
+ capture_output=True,
222
+ text=True,
223
+ check=True,
224
+ )
225
+ rendered_svg = (work_directory / "diagram.svg").read_text(encoding="utf-8")
226
+ assert rendered_svg == f"<svg>{len(CHAIN_TEXT)}</svg>"
227
+ assert "svg bytes:" in completed.stdout
228
+
229
+
230
+ def test_decide_hook_output_is_silent_for_other_commands(
231
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
232
+ ) -> None:
233
+ monkeypatch.setenv(CLAUDE_CONFIG_DIRECTORY_VARIABLE, str(tmp_path))
234
+ assert npx_pele_guidance.decide_hook_output(_payload("npm install pele")) is None
235
+
236
+
237
+ def test_hook_prints_the_guidance_as_json_for_npx_pele(tmp_path: Path) -> None:
238
+ completed = _run_hook(json.dumps(_payload("npx pele chain.mmd")), tmp_path)
239
+ hook_output = json.loads(completed.stdout)
240
+ assert hook_output["hookSpecificOutput"]["hookEventName"] == "PreToolUse"
241
+ assert "permissionDecision" not in hook_output["hookSpecificOutput"]
242
+ assert "playground at pele.run" in hook_output["hookSpecificOutput"]["additionalContext"]
243
+
244
+
245
+ def test_hook_is_silent_on_malformed_stdin(tmp_path: Path) -> None:
246
+ completed = _run_hook("not json", tmp_path)
247
+ assert completed.returncode == 0
248
+ assert completed.stdout == ""
249
+
250
+
251
+ def test_pele_argument_outside_the_package_slot_gets_only_the_hint(tmp_path: Path) -> None:
252
+ completed = _run_hook(json.dumps(_payload("npx prettier --check pele")), tmp_path)
253
+ assert completed.returncode == 0
254
+ hook_output = json.loads(completed.stdout)
255
+ assert "permissionDecision" not in hook_output["hookSpecificOutput"]
256
+ assert "playground at pele.run" in hook_output["hookSpecificOutput"]["additionalContext"]
257
+
258
+
259
+ def test_installed_guidance_points_at_the_manifest_main_entry(
260
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
261
+ ) -> None:
262
+ monkeypatch.setenv(CLAUDE_CONFIG_DIRECTORY_VARIABLE, str(tmp_path))
263
+ package_directory = npx_pele_guidance.pele_package_directory_under(tmp_path)
264
+ (package_directory / "lib").mkdir(parents=True)
265
+ (package_directory / PACKAGE_MANIFEST_NAME).write_text(
266
+ json.dumps({"name": PELE_PACKAGE_NAME, "version": "2.0.0", "main": "./lib/index.js"}),
267
+ encoding="utf-8",
268
+ )
269
+ entry_path = package_directory / "lib" / "index.js"
270
+ entry_path.write_text("export function render(text) {\n return { svg: '' };\n}\n", encoding="utf-8")
271
+ manifest = npx_pele_guidance.installed_pele_manifest(package_directory)
272
+ assert npx_pele_guidance.installed_pele_entry(package_directory, manifest) == entry_path
273
+ assert str(entry_path) in npx_pele_guidance.guidance_text()
274
+ assert "pele.run" not in npx_pele_guidance.guidance_text()
275
+
276
+
277
+ def test_installed_snippet_prints_usage_without_a_diagram(
278
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
279
+ ) -> None:
280
+ claude_root = tmp_path / "claude"
281
+ _write_installed_pele(claude_root, "1.12.0")
282
+ monkeypatch.setenv(CLAUDE_CONFIG_DIRECTORY_VARIABLE, str(claude_root))
283
+ work_directory = tmp_path / "work"
284
+ work_directory.mkdir()
285
+ context_text = npx_pele_guidance.guidance_text()
286
+ snippet = context_text.split(JS_FENCE_OPEN, 1)[1].split(JS_FENCE_CLOSE, 1)[0]
287
+ (work_directory / "pele-render.mjs").write_text(snippet, encoding="utf-8")
288
+ completed = subprocess.run(
289
+ ["node", "pele-render.mjs"],
290
+ cwd=work_directory,
291
+ capture_output=True,
292
+ text=True,
293
+ )
294
+ assert completed.returncode == 1
295
+ assert "usage: node pele-render.mjs <diagram.mmd>" in completed.stderr
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-dev-env",
3
- "version": "8.108.2",
3
+ "version": "8.109.0",
4
4
  "description": "Claude Code development standards — rules, hooks, agents, commands, and skills",
5
5
  "type": "module",
6
6
  "bin": {