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 engine's safe area - prepare's margin in the prompt as rows and columns, the feet on its bottom row, a pixel on the canvas edge fails, a body past it by more than a pixel warns, --grid included
Soohong Kimb2b64a20e8fd
diff --git a/tests/gen/test_pixel_draw.py b/tests/gen/test_pixel_draw.pyindex 919ce73..f02d1d2 100644--- a/tests/gen/test_pixel_draw.py+++ b/tests/gen/test_pixel_draw.py@@ -534,3 +534,76 @@ 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","--effort", "--format", "--batch", "--batch-wait", "--max-tokens", "--pick", "--out-dir", "--resume"} <= flags+++def pixel_request(**changes) -> pixel_draw.PixelRequest:+ base = dict(subject="a knight", size=32, kind="sprite", frames=("sprite",), walk_cycle=False,+ palette=pg.palette_rule("free"), candidates=1, effort="high", answer_format="chargrid",+ batch=False, max_tokens=32000, pick="vision")+ return pixel_draw.PixelRequest(**{**base, **changes})++[email protected]("size,margin,inner,first,last", [(16, 1, 14, 2, 15), (24, 2, 20, 3, 22), (32, 3, 26, 4, 29),+ (48, 4, 40, 5, 44)])+def test_the_prompt_says_the_safe_area_in_pixels(size, margin, inner, first, last):+ """The engine's safe area (prepare's 9.4% margin, floored), in numbers the model can count."""+ sprite = pixel_draw.generation_prompt(pixel_request(size=size))+ assert "fill most of the canvas" not in sprite and "1-2 pixel margin" not in sprite+ assert (f"Safe area: keep a {margin}-pixel margin on every side and draw the subject's body inside the central "+ f"{inner}x{inner} pixels: rows {first}-{last} and columns {first}-{last}, counting rows and columns "+ "from 1.") in sprite+ assert f"the outermost rows and columns (rows 1 and {size}, columns 1 and {size})" in sprite.lower()+ assert (f"Floor: row {last}, the bottom row of the safe area, is the ground. The feet (or the lowest point the "+ f"subject rests on) stand on row {last}, and nothing of the body goes below it.") in sprite+ if margin > 1:+ assert (f"Only a thin detail (a weapon tip, a strand of hair, an effect) may reach into the margin, at most "+ f"{margin - 1} pixel{'s' if margin > 2 else ''} past the safe area;") in sprite+ else:+ assert "at this size nothing reaches past the safe area." in sprite+++def test_the_prompt_keeps_every_frame_on_the_floor_row_and_icons_off_it():+ walk = pixel_draw.generation_prompt(pixel_request(frames=("walk1", "walk2", "walk3", "walk4"), walk_cycle=True))+ assert "stand on row 29 in every frame, and nothing of the body goes below it." in walk+ assert "the same bottom row" not in walk+ icon = pixel_draw.generation_prompt(pixel_request(kind="icon", frames=("icon",)))+ assert "draw the icon inside the central 26x26 pixels: rows 4-29 and columns 4-29" in icon+ assert "Floor:" not in icon+ tile = pixel_draw.generation_prompt(pixel_request(kind="tile", frames=("tile",)))+ assert "Safe area" not in tile and "Floor:" not in tile+ tiny = pixel_draw.generation_prompt(pixel_request(size=8))+ assert ("Safe area: at 8x8 there is no margin; the subject may use the whole canvas, with the four corner pixels "+ "transparent.") in tiny+ assert "Floor: row 8, the bottom row of the safe area, is the ground." in tiny+++def test_the_prompt_takes_the_margin_from_prepare(monkeypatch):+ from sprite_gen.gen import prepare+ monkeypatch.setattr(prepare, "DEFAULT_SAFE_MARGIN_RATIO", 0.125)+ prompt = pixel_draw.generation_prompt(pixel_request())+ assert "keep a 4-pixel margin on every side" in prompt and "the central 24x24 pixels: rows 5-28" in prompt+++def test_the_pick_knows_the_margin_is_meant():+ pick = pixel_draw.pick_prompt(pixel_request(), ["A", "B"])+ assert ("The request was: a knight Canvas 32x32 pixels per frame, the body kept inside the central 26x26 with a "+ "3-pixel margin.") in pick+ tile = pixel_draw.pick_prompt(pixel_request(kind="tile", frames=("tile",), subject="grass"), ["A", "B"])+ assert "The request was: grass Canvas 32x32 pixels per frame." in tile+++def test_an_edited_grid_goes_through_the_safe_area_gate(tmp_path):+ grid, _ = pg.parse_answer(SPRITE16, "chargrid", 16, 16, ("front",))+ full = tmp_path / "full.txt"+ full.write_text(pg.to_text(grid), encoding="utf-8") # drawn under the old prompt: rows 1 and 16 are used+ with pytest.raises(SystemExit, match=r"full.txt fails G8"):+ pixel_draw.main(["--grid", str(full), "--out-dir", str(tmp_path / "full")])+ report = report_of(tmp_path / "full")+ assert report["status"] == "failed" and report["gates"]["G8"]["frames"]["front"]["edge_pixels"] == 14+ inside = tmp_path / "inside.txt"+ rows = ["." * 16] + ["...." + "a" * 8 + "...."] * 14 + ["." * 16]+ inside.write_text("PALETTE\n. transparent\na #2a1f2e\nFRAME front\n" + "\n".join(rows) + "\nEND\n", encoding="utf-8")+ assert pixel_draw.main(["--grid", str(inside), "--out-dir", str(tmp_path / "inside")]) == 0+ report = report_of(tmp_path / "inside")+ assert report["status"] == "ok" and report["warnings"] == []+ assert report["gates"]["G8"]["floor_row"] == 14 and report["gates"]["G8"]["safe_area"] == [14, 14]diff --git a/tests/gen/test_pixel_gates.py b/tests/gen/test_pixel_gates.pyindex 55f5161..ddd151e 100644--- a/tests/gen/test_pixel_gates.py+++ b/tests/gen/test_pixel_gates.py@@ -7,6 +7,7 @@ from pathlib import Pathimport pytest+from sprite_gen.gen import pixel_gates, preparefrom sprite_gen.gen import pixel_grid as pgfrom sprite_gen.gen.pixel_gates import evaluate, g6_frames, seam_ratio@@ -108,3 +109,112 @@ def test_g6_failures():assert spread["height_spread"] == 2 and not spread["ok"]gates = evaluate({"a": low, "b": [row[:] for row in low]}, kind="sprite", rule=FREE16, walk_cycle=False)assert gates["failed"] == ["G6"]+++# ----------------------------------------------------------------------------- G8 safe area++def canvas(size: int = 32):+ return [[CLEAR] * size for _ in range(size)]+++def paint(frame, x0: int, y0: int, x1: int, y1: int, fill=INK):+ """`frame` with columns x0..x1 of rows y0..y1 opaque (inclusive)."""+ for y in range(y0, y1 + 1):+ for x in range(x0, x1 + 1):+ frame[y][x] = fill+ return frame+++def figure(size: int = 32):+ """A body filling the safe area's height: at 32 px columns 8..23 of rows 3..28, the 26x26 box's+ rows from top to the floor row."""+ m = int(size * prepare.DEFAULT_SAFE_MARGIN_RATIO)+ return paint(canvas(size), size // 4, m, size - 1 - size // 4, size - 1 - m)++[email protected]("size,margin,inner", [(8, 0, 8), (16, 1, 14), (24, 2, 20), (32, 3, 26), (48, 4, 40)])+def test_the_safe_area_is_the_engines(size, margin, inner):+ area = pixel_gates.safe_area(size, size)+ cell = prepare.normalize_cell({"width": size, "height": size}, size, None)+ assert area["margin"] == [margin, margin] == [cell["safe_margin_x"], cell["safe_margin_y"]]+ assert margin == int(size * prepare.DEFAULT_SAFE_MARGIN_RATIO)+ assert area["size"] == [inner, inner]+ assert area["box"] == [margin, margin, size - 1 - margin, size - 1 - margin]+ assert area["floor_row"] == size - 1 - margin # where prepare's fit grounds a frame: the safe area's last row+++def test_the_safe_area_follows_prepares_ratio(monkeypatch):+ monkeypatch.setattr(prepare, "DEFAULT_SAFE_MARGIN_RATIO", 0.125)+ assert pixel_gates.safe_area(32, 32)["margin"] == [4, 4]+ assert pixel_gates.safe_area(32, 16)["margin"] == [4, 2] # an edited grid need not be square+++def test_g8_passes_a_body_inside_the_safe_area():+ gates = evaluate({"f": figure()}, kind="sprite", rule=FREE16, walk_cycle=False)+ g8 = gates["G8"]+ assert gates["ok"] and gates["warnings"] == []+ assert g8["ok"] and g8["margin"] == [3, 3] and g8["safe_area"] == [26, 26] and g8["floor_row"] == 28+ frame = g8["frames"]["f"]+ assert frame["edge_pixels"] == 0 and frame["body_box"] == [8, 3, 23, 28] and frame["baseline"] == 28+ assert frame["overflow"] == {"left": 0, "top": 0, "right": 0, "bottom": 0}+++def test_g8_fails_a_pixel_on_the_canvas_edge():+ for x, y in ((31, 15), (15, 0), (0, 20), (12, 31)):+ frame = figure()+ frame[y][x] = INK+ gates = evaluate({"f": frame}, kind="sprite", rule=FREE16, walk_cycle=False)+ assert not gates["ok"] and gates["failed"] == ["G8"], (x, y)+ assert gates["G8"]["frames"]["f"]["edge_pixels"] == 1 and gates["G8"]["edge_pixels"] == 1+ standing_on_the_edge = paint(canvas(), 8, 6, 23, 31)+ gates = evaluate({"f": standing_on_the_edge}, kind="icon", rule=FREE16, walk_cycle=False)+ assert gates["failed"] == ["G8"] and gates["G8"]["frames"]["f"]["edge_pixels"] == 16+++def test_g8_lets_a_thin_tip_into_the_margin():+ frame = paint(figure(), 24, 15, 30, 15) # a blade from the body's right side to 2 px past the safe area+ gates = evaluate({"f": frame}, kind="sprite", rule=FREE16, walk_cycle=False)+ measure = gates["G8"]["frames"]["f"]+ assert gates["ok"] and gates["warnings"] == []+ assert measure["bbox"] == [8, 3, 30, 28] and measure["body_box"] == [8, 3, 23, 28]+++def test_g8_warns_when_the_body_leaves_the_safe_area_by_more_than_a_pixel():+ one_over = paint(canvas(), 2, 3, 20, 28)+ assert evaluate({"f": one_over}, kind="sprite", rule=FREE16, walk_cycle=False)["warnings"] == []+ two_over = paint(canvas(), 1, 2, 20, 28)+ gates = evaluate({"f": two_over}, kind="sprite", rule=FREE16, walk_cycle=False)+ assert gates["ok"] and gates["G8"]["frames"]["f"]["overflow"] == {"left": 2, "top": 1, "right": 0, "bottom": 0}+ assert gates["warnings"] == ["G8 the body reaches past the 26x26 safe area by more than 1 px "+ "(f: 2 px left) — only a thin detail may go into the 3 px margin"]+++def test_g8_warns_a_sprite_off_the_floor_row():+ floating = paint(canvas(), 8, 3, 23, 26)+ gates = evaluate({"a": floating, "b": paint(canvas(), 9, 3, 23, 26)}, kind="sprite", rule=FREE16, walk_cycle=False)+ assert gates["ok"] and gates["G6"]["ok"]+ assert gates["warnings"] == ["G8 the feet are not on the floor row (the safe area's bottom row): "+ "a 2 px above it, b 2 px above it"]+ below = paint(canvas(), 8, 3, 23, 29)+ assert evaluate({"f": below}, kind="sprite", rule=FREE16, walk_cycle=False)["warnings"] == [+ "G8 the feet are not on the floor row (the safe area's bottom row): f 1 px below it"]+ icon = evaluate({"f": floating}, kind="icon", rule=FREE16, walk_cycle=False)+ assert icon["warnings"] == [] and icon["G8"]["floor_row"] is None+++def test_g8_has_no_edge_to_keep_below_a_one_pixel_margin():+ plus = paint(paint(canvas(8), 3, 0, 4, 7), 0, 3, 7, 4) # touches every side, corners clear+ gates = evaluate({"f": plus}, kind="icon", rule=FREE16, walk_cycle=False)+ assert gates["ok"] and gates["G8"]["margin"] == [0, 0] and gates["G8"]["edge_pixels"] == 0+++def test_g8_leaves_tiles_alone():+ gates = evaluate({"tile": [[GRASS] * 32 for _ in range(32)]}, kind="tile", rule=FREE16, walk_cycle=False)+ assert gates["ok"] and "G8" not in gates+++def test_recorded_answers_from_the_full_canvas_prompt_fail_g8():+ """Drawn when the prompt asked to fill the canvas: the knight's helmet is on row 1 and its boots+ on row 16, the 16 px canvas's own edge rows."""+ gates = evaluate(frames_of("sprite-16-pass.txt", 16, ("front",)), kind="sprite", rule=FREE16, walk_cycle=False)+ assert gates["failed"] == ["G8"] and gates["G8"]["frames"]["front"]["edge_pixels"] == 14