claude-dev-env 8.122.0 → 8.124.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.
- package/bin/ever-registered-hooks.mjs +2 -0
- package/bin/ever-registered-hooks.test.mjs +5 -0
- package/docs/templates/artifact-page/README.md +14 -1
- package/docs/templates/artifact-page/review-rounds.md +1 -1
- package/docs/templates/artifact-page/template.html +26 -18
- package/docs/templates/html-plan/README.md +4 -0
- package/hooks/blocking/artifact_current_page_fixtures/piled_review_page.html +399 -0
- package/hooks/blocking/artifact_current_page_fixtures/rebuilt_review_page.html +403 -0
- package/hooks/blocking/artifact_current_page_gate.py +418 -0
- package/hooks/blocking/artifact_repeated_item_render.cjs +112 -0
- package/hooks/blocking/closing_question_round_gate.py +387 -0
- package/hooks/blocking/closing_question_round_lines.py +109 -0
- package/hooks/blocking/closing_question_round_open_items.py +348 -0
- package/hooks/blocking/closing_question_round_transcript.py +344 -0
- package/hooks/blocking/open_tasks_stop_gate.py +12 -1
- package/hooks/blocking/open_tasks_transcript.py +30 -0
- package/hooks/blocking/open_tasks_transcript_records.py +57 -1
- package/hooks/blocking/test_artifact_current_page_gate.py +415 -0
- package/hooks/blocking/test_closing_question_round_gate.py +983 -0
- package/hooks/blocking/test_closing_question_round_gate_declaration.py +307 -0
- package/hooks/blocking/test_closing_question_round_gate_work.py +349 -0
- package/hooks/blocking/test_closing_question_round_lines.py +70 -0
- package/hooks/blocking/test_closing_question_round_open_items.py +238 -0
- package/hooks/blocking/test_closing_question_round_transcript.py +236 -0
- package/hooks/blocking/test_open_tasks_stop_gate.py +64 -0
- package/hooks/blocking/test_open_tasks_transcript.py +34 -0
- package/hooks/blocking/test_open_tasks_transcript_records.py +34 -0
- package/hooks/features/conduct-gates.md +6 -0
- package/hooks/hooks.json +15 -0
- package/hooks/hooks_constants/artifact_current_page_gate_constants.py +91 -0
- package/hooks/hooks_constants/artifact_dark_mode_gate_constants.py +3 -3
- package/hooks/hooks_constants/closing_question_round_gate_constants.py +167 -0
- package/hooks/hooks_constants/open_tasks_stop_gate_constants.py +8 -0
- package/hooks/hooks_constants/test_artifact_current_page_gate_constants.py +53 -0
- package/hooks/hooks_constants/test_artifact_dark_mode_gate_constants.py +6 -0
- package/hooks/hooks_constants/test_closing_question_round_gate_constants.py +92 -0
- package/hooks/session/artifact_link_record.py +109 -0
- package/hooks/session/test_artifact_link_record.py +101 -0
- package/hooks/test_context_budget_hooks.py +2 -0
- package/package.json +1 -1
- package/scripts/policy_lint/adapter_configuration.py +6 -0
- package/scripts/policy_lint/config/constants.py +1 -0
- package/scripts/tests/test_adapter_configuration.py +8 -0
|
@@ -23,6 +23,15 @@ The gate holds the turn once when all three hold::
|
|
|
23
23
|
allow: "Export the APKs" -> reply only -> stop
|
|
24
24
|
allow: "Export the APKs" -> AskUserQuestion -> stop
|
|
25
25
|
|
|
26
|
+
A Claude Code session that has no tracker tool cannot act on that hold, so the
|
|
27
|
+
gate passes the turn when the transcript shows the session's tool lists and no
|
|
28
|
+
tracker in them::
|
|
29
|
+
|
|
30
|
+
allow: prompt_snapshot tools and deferred_tools_delta name no tracker,
|
|
31
|
+
and no deferred_tools_record or call names one
|
|
32
|
+
block: a deferred_tools_delta lists TaskCreate, even when not yet loaded
|
|
33
|
+
block: no prompt_snapshot tools list or no deferred_tools_delta (Codex, Cursor)
|
|
34
|
+
|
|
26
35
|
The user reads the task list to see what is open, so the gate also holds the
|
|
27
36
|
turn once when completed items sit beside open ones::
|
|
28
37
|
|
|
@@ -167,7 +176,9 @@ def session_task_items(session_id: object, replay: TranscriptReplay) -> list[Tas
|
|
|
167
176
|
|
|
168
177
|
|
|
169
178
|
def is_untracked_work_turn(replay: TranscriptReplay) -> bool:
|
|
170
|
-
"""Return True when the turn worked on an ask and made no tracker call."""
|
|
179
|
+
"""Return True when the turn worked on an ask and made no tracker call in a session that has a tracker."""
|
|
180
|
+
if replay.lists_no_tracker_tool:
|
|
181
|
+
return False
|
|
171
182
|
return replay.has_turn_work_tool and not replay.has_turn_tracker_write and is_ask(replay.turn_prompt_text)
|
|
172
183
|
|
|
173
184
|
|
|
@@ -32,6 +32,12 @@ The replay also notes what the current turn did::
|
|
|
32
32
|
write word such as ``update``
|
|
33
33
|
turn_sent_file_paths the paths the turn's SendUserFile calls sent
|
|
34
34
|
|
|
35
|
+
Across the whole session, the replay also notes whether a tracker tool exists::
|
|
36
|
+
|
|
37
|
+
lists_no_tracker_tool a prompt_snapshot tools list and a deferred_tools_delta
|
|
38
|
+
both appeared, and no tracker name appeared in any
|
|
39
|
+
listing, deferred_tools_record, or tracker call
|
|
40
|
+
|
|
35
41
|
A turn starts at the last user prompt record. A tool result, a record flagged
|
|
36
42
|
``isMeta``, ``isSidechain`` or ``isCompactSummary``, and a ``Stop hook feedback``
|
|
37
43
|
message do not start a turn. Tests model Stop hook feedback as an ``isMeta``
|
|
@@ -56,6 +62,7 @@ if hooks_root_directory not in sys.path:
|
|
|
56
62
|
from hooks_constants.open_tasks_stop_gate_constants import (
|
|
57
63
|
ASSISTANT_RECORD_TYPE,
|
|
58
64
|
ALL_QUESTION_TOOL_NAMES,
|
|
65
|
+
ALL_TRACKER_TOOL_LISTING_NAMES,
|
|
59
66
|
ALL_TRACKER_TOOL_NAMES,
|
|
60
67
|
CCD_PLAN_ADVANCE_KEY,
|
|
61
68
|
CCD_PLAN_CONTENT_KEY,
|
|
@@ -74,6 +81,7 @@ from hooks_constants.open_tasks_stop_gate_constants import (
|
|
|
74
81
|
COMPLETED_STATUS,
|
|
75
82
|
CONTENT_KEY,
|
|
76
83
|
CREATED_TASK_ID_GROUP_INDEX,
|
|
84
|
+
DEFERRED_TOOLS_DELTA_ATTACHMENT_TYPE,
|
|
77
85
|
DYNAMIC_TOOL_ARGUMENTS_KEY,
|
|
78
86
|
DYNAMIC_TOOL_CALL_NAME,
|
|
79
87
|
DYNAMIC_TOOL_NAME_KEY,
|
|
@@ -81,6 +89,7 @@ from hooks_constants.open_tasks_stop_gate_constants import (
|
|
|
81
89
|
INCOMPLETE_STATUSES,
|
|
82
90
|
IN_PROGRESS_STATUS,
|
|
83
91
|
PENDING_STATUS,
|
|
92
|
+
PROMPT_SNAPSHOT_ATTACHMENT_TYPE,
|
|
84
93
|
RECORD_TYPE_KEY,
|
|
85
94
|
TASK_CREATED_PATTERN,
|
|
86
95
|
TASK_CREATE_TOOL_NAME,
|
|
@@ -116,6 +125,7 @@ from open_tasks_transcript_records import (
|
|
|
116
125
|
is_read_tool_name,
|
|
117
126
|
send_call_file_paths,
|
|
118
127
|
is_assistant_record,
|
|
128
|
+
listed_tool_names,
|
|
119
129
|
)
|
|
120
130
|
|
|
121
131
|
|
|
@@ -240,6 +250,19 @@ class TranscriptReplay:
|
|
|
240
250
|
self.has_turn_question_tool = False
|
|
241
251
|
self.has_turn_read_tool = False
|
|
242
252
|
self.turn_sent_file_paths: list[str] = []
|
|
253
|
+
self.all_tool_listing_types: set[str] = set()
|
|
254
|
+
self.has_tracker_tool = False
|
|
255
|
+
|
|
256
|
+
@property
|
|
257
|
+
def lists_no_tracker_tool(self) -> bool:
|
|
258
|
+
"""Return True when the session listed its inline and deferred tools and no tracker ran or appeared."""
|
|
259
|
+
has_full_listing = {PROMPT_SNAPSHOT_ATTACHMENT_TYPE, DEFERRED_TOOLS_DELTA_ATTACHMENT_TYPE} <= self.all_tool_listing_types
|
|
260
|
+
return has_full_listing and not self.has_tracker_tool
|
|
261
|
+
|
|
262
|
+
def note_tool_listing(self, listing_type: str, all_tool_names: list[str]) -> None:
|
|
263
|
+
self.all_tool_listing_types.add(listing_type)
|
|
264
|
+
if not ALL_TRACKER_TOOL_LISTING_NAMES.isdisjoint(all_tool_names):
|
|
265
|
+
self.has_tracker_tool = True
|
|
243
266
|
|
|
244
267
|
def start_turn(self, prompt_text: str) -> None:
|
|
245
268
|
self.turn_prompt_text = prompt_text
|
|
@@ -256,10 +279,15 @@ class TranscriptReplay:
|
|
|
256
279
|
if prompt_text is not None:
|
|
257
280
|
self.start_turn(prompt_text)
|
|
258
281
|
return
|
|
282
|
+
tool_listing = listed_tool_names(all_record_fields)
|
|
283
|
+
if tool_listing is not None:
|
|
284
|
+
self.note_tool_listing(*tool_listing)
|
|
285
|
+
return
|
|
259
286
|
plan_items = codex_plan_items(all_record_fields)
|
|
260
287
|
if plan_items is not None:
|
|
261
288
|
self.latest_items = plan_items
|
|
262
289
|
self.has_turn_tracker_write = True
|
|
290
|
+
self.has_tracker_tool = True
|
|
263
291
|
return
|
|
264
292
|
if is_codex_question_call(all_record_fields):
|
|
265
293
|
self.has_turn_question_tool = True
|
|
@@ -275,6 +303,7 @@ class TranscriptReplay:
|
|
|
275
303
|
if is_typed_assistant_record and each_block.get(TOOL_NAME_KEY) == CCD_PLAN_TOOL_NAME and isinstance(plan_input, dict):
|
|
276
304
|
self.apply_desktop_plan(plan_input)
|
|
277
305
|
self.has_turn_tracker_write = True
|
|
306
|
+
self.has_tracker_tool = True
|
|
278
307
|
else:
|
|
279
308
|
self.apply_block(each_block, is_from_assistant)
|
|
280
309
|
|
|
@@ -328,6 +357,7 @@ class TranscriptReplay:
|
|
|
328
357
|
self.has_turn_read_tool = True
|
|
329
358
|
if tool_name in ALL_TRACKER_TOOL_NAMES:
|
|
330
359
|
self.has_turn_tracker_write = True
|
|
360
|
+
self.has_tracker_tool = True
|
|
331
361
|
elif is_work_tool_name(tool_name):
|
|
332
362
|
self.has_turn_work_tool = True
|
|
333
363
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"""Read a transcript file into records, and read what one record holds.
|
|
2
2
|
|
|
3
3
|
One record can hold the prompt text that starts a turn, a tool call that is work,
|
|
4
|
-
a read, or a question card,
|
|
4
|
+
a read, or a question card, the files a ``SendUserFile`` call sends, or the
|
|
5
|
+
names of the tools a Claude Code session lists.
|
|
5
6
|
``open_tasks_transcript`` folds these readers over every record. Its module
|
|
6
7
|
docstring lists which records start a turn and which tool calls count as work.
|
|
7
8
|
"""
|
|
@@ -15,8 +16,15 @@ from pathlib import Path
|
|
|
15
16
|
from hooks_constants.send_user_file_worktree_root_gate_constants import SEND_USER_FILE_TOOL_NAME
|
|
16
17
|
from hooks_constants.setup_project_paths_constants import DECODE_ERRORS_POLICY
|
|
17
18
|
from hooks_constants.open_tasks_stop_gate_constants import (
|
|
19
|
+
ALL_DEFERRED_TOOLS_DELTA_NAME_KEYS,
|
|
18
20
|
ALL_SKIPPED_PROMPT_FLAGS,
|
|
19
21
|
ALL_CODEX_TOOL_CALL_TYPES,
|
|
22
|
+
ATTACHMENT_KEY,
|
|
23
|
+
DEFERRED_TOOLS_DELTA_ATTACHMENT_TYPE,
|
|
24
|
+
DEFERRED_TOOLS_RECORD_ATTACHMENT_TYPE,
|
|
25
|
+
DEFERRED_TOOLS_RECORD_ENTRIES_KEY,
|
|
26
|
+
PROMPT_SNAPSHOT_ATTACHMENT_TYPE,
|
|
27
|
+
PROMPT_SNAPSHOT_TOOLS_KEY,
|
|
20
28
|
ALL_NON_WORK_TOOL_NAMES,
|
|
21
29
|
ALL_QUESTION_TOOL_NAMES,
|
|
22
30
|
ALL_READ_TOOL_NAMES,
|
|
@@ -204,6 +212,54 @@ def send_call_file_paths(tool_name: str, all_input_fields: dict[str, object]) ->
|
|
|
204
212
|
return [each_path for each_path in all_sent_files if isinstance(each_path, str)]
|
|
205
213
|
|
|
206
214
|
|
|
215
|
+
def _entry_names(all_entries: object) -> list[str]:
|
|
216
|
+
if not isinstance(all_entries, list):
|
|
217
|
+
return []
|
|
218
|
+
return [
|
|
219
|
+
each_entry[TOOL_NAME_KEY]
|
|
220
|
+
for each_entry in all_entries
|
|
221
|
+
if isinstance(each_entry, dict) and isinstance(each_entry.get(TOOL_NAME_KEY), str)
|
|
222
|
+
]
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def listed_tool_names(all_record_fields: dict[str, object]) -> tuple[str, list[str]] | None:
|
|
226
|
+
"""Return the attachment type and tool names of a Claude Code record that lists the session's tools.
|
|
227
|
+
|
|
228
|
+
::
|
|
229
|
+
|
|
230
|
+
prompt_snapshot with a tools list -> ("prompt_snapshot", the inline tool names)
|
|
231
|
+
deferred_tools_delta -> ("deferred_tools_delta", the added, readded,
|
|
232
|
+
surfaced, and loaded names)
|
|
233
|
+
deferred_tools_record -> ("deferred_tools_record", the names ToolSearch loaded)
|
|
234
|
+
prompt_snapshot with no tools list -> None
|
|
235
|
+
any other record -> None
|
|
236
|
+
|
|
237
|
+
Names come only from these fields, so a tool name inside prose never counts.
|
|
238
|
+
"""
|
|
239
|
+
attachment_body = all_record_fields.get(ATTACHMENT_KEY)
|
|
240
|
+
if not isinstance(attachment_body, dict):
|
|
241
|
+
return None
|
|
242
|
+
attachment_type = attachment_body.get(RECORD_TYPE_KEY)
|
|
243
|
+
if attachment_type == PROMPT_SNAPSHOT_ATTACHMENT_TYPE:
|
|
244
|
+
all_inline_tools = attachment_body.get(PROMPT_SNAPSHOT_TOOLS_KEY)
|
|
245
|
+
if not isinstance(all_inline_tools, list):
|
|
246
|
+
return None
|
|
247
|
+
return attachment_type, _entry_names(all_inline_tools)
|
|
248
|
+
if attachment_type == DEFERRED_TOOLS_RECORD_ATTACHMENT_TYPE:
|
|
249
|
+
return attachment_type, _entry_names(attachment_body.get(DEFERRED_TOOLS_RECORD_ENTRIES_KEY))
|
|
250
|
+
if attachment_type == DEFERRED_TOOLS_DELTA_ATTACHMENT_TYPE:
|
|
251
|
+
all_delta_names = [
|
|
252
|
+
each_name
|
|
253
|
+
for each_key in ALL_DEFERRED_TOOLS_DELTA_NAME_KEYS
|
|
254
|
+
for each_delta_value in [attachment_body.get(each_key)]
|
|
255
|
+
if isinstance(each_delta_value, list)
|
|
256
|
+
for each_name in each_delta_value
|
|
257
|
+
if isinstance(each_name, str)
|
|
258
|
+
]
|
|
259
|
+
return attachment_type, all_delta_names
|
|
260
|
+
return None
|
|
261
|
+
|
|
262
|
+
|
|
207
263
|
def is_assistant_record(all_record_fields: dict[str, object]) -> bool:
|
|
208
264
|
"""Return True for a record the assistant wrote, by its type, its role, or its message role."""
|
|
209
265
|
message_body = all_record_fields.get(MESSAGE_KEY)
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
"""Behavior tests for the artifact current page publish gate."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import re
|
|
7
|
+
import shutil
|
|
8
|
+
import subprocess
|
|
9
|
+
import sys
|
|
10
|
+
from collections import Counter
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
import pytest
|
|
14
|
+
|
|
15
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
16
|
+
|
|
17
|
+
from blocking.artifact_current_page_gate import (
|
|
18
|
+
format_repeat_reason,
|
|
19
|
+
full_rule_pointer,
|
|
20
|
+
is_artifact_link,
|
|
21
|
+
link_record_path,
|
|
22
|
+
page_source_after_edits,
|
|
23
|
+
page_title,
|
|
24
|
+
recorded_page_link,
|
|
25
|
+
repeated_items,
|
|
26
|
+
)
|
|
27
|
+
from hooks_constants.artifact_current_page_gate_constants import REPUBLISH_NOTE
|
|
28
|
+
from hooks_constants.artifact_dark_mode_gate_constants import ALLOW_EXIT_CODE, BLOCK_EXIT_CODE
|
|
29
|
+
|
|
30
|
+
GATE_SCRIPT_PATH = Path(__file__).resolve().parent / "artifact_current_page_gate.py"
|
|
31
|
+
FIXTURES_DIRECTORY = Path(__file__).resolve().parent / "artifact_current_page_fixtures"
|
|
32
|
+
PILED_PAGE_PATH = FIXTURES_DIRECTORY / "piled_review_page.html"
|
|
33
|
+
REBUILT_PAGE_PATH = FIXTURES_DIRECTORY / "rebuilt_review_page.html"
|
|
34
|
+
PAGE_LINK = "https://claude.ai/artifact/SamplePageLink01"
|
|
35
|
+
ITEM_IMAGE_STYLE = "<style>img{width:200px;height:300px}</style>"
|
|
36
|
+
PAGE_WITH_ONE_SCREEN = (
|
|
37
|
+
"<title>Sample Run</title>" + ITEM_IMAGE_STYLE + '<img src="screens/home.png" alt="Home">'
|
|
38
|
+
)
|
|
39
|
+
SECOND_DRAW_OF_THE_SCREEN = '<h2>Warnings</h2><img src="screens/home.png" alt="Home again">'
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _renderer_available() -> bool:
|
|
43
|
+
if shutil.which("node") is None:
|
|
44
|
+
return False
|
|
45
|
+
all_repeats, _ = repeated_items("<p>probe</p>")
|
|
46
|
+
return all_repeats is not None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
needs_renderer = pytest.mark.skipif(
|
|
50
|
+
not _renderer_available(), reason="node Playwright with Chromium is not installed"
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _run_gate(tool_input: dict[str, object]) -> subprocess.CompletedProcess[str]:
|
|
55
|
+
return subprocess.run(
|
|
56
|
+
[sys.executable, str(GATE_SCRIPT_PATH)],
|
|
57
|
+
input=json.dumps({"tool_name": "Artifact", "tool_input": tool_input}),
|
|
58
|
+
capture_output=True,
|
|
59
|
+
text=True,
|
|
60
|
+
check=False,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _figure_counts_in_data_block(page_path: Path) -> tuple[int, int]:
|
|
65
|
+
data_block = re.search(
|
|
66
|
+
r'<script type="application/json" id="data">(.*?)</script>',
|
|
67
|
+
page_path.read_text(encoding="utf-8"),
|
|
68
|
+
re.DOTALL,
|
|
69
|
+
)
|
|
70
|
+
assert data_block is not None
|
|
71
|
+
page_data = json.loads(data_block.group(1))
|
|
72
|
+
all_screen_lists = [each_group["screens"] for each_group in page_data.get("groups", [])]
|
|
73
|
+
if isinstance(page_data.get("warnings"), list):
|
|
74
|
+
all_screen_lists += [each_warning["screens"] for each_warning in page_data["warnings"]]
|
|
75
|
+
all_screen_lists += [each_section["screens"] for each_section in page_data.get("sections", [])]
|
|
76
|
+
all_screen_lists.append(page_data.get("pick", {}).get("strips", []))
|
|
77
|
+
all_drawn_names = [
|
|
78
|
+
each_screen[0] for each_screen_list in all_screen_lists for each_screen in each_screen_list
|
|
79
|
+
]
|
|
80
|
+
return len(all_drawn_names), len(Counter(all_drawn_names))
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_should_keep_the_incident_counts_in_the_reduced_fixtures() -> None:
|
|
84
|
+
assert _figure_counts_in_data_block(PILED_PAGE_PATH) == (33, 20)
|
|
85
|
+
assert _figure_counts_in_data_block(REBUILT_PAGE_PATH) == (20, 20)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def test_should_apply_an_edit_whose_old_text_occurs_once() -> None:
|
|
89
|
+
assert page_source_after_edits("<p>old</p>", [{"old_str": "old", "new_str": "new"}]) == (
|
|
90
|
+
"<p>new</p>"
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def test_should_leave_the_page_alone_for_an_ambiguous_or_malformed_edit() -> None:
|
|
95
|
+
assert page_source_after_edits("<p>a a</p>", [{"old_str": "a", "new_str": "b"}]) == "<p>a a</p>"
|
|
96
|
+
assert page_source_after_edits("<p>a</p>", [{"old_str": "a"}, "x"]) == "<p>a</p>"
|
|
97
|
+
assert page_source_after_edits("<p>a</p>", None) == "<p>a</p>"
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_should_apply_no_edit_when_one_edit_of_the_call_misses() -> None:
|
|
101
|
+
all_edits = [{"old_str": "a", "new_str": "x"}, {"old_str": "missing", "new_str": "y"}]
|
|
102
|
+
assert page_source_after_edits("<p>a b</p>", all_edits) == "<p>a b</p>"
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_should_read_the_page_title() -> None:
|
|
106
|
+
assert page_title("<title> Sample Run </title><p>x</p>") == "Sample Run"
|
|
107
|
+
assert page_title("<p>no title</p>") == ""
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def test_should_name_the_record_file_after_the_page_file() -> None:
|
|
111
|
+
assert link_record_path(Path("/work/first-look.html")) == Path(
|
|
112
|
+
"/work/first-look.html.artifact-link.json"
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def test_should_find_no_link_for_a_page_with_no_record(tmp_path: Path) -> None:
|
|
117
|
+
assert recorded_page_link(tmp_path / "page.html", PAGE_WITH_ONE_SCREEN) == ""
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def test_should_read_the_link_from_the_meta_tag_in_the_page(tmp_path: Path) -> None:
|
|
121
|
+
page_source = f'<meta name="artifact-link" content="{PAGE_LINK}">' + PAGE_WITH_ONE_SCREEN
|
|
122
|
+
assert recorded_page_link(tmp_path / "page.html", page_source) == PAGE_LINK
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def test_should_read_the_meta_tag_with_its_attributes_in_either_order(tmp_path: Path) -> None:
|
|
126
|
+
page_source = f"<meta content='{PAGE_LINK}' name='artifact-link'>" + PAGE_WITH_ONE_SCREEN
|
|
127
|
+
assert recorded_page_link(tmp_path / "page.html", page_source) == PAGE_LINK
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def test_should_ignore_a_meta_tag_that_holds_no_artifact_link(tmp_path: Path) -> None:
|
|
131
|
+
page_source = '<meta name="artifact-link" content="<link>">' + PAGE_WITH_ONE_SCREEN
|
|
132
|
+
assert recorded_page_link(tmp_path / "page.html", page_source) == ""
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def test_should_read_the_first_of_two_name_attributes_as_html_does(tmp_path: Path) -> None:
|
|
136
|
+
page_source = (
|
|
137
|
+
f'<meta name="artifact-link" name="other" content="{PAGE_LINK}">' + PAGE_WITH_ONE_SCREEN
|
|
138
|
+
)
|
|
139
|
+
assert recorded_page_link(tmp_path / "page.html", page_source) == PAGE_LINK
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def test_should_tell_an_artifact_link_from_other_values() -> None:
|
|
143
|
+
assert is_artifact_link(PAGE_LINK)
|
|
144
|
+
assert is_artifact_link("https://claude.ai/code/artifact/0e557edc-f0c7")
|
|
145
|
+
assert not is_artifact_link(PAGE_LINK + "/")
|
|
146
|
+
assert not is_artifact_link("<link>")
|
|
147
|
+
assert not is_artifact_link(None)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def test_should_read_attributes_with_no_space_before_them(tmp_path: Path) -> None:
|
|
151
|
+
for each_meta_tag in (
|
|
152
|
+
f'<meta name="artifact-link"content="{PAGE_LINK}">',
|
|
153
|
+
f'<meta/name="artifact-link" content="{PAGE_LINK}">',
|
|
154
|
+
):
|
|
155
|
+
page_source = each_meta_tag + PAGE_WITH_ONE_SCREEN
|
|
156
|
+
assert recorded_page_link(tmp_path / "page.html", page_source) == PAGE_LINK
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def test_should_read_the_name_attribute_beside_a_prefixed_one(tmp_path: Path) -> None:
|
|
160
|
+
page_source = (
|
|
161
|
+
f'<meta og:name="site" name="artifact-link" content="{PAGE_LINK}">' + PAGE_WITH_ONE_SCREEN
|
|
162
|
+
)
|
|
163
|
+
assert recorded_page_link(tmp_path / "page.html", page_source) == PAGE_LINK
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def test_should_ignore_a_record_that_holds_no_artifact_link(tmp_path: Path) -> None:
|
|
167
|
+
page_path = tmp_path / "page.html"
|
|
168
|
+
link_record_path(page_path).write_text(json.dumps({"url": "<link>", "title": "Sample Run"}))
|
|
169
|
+
assert recorded_page_link(page_path, PAGE_WITH_ONE_SCREEN) == ""
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def test_should_read_the_link_from_the_record_beside_the_page(tmp_path: Path) -> None:
|
|
173
|
+
page_path = tmp_path / "page.html"
|
|
174
|
+
link_record_path(page_path).write_text(json.dumps({"url": PAGE_LINK, "title": "Other"}))
|
|
175
|
+
assert recorded_page_link(page_path, PAGE_WITH_ONE_SCREEN) == PAGE_LINK
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def test_should_find_the_link_of_a_page_renamed_in_its_folder(tmp_path: Path) -> None:
|
|
179
|
+
link_record_path(tmp_path / "page.html").write_text(
|
|
180
|
+
json.dumps({"url": PAGE_LINK, "title": "Sample Run"})
|
|
181
|
+
)
|
|
182
|
+
assert recorded_page_link(tmp_path / "page-v2.html", PAGE_WITH_ONE_SCREEN) == PAGE_LINK
|
|
183
|
+
assert recorded_page_link(tmp_path / "other.html", "<title>Other Page</title>") == ""
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def test_should_leave_a_separate_page_with_the_same_title_unlinked(tmp_path: Path) -> None:
|
|
187
|
+
first_page_path = tmp_path / "team-a.html"
|
|
188
|
+
first_page_path.write_text(PAGE_WITH_ONE_SCREEN, encoding="utf-8")
|
|
189
|
+
link_record_path(first_page_path).write_text(
|
|
190
|
+
json.dumps({"url": PAGE_LINK, "title": "Sample Run"})
|
|
191
|
+
)
|
|
192
|
+
assert recorded_page_link(tmp_path / "team-b.html", PAGE_WITH_ONE_SCREEN) == ""
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def test_should_skip_an_old_name_record_whose_link_a_present_page_holds(tmp_path: Path) -> None:
|
|
196
|
+
link_record_path(tmp_path / "first-name.html").write_text(
|
|
197
|
+
json.dumps({"url": PAGE_LINK, "title": "Sample Run"})
|
|
198
|
+
)
|
|
199
|
+
renamed_page_path = tmp_path / "second-name.html"
|
|
200
|
+
renamed_page_path.write_text(PAGE_WITH_ONE_SCREEN, encoding="utf-8")
|
|
201
|
+
link_record_path(renamed_page_path).write_text(
|
|
202
|
+
json.dumps({"url": PAGE_LINK, "title": "Sample Run"})
|
|
203
|
+
)
|
|
204
|
+
assert recorded_page_link(tmp_path / "third-page.html", PAGE_WITH_ONE_SCREEN) == ""
|
|
205
|
+
assert recorded_page_link(renamed_page_path, PAGE_WITH_ONE_SCREEN) == PAGE_LINK
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def test_should_name_each_repeated_item_and_its_count() -> None:
|
|
209
|
+
block_reason = format_repeat_reason([{"kind": "image", "item": "screens/home.png", "times": 3}])
|
|
210
|
+
assert "- screens/home.png is drawn 3 times" in block_reason
|
|
211
|
+
assert "Show each item once" in block_reason
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def test_should_point_each_message_at_the_installed_rule_text() -> None:
|
|
215
|
+
rule_pointer = full_rule_pointer()
|
|
216
|
+
assert "Republish a page" in rule_pointer
|
|
217
|
+
assert "artifact-page" in rule_pointer and "README.md" in rule_pointer
|
|
218
|
+
assert format_repeat_reason([]).endswith(rule_pointer)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
@needs_renderer
|
|
222
|
+
def test_should_report_the_eleven_repeated_screens_of_the_piled_page() -> None:
|
|
223
|
+
all_repeats, _ = repeated_items(PILED_PAGE_PATH.read_text(encoding="utf-8"))
|
|
224
|
+
assert all_repeats is not None
|
|
225
|
+
assert len(all_repeats) == 11
|
|
226
|
+
assert sorted(each_repeat["times"] for each_repeat in all_repeats) == [2] * 9 + [3, 3]
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
@needs_renderer
|
|
230
|
+
def test_should_pass_the_rebuilt_page_that_shows_three_draws_of_one_screen() -> None:
|
|
231
|
+
assert repeated_items(REBUILT_PAGE_PATH.read_text(encoding="utf-8")) == ([], "")
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
@needs_renderer
|
|
235
|
+
def test_should_refuse_the_piled_page_that_a_source_tag_count_passes() -> None:
|
|
236
|
+
piled_source = PILED_PAGE_PATH.read_text(encoding="utf-8")
|
|
237
|
+
assert piled_source.count("<img") == 1
|
|
238
|
+
denied_run = _run_gate({"file_path": str(PILED_PAGE_PATH)})
|
|
239
|
+
assert denied_run.returncode == BLOCK_EXIT_CODE
|
|
240
|
+
assert "screens/sample-theme/screen-12.png is drawn 3 times" in denied_run.stderr
|
|
241
|
+
assert _run_gate({"file_path": str(REBUILT_PAGE_PATH)}).returncode == ALLOW_EXIT_CODE
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
@needs_renderer
|
|
245
|
+
def test_should_pass_a_small_icon_used_many_times() -> None:
|
|
246
|
+
icon_row = '<img src="icons/check.png" width="24" height="24" alt="Done">' * 5
|
|
247
|
+
assert repeated_items(icon_row) == ([], "")
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
@needs_renderer
|
|
251
|
+
def test_should_count_an_image_with_no_size_set_as_an_item() -> None:
|
|
252
|
+
page_source = '<img src="screens/home.png" alt="Home"><p>Later</p><img src="screens/home.png" alt="">'
|
|
253
|
+
all_repeats, _ = repeated_items(page_source)
|
|
254
|
+
assert all_repeats == [{"kind": "image", "item": "screens/home.png", "times": 2}]
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
@needs_renderer
|
|
258
|
+
def test_should_count_two_spellings_of_one_image_file_as_one_item() -> None:
|
|
259
|
+
page_source = (
|
|
260
|
+
ITEM_IMAGE_STYLE
|
|
261
|
+
+ '<img src="screens/home.png" alt="Home">'
|
|
262
|
+
+ '<img src="./screens/home.png" alt="Home again">'
|
|
263
|
+
)
|
|
264
|
+
all_repeats, _ = repeated_items(page_source)
|
|
265
|
+
assert all_repeats == [{"kind": "image", "item": "screens/home.png", "times": 2}]
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
@needs_renderer
|
|
269
|
+
def test_should_count_a_lazy_image_below_the_first_screen() -> None:
|
|
270
|
+
page_source = (
|
|
271
|
+
'<img src="screens/home.png" alt="Home">'
|
|
272
|
+
'<div style="height:4000px"></div>'
|
|
273
|
+
'<img src="screens/home.png" alt="Home" loading="lazy">'
|
|
274
|
+
)
|
|
275
|
+
all_repeats, _ = repeated_items(page_source)
|
|
276
|
+
assert all_repeats == [{"kind": "image", "item": "screens/home.png", "times": 2}]
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
@needs_renderer
|
|
280
|
+
def test_should_pass_a_thumbnail_whose_full_view_is_hidden() -> None:
|
|
281
|
+
page_source = (
|
|
282
|
+
ITEM_IMAGE_STYLE
|
|
283
|
+
+ '<img src="screens/home.png" alt="Home">'
|
|
284
|
+
+ '<dialog><img src="screens/home.png" alt="Home, full size"></dialog>'
|
|
285
|
+
+ '<details><summary>Full size</summary><img src="screens/home.png" alt="Home"></details>'
|
|
286
|
+
)
|
|
287
|
+
assert repeated_items(page_source) == ([], "")
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
@needs_renderer
|
|
291
|
+
def test_should_refuse_a_baseline_drawn_beside_each_variant() -> None:
|
|
292
|
+
page_source = (
|
|
293
|
+
ITEM_IMAGE_STYLE
|
|
294
|
+
+ '<img src="screens/baseline.png" alt="Baseline"><img src="screens/variant-a.png" alt="A">'
|
|
295
|
+
+ '<img src="screens/baseline.png" alt="Baseline"><img src="screens/variant-b.png" alt="B">'
|
|
296
|
+
)
|
|
297
|
+
all_repeats, _ = repeated_items(page_source)
|
|
298
|
+
assert all_repeats == [{"kind": "image", "item": "screens/baseline.png", "times": 2}]
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
@needs_renderer
|
|
302
|
+
def test_should_refuse_two_drawn_elements_with_one_item_key() -> None:
|
|
303
|
+
page_source = (
|
|
304
|
+
'<article data-item="release-date">Pick a release date</article>'
|
|
305
|
+
'<article data-item="login-test">Flaky login test</article>'
|
|
306
|
+
'<article data-item="release-date">Pick a release date</article>'
|
|
307
|
+
'<article data-item="login-test" hidden>Flaky login test</article>'
|
|
308
|
+
)
|
|
309
|
+
all_repeats, _ = repeated_items(page_source)
|
|
310
|
+
assert all_repeats == [{"kind": "data-item", "item": "release-date", "times": 2}]
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
@needs_renderer
|
|
314
|
+
def test_should_refuse_an_edit_that_adds_a_second_draw_of_a_screen(tmp_path: Path) -> None:
|
|
315
|
+
page_path = tmp_path / "page.html"
|
|
316
|
+
page_path.write_text(PAGE_WITH_ONE_SCREEN + "<footer>End</footer>", encoding="utf-8")
|
|
317
|
+
all_edits = [
|
|
318
|
+
{
|
|
319
|
+
"old_str": "<footer>End</footer>",
|
|
320
|
+
"new_str": SECOND_DRAW_OF_THE_SCREEN + "<footer>End</footer>",
|
|
321
|
+
}
|
|
322
|
+
]
|
|
323
|
+
assert _run_gate({"file_path": str(page_path)}).returncode == ALLOW_EXIT_CODE
|
|
324
|
+
denied_run = _run_gate({"file_path": str(page_path), "edits": all_edits})
|
|
325
|
+
assert denied_run.returncode == BLOCK_EXIT_CODE
|
|
326
|
+
assert "screens/home.png is drawn 2 times" in denied_run.stderr
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
@needs_renderer
|
|
330
|
+
def test_should_refuse_a_second_link_and_allow_the_recorded_one(tmp_path: Path) -> None:
|
|
331
|
+
page_path = tmp_path / "page.html"
|
|
332
|
+
page_path.write_text(PAGE_WITH_ONE_SCREEN, encoding="utf-8")
|
|
333
|
+
first_publish = _run_gate({"file_path": str(page_path)})
|
|
334
|
+
assert first_publish.returncode == ALLOW_EXIT_CODE
|
|
335
|
+
assert first_publish.stdout == ""
|
|
336
|
+
link_record_path(page_path).write_text(json.dumps({"url": PAGE_LINK, "title": "Sample Run"}))
|
|
337
|
+
denied_run = _run_gate({"file_path": str(page_path)})
|
|
338
|
+
assert denied_run.returncode == BLOCK_EXIT_CODE
|
|
339
|
+
assert PAGE_LINK in denied_run.stderr
|
|
340
|
+
assert "When the user asks for a separate copy" in denied_run.stderr
|
|
341
|
+
assert "page.html.artifact-link.json" in denied_run.stderr
|
|
342
|
+
allowed_run = _run_gate({"file_path": str(page_path), "url": PAGE_LINK})
|
|
343
|
+
assert allowed_run.returncode == ALLOW_EXIT_CODE
|
|
344
|
+
assert (
|
|
345
|
+
REPUBLISH_NOTE in json.loads(allowed_run.stdout)["hookSpecificOutput"]["additionalContext"]
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def test_should_name_every_place_that_keeps_the_link(tmp_path: Path) -> None:
|
|
350
|
+
page_path = tmp_path / "page.html"
|
|
351
|
+
page_path.write_text(
|
|
352
|
+
f'<meta name="artifact-link" content="{PAGE_LINK}">' + PAGE_WITH_ONE_SCREEN,
|
|
353
|
+
encoding="utf-8",
|
|
354
|
+
)
|
|
355
|
+
link_record_path(page_path).write_text(json.dumps({"url": PAGE_LINK, "title": "Sample Run"}))
|
|
356
|
+
denied_run = _run_gate({"file_path": str(page_path)})
|
|
357
|
+
assert denied_run.returncode == BLOCK_EXIT_CODE
|
|
358
|
+
assert "the artifact-link meta tag from the page and " in denied_run.stderr
|
|
359
|
+
assert "page.html.artifact-link.json, then publish" in denied_run.stderr
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def test_should_name_each_old_name_record_of_a_page_renamed_twice(tmp_path: Path) -> None:
|
|
363
|
+
for each_old_name in ("first-name.html", "second-name.html"):
|
|
364
|
+
link_record_path(tmp_path / each_old_name).write_text(
|
|
365
|
+
json.dumps({"url": PAGE_LINK, "title": "Sample Run"})
|
|
366
|
+
)
|
|
367
|
+
page_path = tmp_path / "third-name.html"
|
|
368
|
+
page_path.write_text(PAGE_WITH_ONE_SCREEN, encoding="utf-8")
|
|
369
|
+
denied_run = _run_gate({"file_path": str(page_path)})
|
|
370
|
+
assert denied_run.returncode == BLOCK_EXIT_CODE
|
|
371
|
+
assert "first-name.html.artifact-link.json" in denied_run.stderr
|
|
372
|
+
assert "second-name.html.artifact-link.json" in denied_run.stderr
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def test_should_name_the_other_link_a_second_place_holds(tmp_path: Path) -> None:
|
|
376
|
+
other_link = "https://claude.ai/artifact/OtherPageLink02"
|
|
377
|
+
page_path = tmp_path / "page.html"
|
|
378
|
+
page_path.write_text(
|
|
379
|
+
f'<meta name="artifact-link" content="{PAGE_LINK}">' + PAGE_WITH_ONE_SCREEN,
|
|
380
|
+
encoding="utf-8",
|
|
381
|
+
)
|
|
382
|
+
link_record_path(page_path).write_text(json.dumps({"url": other_link, "title": "Sample Run"}))
|
|
383
|
+
denied_run = _run_gate({"file_path": str(page_path)})
|
|
384
|
+
assert denied_run.returncode == BLOCK_EXIT_CODE
|
|
385
|
+
assert f"This page already has a link: {PAGE_LINK}" in denied_run.stderr
|
|
386
|
+
assert f"page.html.artifact-link.json (it holds {other_link})" in denied_run.stderr
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def test_should_refuse_a_call_url_that_is_no_artifact_link(tmp_path: Path) -> None:
|
|
390
|
+
page_path = tmp_path / "page.html"
|
|
391
|
+
page_path.write_text(PAGE_WITH_ONE_SCREEN, encoding="utf-8")
|
|
392
|
+
link_record_path(page_path).write_text(json.dumps({"url": PAGE_LINK, "title": "Sample Run"}))
|
|
393
|
+
denied_run = _run_gate({"file_path": str(page_path), "url": PAGE_LINK + "/"})
|
|
394
|
+
assert denied_run.returncode == BLOCK_EXIT_CODE
|
|
395
|
+
assert PAGE_LINK in denied_run.stderr
|
|
396
|
+
assert f"The url {PAGE_LINK}/ in this call is not an artifact link" in denied_run.stderr
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
@needs_renderer
|
|
400
|
+
def test_should_check_a_page_saved_in_another_text_encoding(tmp_path: Path) -> None:
|
|
401
|
+
page_path = tmp_path / "page.html"
|
|
402
|
+
page_path.write_bytes(
|
|
403
|
+
(PAGE_WITH_ONE_SCREEN + "<p>Caf\u00e9</p>" + SECOND_DRAW_OF_THE_SCREEN).encode("latin-1")
|
|
404
|
+
)
|
|
405
|
+
denied_run = _run_gate({"file_path": str(page_path)})
|
|
406
|
+
assert denied_run.returncode == BLOCK_EXIT_CODE
|
|
407
|
+
assert "screens/home.png is drawn 2 times" in denied_run.stderr
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def test_should_allow_reads_assets_and_other_tools(tmp_path: Path) -> None:
|
|
411
|
+
page_path = tmp_path / "page.html"
|
|
412
|
+
page_path.write_text(PAGE_WITH_ONE_SCREEN + SECOND_DRAW_OF_THE_SCREEN, encoding="utf-8")
|
|
413
|
+
assert _run_gate({"action": "read", "url": PAGE_LINK}).returncode == ALLOW_EXIT_CODE
|
|
414
|
+
assert _run_gate({"file_path": str(page_path), "asset": True}).returncode == ALLOW_EXIT_CODE
|
|
415
|
+
assert _run_gate({"file_path": str(tmp_path / "missing.html")}).returncode == ALLOW_EXIT_CODE
|