en un clic
js-in-html-testing
// Test JS logic embedded in HTML using two-layer strategy - Python unit tests + Playwright browser integration tests
// Test JS logic embedded in HTML using two-layer strategy - Python unit tests + Playwright browser integration tests
| name | js-in-html-testing |
| description | Test JS logic embedded in HTML using two-layer strategy - Python unit tests + Playwright browser integration tests |
| user_invocable | false |
For JavaScript logic embedded in HTML files (e.g., diff navigation in viewer.html), use a two-layer testing approach.
Replicate core JS algorithms in Python and verify correctness via pytest.
Best for: pure computation logic, state decisions, matching algorithms — anything that doesn't depend on the DOM.
Example: tests/test_diff_matching.py
# Replicate JS findPrevSameModel / findNextSameModel
def find_diff_parent_by_prefix(entries, idx):
...
def find_next_by_prefix(entries, idx):
...
# Replicate JS updateNavButtons state computation
def compute_nav_button_states(entries, cur_idx):
prev_idx = find_diff_parent_by_prefix(entries, cur_idx)
...
return (prev_enabled, next_enabled)
Advantages: fast (0.02s), no browser dependency, integrates with existing pytest setup.
Generate HTML with test data embedded, open it in real Chromium via Playwright, and verify DOM state and user interactions.
Best for: button disabled states, overlay show/hide, keyboard events, click navigation, etc.
Example: tests/test_nav_browser.py
def _build_test_html():
from claude_tap.viewer import VIEWER_SCRIPT_ANCHOR, _read_viewer_template
template = _read_viewer_template()
records = [json.dumps(e) for e in TEST_ENTRIES]
data_js = "const EMBEDDED_TRACE_DATA = [\n" + ",\n".join(records) + "\n];\n"
# Inject data into template
return template.replace(
VIEWER_SCRIPT_ANCHOR,
f"<script>\n{data_js}</script>\n{VIEWER_SCRIPT_ANCHOR}",
1,
)
@pytest.fixture(scope="module")
def browser_page(html_file):
from playwright.sync_api import sync_playwright
pw = sync_playwright().start()
browser = pw.chromium.launch(headless=True)
page = browser.new_page()
page.goto(f"file://{html_file}")
page.wait_for_selector(".sidebar-item", timeout=5000)
yield page
browser.close()
pw.stop()
def _get_nav_state(page):
return page.evaluate("""() => {
const overlay = document.querySelector('.diff-overlay');
if (!overlay) return { overlayExists: false };
return {
overlayExists: true,
prevDisabled: overlay.querySelector('.diff-nav-prev')?.disabled,
nextDisabled: overlay.querySelector('.diff-nav-next')?.disabled,
title: overlay.querySelector('.diff-title')?.textContent,
};
}""")
# Click buttons
page.click(".diff-nav-next")
# Keyboard navigation
page.keyboard.press("ArrowRight")
# Call internal JS functions
page.evaluate("selectEntry(2)")
page.evaluate("showDiff()")
</script> text (e.g., when Claude discusses code), which breaks <script> block parsing. Escape it: line.replace("</script>", '</scr" + "ipt>')turn, duration_ms, request_id, request.path, etc.)uv pip install playwright# Fast unit tests
uv run pytest tests/test_diff_matching.py -v
# Browser integration tests
uv run pytest tests/test_nav_browser.py -v
# All (excluding slow e2e)
uv run pytest tests/ --ignore=tests/test_e2e.py -v
Run PR-grade real Codex E2E validation through claude-tap, including resume turns, multiple tool calls, optional image input, viewer verification, and screenshot evidence.
Full pre-PR merge-readiness check. Run this before opening or merging a pull request — it validates local gates (lint, format, tests), CI status, screenshot evidence, and PR metadata in one pass. Also useful for reviewing an existing PR's readiness.
Fill missing i18n translations in the viewer source JSON. Run this after adding or modifying English or Chinese UI strings in claude_tap/viewer_i18n.json — it auto-translates to ja, ko, fr, ar, de, ru via OpenRouter.
Validate screenshot and viewer HTML quality for PR evidence. Run this after adding or modifying images under .agents/evidence/pr/ or .agents/recordings/, or after generating a new viewer HTML file. Combines image quality checks (resolution, blankness, file size) with Playwright-based viewer rendering verification.
Generate demo assets (GIF/MP4) from real tmux E2E runs and viewer screenshots using asciinema and Playwright
Validate maintainer docs structure, standards freshness, manifest paths, and plan state. Run this after modifying any file under .agents/docs/standards/, .agents/docs/plans/, .agents/docs/architecture/, or AGENTS.md — it catches stale metadata, broken manifest paths, and plan state drift before CI does.