Skip to content

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

30.3 KB
Raw Blame
# SPDX-License-Identifier: Apache-2.0
"""큐레이터 JS 미러가 파이썬 굽기와 **바이트 동일**한지 (Task Card Verification).

호흡 워프는 두 곳에 구현돼 있다: 굽기(`sprite_gen/effects/breathe.py`)와 라이브 프리뷰
(`sprite_gen/serve/curator/src/breathe.js`). 검출은 서버 한 곳으로 몰았지만 워프는 프리뷰를
위해 미러가 남아 있고, 미러가 갈리면 "미리보기와 결과가 다르다"가 된다.

이 테스트가 그 계약을 고정한다 — 위상 0 을 포함한 전 위상을 비교한다. 위상 0 이
중요한 이유: 진행파 지연 때문에 t=0 도 항등이 아닌데, 예전엔 두 소비자가 그걸
건너뛰어 아틀라스만 원본을 굽는 사고가 있었다 (validator 검증 2026-07-25).

node 가 없으면 skip — CI 에 node 가 없을 수 있고, 그 경우 조용히 통과시키지 않고
skip 사유를 남긴다.
"""

import json
import re
import shutil
import subprocess
import sys
from pathlib import Path

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))

from sprite_gen.effects.breathe import (MAX_ROW_STRAIN, anatomy_fingerprint, breathe_range,  # noqa: E402
                                freeze_anatomy, phase_frame, reference_key, resolve_anatomy,
                                row_strain)
from sprite_gen.frames.extract import solid_alpha_bbox  # noqa: E402
from sprite_gen.serve.serve_curation import CURATOR_DIR  # noqa: E402
from tests.effects.test_breathe import (CFG, _cut_bust, _dome, _humanoid, _key, _outlined_figure,  # noqa: E402
                                        _small_outlined, _winged)

CURATOR_BREATHE = CURATOR_DIR / "src" / "breathe.js"
PHASES = [i / 12 for i in range(12)]

# breathe.js 는 클래식 스크립트(모듈 아님)라 전역 어휘를 공유한다. node 에서 돌리려면
# 문서 API 최소분만 shim 하면 된다 — 워프는 canvas 2D getImageData/putImageData 만 쓴다.
HARNESS = r"""
const fs = require("fs");
const vm = require("vm");

function makeCanvas(w, h, data) {
  const buf = data ? Uint8ClampedArray.from(data) : new Uint8ClampedArray(w * h * 4);
  const ctx = {
    imageSmoothingEnabled: false,
    getImageData: () => ({ data: buf, width: w, height: h }),
    putImageData: (img) => { buf.set(img.data); },
    createImageData: (cw, ch) => ({ data: new Uint8ClampedArray(cw * ch * 4), width: cw, height: ch }),
    drawImage: (src) => { buf.set(src.__buf); },
    clearRect: () => {},
  };
  const cvs = { width: w, height: h, getContext: () => ctx, __buf: buf };
  return cvs;
}

const payload = JSON.parse(fs.readFileSync(process.argv[2], "utf8"));
const sandbox = {
  document: { createElement: () => makeCanvas(payload.width, payload.height) },
  console,
  entries: {}, run: { states: [] }, t: () => "", setStatus: () => {}, fetch: () => {},
};
sandbox.globalThis = sandbox;
vm.createContext(sandbox);
vm.runInContext(fs.readFileSync(payload.script, "utf8"), sandbox);

const out = [];
for (const item of payload.cases) {
  const base = makeCanvas(payload.width, payload.height, item.rgba);
  try {
    const res = sandbox.breatheComposite(base, payload.cfg, item.phase);
    out.push({ ok: true, data: Array.from(res.getContext().getImageData().data) });
  } catch (e) {
    out.push({ ok: false, refused: e.constructor.name === "BreatheRefused", message: e.message });
  }
}
fs.writeFileSync(payload.out, JSON.stringify(out));
"""


def _rgba(image):
    return list(image.convert("RGBA").tobytes())


