The installed PWA now shows in Android's share sheet. A shared link, text, screenshot or file opens the new-session composer on it, with the text in the box and the files attached; nothing is sent. - manifest share_target: multipart POST /share (title, text, url, files */*) - public/share-sw.js: the service worker answers that POST itself, stashes the share in Cache Storage and 303s to /?share=<id>, so a share survives an expired Authelia session or a lab that doesn't answer for a moment - useShareTarget (read in GlobalSpawn): uploads the files, opens the composer (SpawnPrefill.files -> setFiles), ShareNotice covers the upload and offers Retry / Dismiss on failure - backend POST /share fallback for a client without the worker: the text goes to /?q=, no 405 - tests: useShareTarget.test.tsx, backend/test_share_target.py Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
70 lines
2.6 KiB
Python
70 lines
2.6 KiB
Python
"""The share target's server-side fallback (``POST /share`` in main.py): a
|
|
share the PWA's service worker didn't catch lands its text in the composer
|
|
as a ``?q=`` deep link instead of a 405. ``python -m unittest
|
|
test_share_target`` from ``backend/``."""
|
|
|
|
import os
|
|
import pathlib
|
|
import tempfile
|
|
import unittest
|
|
from urllib.parse import parse_qs, urlsplit
|
|
|
|
TMP = pathlib.Path(tempfile.mkdtemp())
|
|
for k, v in {"DB_PATH": TMP / "x.db", "TRANSCRIPTS_DIR": TMP / "t",
|
|
"WORKSPACE": TMP, "REPO_DIR": TMP, "ARCHIVE_DIR": TMP / "arch",
|
|
"META_PATH": TMP / "meta.json", "WORKERS_PATH": TMP / "w.json",
|
|
"WORKERS_TRANSCRIPTS_DIR": TMP / "mirror",
|
|
"CRON_PATH": TMP / "cron.json", "SIDECAR_TOKEN": "",
|
|
"PROFILES_PATH": TMP / "profiles.json", "PI_TRANSCRIPTS_DIR": ""}.items():
|
|
os.environ[k] = str(v)
|
|
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
|
|
import main # noqa: E402
|
|
|
|
|
|
def _q(location: str) -> str:
|
|
return parse_qs(urlsplit(location).query).get("q", [""])[0]
|
|
|
|
|
|
class ShareText(unittest.TestCase):
|
|
def test_title_and_link(self):
|
|
self.assertEqual(main._share_text("A video", "", "https://y.tube/x"), "A video\nhttps://y.tube/x")
|
|
|
|
def test_link_already_in_text(self):
|
|
self.assertEqual(
|
|
main._share_text("Some page", "Some page https://ex.am/ple", "https://ex.am/ple"),
|
|
"Some page https://ex.am/ple",
|
|
)
|
|
|
|
def test_title_that_is_the_link(self):
|
|
self.assertEqual(main._share_text("https://ex.am", "", "https://ex.am"), "https://ex.am")
|
|
|
|
def test_capped(self):
|
|
self.assertEqual(len(main._share_text("", "x" * 10_000, "")), main._SHARE_TEXT_MAX)
|
|
|
|
|
|
class ShareFallback(unittest.TestCase):
|
|
def setUp(self):
|
|
self.c = TestClient(main.app, follow_redirects=False)
|
|
|
|
def test_text_becomes_a_deep_link(self):
|
|
r = self.c.post("/share", files={"files": ("a.png", b"png", "image/png")},
|
|
data={"title": "Read this", "text": "", "url": "https://ex.am"})
|
|
self.assertEqual(r.status_code, 303)
|
|
self.assertEqual(_q(r.headers["location"]), "Read this\nhttps://ex.am")
|
|
|
|
def test_files_only_goes_home(self):
|
|
r = self.c.post("/share", files={"files": ("a.png", b"png", "image/png")})
|
|
self.assertEqual(r.status_code, 303)
|
|
self.assertEqual(r.headers["location"], "/")
|
|
|
|
def test_not_a_form(self):
|
|
r = self.c.post("/share", content=b"\x00", headers={"Content-Type": "multipart/form-data"})
|
|
self.assertEqual(r.status_code, 303)
|
|
self.assertEqual(r.headers["location"], "/")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|