Files
mcphub/tests/plugins/wordpress/test_companion_install_hint.py
airano-ir f203ca88de
Some checks failed
Release / Test before release (push) Has been cancelled
Release / Publish to PyPI (push) Has been cancelled
Release / Publish to Docker Hub (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
feat(v3.12.0): media pipeline, AI image generation, capability probe, companion v2.9.0
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>
2026-04-25 16:25:58 +02:00

159 lines
6.6 KiB
Python

"""Tests for the shared ``companion_install_hint`` helper + its use in
companion-backed handlers' ``companion_unreachable`` error payloads.
The helper's job is to give the caller enough information to actually
install / configure the companion plugin rather than just saying
"companion unreachable". Every handler that routes through the
companion now emits this dict alongside the existing human-readable
``hint`` string.
"""
from __future__ import annotations
import json
from unittest.mock import AsyncMock
import pytest
from plugins.wordpress.client import WordPressClient
from plugins.wordpress.handlers._companion_hint import (
COMPANION_DOWNLOAD_URL,
companion_install_hint,
)
@pytest.fixture
def wp_client():
c = WordPressClient(site_url="https://wp.example.com", username="u", app_password="p")
c.post = AsyncMock(side_effect=RuntimeError("rest_no_route: 404"))
c.get = AsyncMock(side_effect=RuntimeError("rest_no_route: 404"))
return c
# ---------------------------------------------------------------------------
# Helper-level tests
# ---------------------------------------------------------------------------
class TestCompanionInstallHintHelper:
@pytest.mark.unit
def test_download_url_points_at_github_raw(self):
assert COMPANION_DOWNLOAD_URL.startswith("https://github.com/airano-ir/mcphub/raw/main/")
assert COMPANION_DOWNLOAD_URL.endswith("airano-mcp-bridge.zip")
@pytest.mark.unit
def test_returns_required_keys(self):
hint = companion_install_hint(min_version="2.4.0")
assert set(hint.keys()) == {
"install_url",
"install_instructions",
"required_capability",
"companion_min_version",
}
assert hint["companion_min_version"] == "2.4.0"
assert hint["required_capability"] == "manage_options"
@pytest.mark.unit
def test_capability_override(self):
hint = companion_install_hint(min_version="2.3.0", required_capability="edit_posts")
assert hint["required_capability"] == "edit_posts"
assert "edit_posts" in hint["install_instructions"]
@pytest.mark.unit
def test_route_hint_appended_when_provided(self):
hint = companion_install_hint(min_version="2.8.0", route="airano-mcp/v1/foo")
assert hint["route"] == "airano-mcp/v1/foo"
@pytest.mark.unit
def test_install_url_is_stable(self):
hint_a = companion_install_hint(min_version="2.0.0")
hint_b = companion_install_hint(min_version="3.0.0")
# The download URL itself is version-agnostic — it always points
# at the latest zip on main.
assert hint_a["install_url"] == hint_b["install_url"] == COMPANION_DOWNLOAD_URL
# ---------------------------------------------------------------------------
# End-to-end: a handler emits install_hint when the companion is missing.
# ---------------------------------------------------------------------------
class TestHandlerEmitsInstallHint:
@pytest.mark.asyncio
async def test_cache_purge_emits_install_hint_on_failure(self, wp_client):
from plugins.wordpress.handlers.cache_purge import CachePurgeHandler
handler = CachePurgeHandler(wp_client)
out = json.loads(await handler.cache_purge())
assert out["ok"] is False
assert out["error"] == "companion_unreachable"
assert "install_hint" in out
assert out["install_hint"]["install_url"] == COMPANION_DOWNLOAD_URL
assert out["install_hint"]["companion_min_version"] == "2.4.0"
assert out["install_hint"]["required_capability"] == "manage_options"
assert out["install_hint"]["route"] == "airano-mcp/v1/cache-purge"
@pytest.mark.asyncio
async def test_regenerate_thumbnails_emits_install_hint(self, wp_client):
from plugins.wordpress.handlers.regenerate_thumbnails import (
RegenerateThumbnailsHandler,
)
handler = RegenerateThumbnailsHandler(wp_client)
out = json.loads(await handler.regenerate_thumbnails(ids=[1, 2]))
assert out["error"] == "companion_unreachable"
assert out["install_hint"]["companion_min_version"] == "2.8.0"
# Writes need upload_files, not manage_options.
assert out["install_hint"]["required_capability"] == "upload_files"
@pytest.mark.asyncio
async def test_bulk_meta_emits_install_hint(self, wp_client):
from plugins.wordpress.handlers.bulk_meta import BulkMetaHandler
handler = BulkMetaHandler(wp_client)
out = json.loads(
await handler.bulk_update_meta(updates=[{"post_id": 1, "meta": {"k": "v"}}])
)
assert out["error"] == "companion_unreachable"
assert out["install_hint"]["companion_min_version"] == "2.2.0"
@pytest.mark.asyncio
async def test_export_emits_install_hint(self, wp_client):
from plugins.wordpress.handlers.export import ExportHandler
handler = ExportHandler(wp_client)
out = json.loads(await handler.export_content())
assert out["error"] == "companion_unreachable"
assert out["install_hint"]["companion_min_version"] == "2.3.0"
# Export uses edit_posts, not manage_options.
assert out["install_hint"]["required_capability"] == "edit_posts"
@pytest.mark.asyncio
async def test_site_health_emits_install_hint(self, wp_client):
from plugins.wordpress.handlers.site_health import SiteHealthHandler
handler = SiteHealthHandler(wp_client)
out = json.loads(await handler.site_health())
assert out["error"] == "companion_unreachable"
assert out["install_hint"]["companion_min_version"] == "2.6.0"
@pytest.mark.asyncio
async def test_transient_flush_emits_install_hint(self, wp_client):
from plugins.wordpress.handlers.transient_flush import TransientFlushHandler
handler = TransientFlushHandler(wp_client)
out = json.loads(await handler.transient_flush())
assert out["error"] == "companion_unreachable"
assert out["install_hint"]["companion_min_version"] == "2.5.0"
@pytest.mark.asyncio
async def test_capabilities_probe_embeds_install_hint_when_empty(self, wp_client):
"""``_empty_capabilities_payload`` is the fallback shape every
companion probe returns on failure; the install hint must be in
there too so the UI can link-to-install without a second lookup."""
from plugins.wordpress.handlers.capabilities import _empty_capabilities_payload
payload = _empty_capabilities_payload("https://wp.example.com", reason="test")
assert "install_hint" in payload
assert payload["install_hint"]["install_url"] == COMPANION_DOWNLOAD_URL