"""Tests for the image generation agent tools.""" import json import pytest from EvoScientist.tools import image as image_tools @pytest.fixture(autouse=True) def fake_service(monkeypatch, tmp_path): """Patch the service entry points the tools call.""" async def fake_generate(workspace, **kwargs): return { "ok": True, "path": "artifacts/generated_1.png", "paths": ["artifacts/generated_1.png"], "mime_type": "image/png", "model": "gpt-image-2", "size": kwargs.get("size", "1024x1024"), } async def fake_edit(workspace, **kwargs): return { "ok": True, "path": "artifacts/edited_1.png", "paths": ["artifacts/edited_1.png"], "mime_type": "image/png", "model": "gpt-image-2", "size": kwargs.get("size", "1024x1024"), } async def fake_workspace(runtime=None): return tmp_path monkeypatch.setattr(image_tools, "_generate_for_workspace", fake_generate) monkeypatch.setattr(image_tools, "_edit_for_workspace", fake_edit) monkeypatch.setattr(image_tools, "_workspace", fake_workspace) async def test_generate_image_success(): result = json.loads( await image_tools.generate_image.ainvoke({"prompt": "a cat"}) ) assert result["ok"] is True assert result["paths"] == ["artifacts/generated_1.png"] assert result["markdown"] == ["![generated_1](artifacts/generated_1.png)"] async def test_edit_image_success(): result = json.loads( await image_tools.edit_image.ainvoke( {"image_path": "artifacts/src.png", "prompt": "make blue"} ) ) assert result["ok"] is True assert result["paths"] == ["artifacts/edited_1.png"] async def test_error_becomes_json(monkeypatch): from EvoScientist.image_gen.adapters.base import ImageGenError async def boom(workspace, **kwargs): raise ImageGenError("no image models configured") monkeypatch.setattr(image_tools, "_generate_for_workspace", boom) result = json.loads(await image_tools.generate_image.ainvoke({"prompt": "x"})) assert result["ok"] is False assert "no image models" in result["error"] def test_refresh_descriptions_includes_models(monkeypatch, tmp_path): config = tmp_path / "config.yaml" config.write_text( """ image_generation: default_model: gpt-image-2 models: - id: gpt-image-2 name: GPT Image 2 provider: openai api_key: sk-x """, encoding="utf-8", ) monkeypatch.setattr(image_tools, "_settings_path", lambda: config) image_tools.refresh_image_tool_descriptions() assert "GPT Image 2" in image_tools.generate_image.description assert "Default: gpt-image-2" in image_tools.generate_image.description assert "sk-x" not in image_tools.generate_image.description assert "workspace-relative path" in image_tools.generate_image.description assert "workspace-relative path" in image_tools.edit_image.description def test_tools_package_exports(): from EvoScientist import tools assert tools.generate_image is image_tools.generate_image assert tools.edit_image is image_tools.edit_image