diff --git a/board/copy_contract.py b/board/copy_contract.py new file mode 100644 index 00000000..eef032e3 --- /dev/null +++ b/board/copy_contract.py @@ -0,0 +1,46 @@ +"""Shared regression assertion for rendered dashboard copy buttons. + +Use from a renderer's HTML tests, after rendering a representative page. This +checks the actual clipboard attributes, independently of skill-name registries +or prompt conversion helpers. It performs no network or filesystem access. +""" + +import re +from html.parser import HTMLParser + + +class _CopyButtons(HTMLParser): + def __init__(self): + super().__init__(convert_charrefs=True) + self.count = 0 + self.violations = [] + + def handle_starttag(self, tag, attrs): + if tag != "button": + return + attrs = dict(attrs) + terminal = "term" in (attrs.get("class") or "").split() or (attrs.get("aria-label") or "").lower().startswith("copy command:") + for key in ("data-cmd", "data-copy"): + if key not in attrs: + continue + self.count += 1 + payload = attrs[key] or "" + # Absolute executable paths (/usr/bin/tool) are not slash skills. + # A bare /tool is ambiguous and must be marked as a terminal chip. + if not terminal and re.match(r"^\s*/[a-zA-Z][\w-]*(?=\s|$)", payload): + self.violations.append(f"{key}: {payload.strip()[:160]}") + + +def assert_portable_copy_payloads(page): + """Fail on slash-skill clipboard payloads, or a vacuous page with no buttons. + + Both data-cmd (board family) and data-copy (Eyes) are checked. HTML entities + are decoded by the parser; shell commands and explicit terminal chips are + preserved. This is an assertion only: never rewrite the renderer's output. + """ + parser = _CopyButtons() + parser.feed(page) + parser.close() + assert parser.count, "No copy payloads found; render an actionable dashboard fixture" + assert not parser.violations, "Assistant-specific copy payloads:\n" + "\n".join( + parser.violations) diff --git a/tests/test_board.py b/tests/test_board.py index 86272dcc..188952af 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -453,6 +453,10 @@ def test_html_is_self_contained_with_one_tap_payloads(tmp_path): "runs.json": _run_json("failure")})) r = _run(["--html"], tmp_path, stub) page = r.stdout + import sys + sys.path.insert(0, str(BRAIN_HOME / "board")) + from copy_contract import assert_portable_copy_payloads + assert_portable_copy_payloads(page) # Self-containment: inline script and href anchors are allowed; external # ASSETS are not (the invariant the Heart board's tests settled on). # data-cmd payloads legitimately carry URLs, so strip them first. diff --git a/tests/test_copy_contract.py b/tests/test_copy_contract.py new file mode 100644 index 00000000..afac96d4 --- /dev/null +++ b/tests/test_copy_contract.py @@ -0,0 +1,46 @@ +"""The output guard catches regressions without a hard-coded skill registry.""" +import sys +from html import escape +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "board")) +from copy_contract import assert_portable_copy_payloads # noqa: E402 + + +@pytest.mark.parametrize("attribute", ["data-cmd", "data-copy"]) +@pytest.mark.parametrize("payload", ["/health", " /new_skill task\nKeep edits.", + "/future-skill --auto"]) +def test_rejects_client_commands_in_either_clipboard_attribute(attribute, payload): + page = f'' + with pytest.raises(AssertionError, match="Assistant-specific copy payloads"): + assert_portable_copy_payloads(page) + + +def test_checks_entities_and_every_button_not_just_the_first(): + page = '' + page += '' + with pytest.raises(AssertionError, match="future_skill"): + assert_portable_copy_payloads(page) + + +@pytest.mark.parametrize("payload", ["Use the health skill. Inspect CI.", + "bash bin/morning.sh", "pyauto-heart tick && pyauto-heart readiness", + "/usr/bin/tool --flag", "Read /tmp/input and use the health skill."]) +def test_prose_shell_commands_and_absolute_paths_are_allowed(payload): + assert_portable_copy_payloads( + f'') + + +@pytest.mark.parametrize("marker", ['class="copy term"', + 'aria-label="copy command: run the check"']) +def test_explicit_terminal_buttons_can_copy_a_root_executable(marker): + assert_portable_copy_payloads(f'') + + +@pytest.mark.parametrize("page", ["", 'Health', + '']) +def test_missing_clipboard_buttons_cannot_pass_vacuously(page): + with pytest.raises(AssertionError, match="No copy payloads"): + assert_portable_copy_payloads(page) diff --git a/tests/test_cortex_conductor.py b/tests/test_cortex_conductor.py index 52bbfe63..999d3842 100644 --- a/tests/test_cortex_conductor.py +++ b/tests/test_cortex_conductor.py @@ -274,6 +274,8 @@ def test_active_cards_lead_and_a_broken_tree_is_said_out_loud(tmp_skeleton): def test_the_html_twin_wears_the_theme_with_real_copy_buttons(skeleton): c = _cortex.census(skeleton) html = _cortex.render_dashboard_html(c) + from copy_contract import assert_portable_copy_payloads + assert_portable_copy_payloads(html) assert html.startswith("") assert "