@pytest.mark.skipif(shutil.which("node") is None, reason="node 가 없어 JS 미러를 실행할 수 없다")
@pytest.mark.parametrize("build,overrides", [
    (_humanoid, {}), (_winged, {}),
    # 다듬기(_thin_outline_1px/breatheThinOutline)가 **실제로 픽셀을 치환하는** 유일한
    # 픽스처 — 기본 도형은 depth 0.06 에서 다듬기가 0바이트라, 이 케이스가 없으면
    # JS 다듬기 미러가 파이썬과 갈라져도 패리티 스위트가 못 잡는다
    # (validator 지정 2026-07-30: depth 0.08 24위상에서 294바이트 치환 실측).
    (_small_outlined, {"depth": 0.08, "breaths": 3}),
    # 가로/세로 진폭 분리 + 수동 밴드 (2026-07-30): 가로 강함 / 가로 항등 / 밴드 앵커 보호.
    # 이 케이스들이 없으면 JS 의 depthX 해석·수동 protect 앵커가 파이썬과 갈라져도 못 잡는다.
    # 기본값 돔은 따로 두지 않는다 — 날개처럼 자동 램프를 타고, 아래 돔 3칸이 돔을 태운다.
    (_dome, {"depth_x": 0.12}),
    (_dome, {"depth": 0.10, "depth_x": 0.0}),
    (_dome, {"depth": 0.10, "torso_half": 6}),
    # 큰 도트 그림 — 다듬기·부속 보호·행 사상이 수백 줄에서 돈다. 굽기를 numpy 로 바꿔도 미러(바꾸지
    # 않은 화소 순회)와 바이트가 같은지 여기서 본다.
    (_outlined_figure, {}),
    # 가로·세로 최소·최대 (2026-10-29): 최소 0 / 양쪽 / 원래 크기를 안 지나는 범위. 이득 계수(B + A·wave)의 연산 순서가
    # 파이썬과 갈리면 여기서 잡힌다 — 세기 줄은 B = 0 이라 위 칸들이 못 본다.
    (_outlined_figure, {"width_min": 0.0, "width_max": 0.06, "height_min": -0.05, "height_max": 0.0, "lag": 0.0}),
    (_dome, {"width_min": -0.03, "width_max": 0.08, "height_min": -0.06, "height_max": 0.02}),
    (_small_outlined, {"width_min": 0.02, "width_max": 0.07, "height_min": -0.05, "height_max": -0.01,
                       "breaths": 3}),
], ids=["humanoid", "winged", "small-outlined-thinning",
        "dome-depth-x", "dome-depth-x-zero", "dome-manual-band", "outlined-figure",
        "figure-min-zero", "dome-both-sides", "small-outlined-off-the-original"])
def test_curator_mirror_is_byte_identical_to_the_bake(build, overrides, tmp_path):
    src = build()
    cfg = {**CFG, **overrides}
    bounds = {k: cfg[k] for k in ("width_min", "width_max", "height_min", "height_max") if k in cfg}
    if bounds:
        del cfg["depth"]                             # 최소·최대 줄은 세기를 같이 쓰지 않는다
    cfg["anatomy"] = freeze_anatomy(src, cfg, _key())

    payload = {
        "script": str(CURATOR_BREATHE),
        "width": src.width,
        "height": src.height,
        "cfg": {**({"depth": cfg["depth"]} if "depth" in cfg else bounds), "breaths": cfg["breaths"], "lag": cfg["lag"],
                **({"depth_x": cfg["depth_x"]} if "depth_x" in cfg else {}),
                **({"torso_half": cfg["torso_half"]} if "torso_half" in cfg else {}),
                "anatomy": cfg["anatomy"]},
        "cases": [{"phase": p, "rgba": _rgba(src)} for p in PHASES],
        "out": str(tmp_path / "js.json"),
    }
    payload_path = tmp_path / "payload.json"
    payload_path.write_text(json.dumps(payload), encoding="utf-8")
    harness = tmp_path / "harness.cjs"
    harness.write_text(HARNESS, encoding="utf-8")

    proc = subprocess.run([shutil.which("node"), str(harness), str(payload_path)],
                          capture_output=True, encoding="utf-8")
    assert proc.returncode == 0, f"node 하네스 실패:\n{proc.stderr}"
    js_frames = json.loads((tmp_path / "js.json").read_text(encoding="utf-8"))

    assert len(js_frames) == len(PHASES)
    for phase, js in zip(PHASES, js_frames):
        assert js["ok"], f"{build.__name__} 위상 {phase:.4f}: JS 가 거부했다 — {js['message']}"
        py = _rgba(phase_frame(src, cfg, phase))
        diff = sum(1 for a, b in zip(py, js["data"]) if a != b)
        assert diff == 0, f"{build.__name__} 위상 {phase:.4f}: JS 미러가 굽기와 {diff}바이트 다르다"


