Emdash source with visual editor image upload fix
Fixes: 1. media.ts: wrap placeholder generation in try-catch 2. toolbar.ts: check r.ok, display error message in popover
This commit is contained in:
420
packages/core/tests/unit/media/normalize.test.ts
Normal file
420
packages/core/tests/unit/media/normalize.test.ts
Normal file
@@ -0,0 +1,420 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
|
||||
import { normalizeMediaValue } from "../../../src/media/normalize.js";
|
||||
import type { MediaProvider, MediaProviderItem } from "../../../src/media/types.js";
|
||||
|
||||
function mockProvider(getResult: MediaProviderItem | null = null): MediaProvider {
|
||||
return {
|
||||
list: vi.fn().mockResolvedValue({ items: [], nextCursor: undefined }),
|
||||
get: vi.fn().mockResolvedValue(getResult),
|
||||
getEmbed: vi.fn().mockReturnValue({ type: "image", src: "/test" }),
|
||||
};
|
||||
}
|
||||
|
||||
function getProvider(
|
||||
providers: Record<string, MediaProvider>,
|
||||
): (id: string) => MediaProvider | undefined {
|
||||
return (id: string) => providers[id];
|
||||
}
|
||||
|
||||
describe("normalizeMediaValue", () => {
|
||||
it("returns null for null input", async () => {
|
||||
const result = await normalizeMediaValue(null, getProvider({}));
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for undefined input", async () => {
|
||||
const result = await normalizeMediaValue(undefined, getProvider({}));
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("converts bare HTTP URL to external MediaValue", async () => {
|
||||
const result = await normalizeMediaValue("https://example.com/photo.jpg", getProvider({}));
|
||||
expect(result).toEqual({
|
||||
provider: "external",
|
||||
id: "",
|
||||
src: "https://example.com/photo.jpg",
|
||||
});
|
||||
});
|
||||
|
||||
it("converts bare HTTPS URL to external MediaValue", async () => {
|
||||
const result = await normalizeMediaValue("http://example.com/photo.jpg", getProvider({}));
|
||||
expect(result).toEqual({
|
||||
provider: "external",
|
||||
id: "",
|
||||
src: "http://example.com/photo.jpg",
|
||||
});
|
||||
});
|
||||
|
||||
it("converts bare internal media URL to full local MediaValue via provider", async () => {
|
||||
const providerItem: MediaProviderItem = {
|
||||
id: "01ABC",
|
||||
filename: "photo.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
alt: "A photo",
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
};
|
||||
const local = mockProvider(providerItem);
|
||||
|
||||
const result = await normalizeMediaValue(
|
||||
"/_emdash/api/media/file/01ABC.jpg",
|
||||
getProvider({ local }),
|
||||
);
|
||||
|
||||
expect(local.get).toHaveBeenCalledWith("01ABC.jpg");
|
||||
expect(result).toEqual({
|
||||
provider: "local",
|
||||
id: "01ABC",
|
||||
filename: "photo.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
alt: "A photo",
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back to external for internal URL when local provider unavailable", async () => {
|
||||
const result = await normalizeMediaValue("/_emdash/api/media/file/01ABC.jpg", getProvider({}));
|
||||
expect(result).toEqual({
|
||||
provider: "external",
|
||||
id: "",
|
||||
src: "/_emdash/api/media/file/01ABC.jpg",
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back to external for internal URL when provider.get returns null", async () => {
|
||||
const local = mockProvider(null);
|
||||
const result = await normalizeMediaValue(
|
||||
"/_emdash/api/media/file/01ABC.jpg",
|
||||
getProvider({ local }),
|
||||
);
|
||||
expect(result).toEqual({
|
||||
provider: "external",
|
||||
id: "",
|
||||
src: "/_emdash/api/media/file/01ABC.jpg",
|
||||
});
|
||||
});
|
||||
|
||||
it("fills missing dimensions from local provider", async () => {
|
||||
const providerItem: MediaProviderItem = {
|
||||
id: "01ABC",
|
||||
filename: "photo.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
};
|
||||
const local = mockProvider(providerItem);
|
||||
|
||||
const result = await normalizeMediaValue(
|
||||
{
|
||||
provider: "local",
|
||||
id: "01ABC",
|
||||
alt: "My photo",
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
},
|
||||
getProvider({ local }),
|
||||
);
|
||||
|
||||
expect(local.get).toHaveBeenCalledWith("01ABC");
|
||||
expect(result).toMatchObject({
|
||||
provider: "local",
|
||||
id: "01ABC",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
alt: "My photo",
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
});
|
||||
});
|
||||
|
||||
it("fills missing storageKey from local provider", async () => {
|
||||
const providerItem: MediaProviderItem = {
|
||||
id: "01ABC",
|
||||
filename: "photo.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
};
|
||||
const local = mockProvider(providerItem);
|
||||
|
||||
const result = await normalizeMediaValue(
|
||||
{
|
||||
provider: "local",
|
||||
id: "01ABC",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
},
|
||||
getProvider({ local }),
|
||||
);
|
||||
|
||||
expect(local.get).toHaveBeenCalledWith("01ABC");
|
||||
expect(result).toMatchObject({
|
||||
provider: "local",
|
||||
id: "01ABC",
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
});
|
||||
});
|
||||
|
||||
it("fills missing mimeType and filename from local provider", async () => {
|
||||
const providerItem: MediaProviderItem = {
|
||||
id: "01ABC",
|
||||
filename: "photo.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
};
|
||||
const local = mockProvider(providerItem);
|
||||
|
||||
const result = await normalizeMediaValue(
|
||||
{
|
||||
provider: "local",
|
||||
id: "01ABC",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
},
|
||||
getProvider({ local }),
|
||||
);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
filename: "photo.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
});
|
||||
});
|
||||
|
||||
it("fills dimensions from external provider", async () => {
|
||||
const providerItem: MediaProviderItem = {
|
||||
id: "cf-abc123",
|
||||
filename: "hero.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
meta: { variants: ["public"] },
|
||||
};
|
||||
const cfImages = mockProvider(providerItem);
|
||||
|
||||
const result = await normalizeMediaValue(
|
||||
{
|
||||
provider: "cloudflare-images",
|
||||
id: "cf-abc123",
|
||||
alt: "Hero banner",
|
||||
previewUrl: "https://imagedelivery.net/hash/cf-abc123/w=400",
|
||||
},
|
||||
getProvider({ "cloudflare-images": cfImages }),
|
||||
);
|
||||
|
||||
expect(cfImages.get).toHaveBeenCalledWith("cf-abc123");
|
||||
expect(result).toMatchObject({
|
||||
provider: "cloudflare-images",
|
||||
id: "cf-abc123",
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
alt: "Hero banner",
|
||||
previewUrl: "https://imagedelivery.net/hash/cf-abc123/w=400",
|
||||
});
|
||||
});
|
||||
|
||||
it("does not call provider when dimensions already present", async () => {
|
||||
const cfImages = mockProvider(null);
|
||||
|
||||
const value = {
|
||||
provider: "cloudflare-images",
|
||||
id: "cf-abc123",
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
filename: "hero.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
alt: "Hero banner",
|
||||
previewUrl: "https://imagedelivery.net/hash/cf-abc123/w=400",
|
||||
meta: { variants: ["public"] },
|
||||
};
|
||||
|
||||
const result = await normalizeMediaValue(value, getProvider({ "cloudflare-images": cfImages }));
|
||||
|
||||
expect(cfImages.get).not.toHaveBeenCalled();
|
||||
expect(result).toEqual(value);
|
||||
});
|
||||
|
||||
it("preserves caller alt over provider alt", async () => {
|
||||
const providerItem: MediaProviderItem = {
|
||||
id: "01ABC",
|
||||
filename: "photo.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
alt: "Provider alt text",
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
};
|
||||
const local = mockProvider(providerItem);
|
||||
|
||||
const result = await normalizeMediaValue(
|
||||
{
|
||||
provider: "local",
|
||||
id: "01ABC",
|
||||
alt: "User alt text",
|
||||
},
|
||||
getProvider({ local }),
|
||||
);
|
||||
|
||||
expect(result!.alt).toBe("User alt text");
|
||||
});
|
||||
|
||||
it("uses provider alt when caller alt is not set", async () => {
|
||||
const providerItem: MediaProviderItem = {
|
||||
id: "01ABC",
|
||||
filename: "photo.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
alt: "Provider alt text",
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
};
|
||||
const local = mockProvider(providerItem);
|
||||
|
||||
const result = await normalizeMediaValue(
|
||||
{
|
||||
provider: "local",
|
||||
id: "01ABC",
|
||||
},
|
||||
getProvider({ local }),
|
||||
);
|
||||
|
||||
expect(result!.alt).toBe("Provider alt text");
|
||||
});
|
||||
|
||||
it("returns value as-is for unknown provider", async () => {
|
||||
const value = {
|
||||
provider: "some-unknown-provider",
|
||||
id: "item-123",
|
||||
width: 800,
|
||||
height: 600,
|
||||
alt: "Some image",
|
||||
};
|
||||
|
||||
const result = await normalizeMediaValue(value, getProvider({}));
|
||||
expect(result).toEqual(value);
|
||||
});
|
||||
|
||||
it("does not fail when provider.get returns null", async () => {
|
||||
const local = mockProvider(null);
|
||||
|
||||
const value = {
|
||||
provider: "local",
|
||||
id: "01ABC",
|
||||
alt: "My photo",
|
||||
};
|
||||
|
||||
const result = await normalizeMediaValue(value, getProvider({ local }));
|
||||
expect(result).toEqual(value);
|
||||
});
|
||||
|
||||
it("does not fail when provider has no get method", async () => {
|
||||
const local: MediaProvider = {
|
||||
list: vi.fn().mockResolvedValue({ items: [] }),
|
||||
getEmbed: vi.fn().mockReturnValue({ type: "image", src: "/test" }),
|
||||
// no get method
|
||||
};
|
||||
|
||||
const value = {
|
||||
provider: "local",
|
||||
id: "01ABC",
|
||||
alt: "My photo",
|
||||
};
|
||||
|
||||
const result = await normalizeMediaValue(value, getProvider({ local }));
|
||||
expect(result).toEqual(value);
|
||||
});
|
||||
|
||||
it("returns external value with src as-is (no dimension detection)", async () => {
|
||||
const value = {
|
||||
provider: "external",
|
||||
id: "",
|
||||
src: "https://example.com/photo.jpg",
|
||||
alt: "A photo",
|
||||
width: 800,
|
||||
height: 600,
|
||||
};
|
||||
|
||||
const result = await normalizeMediaValue(value, getProvider({}));
|
||||
expect(result).toEqual(value);
|
||||
});
|
||||
|
||||
it("does not call provider for external values without dimensions", async () => {
|
||||
const value = {
|
||||
provider: "external",
|
||||
id: "",
|
||||
src: "https://example.com/photo.jpg",
|
||||
alt: "A photo",
|
||||
};
|
||||
|
||||
const result = await normalizeMediaValue(value, getProvider({}));
|
||||
expect(result).toEqual(value);
|
||||
});
|
||||
|
||||
it("strips src from local media values", async () => {
|
||||
const providerItem: MediaProviderItem = {
|
||||
id: "01ABC",
|
||||
filename: "photo.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
};
|
||||
const local = mockProvider(providerItem);
|
||||
|
||||
const result = await normalizeMediaValue(
|
||||
{
|
||||
provider: "local",
|
||||
id: "01ABC",
|
||||
src: "/_emdash/api/media/file/01ABC.jpg",
|
||||
alt: "My photo",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
},
|
||||
getProvider({ local }),
|
||||
);
|
||||
|
||||
// src should be removed for local media - it's derived at display time
|
||||
expect(result!.src).toBeUndefined();
|
||||
});
|
||||
|
||||
it("defaults provider to local when not specified", async () => {
|
||||
const providerItem: MediaProviderItem = {
|
||||
id: "01ABC",
|
||||
filename: "photo.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
width: 1200,
|
||||
height: 800,
|
||||
meta: { storageKey: "01ABC.jpg" },
|
||||
};
|
||||
const local = mockProvider(providerItem);
|
||||
|
||||
const result = await normalizeMediaValue({ id: "01ABC" }, getProvider({ local }));
|
||||
|
||||
expect(result!.provider).toBe("local");
|
||||
expect(local.get).toHaveBeenCalledWith("01ABC");
|
||||
});
|
||||
|
||||
it("handles provider.get throwing gracefully", async () => {
|
||||
const local: MediaProvider = {
|
||||
list: vi.fn().mockResolvedValue({ items: [] }),
|
||||
get: vi.fn().mockRejectedValue(new Error("DB error")),
|
||||
getEmbed: vi.fn().mockReturnValue({ type: "image", src: "/test" }),
|
||||
};
|
||||
|
||||
const value = {
|
||||
provider: "local",
|
||||
id: "01ABC",
|
||||
alt: "My photo",
|
||||
};
|
||||
|
||||
const result = await normalizeMediaValue(value, getProvider({ local }));
|
||||
expect(result).toEqual(value);
|
||||
});
|
||||
});
|
||||
143
packages/core/tests/unit/media/placeholder.test.ts
Normal file
143
packages/core/tests/unit/media/placeholder.test.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
|
||||
import { generatePlaceholder } from "../../../src/media/placeholder.js";
|
||||
|
||||
const CSS_RGB_PATTERN = /^rgb\(\d+,\s?\d+,\s?\d+\)$/;
|
||||
|
||||
/** Minimal 4x4 solid red JPEG */
|
||||
const JPEG_4x4 = Buffer.from(
|
||||
"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAAEAAQDAREAAhEBAxEB/8QAFAABAAAAAAAAAAAAAAAAAAAACP/EABQQAQAAAAAAAAAAAAAAAAAAAAD/xAAVAQEBAAAAAAAAAAAAAAAAAAAHCf/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/ADoDFU3/2Q==",
|
||||
"base64",
|
||||
);
|
||||
|
||||
/** Minimal 4x4 solid red PNG */
|
||||
const PNG_4x4 = Buffer.from(
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAQAAAAEAQMAAACTPww9AAAAIGNIUk0AAHomAACAhAAA+gAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAAGUExURf8AAP///0EdNBEAAAABYktHRAH/Ai3eAAAAB3RJTUUH6gIcETMVn1ZhnwAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyNi0wMi0yOFQxNzo1MToyMCswMDowMJE6EiQAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMjYtMDItMjhUMTc6NTE6MjArMDA6MDDgZ6qYAAAAKHRFWHRkYXRlOnRpbWVzdGFtcAAyMDI2LTAyLTI4VDE3OjUxOjIwKzAwOjAwt3KLRwAAAAtJREFUCNdjYIAAAAAIAAEvIN0xAAAAAElFTkSuQmCC",
|
||||
"base64",
|
||||
);
|
||||
|
||||
/** 100x100 solid blue JPEG (for downsampling test) */
|
||||
const JPEG_100x100 = Buffer.from(
|
||||
"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCABkAGQDAREAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAn/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFgEBAQEAAAAAAAAAAAAAAAAAAAYJ/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMRAD8Anu1TQ4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//2Q==",
|
||||
"base64",
|
||||
);
|
||||
|
||||
describe("generatePlaceholder", () => {
|
||||
it("generates blurhash and dominantColor from a JPEG", async () => {
|
||||
const result = await generatePlaceholder(new Uint8Array(JPEG_4x4), "image/jpeg");
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.blurhash).toBeTruthy();
|
||||
expect(typeof result!.blurhash).toBe("string");
|
||||
expect(result!.dominantColor).toBeTruthy();
|
||||
expect(typeof result!.dominantColor).toBe("string");
|
||||
});
|
||||
|
||||
it("generates blurhash and dominantColor from a PNG", async () => {
|
||||
const result = await generatePlaceholder(new Uint8Array(PNG_4x4), "image/png");
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.blurhash).toBeTruthy();
|
||||
expect(result!.dominantColor).toBeTruthy();
|
||||
});
|
||||
|
||||
it("returns a valid CSS color string for dominantColor", async () => {
|
||||
const result = await generatePlaceholder(new Uint8Array(JPEG_4x4), "image/jpeg");
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
// Should be rgb() format from rgbColorToCssString
|
||||
expect(result!.dominantColor).toMatch(CSS_RGB_PATTERN);
|
||||
});
|
||||
|
||||
it("returns null for non-image MIME types", async () => {
|
||||
const buffer = new Uint8Array([0, 1, 2, 3]);
|
||||
const result = await generatePlaceholder(buffer, "application/pdf");
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for unsupported image types", async () => {
|
||||
const buffer = new Uint8Array([0, 1, 2, 3]);
|
||||
const result = await generatePlaceholder(buffer, "image/svg+xml");
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for corrupt image data", async () => {
|
||||
const buffer = new Uint8Array([0xff, 0xd8, 0xff, 0xe0, 0, 0, 0]);
|
||||
const result = await generatePlaceholder(buffer, "image/jpeg");
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("handles larger images by downsampling", async () => {
|
||||
const result = await generatePlaceholder(new Uint8Array(JPEG_100x100), "image/jpeg");
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.blurhash).toBeTruthy();
|
||||
// Blurhash string length should be reasonable (not huge from 100x100)
|
||||
expect(result!.blurhash.length).toBeLessThan(50);
|
||||
});
|
||||
|
||||
it("returns null when image dimensions from headers exceed memory budget", async () => {
|
||||
// Minimal valid JPEG with SOF0 declaring 5000x4000 dimensions.
|
||||
// SOF0 marker (FFC0) stores height (2 bytes) then width (2 bytes).
|
||||
// 5000×4000×4 = 80 MB > 32 MB threshold.
|
||||
const sof0 = new Uint8Array([
|
||||
0xff,
|
||||
0xd8, // SOI
|
||||
0xff,
|
||||
0xe0,
|
||||
0x00,
|
||||
0x10, // APP0 marker + length
|
||||
0x4a,
|
||||
0x46,
|
||||
0x49,
|
||||
0x46,
|
||||
0x00, // "JFIF\0"
|
||||
0x01,
|
||||
0x01,
|
||||
0x00,
|
||||
0x00,
|
||||
0x01,
|
||||
0x00,
|
||||
0x01,
|
||||
0x00,
|
||||
0x00, // JFIF fields
|
||||
0xff,
|
||||
0xc0,
|
||||
0x00,
|
||||
0x0b, // SOF0 marker + length
|
||||
0x08, // precision
|
||||
0x0f,
|
||||
0xa0, // height = 4000
|
||||
0x13,
|
||||
0x88, // width = 5000
|
||||
0x01, // number of components
|
||||
0x01,
|
||||
0x11,
|
||||
0x00, // component
|
||||
]);
|
||||
const result = await generatePlaceholder(sof0, "image/jpeg");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null when fallback dimensions exceed memory budget", async () => {
|
||||
// Unrecognizable buffer — image-size can't parse it, so fallback dims are used
|
||||
const buffer = new Uint8Array([0x00, 0x01, 0x02, 0x03]);
|
||||
const result = await generatePlaceholder(buffer, "image/jpeg", {
|
||||
width: 5000,
|
||||
height: 4000,
|
||||
});
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("still generates placeholder for small images with dimensions param", async () => {
|
||||
const result = await generatePlaceholder(new Uint8Array(JPEG_4x4), "image/jpeg", {
|
||||
width: 4,
|
||||
height: 4,
|
||||
});
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.blurhash).toBeTruthy();
|
||||
});
|
||||
});
|
||||
57
packages/core/tests/unit/media/thumbnail.test.ts
Normal file
57
packages/core/tests/unit/media/thumbnail.test.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { THUMBNAIL_MAX_DIMENSION, computeThumbnailSize } from "../../../src/media/thumbnail.js";
|
||||
|
||||
describe("computeThumbnailSize", () => {
|
||||
it("scales a square image to the max dimension", () => {
|
||||
expect(computeThumbnailSize(5000, 5000)).toEqual({
|
||||
width: THUMBNAIL_MAX_DIMENSION,
|
||||
height: THUMBNAIL_MAX_DIMENSION,
|
||||
});
|
||||
});
|
||||
|
||||
it("scales a wide image to fit within the bounding box", () => {
|
||||
const result = computeThumbnailSize(4000, 2000);
|
||||
expect(result.width).toBe(THUMBNAIL_MAX_DIMENSION);
|
||||
expect(result.height).toBe(THUMBNAIL_MAX_DIMENSION / 2);
|
||||
});
|
||||
|
||||
it("scales a tall image to fit within the bounding box", () => {
|
||||
const result = computeThumbnailSize(2000, 4000);
|
||||
expect(result.width).toBe(THUMBNAIL_MAX_DIMENSION / 2);
|
||||
expect(result.height).toBe(THUMBNAIL_MAX_DIMENSION);
|
||||
});
|
||||
|
||||
it("clamps extreme tall aspect ratios to the bounding box", () => {
|
||||
// Without clamping, naive code would produce a 64×537600 canvas.
|
||||
const result = computeThumbnailSize(100, 840_000);
|
||||
expect(result.width).toBeLessThanOrEqual(THUMBNAIL_MAX_DIMENSION);
|
||||
expect(result.height).toBeLessThanOrEqual(THUMBNAIL_MAX_DIMENSION);
|
||||
expect(result.width).toBeGreaterThanOrEqual(1);
|
||||
expect(result.height).toBe(THUMBNAIL_MAX_DIMENSION);
|
||||
});
|
||||
|
||||
it("clamps extreme wide aspect ratios to the bounding box", () => {
|
||||
const result = computeThumbnailSize(840_000, 100);
|
||||
expect(result.width).toBe(THUMBNAIL_MAX_DIMENSION);
|
||||
expect(result.height).toBeGreaterThanOrEqual(1);
|
||||
expect(result.height).toBeLessThanOrEqual(THUMBNAIL_MAX_DIMENSION);
|
||||
});
|
||||
|
||||
it("never upscales smaller images", () => {
|
||||
expect(computeThumbnailSize(10, 20)).toEqual({ width: 10, height: 20 });
|
||||
expect(computeThumbnailSize(1, 1)).toEqual({ width: 1, height: 1 });
|
||||
});
|
||||
|
||||
it("returns a 1x1 fallback for zero or negative dimensions", () => {
|
||||
expect(computeThumbnailSize(0, 100)).toEqual({ width: 1, height: 1 });
|
||||
expect(computeThumbnailSize(100, 0)).toEqual({ width: 1, height: 1 });
|
||||
expect(computeThumbnailSize(-5, 10)).toEqual({ width: 1, height: 1 });
|
||||
});
|
||||
|
||||
it("rounds fractional dimensions", () => {
|
||||
const result = computeThumbnailSize(300, 199);
|
||||
expect(Number.isInteger(result.width)).toBe(true);
|
||||
expect(Number.isInteger(result.height)).toBe(true);
|
||||
});
|
||||
});
|
||||
133
packages/core/tests/unit/media/url.test.ts
Normal file
133
packages/core/tests/unit/media/url.test.ts
Normal file
@@ -0,0 +1,133 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
|
||||
import {
|
||||
buildRenderMediaUrl,
|
||||
createPublicMediaUrlResolver,
|
||||
resolvePublicMediaUrl,
|
||||
} from "../../../src/media/url.js";
|
||||
import type { Storage } from "../../../src/storage/types.js";
|
||||
|
||||
function storageWith(publicUrl: string): Storage {
|
||||
return {
|
||||
upload: async () => ({ key: "", url: "", size: 0 }),
|
||||
download: async () => {
|
||||
throw new Error("not used");
|
||||
},
|
||||
delete: async () => {},
|
||||
exists: async () => true,
|
||||
list: async () => ({ files: [] }),
|
||||
getSignedUploadUrl: async () => {
|
||||
throw new Error("not used");
|
||||
},
|
||||
getPublicUrl: (key) => `${publicUrl}/${key}`,
|
||||
};
|
||||
}
|
||||
|
||||
describe("resolvePublicMediaUrl", () => {
|
||||
it("returns an empty string when storageKey is empty", () => {
|
||||
expect(resolvePublicMediaUrl(null, "")).toBe("");
|
||||
});
|
||||
|
||||
it("uses the proxied media endpoint when no storage is provided", () => {
|
||||
expect(resolvePublicMediaUrl(null, "01ABC.jpg")).toBe("/_emdash/api/media/file/01ABC.jpg");
|
||||
});
|
||||
|
||||
it("uses storage.getPublicUrl when a storage adapter is provided", () => {
|
||||
const storage = storageWith("https://media.example.com");
|
||||
expect(resolvePublicMediaUrl(storage, "01ABC.jpg")).toBe("https://media.example.com/01ABC.jpg");
|
||||
});
|
||||
});
|
||||
|
||||
describe("createPublicMediaUrlResolver", () => {
|
||||
it("returns a closure that reuses the storage adapter", () => {
|
||||
const resolver = createPublicMediaUrlResolver(storageWith("https://media.example.com"));
|
||||
expect(resolver("01ABC.jpg")).toBe("https://media.example.com/01ABC.jpg");
|
||||
expect(resolver("01XYZ.png")).toBe("https://media.example.com/01XYZ.png");
|
||||
});
|
||||
|
||||
it("falls back to the internal proxy when no storage is given", () => {
|
||||
const resolver = createPublicMediaUrlResolver(null);
|
||||
expect(resolver("01ABC.jpg")).toBe("/_emdash/api/media/file/01ABC.jpg");
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildRenderMediaUrl", () => {
|
||||
const resolveCdn = (key: string) => `https://media.example.com/${key}`;
|
||||
|
||||
it("routes an explicit storageKey through resolve", () => {
|
||||
expect(buildRenderMediaUrl(resolveCdn, { storageKey: "01ABC.jpg" })).toBe(
|
||||
"https://media.example.com/01ABC.jpg",
|
||||
);
|
||||
});
|
||||
|
||||
it("uses the internal proxy for storageKey when resolve is absent", () => {
|
||||
expect(buildRenderMediaUrl(undefined, { storageKey: "01ABC.jpg" })).toBe(
|
||||
"/_emdash/api/media/file/01ABC.jpg",
|
||||
);
|
||||
});
|
||||
|
||||
it("rewrites an internal url via resolve so publicUrl is honored", () => {
|
||||
expect(
|
||||
buildRenderMediaUrl(resolveCdn, {
|
||||
url: "/_emdash/api/media/file/01ABC.jpg",
|
||||
id: "01ABC",
|
||||
}),
|
||||
).toBe("https://media.example.com/01ABC.jpg");
|
||||
});
|
||||
|
||||
it("leaves an external url untouched even when resolve is given", () => {
|
||||
expect(
|
||||
buildRenderMediaUrl(resolveCdn, {
|
||||
url: "https://other-cdn.example.com/01ABC.jpg",
|
||||
}),
|
||||
).toBe("https://other-cdn.example.com/01ABC.jpg");
|
||||
});
|
||||
|
||||
it("returns an internal url as-is when no resolve is given", () => {
|
||||
expect(
|
||||
buildRenderMediaUrl(undefined, {
|
||||
url: "/_emdash/api/media/file/01ABC.jpg",
|
||||
}),
|
||||
).toBe("/_emdash/api/media/file/01ABC.jpg");
|
||||
});
|
||||
|
||||
it("uses the internal proxy for a bare id", () => {
|
||||
expect(buildRenderMediaUrl(resolveCdn, { id: "01ABC" })).toBe("/_emdash/api/media/file/01ABC");
|
||||
});
|
||||
|
||||
it("returns an empty string when no fields are usable", () => {
|
||||
expect(buildRenderMediaUrl(resolveCdn, {})).toBe("");
|
||||
});
|
||||
|
||||
it("does not rewrite a url that only shares the media prefix", () => {
|
||||
expect(
|
||||
buildRenderMediaUrl(resolveCdn, {
|
||||
url: "/_emdash/api/media/file-list/01ABC.jpg",
|
||||
}),
|
||||
).toBe("/_emdash/api/media/file-list/01ABC.jpg");
|
||||
});
|
||||
|
||||
it("passes an internal url through when the captured key contains a slash", () => {
|
||||
expect(
|
||||
buildRenderMediaUrl(resolveCdn, {
|
||||
url: "/_emdash/api/media/file/../other-tenant/secret.pdf",
|
||||
}),
|
||||
).toBe("/_emdash/api/media/file/../other-tenant/secret.pdf");
|
||||
});
|
||||
|
||||
it("passes an internal url through when the captured key contains a query string", () => {
|
||||
expect(
|
||||
buildRenderMediaUrl(resolveCdn, {
|
||||
url: "/_emdash/api/media/file/01ABC.jpg?v=2",
|
||||
}),
|
||||
).toBe("/_emdash/api/media/file/01ABC.jpg?v=2");
|
||||
});
|
||||
|
||||
it("passes an internal url through when the captured key is percent-encoded", () => {
|
||||
expect(
|
||||
buildRenderMediaUrl(resolveCdn, {
|
||||
url: "/_emdash/api/media/file/01%2FABC.jpg",
|
||||
}),
|
||||
).toBe("/_emdash/api/media/file/01%2FABC.jpg");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user