Files
EvoScientist/tests/test_image_tools.py
m4 aae8d0a379 feat: workspace file references, read-file-images middleware, image model enabled flag
In-progress work committed to unblock the config import/export plan:
- prompts: FILE_REFERENCES section for workspace-relative file citation
- backends: resolve quoted virtual absolute paths onto the sandbox workspace
- middleware: read_file_images middleware; message_budget extensions
- image_gen/model_registry: image model 'enabled' flag refactor
- memory/launch, gateway/background_runs, tools/image follow-ons
- scripts: dev_backend.sh, release.sh
- tests for the above
2026-08-12 19:43:35 +08:00

100 lines
3.2 KiB
Python

"""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