Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

test(pixel-draw): the Claude subscription before the API key

Soohong Kim4fb0041f3c46
diff --git a/tests/gen/test_anthropic_api.py b/tests/gen/test_anthropic_api.py
index 92b7822..3a63012 100644
--- a/tests/gen/test_anthropic_api.py
+++ b/tests/gen/test_anthropic_api.py
@@ -264,11 +264,3 @@ def test_the_price_of_a_call_is_its_billed_tokens_at_the_published_prices(monkey
usage = {"input_tokens": 1}
assert api.cost_usd(usage, batch=False) == 8.00
assert asked == [usage]
-
-
-def test_credential():
- with pytest.raises(SystemExit, match="ANTHROPIC_API_KEY is not set"):
- api.resolve_credential({})
- with pytest.raises(SystemExit, match="set but empty"):
- api.resolve_credential({"ANTHROPIC_API_KEY": " "})
- assert api.resolve_credential({"ANTHROPIC_API_KEY": KEY}) == KEY
diff --git a/tests/gen/test_claude_login.py b/tests/gen/test_claude_login.py
new file mode 100644
index 0000000..d5180be
--- /dev/null
+++ b/tests/gen/test_claude_login.py
@@ -0,0 +1,191 @@
+# SPDX-License-Identifier: Apache-2.0
+"""Claude calls on the person's subscription first (`claude_login`): which credential a call runs on,
+what the child `claude -p` inherits, how a message maps onto the binary's flags and stdin, and how
+its stream-json answer, a cut answer and an error come back. No binary, no network: a fake `run`
+answers with the event shapes `claude` 2.1.300 printed (2026-10-15)."""
+from __future__ import annotations
+
+import json
+import subprocess
+
+import pytest
+
+from sprite_gen.gen import anthropic_api as api
+from sprite_gen.gen import claude_login
+from sprite_gen.gen.anthropic_api import ApiError
+
+KEY = "sk-ant-api03-" + "Synthetic0Key1For2Tests" * 2
+
+
+def status(code: int, payload) -> "subprocess.CompletedProcess[str]":
+ text = payload if isinstance(payload, str) else json.dumps(payload)
+ return subprocess.CompletedProcess(["claude"], code, stdout=text, stderr="")
+
+
+class Run:
+ """A fake `subprocess.run`: records what it was asked and answers in turn."""
+
+ def __init__(self, *answers):
+ self.answers = list(answers)
+ self.calls: list[dict] = []
+
+ def __call__(self, args, **kwargs):
+ self.calls.append({"args": args, **kwargs})
+ answer = self.answers.pop(0)
+ if isinstance(answer, BaseException):
+ raise answer
+ return answer
+
+
+SIGNED_IN = status(0, {"loggedIn": True, "authMethod": "claude.ai", "subscriptionType": "max"})
+SIGNED_OUT = status(1, {"loggedIn": False, "authMethod": "none"})
+
+
+def test_the_child_runs_without_a_key_and_without_the_calling_session():
+ env = {"PATH": "/usr/bin", "HOME": "/home/x", "ANTHROPIC_API_KEY": KEY, "ANTHROPIC_AUTH_TOKEN": "t",
+ "CLAUDECODE": "1", "CLAUDE_PID": "7", "CLAUDE_EFFORT": "max", "CLAUDE_CODE_EFFORT_LEVEL": "max",
+ "CLAUDE_CODE_SESSION_ID": "s", "CLAUDE_CODE_ENTRYPOINT": "cli", "CLAUDE_CODE_OAUTH_TOKEN": "o",
+ "CLAUDE_CODE_USE_VERTEX": "1", "ORCHESTRATOR_MEMBER_ID": "m"}
+ child = claude_login.child_env(env)
+ assert child == {"PATH": "/usr/bin", "HOME": "/home/x", "CLAUDE_CODE_OAUTH_TOKEN": "o",
+ "CLAUDE_CODE_USE_VERTEX": "1"}
+
+
[email protected]("answer, expected", [
+ (SIGNED_IN, claude_login.Login("claude.ai", "max")),
+ (status(0, {"loggedIn": True, "authMethod": "oauth_token", "subscriptionType": "team"}),
+ claude_login.Login("oauth_token", "team")),
+ (SIGNED_OUT, None),
+ (status(0, {"loggedIn": True, "authMethod": "api_key"}), None),
+ (status(0, {"loggedIn": True, "authMethod": "third_party"}), None),
+ (FileNotFoundError("claude"), None),
+])
+def test_the_login_is_a_subscription_or_none(answer, expected):
+ run = Run(answer)
+ assert claude_login.login({"PATH": "/usr/bin", "ANTHROPIC_API_KEY": KEY}, run=run) == expected
+ (call,) = run.calls
+ assert call["args"][1:] == ["auth", "status", "--json"]
+ assert "ANTHROPIC_API_KEY" not in call["env"] # the status of the login the child would use
+
+
[email protected]("answer", [status(0, "not json"), status(2, {"loggedIn": False}),
+ subprocess.TimeoutExpired(["claude"], 30), PermissionError("denied")])
+def test_an_unreadable_status_is_refused_not_read_as_no_login(answer):
+ with pytest.raises(SystemExit, match="refusing to guess whether your Claude subscription is signed in"):
+ claude_login.login({}, run=Run(answer))
+
+
+def test_the_login_comes_before_the_key():
+ run = Run(SIGNED_IN)
+ credential = claude_login.resolve_credential(env={"ANTHROPIC_API_KEY": KEY}, run=run)
+ assert credential == claude_login.Credential("claude-login", login=claude_login.Login("claude.ai", "max"))
+ assert credential.token is None
+
+
+def test_the_key_only_without_a_login():
+ credential = claude_login.resolve_credential(env={"ANTHROPIC_API_KEY": KEY}, run=Run(SIGNED_OUT))
+ assert credential.source == "ANTHROPIC_API_KEY" and credential.token == KEY
+
+
+def test_neither_is_refused_naming_both():
+ with pytest.raises(SystemExit) as caught:
+ claude_login.resolve_credential(env={}, run=Run(FileNotFoundError("claude")))
+ message = str(caught.value)
+ assert "no subscription login (`claude auth status`) and ANTHROPIC_API_KEY is not set" in message
+ assert "`claude` then `/login`" in message
+ with pytest.raises(SystemExit, match="ANTHROPIC_API_KEY is set but empty"):
+ claude_login.resolve_credential(env={"ANTHROPIC_API_KEY": " "}, run=Run(SIGNED_OUT))
+
+
+def test_a_batch_runs_on_the_key_and_does_not_ask_the_login():
+ run = Run()
+ credential = claude_login.resolve_credential(batch=True, env={"ANTHROPIC_API_KEY": KEY}, run=run)
+ assert credential.source == "ANTHROPIC_API_KEY" and run.calls == []
+ with pytest.raises(SystemExit, match="--batch is Anthropic's Message Batches API.*drop --batch"):
+ claude_login.resolve_credential(batch=True, env={}, run=run)
+
+
+def stream(*events) -> "subprocess.CompletedProcess[str]":
+ return subprocess.CompletedProcess(["claude"], 0, stdout="".join(json.dumps(e) + "\n" for e in events), stderr="")
+
+
+USAGE = {"input_tokens": 2, "cache_creation_input_tokens": 689, "cache_read_input_tokens": 0, "output_tokens": 52,
+ "output_tokens_details": {"thinking_tokens": 30}}
+INIT = {"type": "system", "subtype": "init", "session_id": "s"}
+
+
+def assistant(*blocks, message_id="msg_1"):
+ return {"type": "assistant", "message": {"id": message_id, "role": "assistant", "content": list(blocks)}}
+
+
+def result(**fields):
+ return {"type": "result", "subtype": "success", "is_error": False, "stop_reason": "end_turn", "usage": USAGE,
+ "result": "", **fields}
+
+
+def test_a_message_maps_onto_the_flags_and_stdin(monkeypatch):
+ monkeypatch.setenv("ANTHROPIC_API_KEY", KEY)
+ monkeypatch.setenv("CLAUDE_CODE_SESSION_ID", "parent")
+ image = {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "iVBORw0K"}}
+ body = api.message_body(system=None, content=[image, {"type": "text", "text": "pick"}], effort="low",
+ max_tokens=16000, schema={"type": "object"})
+ run = Run(stream(INIT, assistant({"type": "thinking", "thinking": "A reads best"}),
+ assistant({"type": "tool_use", "name": "StructuredOutput"}),
+ result(stop_reason="tool_use", result='{"best":"A"}', structured_output={"best": "A"})))
+ reply = claude_login.send_message(body, label="pixel-draw pick", run=run)
+ (call,) = run.calls
+ args = call["args"]
+ assert args[1:] == ["-p", "--model", "claude-opus-5-5", "--effort", "low", "--input-format", "stream-json",
+ "--output-format", "stream-json", "--verbose", "--system-prompt", "", "--tools", "",
+ "--strict-mcp-config", "--no-session-persistence", "--safe-mode", "--max-turns", "1",
+ "--json-schema", '{"type":"object"}']
+ assert json.loads(call["input"]) == {"type": "user", "message": {"role": "user", "content": body["messages"][0]["content"]}}
+ assert call["env"]["CLAUDE_CODE_MAX_OUTPUT_TOKENS"] == "16000"
+ assert "ANTHROPIC_API_KEY" not in call["env"] and "CLAUDE_CODE_SESSION_ID" not in call["env"]
+ assert call["timeout"] == claude_login.CALL_TIMEOUT_SECONDS and call["cwd"]
+ assert json.loads(reply.text) == {"best": "A"} and reply.thinking == "A reads best"
+ assert (reply.mode, reply.stop_reason, reply.usage, reply.message_id) == ("subscription", "tool_use", USAGE, "msg_1")
+
+
+def test_a_text_prompt_becomes_one_text_block():
+ body = api.message_body(system="You draw.", content="Draw: a slime", effort="high", max_tokens=32000)
+ run = Run(stream(INIT, assistant({"type": "text", "text": "```pixels\n...```"}), result(result="```pixels\n...```")))
+ reply = claude_login.send_message(body, label="pixel-draw c1-gen1", run=run)
+ (call,) = run.calls
+ assert call["args"][call["args"].index("--system-prompt") + 1] == "You draw."
+ assert "--json-schema" not in call["args"]
+ assert json.loads(call["input"])["message"]["content"] == [{"type": "text", "text": "Draw: a slime"}]
+ assert reply.text == "```pixels\n...```" and reply.stop_reason == "end_turn"
+
+
+def test_an_answer_cut_at_the_cap_is_max_tokens_with_what_was_written():
+ body = api.message_body(system="s", content="Draw", effort="high", max_tokens=64000)
+ cut = result(is_error=True, stop_reason="stop_sequence", terminal_reason="api_error",
+ result="API Error: Claude's response exceeded the 64000 output token maximum. To configure this "
+ "behavior, set the CLAUDE_CODE_MAX_OUTPUT_TOKENS environment variable.")
+ reply = claude_login.send_message(body, label="c1-gen1", run=Run(stream(INIT, assistant(
+ {"type": "text", "text": "PALETTE"}), assistant({"type": "text", "text": "\n. transparent"}), cut)))
+ assert (reply.stop_reason, reply.text, reply.mode) == ("max_tokens", "PALETTE\n. transparent", "subscription")
+
+
+def test_a_refused_or_limited_login_is_an_error_not_a_switch_to_the_key():
+ body = api.message_body(system="s", content="Draw", effort="high", max_tokens=64000)
+ limited = result(is_error=True, subtype="success", api_error_status=429,
+ result="Claude AI usage limit reached|1760500000", usage={"output_tokens": 0})
+ with pytest.raises(ApiError) as caught:
+ claude_login.send_message(body, label="pixel-draw c1-gen1", run=Run(stream(INIT, limited)))
+ assert caught.value.line == ("pixel-draw c1-gen1: the Claude subscription did not answer (success, HTTP 429): "
+ "Claude AI usage limit reached|1760500000")
+ assert caught.value.status == 429 and caught.value.billed is False
+
+
+def test_no_result_a_timeout_and_a_missing_binary_are_errors():
+ body = api.message_body(system="s", content="Draw", effort="high", max_tokens=64000)
+ broken = subprocess.CompletedProcess(["claude"], 1, stdout="", stderr="Error: something broke")
+ with pytest.raises(ApiError, match=r"ended with exit 1 and no result: 'Error: something broke'"):
+ claude_login.send_message(body, label="c1-gen1", run=Run(broken))
+ with pytest.raises(ApiError, match=r"gave no answer in .*SPRITE_GEN_CLAUDE_CALL_TIMEOUT_SECONDS"):
+ claude_login.send_message(body, label="c1-gen1", run=Run(subprocess.TimeoutExpired(["claude"], 1)))
+ with pytest.raises(ApiError, match="did not start") as caught:
+ claude_login.send_message(body, label="c1-gen1", run=Run(FileNotFoundError("claude")))
+ assert caught.value.billed is False
diff --git a/tests/gen/test_gen.py b/tests/gen/test_gen.py
index e5f11af..8cc70d1 100644
--- a/tests/gen/test_gen.py
+++ b/tests/gen/test_gen.py
@@ -63,6 +63,18 @@ def test_provider_subprocess_env_scrubs_orchestrator_session_env(monkeypatch) ->
assert env.get("PATH") == "/usr/bin" # 일반 env 는 유지
+def test_provider_subprocess_env_reads_a_given_environment() -> None:
+ env = gen_base.provider_subprocess_env({"PATH": "/bin", "ORCHESTRATOR_MEMBER_ID": "w"})
+ assert env == {"PATH": "/bin"}
+
+
+def test_env_api_key_is_the_key_none_or_a_refusal_of_an_empty_one() -> None:
+ assert gen_base.env_api_key("SOME_API_KEY", {"SOME_API_KEY": " k "}, who="v") == "k"
+ assert gen_base.env_api_key("SOME_API_KEY", {}, who="v") is None
+ with pytest.raises(SystemExit, match="v: SOME_API_KEY is set but empty; give it a value or unset it"):
+ gen_base.env_api_key("SOME_API_KEY", {"SOME_API_KEY": " "}, who="v")
+
+
def test_provider_binary_resolves_windows_style_path_shims(monkeypatch) -> None:
monkeypatch.setattr(gen_base.shutil, "which", lambda name: f"C:/tools/{name}.CMD")
assert gen_base.provider_binary("codex") == "C:/tools/codex.CMD"
diff --git a/tests/gen/test_pixel_draw.py b/tests/gen/test_pixel_draw.py
index ed0a5df..e15718f 100644
--- a/tests/gen/test_pixel_draw.py
+++ b/tests/gen/test_pixel_draw.py
@@ -17,7 +17,7 @@ from PIL import Image
from conftest import help_options
from sprite_gen.gen import anthropic_api as api
-from sprite_gen.gen import pixel_draw, refusal
+from sprite_gen.gen import claude_login, pixel_draw, refusal
from sprite_gen.gen import pixel_grid as pg
from sprite_gen.gen.anthropic_api import ApiError, Reply
@@ -92,7 +92,9 @@ class FakeClaude:
@pytest.fixture
def claude(monkeypatch):
+ """The API key route: no subscription login here (`claude_login.login`), `ANTHROPIC_API_KEY` set."""
monkeypatch.setenv("ANTHROPIC_API_KEY", KEY)
+ monkeypatch.setattr(claude_login, "login", lambda env=None, run=None: None)
monkeypatch.setattr(pixel_draw, "BATCH_POLL_SECONDS", 0)
def make(script, **kwargs) -> FakeClaude:
@@ -538,14 +540,67 @@ def test_an_edited_grid_is_checked_and_rendered_without_a_call(tmp_path, claude,
pixel_draw.main(["--grid", str(edited), "--out-dir", str(tmp_path / "fourth"), "--candidates", "2"])
-def test_no_key_is_refused_before_the_folder_is_made(tmp_path, monkeypatch):
+def test_no_credential_is_refused_before_the_folder_is_made(tmp_path, monkeypatch):
monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False)
+ monkeypatch.setattr(claude_login, "login", lambda env=None, run=None: None)
out = tmp_path / "run"
- with pytest.raises(SystemExit, match="ANTHROPIC_API_KEY is not set"):
+ with pytest.raises(SystemExit, match=r"no subscription login \(`claude auth status`\) and ANTHROPIC_API_KEY is not set"):
draw(out, "--subject", "a knight")
assert not out.exists()
+class FakeLogin:
+ """The subscription route: `claude -p` answers by call id; the API must not be reached."""
+
+ def __init__(self, monkeypatch, script):
+ self.script = dict(script)
+ self.asked: list[tuple[str, dict]] = []
+ monkeypatch.setattr(claude_login, "login", lambda env=None, run=None: claude_login.Login("claude.ai", "max"))
+ monkeypatch.setattr(claude_login, "send_message", self.send)
+ for name in ("stream_message", "submit_batch", "get_batch", "batch_results"):
+ monkeypatch.setattr(api, name, lambda *a, **k: pytest.fail("the API was reached on a subscription run"))
+
+ def send(self, body, *, label, env=None, run=None):
+ call_id = label.split()[-1]
+ self.asked.append((call_id, body))
+ return replace(self.script[call_id], mode="subscription", request_id=None)
+
+
+def test_a_signed_in_subscription_draws_with_no_charge_even_with_a_key_set(tmp_path, monkeypatch, capsys):
+ monkeypatch.setenv("ANTHROPIC_API_KEY", KEY)
+ fake = FakeLogin(monkeypatch, {"c1-gen1": reply(ICON_ROW), "c1-gen1-fix1": reply(ICON_ROW_FIX)})
+ out = tmp_path / "run"
+ assert draw(out, "--subject", "a chest", "--kind", "icon", "--candidates", "1") == 0
+ report = report_of(out)
+ err = capsys.readouterr().err
+ assert "per-call API charge" not in err and "subscription" in err # the call lines say what it ran on
+ assert asked(fake) == ["c1-gen1", "c1-gen1-fix1"]
+ assert report["status"] == "ok" and report["auth_source"] == "claude-login" and report["cost_usd"] == 0.0
+ assert [(c["mode"], c["auth_source"], c["cost_usd"]) for c in report["calls"]] == [
+ ("subscription", "claude-login", 0.0)] * 2
+ assert report["calls"][0]["thinking_tokens"] == 1000 # usage is kept: it counts against the plan
+
+
+def test_a_batch_runs_on_the_key_even_when_signed_in(tmp_path, claude, monkeypatch, capsys):
+ monkeypatch.setattr(claude_login, "login", lambda env=None, run=None: pytest.fail("a batch asked the login"))
+ fake = claude({"c1-gen1": reply(SPRITE16)})
+ out = tmp_path / "run"
+ assert draw(out, "--subject", "a knight", "--size", "16", "--candidates", "1", "--batch") == 0
+ assert "[gen] claude: running on ANTHROPIC_API_KEY — this is a per-call API charge" in capsys.readouterr().err
+ assert [cid for cid, _ in fake.submitted[0][1]] == ["c1-gen1"]
+ assert report_of(out)["auth_source"] == "ANTHROPIC_API_KEY"
+
+
+def test_a_run_read_back_whole_asks_nothing_and_announces_no_charge(tmp_path, claude, capsys):
+ claude({"c1-gen1": reply(SPRITE16)})
+ out = tmp_path / "run"
+ assert draw(out, "--subject", "a knight", "--size", "16", "--candidates", "1") == 0
+ assert "per-call API charge" in capsys.readouterr().err
+ again = claude({})
+ assert pixel_draw.main(["--out-dir", str(out), "--resume"]) == 0
+ assert again.asked == [] and "per-call API charge" not in capsys.readouterr().err
+
+
def test_the_cli_names_the_verb_and_its_flags():
flags = help_options("-m", "sprite_gen.cli", "pixel-draw")
assert {"--subject", "--spec", "--grid", "--size", "--kind", "--frames", "--walk-cycle", "--palette", "--candidates",