@pytest.mark.skipif(shutil.which("node") is None, reason="node 가 없어 JS 미러를 실행할 수 없다")
def test_the_mirror_refuses_exactly_what_the_bake_refuses(tmp_path):
    """굽기가 `SystemExit` 으로 거부하는 프레임은 미러도 만들어 주면 안 된다.

    미러가 조용히 잘라 내보내면 (a) 프리뷰는 멀쩡한데 굽기가 죽고 (b) `row-export` 의
    WebM/MP4 는 서버를 안 거치므로 **잘린 영상이 사용자 손에 들어간다** (validator 실측
    2026-07-25: 여백 0 셀에서 불투명 73px 소실, 오류 0건). 여기서 여백을 없애 그 상황을
    강제로 만든다. 거부는 도형과 무관한 `clipped` 계수 하나라 한 도형이면 된다."""
    src = _humanoid()
    src = src.crop(solid_alpha_bbox(src))            # 여백 0 — 늘어나면 반드시 잘린다
    cfg = dict(CFG)
    cfg["anatomy"] = freeze_anatomy(src, cfg, _key())

    refused_py = []
    for phase in PHASES:
        try:
            phase_frame(src, cfg, phase)
        except SystemExit:
            refused_py.append(phase)
    assert refused_py, "이 픽스처는 굽기가 거부하는 위상을 가져야 의미가 있다"

    payload = {
        "script": str(CURATOR_BREATHE), "width": src.width, "height": src.height,
        "cfg": {"depth": cfg["depth"], "breaths": cfg["breaths"], "lag": cfg["lag"],
                "anatomy": cfg["anatomy"]},
        "cases": [{"phase": p, "rgba": _rgba(src)} for p in PHASES],
        "out": str(tmp_path / "js.json"),
    }
    (tmp_path / "payload.json").write_text(json.dumps(payload), encoding="utf-8")
    (tmp_path / "harness.cjs").write_text(HARNESS, encoding="utf-8")
    proc = subprocess.run([shutil.which("node"), str(tmp_path / "harness.cjs"),
                           str(tmp_path / "payload.json")], capture_output=True, encoding="utf-8")
    assert proc.returncode == 0, f"node 하네스 실패:\n{proc.stderr}"
    js = json.loads((tmp_path / "js.json").read_text(encoding="utf-8"))

    for phase, got in zip(PHASES, js):
        py_refused = phase in refused_py
        assert got["ok"] != py_refused, (
            f"위상 {phase:.4f}: 굽기 거부={py_refused} 인데 "
            f"미러는 {'거부' if not got['ok'] else '통과'} — 계약이 한쪽에만 있다")
        if not got["ok"]:
            assert got["refused"], f"BreatheRefused 가 아닌 예외로 죽었다: {got['message']}"


def _wording(message: str) -> str:
    """거절 문구에서 두 쪽이 같아야 하는 부분 — 굽기만 붙이는 머리말과 콘텐츠 상자를 뺀다."""
    return re.sub(r" \(셀 [^)]*\)", "", message.removeprefix("breathe: "))


@pytest.mark.skipif(shutil.which("node") is None, reason="node 가 없어 JS 미러를 실행할 수 없다")
@pytest.mark.parametrize("touch", [True, False], ids=["border", "off-border"])
def test_the_mirror_words_a_clip_as_the_bake_does(touch, tmp_path):
    """미러의 거절 문구도 굽기와 같다 — 나간 변과 변별 개수, 그 변이 그림 자체의 테두리면 smooth 안내.
    큐레이터 미리보기가 굽기와 다른 까닭을 말하면 사용자는 둘 중 어느 말을 믿을지 모른다."""
    src = _cut_bust(touch)
    cfg = dict(CFG)
    cfg["anatomy"] = freeze_anatomy(src, cfg, _key())
    said = {}
    for phase in PHASES:
        try:
            phase_frame(src, cfg, phase)
        except SystemExit as exc:
            said[phase] = str(exc)
    assert said, "이 픽스처는 굽기가 거부하는 위상을 가져야 의미가 있다"

    payload = {
        "script": str(CURATOR_BREATHE), "width": src.width, "height": src.height,
        "cfg": {"depth": cfg["depth"], "breaths": cfg["breaths"], "lag": cfg["lag"], "anatomy": cfg["anatomy"]},
        "cases": [{"phase": p, "rgba": _rgba(src)} for p in said],
        "out": str(tmp_path / "js.json"),
    }
    (tmp_path / "payload.json").write_text(json.dumps(payload), encoding="utf-8")
    (tmp_path / "harness.cjs").write_text(HARNESS, encoding="utf-8")
    proc = subprocess.run([shutil.which("node"), str(tmp_path / "harness.cjs"),
                           str(tmp_path / "payload.json")], capture_output=True, encoding="utf-8")
    assert proc.returncode == 0, f"node 하네스 실패:\n{proc.stderr}"
    js = json.loads((tmp_path / "js.json").read_text(encoding="utf-8"))
    for (phase, message), got in zip(said.items(), js):
        assert not got["ok"] and got["refused"], f"위상 {phase:.4f}: 굽기는 거부했는데 미러는 {got}"
        assert _wording(got["message"]) == _wording(message), f"위상 {phase:.4f}"
    assert ("--mode smooth" in message) == touch


