import providers.nano_banana as nb

def test_missing_key_reports_failed(monkeypatch):
    monkeypatch.delenv("GEMINI_API_KEY", raising=False)
    calls = []
    monkeypatch.setattr(nb, "webhook_update_task", lambda *a, **k: calls.append((a, k)))
    nb.run_nano_banana_generation("t1", {"prompt": "a cat"})
    assert calls[-1][0][1] == "failed"

def test_success_extracts_inline_data(monkeypatch):
    monkeypatch.setenv("GEMINI_API_KEY", "g-test")
    calls = []
    monkeypatch.setattr(nb, "webhook_update_task", lambda *a, **k: calls.append((a, k)))
    monkeypatch.setattr(nb, "save_b64_png", lambda tid, b64: "/media/image/t1.png")

    class FakeResp:
        status_code = 200
        def json(self):
            return {"candidates": [{"content": {"parts": [
                {"inlineData": {"mimeType": "image/png", "data": "ZmFrZQ=="}}
            ]}}]}
    monkeypatch.setattr(nb.requests, "post", lambda *a, **k: FakeResp())

    nb.run_nano_banana_generation("t1", {"prompt": "a cat"})
    assert [c for c in calls if c[0][1] == "succeeded"]
