import importlib.util
import io
import sys
import unittest
from argparse import Namespace
from contextlib import redirect_stderr
from pathlib import Path

MODULE_PATH = Path(__file__).resolve().parents[1] / "scripts" / "generate_illustration.py"
sys.path.insert(0, str(MODULE_PATH.parent))
SPEC = importlib.util.spec_from_file_location("generate_illustration", MODULE_PATH)
generate_illustration = importlib.util.module_from_spec(SPEC)
assert SPEC.loader is not None
SPEC.loader.exec_module(generate_illustration)

class GatewayPayloadTests(unittest.TestCase):
    def test_payload_uses_declared_model(self):
        payload = generate_illustration.build_payload(
            prompt="publication-ready kitten illustration",
            model="image-gen/gemini-3.1-flash-image-preview",
            size="1024x1024",
        )

        self.assertEqual(payload["model"], "image-gen/gemini-3.1-flash-image-preview")
        self.assertEqual(payload["n"], 1)
        self.assertEqual(payload["response_format"], "url")

    def test_rejects_undeclared_model(self):
        with redirect_stderr(io.StringIO()):
            with self.assertRaises(SystemExit):
                generate_illustration.build_payload(
                    prompt="publication-ready kitten illustration",
                    model="undeclared/private-model",
                    size="1024x1024",
                )

    def test_dry_run_does_not_require_token(self):
        args = Namespace(
            model="image-gen/gemini-3.1-flash-image-preview",
            size="1024x1024",
            runtime_origin="https://leapskills.example",
            timeout=1,
            dry_run=True,
        )

        result = generate_illustration.generate_one(
            args=args,
            prompt="publication-ready kitten illustration",
            out=Path("out.png"),
        )

        self.assertTrue(result["dry_run"])
        self.assertEqual(result["endpoint_path"], "/v1/images/generations")
        self.assertEqual(result["runtime_origin"], "https://leapskills.example")
        self.assertEqual(result["skill_slug"], "design")

if __name__ == "__main__":
    unittest.main()