@pytest.mark.skipif(shutil.which("node") is None, reason="node 가 없어 JS 미러를 실행할 수 없다")
def test_the_mirror_matches_on_frames_other_than_the_frozen_one(tmp_path):
    """줄의 **다른** 프레임에서도 굽기와 미러가 같아야 한다.

    사이드카에는 해부가 한 벌뿐이고 미러는 그걸 그대로 쓴다. 굽기가 프레임마다 다시
    재던 시절엔 깜빡임처럼 프레임이 조금만 달라도 갈렸다 (validator 실측 2026-07-25:
    골든 픽스처 idle 4프레임에서 최대 220바이트, 불투명 픽셀 **수**까지 불일치).
    지금은 줄 전체가 한 벌을 쓰므로 같아야 한다 — 이 테스트가 그 계약이다.

    기존 미러 테스트는 `freeze_anatomy(src)` 한 프레임만 비교해 이 경로가 통째로
    그물 밖이었다."""
    frozen_src = _humanoid()
    cfg = dict(CFG)
    cfg["anatomy"] = freeze_anatomy(frozen_src, cfg, _key())

    # 같은 줄의 '다른 프레임' — 알파 무변 RGB 덧칠 (큐레이터 픽셀 편집기의 문서화된 기능)
    other = frozen_src.copy()
    px = other.load()
    box = solid_alpha_bbox(other)
    axis = (box[0] + box[2]) // 2                    # 몸통 축 — 반드시 불투명하다
    painted = 0
    for y in range(box[1] + 4, box[1] + 14):
        for x in range(axis - 3, axis + 4):
            if px[x, y][3] >= 128:
                px[x, y] = (12, 14, 18, px[x, y][3])
                painted += 1
    assert painted, "덧칠할 불투명 픽셀을 못 찾았다"
    assert other.tobytes() != frozen_src.tobytes(), "픽스처가 실제로 달라야 의미가 있다"

    anat = resolve_anatomy(frozen_src, cfg)   # 줄 전체가 기준 프레임의 한 벌을 쓴다

    payload = {
        "script": str(CURATOR_BREATHE), "width": other.width, "height": other.height,
        "cfg": {"depth": cfg["depth"], "breaths": cfg["breaths"], "lag": cfg["lag"],
                "anatomy": cfg["anatomy"]},
        "cases": [{"phase": p, "rgba": _rgba(other)} for p in PHASES],
        "out": str(tmp_path / "js.json"),
    }
    (tmp_path / "payload.json").write_text(json.dumps(payload), encoding="utf-8")
    (tmp_path / "harness.cjs").write_text(HARNESS, encoding="utf-8")
    proc = subprocess.run([shutil.which("node"), str(tmp_path / "harness.cjs"),
                           str(tmp_path / "payload.json")], capture_output=True, encoding="utf-8")
    assert proc.returncode == 0, f"node 하네스 실패:\n{proc.stderr}"
    js = json.loads((tmp_path / "js.json").read_text(encoding="utf-8"))

    for phase, got in zip(PHASES, js):
        assert got["ok"], f"위상 {phase:.4f}: JS 가 거부했다 — {got['message']}"
        py = _rgba(phase_frame(other, cfg, phase, anat))   # 줄 해부 한 벌로 굽는다
        diff = sum(1 for a, b in zip(py, got["data"]) if a != b)
        assert diff == 0, f"위상 {phase:.4f}: 얼린 프레임 밖에서 {diff}바이트 갈렸다"


