Files
emdash-patch-imageupload/packages/core/tests/unit/media/thumbnail.test.ts
kunthawat 2d1be52177 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
2026-05-03 10:44:54 +07:00

58 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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);
});
});