# Conflicts: # apps/desktop/e2e/archived-hidden-session-recoverable.spec.ts # apps/desktop/e2e/bot-chat-message-agent-friendly-name.spec.ts # apps/desktop/e2e/bot-mailbox-unreadable-ticket.spec.ts # apps/desktop/e2e/bot-mode-roster-localized.spec.ts # apps/desktop/e2e/bot-mode-row-click-mirrors-registry.spec.ts # apps/desktop/e2e/bot-mode-tab-shows-bot-name.spec.ts # apps/desktop/e2e/bot-roster-group-row-organisation.spec.ts # apps/desktop/e2e/bot-roster-ignores-infra-dirs.spec.ts # apps/desktop/e2e/bot-roster-timestamp-meta.spec.ts # apps/desktop/e2e/bot-roster-user-sections.spec.ts # apps/desktop/e2e/bot-routines-pane-narrow.spec.ts # apps/desktop/e2e/bot-row-open-recent-session.spec.ts # apps/desktop/e2e/bot-tile-ignores-ambient-composer-model.spec.ts # apps/desktop/e2e/group-composer-auto-grow.spec.ts # apps/desktop/e2e/group-create-gate-remote-roster.spec.ts # apps/desktop/e2e/group-prompt-renamed-primary-handle.spec.ts # apps/desktop/e2e/hosted-room-backend-continuity.spec.ts # apps/desktop/e2e/hosted-room-legacy-store-migration.spec.ts # apps/desktop/e2e/settings-scope-chips-bot-title.spec.ts # apps/desktop/e2e/worktree-branch-status.spec.ts # apps/desktop/electron/backend-probes.test.ts # apps/desktop/electron/connection-apply.test.ts # apps/desktop/electron/desktop-electron-pin.test.ts # apps/desktop/electron/desktop-uninstall.test.ts # apps/desktop/electron/gateway-file-download-transport.test.ts # apps/desktop/electron/gateway-stop-before-update.test.ts # apps/desktop/electron/github-api-auth.test.ts # apps/desktop/electron/registry-primary-profile-scope.test.ts # apps/desktop/electron/update-api-check.test.ts # apps/desktop/electron/update-handoff-marker.test.ts # apps/desktop/electron/venv-blocker-scan.test.ts # apps/desktop/scripts/after-extract.test.mjs # apps/desktop/scripts/local-pack-publish.test.mjs # apps/desktop/scripts/tasks-scroll.test.mjs # apps/desktop/src/app/settings/model-settings.test.tsx # apps/desktop/src/app/updates-overlay.blockers.test.tsx # apps/desktop/src/components/desktop-install-overlay.test.tsx # apps/desktop/src/lib/update-copy.test.ts # scripts/ci/check_os_marker_fakes.py # tests-js/desktop-mac-usage-descriptions.test.ts # tests-js/node-engine-alignment.test.ts # tests/agent/lsp/test_install_and_lint_fixes.py # tests/agent/test_command_token_source.py # tests/agent/test_compression_boundary_hook.py # tests/agent/test_create_openai_client_ssl_verify.py # tests/agent/test_custom_provider_ca_probes.py # tests/agent/test_endpoint_blackhole.py # tests/agent/test_estimator_parity.py # tests/agent/test_in_place_compaction.py # tests/agent/test_moa_loop_mode.py # tests/agent/test_model_metadata.py # tests/agent/test_skill_session_platform_gate.py # tests/agent/test_skill_utils.py # tests/agent/test_ssl_ca_guard.py # tests/computer_use/test_doctor.py # tests/cron/test_codex_execution_paths.py # tests/cron/test_cron_bot_chat_delivery.py # tests/cron/test_cron_script.py # tests/cron/test_media_delivery_parity.py # tests/cron/test_misfire_catchup.py # tests/cron/test_parallel_pool.py # tests/cron/test_recurring_eagain_redispatch.py # tests/gateway/test_choice_picker.py # tests/gateway/test_control_socket_windows_live.py # tests/gateway/test_dingtalk.py # tests/gateway/test_feishu.py # tests/gateway/test_feishu_onboard.py # tests/gateway/test_gateway_shutdown.py # tests/gateway/test_matrix.py # tests/gateway/test_model_command_custom_providers.py # tests/gateway/test_reasoning_command.py # tests/gateway/test_runtime_footer.py # tests/gateway/test_session.py # tests/gateway/test_session_hygiene.py # tests/gateway/test_status.py # tests/gateway/test_teams.py # tests/gateway/test_turn_lease.py # tests/gateway/test_whatsapp_connect.py # tests/hermes_cli/test_approvals_command.py # tests/hermes_cli/test_auth_store_lock_concurrent.py # tests/hermes_cli/test_backup.py # tests/hermes_cli/test_banner_git_state.py # tests/hermes_cli/test_certifi_repair.py # tests/hermes_cli/test_cmd_update.py # tests/hermes_cli/test_compat_manifest_targets.py # tests/hermes_cli/test_computer_use_cli.py # tests/hermes_cli/test_cpr_local_leak.py # tests/hermes_cli/test_dashboard_auth_gate.py # tests/hermes_cli/test_dashboard_procs_kill_grace.py # tests/hermes_cli/test_desktop_lifecycle_windows_live.py # tests/hermes_cli/test_doctor.py # tests/hermes_cli/test_doctor_command_install.py # tests/hermes_cli/test_fleet_config_migration_windows_live.py # tests/hermes_cli/test_gateway.py # tests/hermes_cli/test_gateway_platform_gating.py # tests/hermes_cli/test_gateway_restart_loop.py # tests/hermes_cli/test_gateway_task_probe.py # tests/hermes_cli/test_gateway_wsl.py # tests/hermes_cli/test_gui_command.py # tests/hermes_cli/test_install_cua_driver.py # tests/hermes_cli/test_kanban_db.py # tests/hermes_cli/test_lazy_command_exports.py # tests/hermes_cli/test_lazy_refresh_venv_repair.py # tests/hermes_cli/test_linux_desktop_entry.py # tests/hermes_cli/test_local_runtime.py # tests/hermes_cli/test_local_runtime_updates.py # tests/hermes_cli/test_managed_uv.py # tests/hermes_cli/test_mcp_reload_confirm_gate.py # tests/hermes_cli/test_nous_subscription.py # tests/hermes_cli/test_npm_engine.py # tests/hermes_cli/test_personality_none.py # tests/hermes_cli/test_pet_toggle.py # tests/hermes_cli/test_plan_reconciliation_windows_live.py # tests/hermes_cli/test_plugin_event_bus.py # tests/hermes_cli/test_plugin_manifest_v2.py # tests/hermes_cli/test_plugin_packs.py # tests/hermes_cli/test_plugins_cmd.py # tests/hermes_cli/test_plugins_cmd_enable_disable_nested.py # tests/hermes_cli/test_process_identity.py # tests/hermes_cli/test_profiles.py # tests/hermes_cli/test_profiles_sidebar_cache.py # tests/hermes_cli/test_pty_bridge.py # tests/hermes_cli/test_resolve_turn_limit.py # tests/hermes_cli/test_serve_runtime_inventory.py # tests/hermes_cli/test_session_vacuum_config.py # tests/hermes_cli/test_set_config_value.py # tests/hermes_cli/test_signal_handler_kanban_worker.py # tests/hermes_cli/test_slash_confirm_windows.py # tests/hermes_cli/test_stale_pid_guard.py # tests/hermes_cli/test_startup_fast_guards.py # tests/hermes_cli/test_status.py # tests/hermes_cli/test_telegram_managed_bot.py # tests/hermes_cli/test_tools_config.py # tests/hermes_cli/test_update_apply_shallow_count.py # tests/hermes_cli/test_update_autostash.py # tests/hermes_cli/test_update_concurrent_quarantine.py # tests/hermes_cli/test_update_fetch_failure_classifier.py # tests/hermes_cli/test_update_fleet_probe_resume_token.py # tests/hermes_cli/test_update_handoff_backend_reap.py # tests/hermes_cli/test_update_handoff_desktop_rebuild.py # tests/hermes_cli/test_update_head_moved_gate.py # tests/hermes_cli/test_update_host_obligation.py # tests/hermes_cli/test_update_import_guard.py # tests/hermes_cli/test_update_interrupted_recovery.py # tests/hermes_cli/test_update_inventory.py # tests/hermes_cli/test_update_launchd_unloaded_gateway.py # tests/hermes_cli/test_update_missing_configured_deps.py # tests/hermes_cli/test_update_modified_notice.py # tests/hermes_cli/test_update_multiplex_migration_hook.py # tests/hermes_cli/test_update_no_gateway_restart.py # tests/hermes_cli/test_update_orphan_backend_reap.py # tests/hermes_cli/test_update_parked_branch_guard.py # tests/hermes_cli/test_update_post_pull_syntax_guard.py # tests/hermes_cli/test_update_receipt.py # tests/hermes_cli/test_update_self_lock.py # tests/hermes_cli/test_update_shim_fail_closed.py # tests/hermes_cli/test_update_shim_self_lock.py # tests/hermes_cli/test_update_sqlite_remediation.py # tests/hermes_cli/test_update_stale_dashboard.py # tests/hermes_cli/test_update_stale_virtualenv.py # tests/hermes_cli/test_update_venv_health.py # tests/hermes_cli/test_update_venv_ownership_preflight.py # tests/hermes_cli/test_update_wedged_gateway.py # tests/hermes_cli/test_update_yes_flag.py # tests/hermes_cli/test_update_zip_two_phase.py # tests/hermes_cli/test_urllib_security.py # tests/hermes_cli/test_ux_messages_auth_config.py # tests/hermes_cli/test_ux_messages_startup.py # tests/hermes_cli/test_venv_holder_classifier.py # tests/hermes_cli/test_verify_console_scripts.py # tests/hermes_cli/test_verify_core_dependencies.py # tests/hermes_cli/test_web_server.py # tests/hermes_cli/test_web_server_console_ws.py # tests/hermes_cli/test_web_server_ws_ping.py # tests/hermes_cli/test_web_ui_build.py # tests/hermes_state/test_fts_rebuild_admission.py # tests/hermes_state/test_hermes_state.py # tests/plugins/memory/test_memory_lazy_install.py # tests/plugins/test_google_meet_plugin.py # tests/plugins/test_langfuse_plugin.py # tests/plugins/test_security_guidance_plugin.py # tests/plugins/test_transform_llm_output_hook.py # tests/scripts/desktop_update/test_desktop_update_windows_gateway_flag.py # tests/scripts/desktop_update/test_desktop_update_windows_python_handoff.py # tests/scripts/desktop_update/test_desktop_update_windows_timestamp.py # tests/scripts/install/test_install_clone_throttle_fallback.py # tests/scripts/install/test_install_lockfile_churn.py # tests/scripts/install/test_install_no_initial_commit.py # tests/scripts/install/test_install_sh_browser_install.py # tests/scripts/install/test_install_sh_node_prerelease.py # tests/scripts/install/test_install_sh_symlink_stomp.py # tests/scripts/install/test_install_sh_uv_lock_config.py # tests/scripts/install/test_install_unmerged_index.py # tests/scripts/test_contributor_map.py # tests/scripts/test_run_tests_parallel.py # tests/skills/test_competitor_news_monitor_skill.py # tests/skills/test_document_to_action_items_skill.py # tests/skills/test_google_workspace_setup.py # tests/skills/test_google_workspace_setup_deps.py # tests/skills/test_grounded_citations_skill.py # tests/skills/test_ip_as_logo_skill.py # tests/skills/test_live_dashboard_skill.py # tests/skills/test_mcp_oauth_remote_gateway_skill.py # tests/skills/test_office_document_skills.py # tests/skills/test_openclaw_migration.py # tests/skills/test_product_price_monitor_skill.py # tests/skills/test_scrollcraft_skill.py # tests/skills/test_setup_wizard_generator_skill.py # tests/skills/test_weekly_review_planning_skill.py # tests/test_engines_satisfiable.py # tests/test_fast_safe_load.py # tests/test_hermes_bootstrap.py # tests/test_hermes_constants.py # tests/test_hermes_logging.py # tests/test_managed_runtime_resolution.py # tests/test_model_tools_async_bridge.py # tests/test_packaging_build_guard.py # tests/test_packaging_metadata.py # tests/test_yaml_indent_consistency.py # tests/tools/test_approval_timeout_overflow.py # tests/tools/test_base_environment.py # tests/tools/test_bot_mode_dm.py # tests/tools/test_browser_chromium_check.py # tests/tools/test_browser_hardening.py # tests/tools/test_browser_homebrew_paths.py # tests/tools/test_browser_npx_warmup.py # tests/tools/test_browser_orphan_reaper.py # tests/tools/test_browser_real_profile.py # tests/tools/test_browser_use_cli.py # tests/tools/test_clipboard.py # tests/tools/test_code_execution.py # tests/tools/test_code_execution_modes.py # tests/tools/test_code_execution_windows_env.py # tests/tools/test_computer_use.py # tests/tools/test_delegate_liveness_timeout.py # tests/tools/test_execute_code_approval_cluster.py # tests/tools/test_execution_flag_detection.py # tests/tools/test_fal_common.py # tests/tools/test_file_operations.py # tests/tools/test_file_tools.py # tests/tools/test_file_tools_cwd_resolution.py # tests/tools/test_file_tools_live.py # tests/tools/test_lazy_deps.py # tests/tools/test_lazy_deps_durable_target.py # tests/tools/test_lazy_deps_managed.py # tests/tools/test_local_env_blocklist.py # tests/tools/test_local_tempdir.py # tests/tools/test_macos_protected_search.py # tests/tools/test_mcp_npx_cached_bin.py # tests/tools/test_oneshot_completion_linger.py # tests/tools/test_process_registry.py # tests/tools/test_read_file_schema_gating.py # tests/tools/test_skill_improvements.py # tests/tools/test_skills_sync.py # tests/tools/test_termux_api_detection.py # tests/tools/test_tirith_security.py # tests/tools/test_transcription_tools.py # tests/tools/test_tts_streaming.py # tests/tools/test_wake_word.py # tests/tui_gateway/test_compute_host_borrowed_lease.py # tests/tui_gateway/test_compute_host_turn_protocol.py # tests/tui_gateway/test_isolated_orphan_activity.py # tests/tui_gateway/test_protocol.py # tests/tui_gateway/test_slash_worker_profile_home.py # tests/tui_gateway/test_subprocess_encoding.py # tests/tui_gateway/test_tui_gateway_server.py # ui-tui/src/__tests__/terminalParity.test.ts # ui-tui/src/__tests__/termuxComposerLayout.test.ts # ui-tui/src/__tests__/textInputFastEcho.test.ts
812 lines
35 KiB
Python
812 lines
35 KiB
Python
#!/usr/bin/env python3
|
|
"""Tests for the OpenRouter-compatible image gen provider (OpenRouter + Nous)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
_RUNTIME = "hermes_cli.runtime_provider.resolve_runtime_provider"
|
|
_PNG_DATA_URI = "data:image/png;base64,dGVzdC1pbWFnZS1kYXRh" # "test-image-data"
|
|
|
|
|
|
def _runtime_ok(**over):
|
|
base = {
|
|
"provider": "openrouter",
|
|
"api_mode": "chat_completions",
|
|
"base_url": "https://openrouter.ai/api/v1",
|
|
"api_key": "sk-or-test",
|
|
"source": "env",
|
|
}
|
|
base.update(over)
|
|
return base
|
|
|
|
|
|
def _mock_chat_response(images):
|
|
resp = MagicMock()
|
|
resp.status_code = 200
|
|
resp.raise_for_status = MagicMock()
|
|
resp.json.return_value = {
|
|
"choices": [
|
|
{
|
|
"message": {
|
|
"role": "assistant",
|
|
"content": "",
|
|
"images": [
|
|
{"type": "image_url", "image_url": {"url": u}} for u in images
|
|
],
|
|
}
|
|
}
|
|
]
|
|
}
|
|
return resp
|
|
|
|
|
|
def _openrouter():
|
|
from plugins.image_gen.openrouter import OpenRouterCompatImageProvider
|
|
|
|
return OpenRouterCompatImageProvider(
|
|
provider_name="openrouter",
|
|
display_name="OpenRouter",
|
|
runtime_name="openrouter",
|
|
config_key="openrouter",
|
|
model_env_var="OPENROUTER_IMAGE_MODEL",
|
|
setup_schema={"name": "OpenRouter (image)", "badge": "paid", "env_vars": []},
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Provider class
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestProviderClass:
|
|
|
|
|
|
def test_capabilities_support_image_input(self):
|
|
caps = _openrouter().capabilities()
|
|
assert "image" in caps["modalities"]
|
|
assert caps["max_reference_images"] >= 1
|
|
|
|
def test_is_available_with_key(self):
|
|
with patch(_RUNTIME, return_value=_runtime_ok()):
|
|
assert _openrouter().is_available() is True
|
|
|
|
|
|
|
|
def test_default_model_ignores_runtime_overrides(self, monkeypatch):
|
|
"""Catalog defaults must not inherit another provider's saved model."""
|
|
from plugins.image_gen.openrouter import DEFAULT_MODEL
|
|
|
|
monkeypatch.setenv("OPENROUTER_IMAGE_MODEL", "custom/provider-image-model")
|
|
stale = {"model": "gpt-image-2-medium"}
|
|
with patch("plugins.image_gen.openrouter._load_image_gen_config", return_value=stale):
|
|
provider = _openrouter()
|
|
assert provider.default_model() == DEFAULT_MODEL
|
|
assert provider._resolve_model() == "custom/provider-image-model"
|
|
|
|
|
|
def test_model_env_override(self, monkeypatch):
|
|
monkeypatch.setenv("OPENROUTER_IMAGE_MODEL", "black-forest-labs/flux.2-pro")
|
|
assert _openrouter()._resolve_model() == "black-forest-labs/flux.2-pro"
|
|
assert _openrouter()._resolve_model_chain() == ["black-forest-labs/flux.2-pro"]
|
|
|
|
|
|
def test_nous_honors_top_level_model(self):
|
|
from plugins.image_gen.openrouter import _build_providers
|
|
|
|
cfg = {"model": "openai/gpt-image-2"}
|
|
nous = {p.name: p for p in _build_providers()}["nous"]
|
|
with patch("plugins.image_gen.openrouter._load_image_gen_config", return_value=cfg):
|
|
assert nous._resolve_model_chain() == ["openai/gpt-image-2"]
|
|
|
|
def test_explicit_model_kwarg_wins_over_config(self):
|
|
cfg = {"model": "openai/gpt-image-2"}
|
|
with patch("plugins.image_gen.openrouter._load_image_gen_config", return_value=cfg):
|
|
assert _openrouter()._resolve_model_chain("google/gemini-3-pro-image") == [
|
|
"google/gemini-3-pro-image"
|
|
]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Live model catalog
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _mock_models_response(entries):
|
|
resp = MagicMock()
|
|
resp.status_code = 200
|
|
resp.raise_for_status = MagicMock()
|
|
resp.json.return_value = {"data": entries}
|
|
return resp
|
|
|
|
|
|
class TestLiveCatalog:
|
|
def test_live_catalog_lists_all_image_output_models(self):
|
|
"""Every image-output model on the endpoint is selectable — including
|
|
ones released after this code shipped."""
|
|
entries = [
|
|
{
|
|
"id": "openai/gpt-5.4-image-2",
|
|
"name": "GPT-5.4 Image 2",
|
|
"architecture": {"output_modalities": ["image"], "input_modalities": ["text", "image"]},
|
|
},
|
|
{
|
|
"id": "some-lab/brand-new-image-model",
|
|
"name": "Brand New",
|
|
"architecture": {"output_modalities": ["image", "text"], "input_modalities": ["text"]},
|
|
},
|
|
{
|
|
"id": "openai/gpt-5.4", # text-only: excluded
|
|
"architecture": {"output_modalities": ["text"], "input_modalities": ["text"]},
|
|
},
|
|
{
|
|
"id": "openrouter/auto", # router pseudo-model: excluded
|
|
"architecture": {"output_modalities": ["image", "text"], "input_modalities": ["text"]},
|
|
},
|
|
]
|
|
provider = _openrouter()
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), patch(
|
|
"requests.get", return_value=_mock_models_response(entries)
|
|
):
|
|
models = provider.list_models()
|
|
ids = [m["id"] for m in models]
|
|
assert "openai/gpt-5.4-image-2" in ids
|
|
assert "some-lab/brand-new-image-model" in ids
|
|
assert "openai/gpt-5.4" not in ids
|
|
assert "openrouter/auto" not in ids
|
|
# Default chain models sort first.
|
|
assert ids[0] == "openai/gpt-5.4-image-2"
|
|
|
|
def test_live_failure_falls_back_to_static_chain(self):
|
|
provider = _openrouter()
|
|
with patch(_RUNTIME, side_effect=RuntimeError("no creds")):
|
|
models = provider.list_models()
|
|
from plugins.image_gen.openrouter import DEFAULT_MODEL, _FALLBACK_MODEL
|
|
|
|
assert [m["id"] for m in models] == [DEFAULT_MODEL, _FALLBACK_MODEL]
|
|
|
|
def test_live_catalog_is_cached(self):
|
|
provider = _openrouter()
|
|
entries = [
|
|
{
|
|
"id": "openai/gpt-5.4-image-2",
|
|
"architecture": {"output_modalities": ["image"], "input_modalities": ["text"]},
|
|
}
|
|
]
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), patch(
|
|
"requests.get", return_value=_mock_models_response(entries)
|
|
) as mock_get:
|
|
provider.list_models()
|
|
provider.list_models()
|
|
assert mock_get.call_count == 1
|
|
|
|
def test_picker_merges_image_api_and_chat_catalogs(self):
|
|
"""OpenRouter picker = union of /images/models and image-output
|
|
/models entries, deduped, defaults first."""
|
|
from plugins.image_gen.openrouter import _build_providers
|
|
|
|
orp = {p.name: p for p in _build_providers()}["openrouter"]
|
|
|
|
def fake_get(url, **kw):
|
|
resp = MagicMock()
|
|
resp.raise_for_status = MagicMock()
|
|
if url.endswith("/images/models"):
|
|
resp.json.return_value = {"data": [
|
|
{"id": "bytedance-seed/seedream-4.5", "name": "Seedream 4.5",
|
|
"architecture": {"input_modalities": ["text", "image"],
|
|
"output_modalities": ["image"]}},
|
|
{"id": "openai/gpt-5.4-image-2", "name": "GPT-5.4 Image 2",
|
|
"architecture": {"input_modalities": ["text", "image"],
|
|
"output_modalities": ["image"]}},
|
|
]}
|
|
else:
|
|
resp.json.return_value = {"data": [
|
|
{"id": "openai/gpt-5.4-image-2",
|
|
"architecture": {"output_modalities": ["image"],
|
|
"input_modalities": ["text", "image"]}},
|
|
{"id": "google/gemini-3-pro-image",
|
|
"architecture": {"output_modalities": ["image"],
|
|
"input_modalities": ["text", "image"]}},
|
|
]}
|
|
return resp
|
|
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), patch("requests.get", side_effect=fake_get):
|
|
ids = [m["id"] for m in orp.list_models()]
|
|
assert ids[0] == "openai/gpt-5.4-image-2" # default first
|
|
assert "bytedance-seed/seedream-4.5" in ids # Image-API-only model present
|
|
assert "google/gemini-3-pro-image" in ids # chat-catalog model present
|
|
assert len(ids) == len(set(ids)) # deduped
|
|
|
|
def test_nous_portal_picker_excludes_image_api_catalog(self):
|
|
"""Nous Portal has no /images route; its picker must not offer
|
|
Image-API-only models it cannot serve."""
|
|
from plugins.image_gen.openrouter import _build_providers
|
|
|
|
nous = {p.name: p for p in _build_providers()}["nous"]
|
|
with patch(_RUNTIME, side_effect=RuntimeError("no creds")):
|
|
ids = [m["id"] for m in nous.list_models()]
|
|
from plugins.image_gen.openrouter import DEFAULT_MODEL, _FALLBACK_MODEL
|
|
|
|
assert ids == [DEFAULT_MODEL, _FALLBACK_MODEL]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestHelpers:
|
|
def test_to_image_url_part_passthrough_url(self):
|
|
from plugins.image_gen.openrouter import _to_image_url_part
|
|
|
|
assert _to_image_url_part("https://x/y.png") == "https://x/y.png"
|
|
assert _to_image_url_part("data:image/png;base64,AAAA") == "data:image/png;base64,AAAA"
|
|
|
|
|
|
def test_to_image_url_part_blocks_credential_store(self, tmp_path, monkeypatch):
|
|
from plugins.image_gen.openrouter import _to_image_url_part
|
|
|
|
hermes_home = tmp_path / ".hermes"
|
|
hermes_home.mkdir()
|
|
auth_json = hermes_home / "auth.json"
|
|
auth_json.write_text('{"api_key":"sk-secret"}', encoding="utf-8")
|
|
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
|
|
|
|
with pytest.raises(ValueError, match="credential store"):
|
|
_to_image_url_part(str(auth_json))
|
|
|
|
|
|
def test_extract_images(self):
|
|
from plugins.image_gen.openrouter import _extract_images
|
|
|
|
payload = {
|
|
"choices": [
|
|
{"message": {"images": [{"image_url": {"url": "data:image/png;base64,AA"}}]}}
|
|
]
|
|
}
|
|
assert _extract_images(payload) == ["data:image/png;base64,AA"]
|
|
|
|
|
|
def test_access_error_hint_for_gated_openai_model(self):
|
|
from plugins.image_gen.openrouter import _FALLBACK_MODEL, _access_error_hint
|
|
|
|
hint = _access_error_hint(
|
|
"OpenRouter", "openai/gpt-5.4-image-2", "OPENROUTER_IMAGE_MODEL", 404, "No endpoints found"
|
|
)
|
|
assert hint is not None
|
|
assert "openai/gpt-5.4-image-2" in hint
|
|
assert "OPENROUTER_IMAGE_MODEL" in hint
|
|
assert _FALLBACK_MODEL in hint
|
|
# Stays a single line under the humanizer's 200-char truncation.
|
|
assert "\n" not in hint and len(hint) <= 200
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# generate()
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestGenerate:
|
|
def test_missing_credentials(self):
|
|
with patch(_RUNTIME, return_value=_runtime_ok(api_key="")):
|
|
result = _openrouter().generate(prompt="a pet")
|
|
assert result["success"] is False
|
|
assert result["error_type"] == "missing_api_key"
|
|
|
|
def test_success_data_uri(self):
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=_mock_chat_response([_PNG_DATA_URI])), \
|
|
patch(
|
|
"plugins.image_gen.openrouter.save_b64_image",
|
|
return_value=Path("/tmp/openrouter_gen.png"),
|
|
) as mock_save:
|
|
result = _openrouter().generate(prompt="a pet")
|
|
|
|
assert result["success"] is True
|
|
assert result["image"] == str(Path("/tmp/openrouter_gen.png"))
|
|
assert result["provider"] == "openrouter"
|
|
mock_save.assert_called_once()
|
|
|
|
|
|
def test_empty_response(self):
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=_mock_chat_response([])):
|
|
result = _openrouter().generate(prompt="a pet")
|
|
assert result["success"] is False
|
|
assert result["error_type"] == "empty_response"
|
|
|
|
def test_payload_shape_and_references(self, tmp_path):
|
|
"""Wire payload must carry image modalities, aspect_ratio, and the
|
|
reference image inlined as a data URI (this is what makes pet rows
|
|
stay on-model)."""
|
|
ref = tmp_path / "base.png"
|
|
ref.write_bytes(b"\x89PNG\r\n")
|
|
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=_mock_chat_response([_PNG_DATA_URI])) as mock_post, \
|
|
patch("plugins.image_gen.openrouter.save_b64_image", return_value=Path("/tmp/x.png")):
|
|
_openrouter().generate(
|
|
prompt="a pet", aspect_ratio="square", reference_images=[str(ref)]
|
|
)
|
|
|
|
payload = mock_post.call_args.kwargs["json"]
|
|
assert payload["modalities"] == ["image", "text"]
|
|
assert payload["image_config"]["aspect_ratio"] == "1:1"
|
|
content = payload["messages"][0]["content"]
|
|
assert content[0] == {"type": "text", "text": "a pet"}
|
|
image_parts = [c for c in content if c["type"] == "image_url"]
|
|
assert len(image_parts) == 1
|
|
assert image_parts[0]["image_url"]["url"].startswith("data:image/png;base64,")
|
|
|
|
def test_auth_header(self):
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=_mock_chat_response([_PNG_DATA_URI])) as mock_post, \
|
|
patch("plugins.image_gen.openrouter.save_b64_image", return_value=Path("/tmp/x.png")):
|
|
_openrouter().generate(prompt="a pet")
|
|
|
|
headers = mock_post.call_args.kwargs["headers"]
|
|
assert headers["Authorization"] == "Bearer sk-or-test"
|
|
|
|
def test_generate_uses_model_kwarg_from_dispatch(self):
|
|
"""image_generate passes image_gen.model as a model kwarg — honor it."""
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=_mock_chat_response([_PNG_DATA_URI])) as mock_post, \
|
|
patch("plugins.image_gen.openrouter.save_b64_image", return_value=Path("/tmp/x.png")):
|
|
result = _openrouter().generate(prompt="a pet", model="openai/gpt-image-2")
|
|
|
|
assert result["success"] is True
|
|
assert result["model"] == "openai/gpt-image-2"
|
|
assert mock_post.call_args.kwargs["json"]["model"] == "openai/gpt-image-2"
|
|
|
|
def test_posts_to_resolved_base_url(self):
|
|
"""Nous routes to its own base URL — proves the same code serves both."""
|
|
nous_runtime = _runtime_ok(
|
|
provider="nous", base_url="https://inference.nousresearch.com/v1", api_key="nous-tok"
|
|
)
|
|
with patch(_RUNTIME, return_value=nous_runtime), \
|
|
patch("requests.post", return_value=_mock_chat_response([_PNG_DATA_URI])) as mock_post, \
|
|
patch("plugins.image_gen.openrouter.save_b64_image", return_value=Path("/tmp/x.png")):
|
|
from plugins.image_gen.openrouter import _build_providers
|
|
|
|
nous = {p.name: p for p in _build_providers()}["nous"]
|
|
result = nous.generate(prompt="a pet")
|
|
|
|
assert result["success"] is True
|
|
assert result["provider"] == "nous"
|
|
url = mock_post.call_args[0][0]
|
|
assert url == "https://inference.nousresearch.com/v1/chat/completions"
|
|
|
|
def test_api_error(self):
|
|
import requests as req_lib
|
|
|
|
resp = MagicMock()
|
|
resp.status_code = 401
|
|
resp.text = "Unauthorized"
|
|
resp.json.return_value = {"error": {"message": "Invalid API key"}}
|
|
resp.raise_for_status.side_effect = req_lib.HTTPError(response=resp)
|
|
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=resp) as mock_post:
|
|
result = _openrouter().generate(prompt="a pet")
|
|
assert result["success"] is False
|
|
assert result["error_type"] == "api_error"
|
|
assert mock_post.call_count == 1
|
|
|
|
def test_timeout(self):
|
|
import requests as req_lib
|
|
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", side_effect=req_lib.Timeout()):
|
|
result = _openrouter().generate(prompt="a pet")
|
|
assert result["success"] is False
|
|
assert result["error_type"] == "timeout"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Registration + pet integration
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Dedicated Image API surface (POST /images/generations)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _openrouter_image_api():
|
|
"""The provider as `_build_providers` really configures it (surface on)."""
|
|
from plugins.image_gen.openrouter import _build_providers
|
|
|
|
return {p.name: p for p in _build_providers()}["openrouter"]
|
|
|
|
|
|
def _mock_image_api_response(entries=None, usage=None):
|
|
resp = MagicMock()
|
|
resp.status_code = 200
|
|
resp.raise_for_status = MagicMock()
|
|
body = {"created": 0, "data": entries if entries is not None else [
|
|
{"b64_json": "dGVzdA==", "media_type": "image/png"}
|
|
]}
|
|
if usage is not None:
|
|
body["usage"] = usage
|
|
resp.json.return_value = body
|
|
return resp
|
|
|
|
|
|
class TestImageApiSurface:
|
|
@pytest.fixture(autouse=True)
|
|
def _isolate(self, monkeypatch):
|
|
"""No config bleed, no catalog cache bleed between tests."""
|
|
import plugins.image_gen.openrouter as mod
|
|
|
|
mod._CATALOG_CACHE.clear()
|
|
monkeypatch.setattr(mod, "_load_image_gen_config", lambda: {})
|
|
for knob in ("QUALITY", "BACKGROUND", "RESOLUTION", "SEED", "N",
|
|
"ASPECT_RATIO", "TIMEOUT", "SURFACE"):
|
|
monkeypatch.delenv(f"OPENROUTER_IMAGE_API_{knob}", raising=False)
|
|
monkeypatch.delenv("OPENROUTER_IMAGE_MODEL", raising=False)
|
|
yield
|
|
mod._CATALOG_CACHE.clear()
|
|
|
|
# -- routing ---------------------------------------------------------
|
|
|
|
def test_curated_model_routes_without_any_probe(self):
|
|
"""The static table answers the common case offline."""
|
|
from plugins.image_gen.openrouter import _select_surface
|
|
|
|
with patch("requests.get", side_effect=AssertionError("must not probe")):
|
|
assert _select_surface("openai/gpt-image-2", "https://x/api/v1", "k", "openrouter") == "images"
|
|
|
|
def test_chat_defaults_stay_on_chat_even_though_the_catalog_lists_them(self):
|
|
"""The regression this guards: /images/models is a superset that
|
|
includes DEFAULT_MODEL and _FALLBACK_MODEL. Routing on catalog
|
|
membership would silently move every existing default call."""
|
|
from plugins.image_gen.openrouter import (
|
|
DEFAULT_MODEL,
|
|
_FALLBACK_MODEL,
|
|
_select_surface,
|
|
)
|
|
|
|
catalog = MagicMock()
|
|
catalog.raise_for_status = MagicMock()
|
|
catalog.json.return_value = {
|
|
"data": [{"id": DEFAULT_MODEL}, {"id": _FALLBACK_MODEL}]
|
|
}
|
|
with patch("requests.get", return_value=catalog):
|
|
assert _select_surface(DEFAULT_MODEL, "https://x/api/v1", "k", "openrouter") == "chat"
|
|
assert _select_surface(_FALLBACK_MODEL, "https://x/api/v1", "k", "openrouter") == "chat"
|
|
|
|
def test_unknown_catalog_model_routes_to_image_api(self):
|
|
"""An id past the curated snapshot but in the live catalog is served
|
|
by the dedicated API — a model picked from the live picker must not
|
|
fall onto chat-completions and 404."""
|
|
import plugins.image_gen.openrouter as orp
|
|
from plugins.image_gen.openrouter import _select_surface
|
|
|
|
orp._CATALOG_CACHE.clear()
|
|
catalog = MagicMock()
|
|
catalog.raise_for_status = MagicMock()
|
|
catalog.json.return_value = {"data": [{"id": "brandnew/model-9"}]}
|
|
with patch("requests.get", return_value=catalog) as mock_get:
|
|
assert _select_surface("brandnew/model-9", "https://x/api/v1", "k", "openrouter") == "images"
|
|
assert _select_surface("brandnew/model-9", "https://x/api/v1", "k", "openrouter") == "images"
|
|
# Catalog probe is cached — one fetch serves repeat calls.
|
|
assert mock_get.call_count == 1
|
|
|
|
def test_unknown_model_not_in_catalog_stays_on_chat(self):
|
|
import plugins.image_gen.openrouter as orp
|
|
from plugins.image_gen.openrouter import _select_surface
|
|
|
|
orp._CATALOG_CACHE.clear()
|
|
catalog = MagicMock()
|
|
catalog.raise_for_status = MagicMock()
|
|
catalog.json.return_value = {"data": [{"id": "something/else"}]}
|
|
with patch("requests.get", return_value=catalog):
|
|
assert _select_surface("not-served/model", "https://x/api/v1", "k", "openrouter") == "chat"
|
|
|
|
def test_failed_probe_costs_nothing(self):
|
|
import plugins.image_gen.openrouter as orp
|
|
from plugins.image_gen.openrouter import _select_surface
|
|
|
|
orp._CATALOG_CACHE.clear()
|
|
with patch("requests.get", side_effect=OSError("network down")):
|
|
assert _select_surface("unknown/model", "https://x/api/v1", "k", "openrouter") == "chat"
|
|
|
|
def test_surface_can_be_forced_both_ways(self, monkeypatch):
|
|
from plugins.image_gen.openrouter import DEFAULT_MODEL, _select_surface
|
|
|
|
monkeypatch.setenv("OPENROUTER_IMAGE_API_SURFACE", "images")
|
|
with patch("requests.get", side_effect=AssertionError("must not probe")):
|
|
assert _select_surface(DEFAULT_MODEL, "https://x/api/v1", "k", "openrouter") == "images"
|
|
|
|
monkeypatch.setenv("OPENROUTER_IMAGE_API_SURFACE", "chat")
|
|
with patch("requests.get", side_effect=AssertionError("must not probe")):
|
|
assert _select_surface("openai/gpt-image-2", "https://x/api/v1", "k", "openrouter") == "chat"
|
|
|
|
def test_image_api_model_posts_to_images_generations(self):
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=_mock_image_api_response()) as mock_post, \
|
|
patch("plugins.image_gen.openrouter.save_b64_image", return_value=Path("/tmp/i.png")):
|
|
result = _openrouter_image_api().generate(
|
|
prompt="a red square", aspect_ratio="square", model="openai/gpt-image-2"
|
|
)
|
|
|
|
assert result["success"] is True
|
|
assert mock_post.call_args[0][0] == "https://openrouter.ai/api/v1/images/generations"
|
|
payload = mock_post.call_args.kwargs["json"]
|
|
assert payload["model"] == "openai/gpt-image-2"
|
|
assert payload["prompt"] == "a red square"
|
|
assert payload["aspect_ratio"] == "1:1"
|
|
assert "messages" not in payload and "modalities" not in payload
|
|
assert result["endpoint"] == "images/generations"
|
|
|
|
def test_chat_model_still_uses_chat_completions(self):
|
|
"""The new surface must not capture the existing default chain."""
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=_mock_chat_response([_PNG_DATA_URI])) as mock_post, \
|
|
patch("plugins.image_gen.openrouter.save_b64_image", return_value=Path("/tmp/x.png")):
|
|
result = _openrouter_image_api().generate(prompt="a pet")
|
|
|
|
assert result["success"] is True
|
|
assert mock_post.call_args[0][0].endswith("/chat/completions")
|
|
|
|
def test_nous_never_uses_the_image_api(self):
|
|
"""Nous Portal proxies chat-completions and has no /images route."""
|
|
from plugins.image_gen.openrouter import _build_providers
|
|
|
|
nous_runtime = _runtime_ok(
|
|
provider="nous", base_url="https://inference.nousresearch.com/v1", api_key="nous-tok"
|
|
)
|
|
with patch(_RUNTIME, return_value=nous_runtime), \
|
|
patch("requests.post", return_value=_mock_chat_response([_PNG_DATA_URI])) as mock_post, \
|
|
patch("plugins.image_gen.openrouter.save_b64_image", return_value=Path("/tmp/x.png")):
|
|
nous = {p.name: p for p in _build_providers()}["nous"]
|
|
result = nous.generate(prompt="a pet", model="openai/gpt-image-2")
|
|
|
|
assert result["success"] is True
|
|
assert mock_post.call_args[0][0] == "https://inference.nousresearch.com/v1/chat/completions"
|
|
|
|
# -- per-model parameter filtering ------------------------------------
|
|
|
|
def test_aspect_ratio_is_mapped_per_model(self):
|
|
from plugins.image_gen.openrouter import _build_image_api_payload
|
|
|
|
gemini, _ = _build_image_api_payload(
|
|
model_id="google/gemini-3.1-flash-lite-image", prompt="p",
|
|
semantic_aspect="landscape", references=[], config_key="openrouter", kwargs={},
|
|
)
|
|
mini, _ = _build_image_api_payload(
|
|
model_id="openai/gpt-image-1-mini", prompt="p",
|
|
semantic_aspect="landscape", references=[], config_key="openrouter", kwargs={},
|
|
)
|
|
# gpt-image-1-mini has no 16:9 at all, so landscape degrades to 3:2.
|
|
assert gemini["aspect_ratio"] == "16:9"
|
|
assert mini["aspect_ratio"] == "3:2"
|
|
|
|
def test_unsupported_parameter_is_dropped_and_explained(self):
|
|
"""The endpoint silently ignores unknown fields, so we must filter."""
|
|
from plugins.image_gen.openrouter import _build_image_api_payload
|
|
|
|
payload, notes = _build_image_api_payload(
|
|
model_id="openai/gpt-image-2", prompt="p", semantic_aspect="square",
|
|
references=[], config_key="openrouter", kwargs={"background": "transparent"},
|
|
)
|
|
assert "background" not in payload
|
|
assert any("background" in n for n in notes)
|
|
|
|
payload, notes = _build_image_api_payload(
|
|
model_id="openai/gpt-image-1-mini", prompt="p", semantic_aspect="square",
|
|
references=[], config_key="openrouter", kwargs={"background": "transparent"},
|
|
)
|
|
assert payload["background"] == "transparent"
|
|
|
|
def test_n_is_clamped_to_the_model_cap(self):
|
|
from plugins.image_gen.openrouter import _build_image_api_payload
|
|
|
|
payload, notes = _build_image_api_payload(
|
|
model_id="qwen/qwen-image-3-pro", prompt="p", semantic_aspect="square",
|
|
references=[], config_key="openrouter", kwargs={"n": 20},
|
|
)
|
|
assert payload["n"] == 6
|
|
assert any("cap of 6" in n for n in notes)
|
|
|
|
def test_unknown_model_omits_the_aspect_ratio(self):
|
|
"""An out-of-enum ratio is a hard 400, so never guess one."""
|
|
from plugins.image_gen.openrouter import _build_image_api_payload
|
|
|
|
payload, notes = _build_image_api_payload(
|
|
model_id="brandnew/model-9", prompt="p", semantic_aspect="landscape",
|
|
references=[], config_key="openrouter", kwargs={},
|
|
)
|
|
assert "aspect_ratio" not in payload
|
|
assert any("catalog" in n for n in notes)
|
|
|
|
def test_env_knob_applies(self, monkeypatch):
|
|
from plugins.image_gen.openrouter import _build_image_api_payload
|
|
|
|
monkeypatch.setenv("OPENROUTER_IMAGE_API_QUALITY", "high")
|
|
payload, _ = _build_image_api_payload(
|
|
model_id="openai/gpt-image-2", prompt="p", semantic_aspect="square",
|
|
references=[], config_key="openrouter", kwargs={},
|
|
)
|
|
assert payload["quality"] == "high"
|
|
|
|
# -- references --------------------------------------------------------
|
|
|
|
def test_references_use_the_per_model_cap(self, tmp_path):
|
|
"""Image API models take far more references than chat's 3."""
|
|
refs = []
|
|
for i in range(5):
|
|
p = tmp_path / f"r{i}.png"
|
|
p.write_bytes(b"\x89PNG\r\n")
|
|
refs.append(str(p))
|
|
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=_mock_image_api_response()) as mock_post, \
|
|
patch("plugins.image_gen.openrouter.save_b64_image", return_value=Path("/tmp/i.png")):
|
|
result = _openrouter_image_api().generate(
|
|
prompt="edit", model="openai/gpt-image-2", reference_image_urls=refs
|
|
)
|
|
|
|
payload = mock_post.call_args.kwargs["json"]
|
|
assert len(payload["input_references"]) == 5 # chat would have clamped to 3
|
|
assert payload["input_references"][0]["image_url"]["url"].startswith("data:image/png;base64,")
|
|
assert result["modality"] == "image"
|
|
|
|
def test_unreadable_sole_reference_fails_instead_of_degrading(self):
|
|
"""Degrading an edit to text-to-image bills an unrelated picture."""
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post") as mock_post:
|
|
result = _openrouter_image_api().generate(
|
|
prompt="edit this", model="openai/gpt-image-2",
|
|
image_url="/nonexistent/definitely-missing.png",
|
|
)
|
|
|
|
assert result["success"] is False
|
|
assert result["error_type"] == "io_error"
|
|
mock_post.assert_not_called()
|
|
|
|
# -- response handling -------------------------------------------------
|
|
|
|
def test_cost_and_extras_are_surfaced(self):
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=_mock_image_api_response(
|
|
usage={"cost": 0.0336, "total_tokens": 1128})), \
|
|
patch("plugins.image_gen.openrouter.save_b64_image", return_value=Path("/tmp/i.png")):
|
|
result = _openrouter_image_api().generate(
|
|
prompt="p", aspect_ratio="portrait", model="krea/krea-2-medium"
|
|
)
|
|
|
|
assert result["cost_usd"] == 0.0336
|
|
assert result["total_tokens"] == 1128
|
|
assert result["exact_aspect_ratio"] == "9:16"
|
|
assert result["image"] == str(Path("/tmp/i.png"))
|
|
|
|
_USAGE = {"prompt_tokens": 1000, "completion_tokens": 128, "total_tokens": 1128}
|
|
|
|
@pytest.mark.parametrize("surface, model, usage, images", [
|
|
("chat", "openai/gpt-5.4-image-2", _USAGE, True), # default chain: token-billed via /chat/completions
|
|
("images", "krea/krea-2-medium", _USAGE, True), # curated Image API model
|
|
("images", "krea/krea-2-medium", None, True), # flat-fee body without usage: no write
|
|
("chat", "openai/gpt-5.4-image-2", _USAGE, False), # billed HTTP 200 with text but no image
|
|
("images", "krea/krea-2-medium", _USAGE, False), # billed HTTP 200 with empty ``data``
|
|
])
|
|
def test_token_usage_reaches_session_accounting(self, surface, model, usage, images):
|
|
"""A response carrying token usage records one ``image_generation`` row on the ambient
|
|
session — also when it carries no image (the provider billed the tokens anyway); a body
|
|
without usage records nothing."""
|
|
from agent import aux_accounting
|
|
|
|
recorded = []
|
|
|
|
class _DB:
|
|
def record_auxiliary_usage(self, *args, **kwargs):
|
|
recorded.append((args, kwargs))
|
|
|
|
if surface == "chat":
|
|
response = _mock_chat_response([_PNG_DATA_URI] if images else [])
|
|
response.json.return_value["usage"] = dict(usage)
|
|
else:
|
|
response = _mock_image_api_response([] if not images else None, usage=usage)
|
|
token = aux_accounting.set_accounting_context(_DB(), "sess-1")
|
|
try:
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=response), \
|
|
patch("plugins.image_gen.openrouter.save_b64_image", return_value=Path("/tmp/i.png")):
|
|
result = _openrouter_image_api().generate(prompt="p", aspect_ratio="portrait", model=model)
|
|
finally:
|
|
aux_accounting.reset_accounting_context(token)
|
|
|
|
assert result["success"] is images
|
|
if not images:
|
|
assert result["error_type"] == "empty_response"
|
|
if usage is None:
|
|
assert recorded == []
|
|
return
|
|
((session_id, task), kwargs), = recorded
|
|
assert (session_id, task) == ("sess-1", "image_generation")
|
|
assert kwargs["model"] == model
|
|
assert kwargs["billing_provider"] == "openrouter"
|
|
assert (kwargs["input_tokens"], kwargs["output_tokens"]) == (1000, 128)
|
|
|
|
|
|
def test_multiple_images_land_in_additional_images(self):
|
|
entries = [
|
|
{"b64_json": "AA==", "media_type": "image/png"},
|
|
{"b64_json": "BB==", "media_type": "image/png"},
|
|
]
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=_mock_image_api_response(entries)), \
|
|
patch("plugins.image_gen.openrouter.save_b64_image",
|
|
side_effect=[Path("/tmp/a.png"), Path("/tmp/b.png")]):
|
|
result = _openrouter_image_api().generate(prompt="p", model="openai/gpt-image-2")
|
|
|
|
assert result["image"] == str(Path("/tmp/a.png"))
|
|
assert result["additional_images"] == [str(Path("/tmp/b.png"))]
|
|
|
|
def test_empty_data_is_typed(self):
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=_mock_image_api_response([])):
|
|
result = _openrouter_image_api().generate(prompt="p", model="openai/gpt-image-2")
|
|
|
|
assert result["success"] is False
|
|
assert result["error_type"] == "empty_response"
|
|
|
|
def test_zod_validation_error_is_flattened(self):
|
|
from plugins.image_gen.openrouter import _extract_image_api_error
|
|
|
|
resp = MagicMock()
|
|
resp.json.return_value = {
|
|
"success": False,
|
|
"error": {
|
|
"name": "ZodError",
|
|
"message": '[{"path":["aspect_ratio"],"message":"Invalid option"}]',
|
|
},
|
|
}
|
|
assert _extract_image_api_error(resp, "fb").startswith("aspect_ratio: Invalid option")
|
|
|
|
def test_auth_error_is_not_retried_as_api_error(self):
|
|
import requests as req_lib
|
|
|
|
resp = MagicMock()
|
|
resp.status_code = 401
|
|
resp.text = "Unauthorized"
|
|
resp.json.return_value = {"error": {"message": "Invalid API key"}}
|
|
resp.raise_for_status.side_effect = req_lib.HTTPError(response=resp)
|
|
|
|
with patch(_RUNTIME, return_value=_runtime_ok()), \
|
|
patch("requests.post", return_value=resp):
|
|
result = _openrouter_image_api().generate(prompt="p", model="openai/gpt-image-2")
|
|
|
|
assert result["success"] is False
|
|
assert result["error_type"] == "auth_error"
|
|
assert "_retryable" not in result
|
|
|
|
def test_catalog_models_are_offered_only_by_openrouter(self):
|
|
from plugins.image_gen.openrouter import _IMAGE_API_MODELS, _build_providers
|
|
|
|
by_name = {p.name: p for p in _build_providers()}
|
|
openrouter_ids = {m["id"] for m in by_name["openrouter"].list_models()}
|
|
nous_ids = {m["id"] for m in by_name["nous"].list_models()}
|
|
assert set(_IMAGE_API_MODELS) <= openrouter_ids
|
|
assert not (set(_IMAGE_API_MODELS) & nous_ids)
|
|
|
|
|
|
|
|
class TestRegistration:
|
|
def test_register_both(self):
|
|
from plugins.image_gen.openrouter import register
|
|
|
|
ctx = MagicMock()
|
|
register(ctx)
|
|
registered = [c.args[0].name for c in ctx.register_image_gen_provider.call_args_list]
|
|
assert set(registered) == {"openrouter", "nous"}
|
|
|
|
def test_both_are_reference_capable_for_pets(self):
|
|
from agent.pet.generate.imagegen import _REF_CAPABLE
|
|
|
|
assert "openrouter" in _REF_CAPABLE
|
|
assert "nous" in _REF_CAPABLE
|