Files
buzz-sheet/lib/media/validation.test.ts
T
2026-09-06 17:16:38 +07:00

36 lines
1.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
hasValidImageSignature,
MAX_MEDIA_BYTES,
mediaUploadSchema,
parseByteRange,
safeObjectFileName,
} from "./validation";
describe("media validation", () => {
it("accepts only supported images no larger than 100 MB", () => {
expect(mediaUploadSchema.safeParse({ fileName: "a.png", mimeType: "image/png", byteSize: MAX_MEDIA_BYTES }).success).toBe(true);
expect(mediaUploadSchema.safeParse({ fileName: "a.svg", mimeType: "image/svg+xml", byteSize: 10 }).success).toBe(false);
expect(mediaUploadSchema.safeParse({ fileName: "a.png", mimeType: "image/png", byteSize: MAX_MEDIA_BYTES + 1 }).success).toBe(false);
});
it("sanitizes object names without losing Thai labels", () => {
expect(safeObjectFileName("../ภาพ ตัวอย่าง.png")).toBe("ภาพ-ตัวอย่าง.png");
expect(safeObjectFileName("///")).toBe("image");
});
it("checks magic bytes against the declared MIME type", () => {
expect(hasValidImageSignature(Uint8Array.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), "image/png")).toBe(true);
expect(hasValidImageSignature(Uint8Array.from([0xff, 0xd8, 0xff]), "image/jpeg")).toBe(true);
expect(hasValidImageSignature(Uint8Array.from([0xff, 0xd8, 0xff]), "image/png")).toBe(false);
});
it("parses bounded and suffix byte ranges", () => {
expect(parseByteRange("bytes=10-19", 100)).toEqual({ start: 10, end: 19 });
expect(parseByteRange("bytes=-10", 100)).toEqual({ start: 90, end: 99 });
expect(parseByteRange("bytes=95-200", 100)).toEqual({ start: 95, end: 99 });
expect(parseByteRange("bytes=200-", 100)).toBeNull();
});
});