diff --git a/.claude/skills/design/scripts/cip/generate.py b/.claude/skills/design/scripts/cip/generate.py index 394b06d..5e695e6 100644 --- a/.claude/skills/design/scripts/cip/generate.py +++ b/.claude/skills/design/scripts/cip/generate.py @@ -33,6 +33,14 @@ from datetime import datetime sys.path.insert(0, str(Path(__file__).parent)) from core import search, get_cip_brief +# These scripts print emoji, which raises UnicodeEncodeError when stdout/stderr is +# a cp1252 pipe or file (Windows without PYTHONUTF8). Same intent as the guard in +# ui-ux-pro-max/scripts/search.py. +for _stream in (sys.stdout, sys.stderr): + _encoding = getattr(_stream, "encoding", None) + if _encoding and _encoding.lower().replace("-", "") != "utf8" and hasattr(_stream, "reconfigure"): + _stream.reconfigure(encoding="utf-8", errors=_stream.errors) + def load_logo_image(logo_path): """Load logo image using PIL for Gemini image editing""" diff --git a/.claude/skills/design/scripts/cip/render-html.py b/.claude/skills/design/scripts/cip/render-html.py index 34e3782..35c1a0d 100644 --- a/.claude/skills/design/scripts/cip/render-html.py +++ b/.claude/skills/design/scripts/cip/render-html.py @@ -19,6 +19,14 @@ from datetime import datetime sys.path.insert(0, str(Path(__file__).parent)) from core import search, get_cip_brief +# These scripts print emoji, which raises UnicodeEncodeError when stdout/stderr is +# a cp1252 pipe or file (Windows without PYTHONUTF8). Same intent as the guard in +# ui-ux-pro-max/scripts/search.py. +for _stream in (sys.stdout, sys.stderr): + _encoding = getattr(_stream, "encoding", None) + if _encoding and _encoding.lower().replace("-", "") != "utf8" and hasattr(_stream, "reconfigure"): + _stream.reconfigure(encoding="utf-8", errors=_stream.errors) + # Deliverable descriptions for presentation DELIVERABLE_INFO = { "business card": { diff --git a/.claude/skills/design/scripts/tests/test_cip_stdout_encoding.py b/.claude/skills/design/scripts/tests/test_cip_stdout_encoding.py new file mode 100644 index 0000000..3115d43 --- /dev/null +++ b/.claude/skills/design/scripts/tests/test_cip_stdout_encoding.py @@ -0,0 +1,66 @@ +"""cip scripts print emoji; they must not crash when stdout is a cp1252 pipe or file.""" + +import base64 +import os +import subprocess +import sys +from pathlib import Path + +CIP = Path(__file__).resolve().parent.parent / "cip" + +# 1x1 transparent PNG +PNG = base64.b64decode( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==" +) + + +def _run(args, tmp_path): + env = dict(os.environ) + # generate.py's load_env() reads .env files from the skill folder and from + # ~/.claude and never overrides a variable that is already set. Point the home + # directory at tmp_path and set the keys to empty strings (treated as unset) + # so no local key can be picked up and no request is ever made. + env.update({ + "HOME": str(tmp_path), + "USERPROFILE": str(tmp_path), + "GEMINI_API_KEY": "", + "GOOGLE_API_KEY": "", + }) + # PYTHONIOENCODING=cp1252 reproduces a Windows console/pipe on any platform. + env.update({"PYTHONUTF8": "0", "PYTHONIOENCODING": "cp1252"}) + return subprocess.run( + [sys.executable, *map(str, args)], + capture_output=True, + stdin=subprocess.DEVNULL, + env=env, + cwd=tmp_path, + encoding="utf-8", + errors="replace", + ) + + +def test_render_html_survives_non_utf8_stdout(tmp_path): + images = tmp_path / "cip" + images.mkdir() + (images / "a.png").write_bytes(PNG) + out = tmp_path / "out.html" + + result = _run( + [CIP / "render-html.py", "--brand", "Cafe", "--images", images, "--output", out], + tmp_path, + ) + + assert result.returncode == 0, result.stderr + assert "HTML presentation generated" in result.stdout + assert "Cafe" in out.read_text(encoding="utf-8") + + +def test_generate_logo_warning_survives_non_utf8_stdout(tmp_path): + # No --logo and no --no-logo-prompt: the script prints its emoji warning and + # reads EOF from stdin. It then stops (exit 1) before any network call because + # google-genai or the API key is missing, so this stays offline. + result = _run([CIP / "generate.py", "--brand", "Cafe"], tmp_path) + + assert "UnicodeEncodeError" not in result.stderr, result.stderr + assert "No logo image provided" in result.stdout + assert result.returncode == 1 diff --git a/cli/assets/skills/design/scripts/cip/generate.py b/cli/assets/skills/design/scripts/cip/generate.py index 394b06d..5e695e6 100644 --- a/cli/assets/skills/design/scripts/cip/generate.py +++ b/cli/assets/skills/design/scripts/cip/generate.py @@ -33,6 +33,14 @@ from datetime import datetime sys.path.insert(0, str(Path(__file__).parent)) from core import search, get_cip_brief +# These scripts print emoji, which raises UnicodeEncodeError when stdout/stderr is +# a cp1252 pipe or file (Windows without PYTHONUTF8). Same intent as the guard in +# ui-ux-pro-max/scripts/search.py. +for _stream in (sys.stdout, sys.stderr): + _encoding = getattr(_stream, "encoding", None) + if _encoding and _encoding.lower().replace("-", "") != "utf8" and hasattr(_stream, "reconfigure"): + _stream.reconfigure(encoding="utf-8", errors=_stream.errors) + def load_logo_image(logo_path): """Load logo image using PIL for Gemini image editing""" diff --git a/cli/assets/skills/design/scripts/cip/render-html.py b/cli/assets/skills/design/scripts/cip/render-html.py index 34e3782..35c1a0d 100644 --- a/cli/assets/skills/design/scripts/cip/render-html.py +++ b/cli/assets/skills/design/scripts/cip/render-html.py @@ -19,6 +19,14 @@ from datetime import datetime sys.path.insert(0, str(Path(__file__).parent)) from core import search, get_cip_brief +# These scripts print emoji, which raises UnicodeEncodeError when stdout/stderr is +# a cp1252 pipe or file (Windows without PYTHONUTF8). Same intent as the guard in +# ui-ux-pro-max/scripts/search.py. +for _stream in (sys.stdout, sys.stderr): + _encoding = getattr(_stream, "encoding", None) + if _encoding and _encoding.lower().replace("-", "") != "utf8" and hasattr(_stream, "reconfigure"): + _stream.reconfigure(encoding="utf-8", errors=_stream.errors) + # Deliverable descriptions for presentation DELIVERABLE_INFO = { "business card": { diff --git a/cli/assets/skills/design/scripts/tests/test_cip_stdout_encoding.py b/cli/assets/skills/design/scripts/tests/test_cip_stdout_encoding.py new file mode 100644 index 0000000..3115d43 --- /dev/null +++ b/cli/assets/skills/design/scripts/tests/test_cip_stdout_encoding.py @@ -0,0 +1,66 @@ +"""cip scripts print emoji; they must not crash when stdout is a cp1252 pipe or file.""" + +import base64 +import os +import subprocess +import sys +from pathlib import Path + +CIP = Path(__file__).resolve().parent.parent / "cip" + +# 1x1 transparent PNG +PNG = base64.b64decode( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==" +) + + +def _run(args, tmp_path): + env = dict(os.environ) + # generate.py's load_env() reads .env files from the skill folder and from + # ~/.claude and never overrides a variable that is already set. Point the home + # directory at tmp_path and set the keys to empty strings (treated as unset) + # so no local key can be picked up and no request is ever made. + env.update({ + "HOME": str(tmp_path), + "USERPROFILE": str(tmp_path), + "GEMINI_API_KEY": "", + "GOOGLE_API_KEY": "", + }) + # PYTHONIOENCODING=cp1252 reproduces a Windows console/pipe on any platform. + env.update({"PYTHONUTF8": "0", "PYTHONIOENCODING": "cp1252"}) + return subprocess.run( + [sys.executable, *map(str, args)], + capture_output=True, + stdin=subprocess.DEVNULL, + env=env, + cwd=tmp_path, + encoding="utf-8", + errors="replace", + ) + + +def test_render_html_survives_non_utf8_stdout(tmp_path): + images = tmp_path / "cip" + images.mkdir() + (images / "a.png").write_bytes(PNG) + out = tmp_path / "out.html" + + result = _run( + [CIP / "render-html.py", "--brand", "Cafe", "--images", images, "--output", out], + tmp_path, + ) + + assert result.returncode == 0, result.stderr + assert "HTML presentation generated" in result.stdout + assert "Cafe" in out.read_text(encoding="utf-8") + + +def test_generate_logo_warning_survives_non_utf8_stdout(tmp_path): + # No --logo and no --no-logo-prompt: the script prints its emoji warning and + # reads EOF from stdin. It then stops (exit 1) before any network call because + # google-genai or the API key is missing, so this stays offline. + result = _run([CIP / "generate.py", "--brand", "Cafe"], tmp_path) + + assert "UnicodeEncodeError" not in result.stderr, result.stderr + assert "No logo image provided" in result.stdout + assert result.returncode == 1