Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
test(gen): a refused request is sent again only within the time limit of its call
Soohong Kim24f587ef98db
diff --git a/tests/gen/test_grok_provider.py b/tests/gen/test_grok_provider.pyindex b3a9642..9d54363 100644--- a/tests/gen/test_grok_provider.py+++ b/tests/gen/test_grok_provider.py@@ -5,10 +5,12 @@ import ioimport jsonimport subprocessimport urllib.error+from types import SimpleNamespaceimport pytestfrom PIL import Image+from http_answer import Answerfrom sprite_gen import genfrom sprite_gen.gen import base as gen_basefrom sprite_gen.gen import grok_provider as grok, retry, xai@@ -34,22 +36,11 @@ def api(monkeypatch, tmp_path):state = {"status": 200, "body": {"data": [{"b64_json": encoded_image()}]}, "calls": [], "slept": []}monkeypatch.setattr(retry, "_sleep", state["slept"].append)- class Response:- def __enter__(self):- return self- def __exit__(self, *args):- pass- @property- def status(self):- return state["status"]- def read(self):- return json.dumps(state["body"]).encode()-def open_request(request, *, timeout):state["calls"].append((request, timeout))if state.get("script"): # one (status, body) per request, in orderstate["status"], state["body"] = state["script"].pop(0)- return Response()+ return Answer(state["body"], status=state["status"])monkeypatch.setattr(xai.urllib.request, "urlopen", open_request)return state@@ -63,7 +54,7 @@ def test_generation_decodes_jpeg_to_png_without_agent(tmp_path, api):assert request.get_header("Authorization") == "Bearer synthetic-secret"assert body == {"model": grok.DEFAULT_MODEL, "prompt": "a blue square", "n": 1,"response_format": "b64_json", "aspect_ratio": "3:2"}- assert timeout == grok.GEN_TIMEOUT_SECONDS+ assert timeout == pytest.approx(grok.GEN_TIMEOUT_SECONDS, abs=1) # the first send waits the whole time limitwith Image.open(result.out) as image, Image.open(io.BytesIO(base64.b64decode(encoded_image()))) as original:assert image.format == "PNG" and image.size == (12, 8)assert image.tobytes() == original.tobytes()@@ -136,6 +127,30 @@ def test_a_rate_limit_still_there_after_two_more_sends_ends_the_call(tmp_path, aassert len(api["calls"]) == 3 and api["slept"] == [8.0, 16.0]+def test_a_rate_limit_is_sent_again_only_within_the_time_limit_of_the_image(tmp_path, api, monkeypatch, capsys):+ """SPRITE_GEN_GEN_TIMEOUT_SECONDS is the image's time limit from its first send (gen.retry): the 8 s wait+ leaves 12 s of 20 for the second send to wait in, the 16 s one would reach it, so the second 429 ends+ the call with its status and its stderr line says why it was not sent again."""+ now = [0.0]++ def sleep(seconds):+ api["slept"].append(seconds)+ now[0] += seconds++ for module in (grok, retry, xai):+ monkeypatch.setattr(module, "time", SimpleNamespace(monotonic=lambda: now[0]))+ monkeypatch.setattr(retry, "_sleep", sleep)+ monkeypatch.setattr(grok, "GEN_TIMEOUT_SECONDS", 20)+ limited = (429, {"code": "resource-exhausted", "error": "slow down"})+ api["script"] = [limited, limited, (200, api["body"])]+ with pytest.raises(SystemExit) as error:+ gen.generate_image("grok", "x", tmp_path / "out.png")+ assert [timeout for _request, timeout in api["calls"]] == [20, 12] and api["slept"] == [8.0]+ assert "(HTTP 429) code=resource-exhausted; sent 2 times, no provider fallback" in str(error.value)+ assert ("[gen] grok refused the request (429 resource-exhausted); nothing was made — not asking again: "+ "the 16s wait would reach the time limit (12s left)") in capsys.readouterr().err++@pytest.mark.parametrize("body", [{}, {"data": []}, {"data": [None]}, {"data": [{}, {}]},{"data": [{"b64_json": "???"}]},{"data": [{"b64_json": base64.b64encode(b"invalid image").decode()}]},diff --git a/tests/gen/test_openai_provider.py b/tests/gen/test_openai_provider.pyindex 10c3865..a8a6592 100644--- a/tests/gen/test_openai_provider.py+++ b/tests/gen/test_openai_provider.py@@ -13,10 +13,12 @@ import osimport reimport subprocessimport urllib.error+from types import SimpleNamespaceimport pytestfrom PIL import Image+from http_answer import Answerfrom sprite_gen import genfrom sprite_gen.gen import grok_provider as grokfrom sprite_gen.gen import openai_provider as openai@@ -42,22 +44,10 @@ def encoded_image(fmt="PNG", *, alpha=False, key=False):return base64.b64encode(buf.getvalue()).decode()-class _GrokResponse:+def _grok_answer(request, api):"""Minimal 200 answer for the xai transport (a PNG, so chroma stays untouched)."""-- def __init__(self, request, api):- api["calls"].append((request, None))-- def __enter__(self):- return self-- def __exit__(self, *args):- pass-- status = 200-- def read(self):- return json.dumps({"data": [{"b64_json": encoded_image()}]}).encode()+ api["calls"].append((request, None))+ return Answer({"data": [{"b64_json": encoded_image()}]})@pytest.fixture@@ -69,32 +59,14 @@ def api(monkeypatch):"slept": []}monkeypatch.setattr(retry, "_sleep", state["slept"].append)- class Response:- def __enter__(self):- return self-- def __exit__(self, *args):- pass-- @property- def status(self):- return state["status"]-- @property- def headers(self):- return state["headers"]-- def read(self):- return json.dumps(state["body"]).encode()-def open_request(request, *, timeout):state["calls"].append((request, timeout))if state.get("script"): # one (status, body, headers) per request, in orderstate["status"], state["body"], state["headers"] = state["script"].pop(0)+ answer = Answer(state["body"], status=state["status"], headers=state["headers"])if state["status"] == 200:- return Response()- raise urllib.error.HTTPError(request.full_url, state["status"], "error", state["headers"],- io.BytesIO(json.dumps(state["body"]).encode()))+ return answer+ raise urllib.error.HTTPError(request.full_url, state["status"], "error", state["headers"], answer)monkeypatch.setattr(openai.urllib.request, "urlopen", open_request)return state@@ -113,7 +85,7 @@ def test_generation_sends_the_billed_fields_and_publishes_a_png(tmp_path, api):assert _body(request) == {"model": openai.DEFAULT_MODEL, "prompt": "a blue square", "n": 1,"size": "1024x1024", "quality": "auto","output_format": "png", "background": "opaque"}- assert timeout == openai.GEN_TIMEOUT_SECONDS+ assert timeout == pytest.approx(openai.GEN_TIMEOUT_SECONDS, abs=1) # the first send waits the whole time limit# `response_format` is rejected for gpt-image models; the answer is always b64.assert "response_format" not in _body(request)with Image.open(result.out) as image:@@ -247,6 +219,31 @@ def test_a_refusal_still_there_after_two_more_sends_ends_the_call(tmp_path, api)assert len(api["calls"]) == 3 and api["slept"] == [8.0, 16.0]+def test_a_refusal_is_sent_again_only_within_the_time_limit_of_the_image(tmp_path, api, monkeypatch, capsys):+ """SPRITE_GEN_GEN_TIMEOUT_SECONDS is the image's time limit from its first send (gen.retry): the waits+ before a refused request goes again count in it and each send waits only what is left of it, so a+ refusal whose next wait would reach it ends the call, its line saying why, before a caller's ceiling."""+ now = [0.0]++ def sleep(seconds):+ api["slept"].append(seconds)+ now[0] += seconds++ for module in (openai, retry, xai):+ monkeypatch.setattr(module, "time", SimpleNamespace(monotonic=lambda: now[0]))+ monkeypatch.setattr(retry, "_sleep", sleep)+ monkeypatch.setattr(openai, "GEN_TIMEOUT_SECONDS", 20)+ limited = (429, {"error": {"type": "rate_limit_error", "code": "rate_limit_exceeded"}}, {})+ api["script"] = [limited, limited, (200, api["body"], {})]+ with pytest.raises(SystemExit) as refused:+ gen.generate_image("openai", "x", tmp_path / "out.png")+ assert [timeout for _request, timeout in api["calls"]] == [20, 12] and api["slept"] == [8.0]+ assert "(HTTP 429) code=rate_limit_exceeded; sent 2 times, no provider fallback" in str(refused.value)+ assert ("[gen] openai refused the request (429 rate_limit_exceeded); nothing was made — not asking again: "+ "the 16s wait would reach the time limit (12s left)") in capsys.readouterr().err+ assert not (tmp_path / "out.png").exists()++@pytest.mark.parametrize("body", [{}, {"data": []}, {"data": [None]}, {"data": [{}, {}]},{"data": [{"b64_json": "???"}]},{"data": [{"b64_json": base64.b64encode(b"invalid image").decode()}]},@@ -370,7 +367,7 @@ def test_grok_login_still_outranks_its_api_key(tmp_path, api, monkeypatch, capsy"key": "subscription-token", "expires_at": "2100-01-01T00:00:00Z"}}))assert xai.resolve_credential().source == xai.AUTH_SOURCE_GROK_LOGINmonkeypatch.setattr(xai.urllib.request, "urlopen",- lambda request, *, timeout: _GrokResponse(request, api))+ lambda request, *, timeout: _grok_answer(request, api))result = gen.generate_image("grok", "x", tmp_path / "grok.png")assert result.extra["auth_source"] == xai.AUTH_SOURCE_GROK_LOGIN# A subscription call says nothing about API billing; only the key route does.@@ -389,7 +386,7 @@ def test_grok_on_an_api_key_announces_the_charge_too(tmp_path, api, monkeypatch,monkeypatch.setenv("XAI_API_KEY", "xai-api-secret")monkeypatch.setenv("GROK_HOME", str(tmp_path / "no-login"))monkeypatch.setattr(xai.urllib.request, "urlopen",- lambda request, *, timeout: _GrokResponse(request, api))+ lambda request, *, timeout: _grok_answer(request, api))gen.generate_image("grok", "x", tmp_path / "grok.png")notice = [line for line in capsys.readouterr().err.splitlines() if "per-call API charge" in line]assert len(notice) == 1 and "XAI_API_KEY" in notice[0]@@ -525,7 +522,7 @@ def test_a_body_that_is_not_json_is_its_text_on_one_line(api, tmp_path, monkeypadef open_request(request, *, timeout):api["calls"].append((request, timeout))- raise urllib.error.HTTPError(request.full_url, 502, "error", {}, io.BytesIO(page.encode()))+ raise urllib.error.HTTPError(request.full_url, 502, "error", {}, Answer(page.encode(), status=502))monkeypatch.setattr(openai.urllib.request, "urlopen", open_request)with pytest.raises(SystemExit) as refused:diff --git a/tests/gen/test_pixel_draw.py b/tests/gen/test_pixel_draw.pyindex 300698b..3111798 100644--- a/tests/gen/test_pixel_draw.py+++ b/tests/gen/test_pixel_draw.py@@ -11,6 +11,7 @@ import ioimport jsonfrom dataclasses import replacefrom pathlib import Path+from types import SimpleNamespaceimport pytestfrom PIL import Image@@ -61,6 +62,7 @@ class FakeClaude:self.submitted: list[tuple[str, list]] = []self.polls = dict(polls or {})self.batch_ids = dict(known or {})+ self.deadlines: list[tuple[str, float | None]] = [] # each batch read and the time limit it was handeddef stream(self, token, body, *, sleep, label, timeout=None):assert token == KEY@@ -79,13 +81,15 @@ class FakeClaude:self.batch_ids[batch_id] = [cid for cid, _ in requests]return {"id": batch_id, "processing_status": "in_progress"}- def get(self, token, batch_id, *, sleep, label):+ def get(self, token, batch_id, *, sleep, label, deadline=None):+ self.deadlines.append(("get", deadline))states = self.polls.setdefault(batch_id, ["ended"])state = states.pop(0) if len(states) > 1 else states[0]return {"id": batch_id, "processing_status": state, "results_url": f"https://results.invalid/{batch_id}","ended_at": "2026-10-12T00:00:00Z" if state == "ended" else None}- def results(self, token, batch, *, sleep, label):+ def results(self, token, batch, *, sleep, label, deadline=None):+ self.deadlines.append(("results", deadline))return {cid: replace(self.script[cid], mode="batch", batch_id=batch["id"], seconds=None)for cid in self.batch_ids[batch["id"]]}@@ -391,6 +395,16 @@ def test_batches_draw_at_half_price_and_repairs_stay_realtime(tmp_path, claude):assert [(b["id"], b["collected"]) for b in report["batches"]] == [("msgbatch_1", True), ("msgbatch_2", True)]+def test_the_batch_wait_is_the_time_limit_its_reads_are_asked_again_within(tmp_path, claude, monkeypatch):+ """--batch-wait holds the reads of the batch, the waits `retry` asks a refused one again after included:+ each status and results read is handed the time the wait ends, counted from when the run began waiting."""+ monkeypatch.setattr(pixel_draw, "time", SimpleNamespace(monotonic=lambda: 100.0, sleep=lambda seconds: None))+ fake = claude({"c1-gen1": reply(SPRITE16)}, polls={"msgbatch_1": ["in_progress", "ended"]})+ assert draw(tmp_path / "run", "--subject", "a knight", "--size", "16", "--candidates", "1", "--batch",+ "--batch-wait", "90") == 0+ assert fake.deadlines == [("get", 190.0), ("get", 190.0), ("results", 190.0)]++def test_a_batch_cut_short_is_collected_on_resume_and_not_bought_twice(tmp_path, claude):script = {"c1-gen1": reply(SPRITE16)}claude(script, polls={"msgbatch_1": ["in_progress"]})diff --git a/tests/gen/test_retry.py b/tests/gen/test_retry.pyindex 8fc6cd0..c706aa2 100644--- a/tests/gen/test_retry.py+++ b/tests/gen/test_retry.py@@ -3,17 +3,19 @@provider documents the answer as a request not taken (Anthropic 429 but its spend cap, 500, 529;OpenAI 429 but its billing codes, 503; xAI 429), a GET on any of 429/5xx it may be answered with;the wait is the answer's retry-after when this run waits that long, else the doubling `backoff` the-video polls wait by; twice at most. Every direct provider call asks it — the Anthropic transport, the-OpenAI and Grok image calls, the facing check and the clip request."""+video polls wait by; twice at most, and only while the wait ends before the time limit of the call the+request belongs to. Every direct provider call asks it — the Anthropic transport, the OpenAI and Grok+image calls, the facing check and the clip request — and hands it that time limit."""from __future__ import annotationsimport io-import json+import timeimport urllib.errorimport pytestfrom PIL import Image+from http_answer import Answerfrom sprite_gen.gen import anthropic_api, facing_vision, grok_provider, openai_provider, retry, video, xaifrom sprite_gen.gen.base import GenRequest@@ -86,39 +88,79 @@ def test_ask_sends_again_says_so_and_returns_the_last_answer(capsys):"[gen] openai refused the request (503); nothing was made — asking again in 1s (2 of 2)"]+class _Clock:+ def __init__(self) -> None:+ self.now = 0.0+ self.slept: list[float] = []++ def monotonic(self) -> float:+ return self.now++ def sleep(self, seconds: float) -> None:+ self.slept.append(seconds)+ self.now += seconds+++def clock(monkeypatch) -> _Clock:+ """The test's clock where `retry` reads it and waits on it."""+ clock = _Clock()+ monkeypatch.setattr(retry, "time", clock)+ monkeypatch.setattr(retry, "_sleep", clock.sleep)+ return clock+++def test_a_request_is_sent_again_only_while_its_wait_ends_before_the_time_limit(clock, capsys):+ """The time limit of the call the request belongs to (`deadline`) holds its waits: the 8 s wait ends+ before 20 s, the 16 s one would not, so the second refusal is the answer, said on stderr in one line."""+ answers = [(429, {"code": "resource-exhausted"}, None)] * 3 + [(200, {}, None)]+ sent = []++ def send():+ sent.append(clock.now)+ return answers.pop(0)++ status, _body, _headers, asks = retry.ask(send, provider="xai", who="xai", label="video", deadline=20.0,+ kind=lambda body: body.get("code"))+ assert (status, asks, sent, clock.slept) == (429, 2, [0.0, 8.0], [8.0])+ assert capsys.readouterr().err.splitlines() == [+ "[video] xai refused the request (429 resource-exhausted); nothing was made — asking again in 8s (1 of 2)",+ "[video] xai refused the request (429 resource-exhausted); nothing was made — not asking again: "+ "the 16s wait would reach the time limit (12s left)"]++[email protected](("provider", "headers", "deadline", "why"), [+ # A wait that ends at the time limit leaves no time to send in.+ ("xai", None, 8.0, "the 8s wait would reach the time limit (8s left)"),+ # The answer's own wait counts the same.+ ("openai", {"retry-after": "30"}, 20.0, "the 30s wait would reach the time limit (20s left)"),+], ids=["backoff", "retry-after"])+def test_a_wait_that_would_reach_the_time_limit_is_not_waited(clock, capsys, provider, headers, deadline, why):+ status, _body, _headers, asks = retry.ask(lambda: (429, {}, headers), provider=provider, who=provider,+ label="gen", deadline=deadline)+ assert (status, asks, clock.slept) == (429, 1, [])+ assert capsys.readouterr().err == f"[gen] {provider} refused the request (429); nothing was made — not asking again: {why}\n"++# --- every direct provider call asks it -------------------------------------------------------------@pytest.fixturedef asked(monkeypatch):- """Every `retry.ask`: the provider and method it was asked for; the answer is the real one's."""+ """Every `retry.ask`: the provider and method it was asked for, and the seconds left of the time limit it+ was handed (None: none); the answer is the real one's."""real, seen = retry.ask, []def recording(send, **kwargs):- seen.append((kwargs["provider"], kwargs.get("method", "POST")))+ deadline = kwargs.get("deadline")+ seen.append((kwargs["provider"], kwargs.get("method", "POST"),+ None if deadline is None else round(deadline - time.monotonic())))return real(send, **kwargs)monkeypatch.setattr(retry, "ask", recording)return seen-class _Answer:- status = 200- headers: dict = {}-- def __init__(self, body: dict):- self.body = body-- def __enter__(self):- return self-- def __exit__(self, *args):- pass-- def read(self):- return json.dumps(self.body).encode()--def _png_b64() -> str:import base64@@ -127,22 +169,23 @@ def _png_b64() -> str:return base64.b64encode(buffer.getvalue()).decode()-def test_the_openai_image_call_asks_it(monkeypatch, tmp_path, asked):+def test_the_openai_image_call_and_its_facing_check_ask_it_within_the_gen_timeout(monkeypatch, tmp_path, asked):monkeypatch.setenv("OPENAI_API_KEY", "synthetic")monkeypatch.setattr(openai_provider.urllib.request, "urlopen",- lambda request, timeout: _Answer({"data": [{"b64_json": _png_b64()}]}))+ lambda request, timeout: Answer({"data": [{"b64_json": _png_b64()}]}))openai_provider.OpenAIProvider().generate(GenRequest("x", tmp_path / "raw.png"), tmp_path)- assert asked == [("openai", "POST")]+ openai_provider.OpenAIProvider().inspect_facing(tmp_path / "raw.png", tmp_path)+ assert asked == [("openai", "POST", openai_provider.GEN_TIMEOUT_SECONDS)] * 2-def test_the_grok_image_call_and_the_facing_check_ask_it(monkeypatch, tmp_path, asked):+def test_the_grok_image_call_and_the_facing_check_ask_it_within_their_timeouts(monkeypatch, tmp_path, asked):monkeypatch.setenv("XAI_API_KEY", "synthetic")monkeypatch.setenv("GROK_HOME", str(tmp_path / "no-login"))- monkeypatch.setattr(xai.urllib.request, "urlopen", lambda request, timeout: _Answer({"data": [{"b64_json": _png_b64()}]}))+ monkeypatch.setattr(xai.urllib.request, "urlopen", lambda request, timeout: Answer({"data": [{"b64_json": _png_b64()}]}))grok_provider.GrokProvider().generate(GenRequest("x", tmp_path / "raw.png"), tmp_path)facing_vision.grok_inspect(tmp_path / "raw.png", credential=xai.Credential("t", xai.AUTH_SOURCE_GROK_LOGIN),call=lambda *a, **k: (200, {"output": []}))- assert asked == [("xai", "POST"), ("xai", "POST")]+ assert asked == [("xai", "POST", grok_provider.GEN_TIMEOUT_SECONDS), ("xai", "POST", xai.HTTP_TIMEOUT_SECONDS)]def test_the_clip_request_asks_it(tmp_path, asked):@@ -159,15 +202,15 @@ def test_the_clip_request_asks_it(tmp_path, asked):credential=video.Credential("t", video.AUTH_SOURCE_GROK_LOGIN), call=call,download=lambda url, token, **_: b"\x00\x00\x00\x18ftypisom" + b"\x00" * 64,sleep=lambda s: None)- assert asked == [("xai", "POST")] and calls == ["POST", "GET"]+ assert asked == [("xai", "POST", video.POLL_TIMEOUT_SECONDS)] and calls == ["POST", "GET"]-def test_the_anthropic_transport_asks_it_for_its_posts_and_its_reads(monkeypatch, asked):+def test_the_anthropic_transport_asks_it_for_its_posts_and_its_reads_within_the_time_limit_handed(monkeypatch, asked):answers = iter([{"id": "msgbatch_1"}, {"id": "msgbatch_1", "processing_status": "in_progress"}])- monkeypatch.setattr(anthropic_api.urllib.request, "urlopen", lambda request, timeout: _Answer(next(answers)))+ monkeypatch.setattr(anthropic_api.urllib.request, "urlopen", lambda request, timeout: Answer(next(answers)))anthropic_api.submit_batch("k", [("c1", {})], sleep=lambda s: None)- anthropic_api.get_batch("k", "msgbatch_1", sleep=lambda s: None)- assert asked == [("anthropic", "POST"), ("anthropic", "GET")]+ anthropic_api.get_batch("k", "msgbatch_1", sleep=lambda s: None, deadline=time.monotonic() + 30)+ assert asked == [("anthropic", "POST", None), ("anthropic", "GET", 30)]# --- a refusal's body is read by one function ---------------------------------------------------------diff --git a/tests/gen/test_video.py b/tests/gen/test_video.pyindex 16dc2f7..7254ab4 100644--- a/tests/gen/test_video.py+++ b/tests/gen/test_video.py@@ -18,7 +18,7 @@ from pathlib import Pathimport pytestfrom PIL import Image-from sprite_gen.gen import video, xai+from sprite_gen.gen import retry, video, xaiNOW = datetime(2026, 9, 8, 12, 0, tzinfo=timezone.utc)MP4 = b"\x00\x00\x00\x18ftypisom" + b"\x00" * 64@@ -85,11 +85,11 @@ class _Clock:@pytest.fixturedef clock(monkeypatch) -> _Clock:- """The test's clock, in place of `time` where video and xai read it: the run's deadline, its waits and the- reads of its answers all count on it, and nothing waits on the real clock. Other modules keep theirs."""+ """The test's clock, in place of `time` where video, xai and retry read it: the run's deadline, its waits+ and the reads of its answers all count on it, and nothing waits on the real clock. Other modules keep theirs."""clock = _Clock()- monkeypatch.setattr(video, "time", clock)- monkeypatch.setattr(xai, "time", clock)+ for module in (video, xai, retry):+ monkeypatch.setattr(module, "time", clock)return clock@@ -637,10 +637,10 @@ def test_a_refused_submission_is_never_sent_again(tmp_path: Path, status) -> Nonassert HTTP_STATUS.search(message).group(1) == str(status)-def test_a_rate_limited_submission_is_sent_again_and_the_poll_timeout_counts_from_the_send_it_answered(+def test_a_rate_limited_submission_is_sent_again_and_the_poll_timeout_counts_from_its_first_send(tmp_path: Path, clock, capsys) -> None:"""xAI's 429 ("reached the rate limit") made nothing: the POST goes again after the doubling wait the- polls wait by, and the poll timeout starts at the send that was accepted, not the first one."""+ polls wait by, and the poll timeout starts at the first send, so that wait is inside it."""answers = [(429, {"code": "resource-exhausted", "error": "slow down"}), (200, {"request_id": "req-1"})]api = _FakeApi()api.post = None@@ -650,7 +650,7 @@ def test_a_rate_limited_submission_is_sent_again_and_the_poll_timeout_counts_froif method == "POST":api.calls.append((method, url, token, body))return answers.pop(0)- assert deadline == 8.0 + 30 # the accepted send came after the 8 s wait+ assert deadline == 30 # the first send, at 0; the accepted one came after the 8 s waitreturn real_call(method, url, token, body, deadline=deadline)result = video.generate_video(_request(tmp_path), credential=video.Credential("t", video.AUTH_SOURCE_GROK_LOGIN),@@ -668,6 +668,23 @@ def test_a_rate_limit_still_there_after_two_more_sends_keeps_its_status_for_the_assert HTTP_STATUS.search(message).group(1) == "429"+def test_a_rate_limited_submission_whose_next_wait_would_reach_the_poll_timeout_is_not_sent_again(+ tmp_path: Path, clock, capsys) -> None:+ """The waits before a refused POST goes again are inside the poll timeout: with 20 s, the 8 s wait leaves+ some of it and the 16 s one would reach it, so the second 429 ends the run, its status kept for the+ caller and its stderr line saying why it was not sent again — before a ceiling set above the timeout."""+ api = _FakeApi(post=(429, {"code": "resource-exhausted", "error": "slow down"}))+ request = _request(tmp_path)+ with pytest.raises(SystemExit) as refused:+ video.generate_video(request, credential=video.Credential("t", video.AUTH_SOURCE_GROK_LOGIN),+ call=api.call, download=api.download, sleep=clock.sleep, poll_timeout=20)+ assert [call[0] for call in api.calls] == ["POST", "POST"] and clock.slept == [8.0]+ assert HTTP_STATUS.search(str(refused.value)).group(1) == "429"+ assert ("[video] xai refused the request (429 resource-exhausted); nothing was made — not asking again: "+ "the 16s wait would reach the time limit (12s left)") in capsys.readouterr().err+ assert not request.out.exists() and api.downloads == []++def test_the_cli_leaves_an_unfinished_report_naming_the_request(tmp_path: Path, monkeypatch, clock, capsys) -> None:api = _FakeApi(polls=[(503, {"error": "unavailable"})])monkeypatch.setattr(video, "resolve_credential", lambda **_: video.Credential("t", video.AUTH_SOURCE_GROK_LOGIN))diff --git a/tests/http_answer.py b/tests/http_answer.pynew file mode 100644index 0000000..2bdce41--- /dev/null+++ b/tests/http_answer.py@@ -0,0 +1,36 @@+# SPDX-License-Identifier: Apache-2.0+"""One HTTP answer as `urllib.request.urlopen` hands it to a direct provider transport, for the offline+tests: its status, headers and body, read whole (`read`) or a chunk at a time by `xai.read_whole` when a+deadline bounds the reads (each read's wait set on the socket under it, kept in `waits`). A refusal+carries one as its body: `urllib.error.HTTPError(url, status, msg, headers, Answer(...))`."""+from __future__ import annotations++import io+import json+from types import SimpleNamespace+from typing import Any+++class Answer:+ def __init__(self, body: Any = b"", *, status: int = 200, headers: Any = None) -> None:+ self.status = status+ self.headers = {} if headers is None else headers+ self._body = io.BytesIO(body if isinstance(body, bytes) else json.dumps(body).encode())+ self.length = None # what http.client keeps of a Content-Length still to come: none was sent+ self.waits: list[float] = []+ self.fp = SimpleNamespace(raw=SimpleNamespace(_sock=SimpleNamespace(settimeout=self.waits.append)))++ def __enter__(self) -> Answer:+ return self++ def __exit__(self, *exc: Any) -> None:+ self._body.close()++ def isclosed(self) -> bool:+ return self._body.closed++ def read(self, amount: int = -1) -> bytes:+ return self._body.read(amount)++ def read1(self, amount: int = -1) -> bytes:+ return self._body.read1(amount)