Files
mcphub/tests/plugins/wordpress/test_media_probe.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

178 lines
5.8 KiB
Python

"""F.5a.6.3 — Tests for wordpress_probe_upload_limits."""
from __future__ import annotations
import json
from unittest.mock import AsyncMock
import pytest
from plugins.wordpress.client import WordPressClient
from plugins.wordpress.handlers.media_probe import ProbeHandler, _ProbeCache
@pytest.fixture
def wp_client():
return WordPressClient(site_url="https://wp.example.com", username="u", app_password="p")
@pytest.fixture
def cache():
return _ProbeCache(ttl=24 * 3600)
@pytest.mark.asyncio
async def test_companion_endpoint_parses_and_caches(wp_client, monkeypatch, cache):
handler = ProbeHandler(wp_client, cache=cache)
payload = {
"upload_max_filesize": "64M",
"post_max_size": "128M",
"memory_limit": "256M",
"max_input_time": "300",
"wp_max_upload_size": 67108864,
}
get_mock = AsyncMock(return_value=payload)
monkeypatch.setattr(wp_client, "get", get_mock)
out_json = await handler.probe_upload_limits()
out = json.loads(out_json)
assert out["source"] == "companion"
assert out["limits"] == payload
assert out["cached"] is False
# F.5a.7: companion payload exposes byte-parsed limits + companion flag.
assert out["companion_available"] is True
assert out["limits_bytes"]["upload_max_filesize"] == 64 * 1024**2
assert out["limits_bytes"]["post_max_size"] == 128 * 1024**2
assert out["limits_bytes"]["wp_max_upload_size"] == 67108864
# The effective ceiling is the smallest of the byte-valued keys.
assert out["limits_bytes"]["effective_ceiling"] == 64 * 1024**2
# First call hit the network exactly once.
assert get_mock.call_count == 1
get_mock.assert_called_with("airano-mcp/v1/upload-limits", use_custom_namespace=True)
@pytest.mark.asyncio
async def test_second_call_is_served_from_cache(wp_client, monkeypatch, cache):
handler = ProbeHandler(wp_client, cache=cache)
payload = {"upload_max_filesize": "64M"}
get_mock = AsyncMock(return_value=payload)
monkeypatch.setattr(wp_client, "get", get_mock)
await handler.probe_upload_limits()
out2 = json.loads(await handler.probe_upload_limits())
assert out2["cached"] is True
# Network only hit once across two probe calls.
assert get_mock.call_count == 1
@pytest.mark.asyncio
async def test_cache_expires_after_ttl(wp_client, monkeypatch):
cache = _ProbeCache(ttl=0.0) # immediate expiry
handler = ProbeHandler(wp_client, cache=cache)
get_mock = AsyncMock(return_value={"upload_max_filesize": "64M"})
monkeypatch.setattr(wp_client, "get", get_mock)
await handler.probe_upload_limits()
await handler.probe_upload_limits()
# ttl=0 → both calls re-fetch.
assert get_mock.call_count == 2
@pytest.mark.asyncio
async def test_companion_failure_falls_back_to_rest_index(wp_client, monkeypatch, cache):
handler = ProbeHandler(wp_client, cache=cache)
async def fake_get(endpoint, **kwargs):
if endpoint == "airano-mcp/v1/upload-limits":
raise RuntimeError("404")
if endpoint == "":
return {"wp_max_upload_size": 4 * 1024 * 1024}
raise AssertionError(f"unexpected endpoint: {endpoint}")
monkeypatch.setattr(wp_client, "get", AsyncMock(side_effect=fake_get))
out = json.loads(await handler.probe_upload_limits())
assert out["source"] == "rest_index"
assert out["limits"]["wp_max_upload_size"] == 4 * 1024 * 1024
@pytest.mark.asyncio
async def test_total_failure_returns_empty_limits(wp_client, monkeypatch, cache):
handler = ProbeHandler(wp_client, cache=cache)
monkeypatch.setattr(wp_client, "get", AsyncMock(side_effect=RuntimeError("totally offline")))
out = json.loads(await handler.probe_upload_limits())
assert out["source"] == "unknown"
assert all(v is None for v in out["limits"].values())
def test_tool_spec_is_read_scope():
from plugins.wordpress.handlers.media_probe import get_tool_specifications
specs = get_tool_specifications()
assert len(specs) == 1
assert specs[0]["name"] == "probe_upload_limits"
assert specs[0]["scope"] == "read"
# F.5a.7: byte parser helpers ------------------------------------------------
class TestParsePhpSize:
@staticmethod
def _parse(v):
from plugins.wordpress.handlers.media_probe import parse_php_size
return parse_php_size(v)
def test_none(self):
assert self._parse(None) is None
def test_empty_string(self):
assert self._parse("") is None
def test_bare_integer_string(self):
assert self._parse("1024") == 1024
def test_megabytes_upper(self):
assert self._parse("64M") == 64 * 1024**2
def test_megabytes_lower(self):
assert self._parse("8m") == 8 * 1024**2
def test_gigabytes(self):
assert self._parse("2G") == 2 * 1024**3
def test_numeric_int(self):
assert self._parse(4096) == 4096
def test_unlimited_minus_one(self):
# PHP "-1" means no limit; treat as unknown.
assert self._parse("-1") is None
assert self._parse(-1) is None
def test_invalid(self):
assert self._parse("garbage") is None
class TestEffectiveCeiling:
def test_returns_min_of_populated(self):
from plugins.wordpress.handlers.media_probe import effective_upload_ceiling
limits = {
"upload_max_filesize": "8M",
"post_max_size": "64M",
"memory_limit": "256M",
"max_input_time": "60",
"wp_max_upload_size": 8388608,
}
# min(8MB, 64MB, 8388608 bytes) = 8388608 bytes
assert effective_upload_ceiling(limits) == 8 * 1024**2
def test_none_when_empty(self):
from plugins.wordpress.handlers.media_probe import effective_upload_ceiling
assert effective_upload_ceiling({}) is None
assert effective_upload_ceiling(None) is None