@pytest.mark.skipif(shutil.which("node") is None, reason="node 가 없어 JS 미러를 실행할 수 없다")
def test_the_mirror_refuses_the_same_strain_cap(tmp_path):
    """행당 변형 상한 거부도 양쪽이 같아야 한다 (validator note: JS 상한 그물이 비어 있었다).

    클리핑 거부만 그물이 있고 상한 거부는 없어서, JS 가드를 죽여도 미러 테스트가
    전부 통과했다. 동작은 동치였지만 회귀를 막는 것이 없었다."""
    # 강체 경계를 아래로 내리면 변형 구간이 좁아지고 정규화 계수가 커져 행당 변형이
    # 상한을 넘는다 — 사람이 `rigid_row` 로 도달할 수 있는 실제 조합이다.
    src = _humanoid()
    base = dict(CFG)
    base["anatomy"] = freeze_anatomy(src, base, _key())
    height = base["anatomy"]["height"]
    cfg = {**base, "rigid_row": int(height * 0.78)}
    anat = resolve_anatomy(src, cfg)
    cfg["anatomy"] = {**anat.as_dict(), "fingerprint": base["anatomy"]["fingerprint"]}
    depth = next((round(0.005 * i, 3) for i in range(1, 41)
                  if row_strain(anat, round(0.005 * i, 3)) > MAX_ROW_STRAIN), None)
    assert depth is not None, (
        f"경계 {cfg['rigid_row']}/{height} 에서도 스키마 범위 안에서 상한을 못 넘긴다 "
        f"(depth 0.20 에서 {row_strain(anat, 0.20):.3f}) — 상한이 도달 불가면 계약이 죽은 것이다")
    over = {**cfg, "depth": depth}

    with pytest.raises(SystemExit):
        phase_frame(src, over, 0.25, anat)

    payload = {
        "script": str(CURATOR_BREATHE), "width": src.width, "height": src.height,
        "cfg": {"depth": depth, "breaths": cfg["breaths"], "lag": cfg["lag"],
                "anatomy": cfg["anatomy"]},
        "cases": [{"phase": 0.25, "rgba": _rgba(src)}],
        "out": str(tmp_path / "js.json"),
    }
    (tmp_path / "payload.json").write_text(json.dumps(payload), encoding="utf-8")
    (tmp_path / "harness.cjs").write_text(HARNESS, encoding="utf-8")
    proc = subprocess.run([shutil.which("node"), str(tmp_path / "harness.cjs"),
                           str(tmp_path / "payload.json")], capture_output=True, encoding="utf-8")
    assert proc.returncode == 0, f"node 하네스 실패:\n{proc.stderr}"
    got = json.loads((tmp_path / "js.json").read_text(encoding="utf-8"))[0]
    assert not got["ok"] and got["refused"], \
        f"굽기는 상한으로 거부하는데 미러는 통과시켰다: {got}"


@pytest.mark.skipif(shutil.which("node") is None, reason="node 가 없어 JS 미러를 실행할 수 없다")
def test_the_mirror_refuses_a_stale_anatomy_against_a_manual_rigid_row(tmp_path):
    """사이드카 `rigid_row` 가 얼린 해부와 어긋나면 미러는 **거부**해야 한다.

    `rigid_row` 는 의도(입력), `anatomy` 는 파생 캐시다. 굽기는 어긋나면 재검출해 의도를
    따르는데 미러는 검출을 못 한다 — 낡은 캐시로 그리면 거짓말이다. 실측 (validator
    2026-07-25): override 31 을 굽기는 따르고 미러는 얼린 23 으로 그려 12위상 전부,
    최대 164바이트 갈렸다. `row-export` 는 이 미러로 WebM/MP4 를 굽는다."""
    src = _humanoid()
    cfg = dict(CFG)
    cfg["anatomy"] = freeze_anatomy(src, cfg, _key())
    frozen_row = cfg["anatomy"]["rigid_row"]
    override = frozen_row + 8
    stale = {**cfg, "rigid_row": override}

    anat = resolve_anatomy(src, stale)                  # 굽기는 의도를 따른다
    assert anat.rigid_row == override

    payload = {
        "script": str(CURATOR_BREATHE), "width": src.width, "height": src.height,
        "cfg": {"depth": cfg["depth"], "breaths": cfg["breaths"], "lag": cfg["lag"],
                "rigid_row": override, "anatomy": cfg["anatomy"]},
        "cases": [{"phase": p, "rgba": _rgba(src)} for p in PHASES],
        "out": str(tmp_path / "js.json"),
    }
    (tmp_path / "payload.json").write_text(json.dumps(payload), encoding="utf-8")
    (tmp_path / "harness.cjs").write_text(HARNESS, encoding="utf-8")
    proc = subprocess.run([shutil.which("node"), str(tmp_path / "harness.cjs"),
                           str(tmp_path / "payload.json")], capture_output=True, encoding="utf-8")
    assert proc.returncode == 0, f"node 하네스 실패:\n{proc.stderr}"
    for phase, got in zip(PHASES, json.loads((tmp_path / "js.json").read_text(encoding="utf-8"))):
        assert not got["ok"] and got["refused"], (
            f"위상 {phase:.4f}: 캐시가 의도와 어긋나는데 미러가 그렸다 — "
            f"프리뷰·WebM 이 굽기와 다른 애니메이션이 된다")

    # 해부를 갱신하면 다시 같아진다 (거부가 영구 차단이 아니라는 것)
    fresh = {**stale, "anatomy": {**anat.as_dict(), "fingerprint": anatomy_fingerprint(_key())}}
    payload["cfg"] = {"depth": cfg["depth"], "breaths": cfg["breaths"], "lag": cfg["lag"],
                      "rigid_row": override, "anatomy": fresh["anatomy"]}
    (tmp_path / "payload.json").write_text(json.dumps(payload), encoding="utf-8")
    proc = subprocess.run([shutil.which("node"), str(tmp_path / "harness.cjs"),
                           str(tmp_path / "payload.json")], capture_output=True, encoding="utf-8")
    assert proc.returncode == 0
    for phase, got in zip(PHASES, json.loads((tmp_path / "js.json").read_text(encoding="utf-8"))):
        assert got["ok"], f"갱신 후에도 거부한다: {got['message']}"
        py = _rgba(phase_frame(src, fresh, phase, anat))
        assert sum(1 for a, b in zip(py, got["data"]) if a != b) == 0, \
            f"위상 {phase:.4f}: 갱신 후 굽기와 갈린다"


