notque/vexjoy-agent / skills/frontend/frontend
frontend
UnverifiedFrontend: UI design, distinctive visual styles, HTML artifacts, Three.js 3D, TypeScript/React/Next.js apps, e-commerce, portfolios, React Native, web performance.
scripts-html-artifact/tests/test_validate_artifact.py
View on GitHub"""Tests for validate-artifact.py — HTML artifact validator."""
from __future__ import annotations
import json
import re
import subprocess
import sys
import tempfile
from pathlib import Path
import pytest
SCRIPT = str(Path(__file__).parent.parent / "validate-artifact.py")
# --- Import module directly for unit tests ---
sys.path.insert(0, str(Path(__file__).parent.parent))
from importlib import import_module
validate_mod = import_module("validate-artifact")
validate_artifact = validate_mod.validate_artifact
# Minimal stand-in for templates/fonts/archivo-black.css: embedded face plus a heading rule.
HEADING_FONT_CSS = (
'@font-face { font-family: "Archivo Black"; src: url(data:font/woff2;base64,AAAA) format("woff2"); }'
" h1, h2, h3 { font-family: var(--font-heading); }"
)
VALID_HTML = """<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Artifact</title>
<style>body { margin: 0; } HEADING_FONT</style>
</head>
<body>
<h1>Hello</h1>
</body>
</html>""".replace("HEADING_FONT", HEADING_FONT_CSS)
VALID_HTML_WITH_COPY = """<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Editor</title>
<style>body { margin: 0; } HEADING_FONT</style>
</head>
<body>
<h1>Editor</h1>
<script>
function copyToClipboard(text) { navigator.clipboard.writeText(text); }
</script>
</body>
</html>""".replace("HEADING_FONT", HEADING_FONT_CSS)
MINIMAL_VALID = """<!DOCTYPE html>
<html>
<head><title>X</title><style>*{}</style></head>
<body><p>content</p></body>
</html>"""
def _write_tmp(content: str) -> Path:
"""Write content to a temp .html file and return its path."""
f = tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False, encoding="utf-8")
f.write(content)
f.close()
return Path(f.name)
def run_validate(file_path: str, compact: bool = False) -> tuple[dict, int]:
"""Run validate-artifact.py and return (parsed JSON, exit code)."""
cmd = [sys.executable, SCRIPT, file_path]
if compact:
cmd.append("--json-compact")
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
parsed = json.loads(result.stdout)
return parsed, result.returncode
class TestValidateArtifactDirect:
"""Unit tests calling validate_artifact() directly."""
def test_valid_html_passes_all(self) -> None:
path = _write_tmp(VALID_HTML)
try:
result = validate_artifact(path)
assert result.valid
assert all(result.checks.values())
assert result.errors == []
assert result.warnings == []
finally:
path.unlink()
# --- Heading font (Archivo Black) ---
def test_heading_without_embedded_font_fails(self) -> None:
path = _write_tmp(VALID_HTML.replace(HEADING_FONT_CSS, ""))
try:
result = validate_artifact(path)
assert not result.valid
assert result.checks["heading_font"] is False
assert any("Archivo Black" in e for e in result.errors)
finally:
path.unlink()
def test_heading_font_linked_not_embedded_fails(self) -> None:
linked = '@font-face { font-family: "Archivo Black"; src: url(https://fonts.gstatic.com/x.woff2); }'
path = _write_tmp(VALID_HTML.replace(HEADING_FONT_CSS, linked + " h1 { font-family: 'Archivo Black'; }"))
try:
result = validate_artifact(path)
assert result.checks["heading_font"] is False
assert any("data: URL" in e for e in result.errors)
finally:
path.unlink()
def test_heading_font_embedded_but_unused_fails(self) -> None:
face_only = HEADING_FONT_CSS.split(" h1, h2, h3")[0]
path = _write_tmp(VALID_HTML.replace(HEADING_FONT_CSS, face_only))
try:
result = validate_artifact(path)
assert result.checks["heading_font"] is False
assert any("no rule applies it" in e for e in result.errors)
finally:
path.unlink()
def test_heading_font_not_required_without_headings(self) -> None:
path = _write_tmp(MINIMAL_VALID)
try:
result = validate_artifact(path)
assert result.checks["heading_font"] is True
finally:
path.unlink()
def test_missing_doctype(self) -> None:
html = "<html><head><title>T</title><style>*{}</style></head><body><p>x</p></body></html>"
path = _write_tmp(html)
try:
result = validate_artifact(path)
assert not result.checks["has_doctype"]
assert not result.valid
finally:
path.unlink()
def test_missing_title(self) -> None:
html = "<!DOCTYPE html><html><head><style>*{}</style></head><body><p>x</p></body></html>"
path = _write_tmp(html)
try:
result = validate_artifact(path)
assert not result.checks["has_title"]
assert not result.valid
finally:
path.unlink()
def test_empty_title(self) -> None:
html = "<!DOCTYPE html><html><head><title> </title><style>*{}</style></head><body><p>x</p></body></html>"
path = _write_tmp(html)
try:
result = validate_artifact(path)
assert not result.checks["has_title"]
finally:
path.unlink()
def test_external_css_fails_self_contained(self) -> None:
html = (
"<!DOCTYPE html><html><head><title>T</title>"
'<link rel="stylesheet" href="https://cdn.example.com/style.css">'
"<style>*{}</style></head><body><p>x</p></body></html>"
)
path = _write_tmp(html)
try:
result = validate_artifact(path)
assert not result.checks["self_contained"]
assert any("external CSS" in e for e in result.errors)
finally:
path.unlink()
def test_external_js_fails_self_contained(self) -> None:
html = (
"<!DOCTYPE html><html><head><title>T</title><style>*{}</style></head>"
'<body><p>x</p><script src="https://cdn.example.com/app.js"></script></body></html>'
)
path = _write_tmp(html)
try:
result = validate_artifact(path)
assert not result.checks["self_contained"]
assert any("external JS" in e for e in result.errors)
finally:
path.unlink()
def test_no_style_is_warning(self) -> None:
html = "<!DOCTYPE html><html><head><title>T</title></head><body><p>x</p></body></html>"
path = _write_tmp(html)
try:
result = validate_artifact(path)
assert not result.checks["has_style"]
# has_style is a warning, not an error — so valid can still be True
assert any("style" in w.lower() for w in result.warnings)
finally:
path.unlink()
def test_missing_viewport_is_warning(self) -> None:
path = _write_tmp(MINIMAL_VALID)
try:
result = validate_artifact(path)
assert not result.checks["has_meta_viewport"]
assert any("viewport" in w for w in result.warnings)
# Warnings don't cause failure
assert result.valid
finally:
path.unlink()
def test_empty_body(self) -> None:
html = "<!DOCTYPE html><html><head><title>T</title><style>*{}</style></head><body> </body></html>"
path = _write_tmp(html)
try:
result = validate_artifact(path)
assert not result.checks["no_empty_body"]
assert not result.valid
finally:
path.unlink()
def test_missing_structure_tags(self) -> None:
html = "<!DOCTYPE html><title>T</title><style>*{}</style><p>content</p>"
path = _write_tmp(html)
try:
result = validate_artifact(path)
assert not result.checks["valid_structure"]
assert any("Missing structural tags" in e for e in result.errors)
finally:
path.unlink()
def test_large_file_warns(self) -> None:
# Create a file just over 500KB
html = VALID_HTML + ("x" * (501 * 1024))
path = _write_tmp(html)
try:
result = validate_artifact(path)
assert not result.checks["reasonable_size"]
assert any("500KB" in w for w in result.warnings)
finally:
path.unlink()
def test_export_button_check_skipped_without_shape(self) -> None:
path = _write_tmp(VALID_HTML)
try:
result = validate_artifact(path)
assert "has_export_button" not in result.checks
finally:
path.unlink()
def test_export_button_check_skipped_for_non_export_shape(self) -> None:
path = _write_tmp(VALID_HTML)
try:
result = validate_artifact(path, shape="spec")
assert "has_export_button" not in result.checks
finally:
path.unlink()
def test_export_button_warning_for_editor_without_copy(self) -> None:
path = _write_tmp(VALID_HTML)
try:
result = validate_artifact(path, shape="editor")
assert not result.checks["has_export_button"]
assert any("copy/export" in w for w in result.warnings)
# Warning, not error — still valid
assert result.valid
finally:
path.unlink()
def test_export_button_warning_for_prototype_without_copy(self) -> None:
path = _write_tmp(VALID_HTML)
try:
result = validate_artifact(path, shape="prototype")
assert not result.checks["has_export_button"]
assert any("copy/export" in w for w in result.warnings)
finally:
path.unlink()
def test_export_button_passes_with_clipboard(self) -> None:
path = _write_tmp(VALID_HTML_WITH_COPY)
try:
result = validate_artifact(path, shape="editor")
assert result.checks["has_export_button"]
assert not any("copy/export" in w for w in result.warnings)
finally:
path.unlink()
def test_export_button_passes_with_copy_word(self) -> None:
html = VALID_HTML.replace("</body>", "<script>function copy() {}</script></body>")
path = _write_tmp(html)
try:
result = validate_artifact(path, shape="prototype")
assert result.checks["has_export_button"]
finally:
path.unlink()
@pytest.mark.slow
class TestCLIInterface:
"""Integration tests via subprocess."""
def test_cli_valid_file(self) -> None:
path = _write_tmp(VALID_HTML)
try:
result, code = run_validate(str(path))
assert code == 0
assert result["valid"] is True
finally:
path.unlink()
def test_cli_invalid_file(self) -> None:
path = _write_tmp("<p>not valid html</p>")
try:
result, code = run_validate(str(path))
assert code == 1
assert result["valid"] is False
finally:
path.unlink()
def test_cli_file_not_found(self) -> None:
result, code = run_validate("/nonexistent/file.html")
assert code == 2
assert result["valid"] is False
assert any("not found" in e.lower() for e in result["errors"])
def test_cli_compact_json(self) -> None:
path = _write_tmp(VALID_HTML)
try:
cmd = [sys.executable, SCRIPT, str(path), "--json-compact"]
proc = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
output = proc.stdout.strip()
assert "\n" not in output.rstrip("\n")
parsed = json.loads(output)
assert parsed["valid"] is True
finally:
path.unlink()
def test_cli_shape_flag_editor(self) -> None:
path = _write_tmp(VALID_HTML)
try:
result, code = run_validate(str(path))
assert "has_export_button" not in result["checks"]
# Now with --shape editor
cmd = [sys.executable, SCRIPT, str(path), "--shape", "editor"]
proc = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
shaped_result = json.loads(proc.stdout)
assert "has_export_button" in shaped_result["checks"]
assert shaped_result["checks"]["has_export_button"] is False
finally:
path.unlink()
def test_cli_shape_flag_with_copy(self) -> None:
path = _write_tmp(VALID_HTML_WITH_COPY)
try:
cmd = [sys.executable, SCRIPT, str(path), "--shape", "editor"]
proc = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
assert proc.returncode == 0
result = json.loads(proc.stdout)
assert result["checks"]["has_export_button"] is True
finally:
path.unlink()
SAVED_DIR = Path(__file__).parent.parent.parent / "templates" / "saved"
FONT_FILE = Path(__file__).parent.parent.parent / "templates" / "fonts" / "archivo-black.css"
@pytest.mark.parametrize("name", sorted(p.stem for p in SAVED_DIR.glob("*.html")))
def test_saved_templates_embed_current_heading_font(name: str) -> None:
"""Saved templates carry their own copy of the font CSS; it must match the source file."""
html = (SAVED_DIR / f"{name}.html").read_text(encoding="utf-8")
font_data = re.search(r"base64,[A-Za-z0-9+/=]+", FONT_FILE.read_text(encoding="utf-8")).group(0)
assert font_data in html, f"{name}.html is missing or has a stale copy of fonts/archivo-black.css"
result = validate_mod.ValidationResult()
validate_mod._check_heading_font(html, result)
assert result.checks["heading_font"], result.errors
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |