b3a7f98e5a
CI / E2E Cloudflare (4/8) (push) Failing after 0s
CI / E2E Cloudflare (8/8) (push) Failing after 1s
CI / Lint (push) Failing after 1s
Auto Extract / Extract (push) Failing after 4s
CI / Version Check (push) Failing after 9s
CI / Integration Tests (push) Failing after 1s
CI / E2E tests (1/8) (push) Failing after 1s
CI / E2E tests (2/8) (push) Failing after 2s
CI / E2E tests (3/8) (push) Failing after 2s
CI / Browser Tests (push) Failing after 1s
CI / E2E tests (5/8) (push) Failing after 1s
CI / E2E Cloudflare (2/8) (push) Failing after 2s
CI / Typecheck (push) Failing after 1s
CI / Changeset Validation (push) Failing after 2s
CI / E2E Cloudflare (5/8) (push) Failing after 1s
CI / E2E Cloudflare (6/8) (push) Failing after 1s
CI / E2E Cloudflare (7/8) (push) Failing after 1s
CodeQL / Analyze (javascript-typescript) (push) Failing after 1s
Format / Format (push) Failing after 0s
CodeQL / Analyze (actions) (push) Failing after 4s
CI / E2E tests (4/8) (push) Failing after 1s
CI / E2E tests (6/8) (push) Failing after 1s
CI / E2E tests (7/8) (push) Failing after 2s
CI / E2E tests (8/8) (push) Failing after 1s
CI / E2E Cloudflare (1/8) (push) Failing after 1s
CI / E2E Cloudflare (3/8) (push) Failing after 2s
Preview Releases / Publish Preview (push) Failing after 0s
zizmor / Run zizmor (push) Failing after 1s
Release / Release (push) Failing after 2s
CI / Smoke Tests (push) Failing after 5m36s
CI / Tests (push) Failing after 6m36s
Release / Sync Templates (push) Has been skipped
CI / E2E Tests (push) Has been cancelled
60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
|
|
import {
|
|
getMediaThumbnailUrl,
|
|
fallbackToOriginalThumbnail,
|
|
MEDIA_THUMBNAIL_WIDTH,
|
|
} from "../../src/lib/media-utils";
|
|
|
|
const LOCAL_IMAGE = "/_emdash/api/media/file/01ABC.jpg";
|
|
|
|
describe("getMediaThumbnailUrl", () => {
|
|
it("routes a local raster image through Astro's /_image endpoint", () => {
|
|
const result = getMediaThumbnailUrl(LOCAL_IMAGE, "image/jpeg");
|
|
expect(result.startsWith("/_image?")).toBe(true);
|
|
|
|
const url = new URL(result, window.location.origin);
|
|
expect(url.pathname).toBe("/_image");
|
|
expect(url.searchParams.get("href")).toBe(`${window.location.origin}${LOCAL_IMAGE}`);
|
|
expect(url.searchParams.get("w")).toBe(String(MEDIA_THUMBNAIL_WIDTH));
|
|
expect(url.searchParams.get("f")).toBe("webp");
|
|
});
|
|
|
|
it("honors a custom width", () => {
|
|
const result = getMediaThumbnailUrl(LOCAL_IMAGE, "image/png", 80);
|
|
const url = new URL(result, window.location.origin);
|
|
expect(url.searchParams.get("w")).toBe("80");
|
|
});
|
|
|
|
it("passes SVGs through unchanged (vector, nothing to downscale)", () => {
|
|
const svg = "/_emdash/api/media/file/01ABC.svg";
|
|
expect(getMediaThumbnailUrl(svg, "image/svg+xml")).toBe(svg);
|
|
});
|
|
|
|
it("passes non-image media through unchanged (an icon renders instead)", () => {
|
|
const pdf = "/_emdash/api/media/file/01ABC.pdf";
|
|
expect(getMediaThumbnailUrl(pdf, "application/pdf")).toBe(pdf);
|
|
});
|
|
|
|
it("passes external/provider URLs through unchanged (already a remote rendition)", () => {
|
|
const external = "https://images.example.com/photo.jpg";
|
|
expect(getMediaThumbnailUrl(external, "image/jpeg")).toBe(external);
|
|
});
|
|
});
|
|
|
|
describe("fallbackToOriginalThumbnail", () => {
|
|
it("swaps in the original URL on first error", () => {
|
|
const img = { dataset: {} as DOMStringMap, src: "/_image?href=...&w=400&f=webp" };
|
|
fallbackToOriginalThumbnail(img, LOCAL_IMAGE);
|
|
expect(img.src).toBe(LOCAL_IMAGE);
|
|
expect(img.dataset.thumbFallback).toBe("1");
|
|
});
|
|
|
|
it("does not loop if the original also fails", () => {
|
|
const img = { dataset: { thumbFallback: "1" } as DOMStringMap, src: LOCAL_IMAGE };
|
|
fallbackToOriginalThumbnail(img, "/some/other/url.jpg");
|
|
// Guard short-circuits: src is left untouched.
|
|
expect(img.src).toBe(LOCAL_IMAGE);
|
|
});
|
|
});
|