@pytest.mark.skipif(shutil.which("node") is None, reason="node 가 없어 JS 미러를 실행할 수 없다")
def test_the_mirror_refuses_a_stale_reference_frame(tmp_path):
    """`rigid_row` 가 auto(null)여도 신선도 검사가 돌아야 한다.

    round-6 의 가드는 `cfg.rigid_row` 가 있을 때만 걸렸다. 그런데 `rigid_row` 는 사람이
    경계를 드래그할 때만 채워지므로 **기본값은 null** 이고, 그 경우 미러엔 신선도 검사가
    하나도 없었다. 굽기는 지문으로 자가 복구하는데 미러는 얼린 숫자로 계속 그린다 —
    큐레이터 픽셀 편집기로 도트를 찍기만 해도 도달한다(호흡을 건드릴 필요조차 없다).
    실측 (validator 2026-07-25): 최대 617바이트, 불투명 픽셀 **수**까지 불일치."""
    base = _humanoid()
    cfg = dict(CFG)
    cfg["anatomy"] = freeze_anatomy(base, cfg, _key())
    assert cfg.get("rigid_row") is None, "이 계약은 override 가 없을 때의 이야기다"

    # 픽셀 편집기로 도트를 찍는다 = 사이드카 `pixels` 가 바뀐다 = 기준 프레임 키가 바뀐다.
    # 근거가 **그림**이 아니라 **사이드카**인 것이 이 설계의 요점이다: 굽기는 BICUBIC 으로
    # 리샘플하고 웹뷰는 NEAREST 라 그림은 구조적으로 다르고, 그림을 근거로 삼으면 회전이
    # 걸린 줄이 영구 불일치가 된다 (validator 실측 2026-07-26, R1).
    edited_key = reference_key(state="idle", variant="plain", request_stamp="1:1",
                               source_index=0, source_stamp="fixture",
                               pixel_ops={"12,40": "#5a3c1e"}, transform=None)
    assert anatomy_fingerprint(edited_key) != cfg["anatomy"]["fingerprint"], \
        "픽셀 편집이 키를 안 바꾼다 — 신선도 판정이 그걸 못 본다"

    harness = tmp_path / "fresh.cjs"
    harness.write_text(
        'const fs=require("fs"),vm=require("vm");'
        'const P=JSON.parse(fs.readFileSync(process.argv[3],"utf8"));'
        'function mk(w,h,d){const b=Uint8ClampedArray.from(d);'
        ' const c={imageSmoothingEnabled:false,getImageData:()=>({data:b,width:w,height:h}),'
        ' putImageData:i=>b.set(i.data),createImageData:(a,e)=>({data:new Uint8ClampedArray(a*e*4),width:a,height:e}),'
        ' drawImage:s=>b.set(s.__buf),clearRect:()=>{}};return{width:w,height:h,getContext:()=>c,__buf:b};}'
        'const sb={document:{createElement:()=>mk(P.w,P.h,new Array(P.w*P.h*4).fill(0))},console,'
        'entries:{},run:{states:[]},t:()=>"",setStatus:()=>{},fetch:()=>{},TextEncoder};'
        'sb.globalThis=sb;vm.createContext(sb);'
        'vm.runInContext(fs.readFileSync(process.argv[2],"utf8"),sb);'
        'let out;try{sb.breatheAssertFresh(P.key,P.cfg);out={ok:true};}'
        'catch(e){out={ok:false,refused:e.constructor.name==="BreatheRefused",message:e.message};}'
        'fs.writeFileSync(P.out,JSON.stringify(out));', encoding="utf-8")
    src_json = tmp_path / "in.json"
    src_json.write_text(json.dumps({
        "w": base.width, "h": base.height, "key": edited_key,
        "cfg": {"depth": cfg["depth"], "breaths": cfg["breaths"], "lag": cfg["lag"],
                "anatomy": cfg["anatomy"]},
        "out": str(tmp_path / "out.json")}), encoding="utf-8")
    proc = subprocess.run([shutil.which("node"), str(harness), str(CURATOR_BREATHE), str(src_json)],
                          capture_output=True, encoding="utf-8")
    assert proc.returncode == 0, f"node 하네스 실패:\n{proc.stderr}"
    got = json.loads((tmp_path / "out.json").read_text(encoding="utf-8"))
    assert not got["ok"] and got["refused"], (
        "기준 프레임이 바뀌었는데 미러가 신선하다고 판정했다 — "
        "프리뷰·WebM 이 굽기와 다른 애니메이션이 된다")

    # 원래 기준 프레임에는 걸리지 않는다 (거부가 상시가 아니라는 것)
    src_json.write_text(json.dumps({
        "w": base.width, "h": base.height, "key": _key(),
        "cfg": {"depth": cfg["depth"], "breaths": cfg["breaths"], "lag": cfg["lag"],
                "anatomy": cfg["anatomy"]},
        "out": str(tmp_path / "out.json")}), encoding="utf-8")
    subprocess.run([shutil.which("node"), str(harness), str(CURATOR_BREATHE), str(src_json)],
                   capture_output=True, encoding="utf-8", check=True)
    assert json.loads((tmp_path / "out.json").read_text(encoding="utf-8"))["ok"], \
        "기준 프레임 그대로인데 거부한다 — 프리뷰가 상시 죽는다"


