Files
LangBot/plugins/misc/StickerHub/tests/test_packaging.py
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

59 lines
1.8 KiB
Python

"""The packaged artifact must keep an empty ``assets/stickers`` directory.
The installer materialises stickers into ``assets/stickers/``. ``lbp build``
honours glob patterns in ``.gitignore`` but not gitignore ``!`` negation, so a
bare ``assets/stickers/*`` drops ``.gitkeep`` too, the archived directory
vanishes, and every stable URL 404s on a fresh install.
"""
from __future__ import annotations
import fnmatch
from pathlib import Path
PLUGIN_ROOT = Path(__file__).resolve().parents[1]
GITIGNORE = PLUGIN_ROOT / ".gitignore"
GITKEEP = PLUGIN_ROOT / "assets" / "stickers" / ".gitkeep"
def patterns() -> list[str]:
"""Mirror ``lbp``'s parse: non-empty, non-comment lines only."""
return [
line.strip()
for line in GITIGNORE.read_text(encoding="utf-8").splitlines()
if line.strip() and not line.strip().startswith("#")
]
def packaged(relative_path: str) -> bool:
"""Mirror ``lbp``'s ``should_ignore``: any matching pattern drops the file."""
normalized = str(Path(relative_path)).replace("\\", "/")
for pattern in patterns():
if pattern.startswith("!"):
continue
if fnmatch.fnmatch(normalized, pattern):
return False
if fnmatch.fnmatch(Path(normalized).name, pattern):
return False
return True
def test_gitkeep_exists():
assert GITKEEP.is_file(), "an empty assets/stickers needs a tracked placeholder"
def test_gitkeep_survives_packaging():
assert packaged("assets/stickers/.gitkeep"), "the packager would drop .gitkeep"
def test_sticker_artifacts_stay_out_of_the_package():
for name in ("cat-abc123.png", "cat-abc123.jpg", "cat-abc123.webp"):
assert not packaged(f"assets/stickers/{name}")
assert not packaged("assets/stickers/cat-abc123.png.tmp")
def test_the_plugin_icon_is_packaged():
assert packaged("assets/icon.svg")