Files
TyperBodyandmultica-agent a91210c43b fix(plugins/misc/StickerHub): copy the URL the page actually holds, inside the real sandbox
Copy was broken twice over. `langbot.api()` already resolves the unwrapped
payload (PageResponse.ok -> HTTP data -> resp.data.data -> the SDK's
resolve(data.data)), so `item.data.url` was always `undefined` and the page
copied an empty string. `writeText('')` resolves without throwing, so it also
never reached the fallback: the UI said "copied" while the clipboard stayed
empty.

The iframe is worse than assumed. It is a `blob:` document under
`sandbox="allow-scripts allow-forms"`, so its origin is opaque: `location.origin`
is "null", relative asset URLs do not load, and `navigator.clipboard` rejects
with NotAllowedError. `/list` returns relative URLs while `public_base_url` is
empty, so even a correct value would have pasted as an unusable path. URLs are
now made absolute against `location.ancestorOrigins[0]` before either the
clipboard or an `<img src>`, and copy reads `item.url` from the list cache
instead of making a redundant `/item` round trip.

Every other call had the same layer mistake: `res.error` is never set on a
resolved value, because failures reject the promise, so no failure ever
surfaced. All call sites now catch, and an empty URL toasts an error rather
than claiming success.

Verified in a real blob: iframe with the real sandbox: relative url -> absolute
-> clipboard read back byte for byte, and the same URL loads as the preview
(0 `/image` calls). Also passes with `navigator.clipboard` removed, proving the
execCommand tier is what carries it.

The host is untouched now, so this is plugin-only; the `allow-modals` flip is
dropped and the clear confirmation stays in-page.

Co-authored-by: multica-agent <github@multica.ai>
2026-11-07 15:09:17 +08:00

112 lines
3.5 KiB
Python

"""Shared fixtures for the StickerHub test suite.
The plugin is driven through a stub Host that mirrors the real plugin-storage
transport (``SET/GET/DELETE_PLUGIN_STORAGE`` / ``GET_PLUGIN_STORAGE_KEYS``)
without needing a live LangBot instance.
"""
from __future__ import annotations
import base64
import os
import sys
from pathlib import Path
from types import SimpleNamespace
import pytest
PLUGIN_ROOT = Path(__file__).resolve().parents[1]
if str(PLUGIN_ROOT) not in sys.path:
sys.path.insert(0, str(PLUGIN_ROOT))
# A shared-runtime worker without an invocation binding must refuse tenant
# state. Keep the tests on the dedicated profile unless they bind explicitly.
os.environ.pop("LANGBOT_PLUGIN_RUNTIME_PROFILE", None)
from langbot_plugin.entities.io.actions.enums import PluginToRuntimeAction as Action # noqa: E402
from langbot_plugin.entities.io.errors import ActionCallError # noqa: E402
from main import StickerHub # noqa: E402
# A tiny PNG-like payload: only the magic header is needed for mime sniffing.
PNG_BYTES = b"\x89PNG\r\n\x1a\n" + b"\x00" * 16
JPEG_BYTES = b"\xff\xd8\xff\xe0" + b"\x00" * 16
class FakeHost:
"""Stub plugin-storage backend with call recording."""
def __init__(self) -> None:
self.data: dict[str, bytes] = {}
self.calls: list[tuple[str, dict]] = []
self.failures: set[str] = set()
async def call_action(self, action, data, *args, **kwargs):
self.calls.append((action, dict(data)))
if action in self.failures:
raise OSError(f"stub refused {action}")
key = data.get("key")
if action == Action.GET_PLUGIN_STORAGE_KEYS:
return {"keys": list(self.data)}
if action == Action.GET_PLUGIN_STORAGE:
if key not in self.data:
# The real Host fails the action for a missing key.
raise ActionCallError(f"Storage with key {key} not found")
return {"value_base64": base64.b64encode(self.data[key]).decode("ascii")}
if action == Action.SET_PLUGIN_STORAGE:
self.data[key] = base64.b64decode(data["value_base64"])
return {}
if action == Action.DELETE_PLUGIN_STORAGE:
self.data.pop(key, None)
return {}
raise AssertionError(f"unexpected action {action}")
def bind(plugin: StickerHub, installation_uuid: str = "installation-a") -> StickerHub:
"""Give one plugin object a trusted installation binding."""
plugin.get_installation_binding = lambda: SimpleNamespace(
instance_uuid="instance-a",
workspace_uuid="workspace-a",
installation_uuid=installation_uuid,
)
return plugin
def build_plugin(
config: dict | None = None,
*,
host: FakeHost | None = None,
installation_uuid: str | None = "installation-a",
) -> tuple[StickerHub, FakeHost]:
host = host or FakeHost()
plugin = StickerHub()
plugin.config = dict(config or {})
plugin.plugin_runtime_handler = host
if installation_uuid is not None:
bind(plugin, installation_uuid)
return plugin, host
@pytest.fixture
def make_plugin():
return build_plugin
@pytest.fixture
def host():
return FakeHost()
@pytest.fixture(autouse=True)
def _isolate_cwd(tmp_path, monkeypatch):
"""Run every test from its own temp dir.
StickerHub materialises served copies into ``assets/stickers/``; keeping
that write inside the test's temporary directory avoids polluting the plugin
source tree.
"""
monkeypatch.chdir(tmp_path)
return tmp_path