# 지문·기준 프레임 키의 교차언어 계약은 `tests/test_breathe_reference_key.py` 가 소유한다
# (값 모양 전수 + 서버 페이로드 → 웹뷰 키 왕복). 여기 있던 이미지 기반 지문 비교는
# 그 설계가 폐기되면서 같이 지웠다 — 두 파일이 같은 계약을 말하면 한쪽만 고쳐진다.


@pytest.mark.skipif(shutil.which("node") is None, reason="node 가 없어 JS 미러를 실행할 수 없다")
def test_the_mirror_refuses_when_it_cannot_verify(tmp_path):
    """지문이 없으면 "확인 불가" 이지 "괜찮음" 이 아니다.

    `refreshAnatomy` 가 실패하면 지문만 무효화한 해부가 남는데, 미러가 그걸 조용히
    통과시켜 낡은 숫자로 그렸다 — 굽기와 164바이트, 불투명 픽셀 수까지 불일치
    (validator 실측 2026-07-26). `row-export` 가 같은 함수를 쓰므로 WebM/MP4 가 GIF 와
    다른 애니메이션으로 나갔다. 지문이 null 이든 키째 없든 `!anat.fingerprint` 한 줄이 받는다."""
    src = _humanoid()
    cfg = dict(CFG)
    frozen = {**freeze_anatomy(src, cfg, _key()), "fingerprint": None}
    cfg["anatomy"] = frozen

    harness = tmp_path / "fresh.cjs"
    harness.write_text(
        'const fs=require("fs"),vm=require("vm");'
        'const P=JSON.parse(fs.readFileSync(process.argv[3],"utf8"));'
        'function mk(w,h,d){const b=Uint8ClampedArray.from(d);'
        ' const c={imageSmoothingEnabled:false,getImageData:()=>({data:b,width:w,height:h}),'
        ' putImageData:i=>b.set(i.data),createImageData:(a,e)=>({data:new Uint8ClampedArray(a*e*4),width:a,height:e}),'
        ' drawImage:s=>b.set(s.__buf),clearRect:()=>{}};return{width:w,height:h,getContext:()=>c,__buf:b};}'
        'const sb={document:{createElement:()=>mk(P.w,P.h,new Array(P.w*P.h*4).fill(0))},console,'
        'entries:{},run:{states:[]},t:()=>"",setStatus:()=>{},fetch:()=>{},TextEncoder};'
        'sb.globalThis=sb;vm.createContext(sb);'
        'vm.runInContext(fs.readFileSync(process.argv[2],"utf8"),sb);'
        'let out;try{sb.breatheAssertFresh(P.key,P.cfg);out={ok:true};}'
        'catch(e){out={ok:false,refused:e.constructor.name==="BreatheRefused",message:e.message};}'
        'fs.writeFileSync(P.out,JSON.stringify(out));', encoding="utf-8")
    src_json = tmp_path / "in.json"
    src_json.write_text(json.dumps({
        "w": src.width, "h": src.height, "rgba": _rgba(src),
        "cfg": {"depth": cfg["depth"], "breaths": cfg["breaths"], "lag": cfg["lag"],
                "anatomy": frozen},
        "out": str(tmp_path / "out.json")}), encoding="utf-8")
    proc = subprocess.run([shutil.which("node"), str(harness), str(CURATOR_BREATHE), str(src_json)],
                          capture_output=True, encoding="utf-8")
    assert proc.returncode == 0, f"node 하네스 실패:\n{proc.stderr}"
    got = json.loads((tmp_path / "out.json").read_text(encoding="utf-8"))
    assert not got["ok"] and got["refused"], (
        "지문이 null 인데 미러가 신선하다고 판정했다 — 확인 불가는 괜찮음이 아니다")


