Three-month batch sync from internal repo (~80 commits) covering Tracks F.5a, F.7e, F.8, F.17, F.18, F.X. WordPress media pipeline - Pillow-based optimization, AI image generation (OpenAI / Stability / Replicate / Google Nano Banana / OpenRouter), chunked + resumable uploads, bulk delete/reassign, idempotent retries. Capability discovery (F.7e) - Per-site credential probe + adapters for WordPress / WooCommerce / Gitea, tier-fit unions granted ∪ roles, capability badge UI with HTMX partial re-check, install hint in every companion-unreachable error. Companion plugin overhaul - Renamed wordpress-plugin/airano-mcp-seo-bridge → wordpress-plugin/airano-mcp-bridge. - Eight new endpoints: /capabilities, /bulk-meta, /export, /cache-purge, /transient-flush, /site-health, /audit-hook, /upload-and-attach. - wp.org Plugin Check pass: i18n, WP_Filesystem, scheme allowlist on audit-hook URL. Other - Gitea ergonomics (F.17): batch files, tree, search, compare, releases, fork. - Opportunistic bcrypt upgrade for legacy SHA-256 admin keys (F.8). - n8n refactor: structured errors, capability probe, missing tools backfilled. - Idempotency-Key dedup for AI media upload retries; WP client fast-fails on unreachable sites. Docs - README + CLAUDE.md drop the fixed "633 tools" claim. The total grows with each release; per-plugin approximations + dashboard-surfaced counts replace it. - Tools/Tests badges removed in favour of "Plugins: 10". Deployment - PyPI mirror chain, optional BUILD_HTTP_PROXY, Alpine→Yandex apk mirror, Debian-slim Plan-B Dockerfile, mirror.gcr.io variant. CI - Black + Ruff clean on Python 3.12; pytest tests/ green. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
223 lines
7.0 KiB
Python
223 lines
7.0 KiB
Python
"""F.5a.8.2 — Tests for wordpress_regenerate_thumbnails."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
|
|
from plugins.wordpress.client import WordPressClient
|
|
from plugins.wordpress.handlers.regenerate_thumbnails import (
|
|
RegenerateThumbnailsHandler,
|
|
get_tool_specifications,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def wp_client():
|
|
return WordPressClient(site_url="https://wp.example.com", username="u", app_password="p")
|
|
|
|
|
|
@pytest.fixture
|
|
def handler(wp_client):
|
|
return RegenerateThumbnailsHandler(wp_client)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Spec
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_spec_shape():
|
|
specs = get_tool_specifications()
|
|
assert len(specs) == 1
|
|
s = specs[0]
|
|
assert s["name"] == "regenerate_thumbnails"
|
|
assert s["scope"] == "write"
|
|
props = s["schema"]["properties"]
|
|
assert {"ids", "all", "offset", "limit"} <= set(props.keys())
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Validation (no round-trip)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_neither_ids_nor_all_is_rejected(handler, wp_client, monkeypatch):
|
|
post_mock = AsyncMock()
|
|
monkeypatch.setattr(wp_client, "post", post_mock)
|
|
|
|
out = json.loads(await handler.regenerate_thumbnails())
|
|
assert out["ok"] is False
|
|
assert out["error"] == "invalid_request"
|
|
post_mock.assert_not_called()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_both_ids_and_all_is_rejected(handler, wp_client, monkeypatch):
|
|
post_mock = AsyncMock()
|
|
monkeypatch.setattr(wp_client, "post", post_mock)
|
|
|
|
out = json.loads(await handler.regenerate_thumbnails(ids=[1], all=True))
|
|
assert out["ok"] is False
|
|
assert "mutually exclusive" in out["message"]
|
|
post_mock.assert_not_called()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_empty_ids_list_is_rejected(handler, wp_client, monkeypatch):
|
|
post_mock = AsyncMock()
|
|
monkeypatch.setattr(wp_client, "post", post_mock)
|
|
|
|
out = json.loads(await handler.regenerate_thumbnails(ids=["abc", 0, -1]))
|
|
assert out["ok"] is False
|
|
assert out["error"] == "invalid_request"
|
|
post_mock.assert_not_called()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Happy path — ids mode
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ids_mode_dedupes_and_caps(handler, wp_client, monkeypatch):
|
|
captured: dict = {}
|
|
|
|
async def _post(route, json_data=None, use_custom_namespace=False):
|
|
captured["route"] = route
|
|
captured["body"] = json_data
|
|
captured["ns"] = use_custom_namespace
|
|
return {
|
|
"mode": "ids",
|
|
"attempted": 3,
|
|
"processed": 3,
|
|
"skipped": [],
|
|
"errors": [],
|
|
"plugin_version": "2.8.0",
|
|
}
|
|
|
|
monkeypatch.setattr(wp_client, "post", _post)
|
|
|
|
# 60 ids with duplicates — should be deduped and capped at 50
|
|
ids = [5, 5, 10, 10] + list(range(1, 57))
|
|
out = json.loads(await handler.regenerate_thumbnails(ids=ids))
|
|
|
|
assert out["ok"] is True
|
|
assert out["mode"] == "ids"
|
|
assert out["processed"] == 3
|
|
assert out["plugin_version"] == "2.8.0"
|
|
assert captured["route"] == "airano-mcp/v1/regenerate-thumbnails"
|
|
assert captured["ns"] is True
|
|
assert "all" not in captured["body"]
|
|
sent = captured["body"]["ids"]
|
|
assert len(sent) <= 50
|
|
assert len(sent) == len(set(sent)) # deduped
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_all_mode_forwards_offset_and_limit(handler, wp_client, monkeypatch):
|
|
captured: dict = {}
|
|
|
|
async def _post(route, json_data=None, use_custom_namespace=False):
|
|
captured["body"] = json_data
|
|
return {
|
|
"mode": "all",
|
|
"attempted": 10,
|
|
"processed": 10,
|
|
"skipped": [],
|
|
"errors": [],
|
|
"offset": 100,
|
|
"limit": 10,
|
|
"has_more": True,
|
|
"next_offset": 110,
|
|
"total": 200,
|
|
"plugin_version": "2.8.0",
|
|
}
|
|
|
|
monkeypatch.setattr(wp_client, "post", _post)
|
|
|
|
out = json.loads(await handler.regenerate_thumbnails(all=True, offset=100, limit=10))
|
|
|
|
assert captured["body"] == {"all": True, "offset": 100, "limit": 10}
|
|
assert out["mode"] == "all"
|
|
assert out["has_more"] is True
|
|
assert out["next_offset"] == 110
|
|
assert out["total"] == 200
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_all_mode_limit_capped_server_side(handler, wp_client, monkeypatch):
|
|
captured: dict = {}
|
|
|
|
async def _post(route, json_data=None, use_custom_namespace=False):
|
|
captured["body"] = json_data
|
|
return {"mode": "all", "attempted": 0, "processed": 0}
|
|
|
|
monkeypatch.setattr(wp_client, "post", _post)
|
|
await handler.regenerate_thumbnails(all=True, limit=9999)
|
|
assert captured["body"]["limit"] == 50
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_all_mode_negative_offset_clamped(handler, wp_client, monkeypatch):
|
|
captured: dict = {}
|
|
|
|
async def _post(route, json_data=None, use_custom_namespace=False):
|
|
captured["body"] = json_data
|
|
return {"mode": "all", "attempted": 0, "processed": 0}
|
|
|
|
monkeypatch.setattr(wp_client, "post", _post)
|
|
await handler.regenerate_thumbnails(all=True, offset=-5)
|
|
assert captured["body"]["offset"] == 0
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Error paths
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_companion_unreachable_returns_structured_error(handler, wp_client, monkeypatch):
|
|
async def _boom(*_a, **_kw):
|
|
raise RuntimeError("404 - rest_no_route")
|
|
|
|
monkeypatch.setattr(wp_client, "post", _boom)
|
|
out = json.loads(await handler.regenerate_thumbnails(ids=[1, 2]))
|
|
assert out["ok"] is False
|
|
assert out["error"] == "companion_unreachable"
|
|
assert "v2.8.0" in out["hint"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_non_dict_response_is_rejected(handler, wp_client, monkeypatch):
|
|
async def _weird(*_a, **_kw):
|
|
return ["not", "a", "dict"]
|
|
|
|
monkeypatch.setattr(wp_client, "post", _weird)
|
|
out = json.loads(await handler.regenerate_thumbnails(ids=[1]))
|
|
assert out["ok"] is False
|
|
assert out["error"] == "invalid_response"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_errors_from_companion_surface_through(handler, wp_client, monkeypatch):
|
|
async def _errs(*_a, **_kw):
|
|
return {
|
|
"mode": "ids",
|
|
"attempted": 2,
|
|
"processed": 0,
|
|
"skipped": [{"id": 42, "reason": "not_image"}],
|
|
"errors": [{"id": 99, "error": "file_missing"}],
|
|
"plugin_version": "2.8.0",
|
|
}
|
|
|
|
monkeypatch.setattr(wp_client, "post", _errs)
|
|
out = json.loads(await handler.regenerate_thumbnails(ids=[42, 99]))
|
|
# processed=0 with at least one error → not ok
|
|
assert out["ok"] is False
|
|
assert out["errors"] == [{"id": 99, "error": "file_missing"}]
|
|
assert out["skipped"] == [{"id": 42, "reason": "not_image"}]
|