diff --git a/.claude/skills/design/scripts/icon/generate.py b/.claude/skills/design/scripts/icon/generate.py index 5e00fa9..7d3a338 100644 --- a/.claude/skills/design/scripts/icon/generate.py +++ b/.claude/skills/design/scripts/icon/generate.py @@ -174,18 +174,68 @@ def apply_color(svg_code, color): return svg_code +# Root opening tag; quoted attribute values may contain ">". +_SVG_ROOT_TAG = re.compile(r""""']|"[^"]*"|'[^']*')*>""") + + +# One token of an opening tag: name[=value], or a stray quoted string. +# Quoted values are consumed whole, so text inside them is never a name. +_TAG_ATTR = re.compile( + r"""([^\s=/>"']+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>"']+))?|"[^"]*"|'[^']*'""") + + +def _set_root_attr(tag, attr, value): + """Set attr on a single opening tag, whatever quote style it uses.""" + for m in _TAG_ATTR.finditer(tag, len(' tag only, leaving the viewBox alone""" if size: - # Update width/height attributes if present - svg_code = re.sub(r'width="[^"]*"', f'width="{size}"', svg_code) - svg_code = re.sub(r'height="[^"]*"', f'height="{size}"', svg_code) - # Add width/height if not present - if 'width=' not in svg_code: - svg_code = svg_code.replace('' in out + assert 'viewBox="0 0 24 24"' in out + root = out[: out.index(">") + 1] + assert 'width="48"' in root and 'height="48"' in root + + +def test_viewbox_size_handles_width_and_height_independently(gen): + out = gen.apply_viewbox_size("", 32) + root = out[: out.index(">") + 1] + assert "width=\"32\"" in root and "height=\"32\"" in root + assert "width='10'" not in root + + +@pytest.mark.parametrize("src, kept", [ + ('', + {"aria-label": "height='2'"}), + ("", + {"aria-label": 'width="2" height="3"'}), + ('', + {"data-note": "a > b width='1'"}), + ("", + {"data-note": 'x>y height="2"'}), +]) +def test_viewbox_size_ignores_attribute_text_inside_quoted_values(gen, src, kept): + out = gen.apply_viewbox_size(src, 24) + root = ET.fromstring(out) + assert root.get("width") == "24" and root.get("height") == "24" + for name, value in kept.items(): + assert root.get(name) == value + for child in root: + assert child.get("width") in (None, "1") + + +def test_generate_icon_uses_large_token_budget(gen, tmp_path): + client = gen.genai.Client.return_value + client.models.generate_content.return_value = _response( + '') + out = tmp_path / "a.svg" + assert gen.generate_icon("crane", output_path=str(out)) == str(out) + cfg = client.models.generate_content.call_args.kwargs["config"] + assert cfg.max_output_tokens >= 16384 + + +def test_generate_icon_reports_max_tokens(gen, capsys): + client = gen.genai.Client.return_value + client.models.generate_content.return_value = _response( + '```svg\n\n' + ' opening tag; quoted attribute values may contain ">". +_SVG_ROOT_TAG = re.compile(r""""']|"[^"]*"|'[^']*')*>""") + + +# One token of an opening tag: name[=value], or a stray quoted string. +# Quoted values are consumed whole, so text inside them is never a name. +_TAG_ATTR = re.compile( + r"""([^\s=/>"']+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>"']+))?|"[^"]*"|'[^']*'""") + + +def _set_root_attr(tag, attr, value): + """Set attr on a single opening tag, whatever quote style it uses.""" + for m in _TAG_ATTR.finditer(tag, len(' tag only, leaving the viewBox alone""" if size: - # Update width/height attributes if present - svg_code = re.sub(r'width="[^"]*"', f'width="{size}"', svg_code) - svg_code = re.sub(r'height="[^"]*"', f'height="{size}"', svg_code) - # Add width/height if not present - if 'width=' not in svg_code: - svg_code = svg_code.replace('' in out + assert 'viewBox="0 0 24 24"' in out + root = out[: out.index(">") + 1] + assert 'width="48"' in root and 'height="48"' in root + + +def test_viewbox_size_handles_width_and_height_independently(gen): + out = gen.apply_viewbox_size("", 32) + root = out[: out.index(">") + 1] + assert "width=\"32\"" in root and "height=\"32\"" in root + assert "width='10'" not in root + + +@pytest.mark.parametrize("src, kept", [ + ('', + {"aria-label": "height='2'"}), + ("", + {"aria-label": 'width="2" height="3"'}), + ('', + {"data-note": "a > b width='1'"}), + ("", + {"data-note": 'x>y height="2"'}), +]) +def test_viewbox_size_ignores_attribute_text_inside_quoted_values(gen, src, kept): + out = gen.apply_viewbox_size(src, 24) + root = ET.fromstring(out) + assert root.get("width") == "24" and root.get("height") == "24" + for name, value in kept.items(): + assert root.get(name) == value + for child in root: + assert child.get("width") in (None, "1") + + +def test_generate_icon_uses_large_token_budget(gen, tmp_path): + client = gen.genai.Client.return_value + client.models.generate_content.return_value = _response( + '') + out = tmp_path / "a.svg" + assert gen.generate_icon("crane", output_path=str(out)) == str(out) + cfg = client.models.generate_content.call_args.kwargs["config"] + assert cfg.max_output_tokens >= 16384 + + +def test_generate_icon_reports_max_tokens(gen, capsys): + client = gen.genai.Client.return_value + client.models.generate_content.return_value = _response( + '```svg\n\n' + '