"""Unit tests for the preview-content policy module (``turnstone/core/preview.py``).
Pure-function coverage: kind resolution precedence (magic bytes → MIME hint →
extension → UTF-8 fallback), the explicit ``kind`` override lanes, base-href
injection, title extraction, and the per-MIME serving headers the route
attaches. The tool executor and the HTTP route are covered separately
(``test_open_preview_tool.py`` / ``test_server_attachments_endpoints.py``).
"""
from __future__ import annotations
from turnstone.core.attachments import IMAGE_SIZE_CAP, PDF_SIZE_CAP, TEXT_DOC_SIZE_CAP
from turnstone.core.preview import (
PREVIEW_BLOB_KIND,
PREVIEW_KINDS,
PREVIEW_SERVE_MIMES,
PREVIEW_SIZE_CAPS,
build_preview_descriptor,
inject_base_href,
page_title,
preview_response_headers,
resolve_preview_kind,
transcode_text,
)
PNG_1x1 = (
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01"
b"\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc\xfc\xcf"
b"\xc0\xc0\xc0\x00\x00\x00\x05\x00\x01\xa5\xf6E@\x00\x00\x00\x00IEND\xaeB`\x82"
)
PDF_MIN = b"%PDF-1.4 fake body"
HTML_DOC = b"
Acme Pricinghi"
class TestResolvePreviewKind:
def test_magic_bytes_win_over_everything(self):
# A PNG claiming to be CSV by both MIME and extension is an image.
assert resolve_preview_kind("text/csv", "data.csv", PNG_1x1) == ("image", "image/png")
assert resolve_preview_kind("text/plain", "doc.txt", PDF_MIN) == (
"pdf",
"application/pdf",
)
def test_mime_hint_html(self):
kind, mime = resolve_preview_kind("text/html; charset=iso-8859-1", "page", HTML_DOC)
assert kind == "web"
assert mime == "text/html; charset=utf-8"
def test_mime_hint_families(self):
assert resolve_preview_kind("text/csv", "x", b"a,b\n1,2")[0] == "table"
assert resolve_preview_kind("application/json", "x", b"[]") == (
"table",
"application/json",
)
assert resolve_preview_kind("text/markdown", "x", b"# hi")[0] == "markdown"
assert resolve_preview_kind("text/x-log", "x", b"line")[0] == "text"
def test_extension_fallback_when_no_mime(self):
assert resolve_preview_kind("", "report.html", HTML_DOC)[0] == "web"
assert resolve_preview_kind("", "data.tsv", b"a\tb")[0] == "table"
assert resolve_preview_kind("", "notes.md", b"# t")[0] == "markdown"
# URL tails strip query/fragment before the extension check.
assert resolve_preview_kind("", "https://x.io/a.csv?dl=1#f", b"a,b")[0] == "table"
def test_utf8_text_fallback(self):
assert resolve_preview_kind("", "LICENSE", b"MIT License") == (
"text",
"text/plain; charset=utf-8",
)
def test_binary_is_not_previewable(self):
assert resolve_preview_kind("", "blob.bin", b"\x00\x01\x02\x03" * 8) is None
# Text-DECLARED binary is misdeclared, not previewable text.
assert resolve_preview_kind("text/plain", "x", b"\x00\xff" * 8) is None
assert resolve_preview_kind("application/octet-stream", "x", b"\x00" * 32) is None
def test_override_validates_bytes(self):
# image override on non-image bytes fails rather than mislabeling.
assert resolve_preview_kind("", "x", b"not an image", "image") is None
assert resolve_preview_kind("", "x", PNG_1x1, "image") == ("image", "image/png")
assert resolve_preview_kind("", "x", b"not a pdf", "pdf") is None
# Text-family override on binary bytes fails.
assert resolve_preview_kind("", "x", b"\x00\x01", "text") is None
def test_override_forces_view(self):
# kind='text' on an HTML doc = view source.
assert resolve_preview_kind("text/html", "p.html", HTML_DOC, "text")[0] == "text"
# kind='table' keeps the real payload type for the client parser.
assert resolve_preview_kind("application/json", "d", b"[1]", "table") == (
"table",
"application/json",
)
assert resolve_preview_kind("", "d.tsv", b"a\tb", "table") == (
"table",
"text/tab-separated-values; charset=utf-8",
)
assert resolve_preview_kind("", "d.txt", b"a,b", "table") == (
"table",
"text/csv; charset=utf-8",
)
def test_unknown_override_rejected(self):
assert resolve_preview_kind("text/plain", "x", b"hi", "hologram") is None
class TestHtmlHelpers:
def test_base_href_inserted_after_head(self):
out = inject_base_href("", "https://a.io/p/q")
assert out.startswith('')
def test_base_href_prepended_without_head(self):
out = inject_base_href("bare
", "https://a.io/")
assert out.startswith('')
def test_existing_base_untouched(self):
doc = ''
assert inject_base_href(doc, "https://other/") == doc
def test_base_href_attribute_escaped(self):
out = inject_base_href("", 'https://a.io/">')
assert "