RANGE_HARNESS = r"""
const fs = require("fs");
const vm = require("vm");
const payload = JSON.parse(fs.readFileSync(process.argv[2], "utf8"));
const sandbox = { console, entries: {}, run: { states: [] }, t: () => "", setStatus: () => {}, fetch: () => {} };
sandbox.globalThis = sandbox;
vm.createContext(sandbox);
vm.runInContext(fs.readFileSync(payload.script, "utf8"), sandbox);
fs.writeFileSync(payload.out, JSON.stringify(payload.cases.map((c) => sandbox.breatheRange(c.anatomy, c.cfg))));
"""


@pytest.mark.skipif(shutil.which("node") is None, reason="node 가 없어 JS 미러를 실행할 수 없다")
def test_the_mirror_reads_the_same_range_as_the_bake(tmp_path):
    """편집기가 세기만 적힌 줄을 네 손잡이로 보여 줄 때 읽는 범위 — 파이썬 breathe_range 와 같은 double 이어야 한다.
    갈리면 손대지 않은 손잡이를 저장해도 옛 판과 다른 바이트가 구워진다."""
    rows = [(_humanoid, {}), (_dome, {"depth": 0.10, "depth_x": 0.0}), (_dome, {"depth_x": 0.12}),
            (_humanoid, {"mode": "smooth"}), (_humanoid, {"mode": "smooth", "depth": 0.01, "depth_x": 0.03}),
            (_humanoid, {"width_min": 0.0, "width_max": 0.05, "height_min": -0.04, "height_max": 0.0})]
    cases = []
    for build, overrides in rows:
        src = build()
        cfg = {**CFG, **overrides}
        if "mode" in cfg or "width_min" in cfg:
            cfg.pop("lag", None)
        if "width_min" in cfg:
            del cfg["depth"]
        cfg["anatomy"] = freeze_anatomy(src, cfg, _key())
        py = breathe_range(resolve_anatomy(src, cfg), cfg)
        cases.append({"anatomy": cfg["anatomy"], "cfg": cfg, "py": {k: list(v) for k, v in py.items()}})

    payload = {"script": str(CURATOR_BREATHE), "out": str(tmp_path / "js.json"),
               "cases": [{"anatomy": c["anatomy"], "cfg": c["cfg"]} for c in cases]}
    (tmp_path / "payload.json").write_text(json.dumps(payload), encoding="utf-8")
    (tmp_path / "harness.cjs").write_text(RANGE_HARNESS, encoding="utf-8")
    proc = subprocess.run([shutil.which("node"), str(tmp_path / "harness.cjs"),
                           str(tmp_path / "payload.json")], capture_output=True, encoding="utf-8")
    assert proc.returncode == 0, f"node 하네스 실패:\n{proc.stderr}"
    for case, js in zip(cases, json.loads((tmp_path / "js.json").read_text(encoding="utf-8"))):
        assert js == case["py"], f"{case['cfg']}: py={case['py']} js={js}"