Files
butcem/src/shared/attachments.test.ts
T

57 lines
2.1 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 {
acceptAttribute,
allowedAttachmentKinds,
detectFileType,
formatFileSize,
kindOf,
} from "./attachments";
const bytes = (...parts: (number[] | string)[]) =>
new Uint8Array(
parts.flatMap((p) => (typeof p === "string" ? [...p].map((c) => c.charCodeAt(0)) : p)),
);
describe("detectFileType", () => {
it("imzalardan türü bulur", () => {
expect(detectFileType(bytes([0xff, 0xd8, 0xff, 0xe0]))).toBe("jpeg");
expect(detectFileType(bytes([0x89], "PNG", [0x0d, 0x0a, 0x1a, 0x0a]))).toBe("png");
expect(detectFileType(bytes("RIFF", [0, 0, 0, 0], "WEBPVP8 "))).toBe("webp");
expect(detectFileType(bytes("%PDF-1.7"))).toBe("pdf");
});
it("HEIC: ana veya uyumlu markadan", () => {
expect(detectFileType(bytes([0, 0, 0, 24], "ftypheic", [0, 0, 0, 0], "mif1heic"))).toBe("heic");
expect(detectFileType(bytes([0, 0, 0, 28], "ftypmif1", [0, 0, 0, 0], "mif1heichevc"))).toBe(
"heic",
);
// MP4 videosu HEIC değildir.
expect(detectFileType(bytes([0, 0, 0, 24], "ftypisom", [0, 0, 2, 0], "isomiso2"))).toBeNull();
});
it("uzantı/metin/kısa içerik reddedilir", () => {
expect(detectFileType(bytes("<svg xmlns="))).toBeNull();
expect(detectFileType(bytes("GIF89a"))).toBeNull();
expect(detectFileType(bytes([0xff, 0xd8]))).toBeNull();
expect(detectFileType(new Uint8Array())).toBeNull();
});
});
describe("tür kuralları", () => {
it("gider yalnızca görsel; gelir ve transfer görsel + PDF", () => {
expect(allowedAttachmentKinds("expense")).toEqual(["image"]);
expect(allowedAttachmentKinds("income")).toEqual(["image", "pdf"]);
expect(allowedAttachmentKinds("transfer")).toEqual(["image", "pdf"]);
expect(acceptAttribute(["image"])).not.toContain("pdf");
expect(acceptAttribute(["image", "pdf"])).toContain("application/pdf");
expect(kindOf("heic")).toBe("image");
expect(kindOf("pdf")).toBe("pdf");
});
it("boyut biçimi", () => {
expect(formatFileSize(900)).toBe("900 B");
expect(formatFileSize(2048)).toBe("2 KB");
expect(formatFileSize(1.5 * 1024 * 1024)).toBe("1,5 MB");
});
});