"""Tests for floor_plan_render — Phase 7 Step 4 (gpt-image-2)."""
from __future__ import annotations

from unittest.mock import MagicMock

from app.modules.pipeline.floor_plan_render import (
    FloorPlanRenderResult,
    render_one_floor_plan,
)


def test_render_one_floor_plan_text_only(tmp_path):
    """ref_paths 비어있으면 images.generate (text-only) 호출."""
    client = MagicMock()
    resp = MagicMock()
    resp.data = [MagicMock(b64_json="aGVsbG8=")]  # base64('hello')
    client.images.generate.return_value = resp
    out = tmp_path / "fp_x.png"

    res = render_one_floor_plan(
        openai_client=client,
        image_model="gpt-image-2.5-sunburst",
        prompt="a top-down floor plan",
        out_path=out,
        ref_paths=[],
        fp_id="fp_x",
    )

    assert isinstance(res, FloorPlanRenderResult)
    assert res.status == "ok"
    assert res.fp_id == "fp_x"
    assert res.ref_used == "text_only"
    assert out.exists() and out.read_bytes() == b"hello"
    assert client.images.generate.called
    assert not client.images.edit.called


def test_render_one_floor_plan_with_ref(tmp_path):
    """parent fp_id ref가 있으면 images.edit 호출 (multi-image edit)."""
    client = MagicMock()
    resp = MagicMock()
    resp.data = [MagicMock(b64_json="aGVsbG8=")]
    client.images.edit.return_value = resp
    ref_png = tmp_path / "fp_parent.png"
    ref_png.write_bytes(b"PARENT")
    out = tmp_path / "fp_x.png"

    res = render_one_floor_plan(
        openai_client=client,
        image_model="gpt-image-2.5-sunburst",
        prompt="x",
        out_path=out,
        ref_paths=[ref_png],
        fp_id="fp_x",
    )

    assert res.status == "ok"
    assert res.ref_used == "ref"
    assert client.images.edit.called
    assert not client.images.generate.called
    assert out.read_bytes() == b"hello"
