Files
Triple-C/app/src/components/projects/home/filePreview.test.ts
T

99 lines
3.6 KiB
TypeScript
Raw Normal View History

2026-08-23 08:30:48 -07:00
import { describe, it, expect } from "vitest";
import {
IMAGE_PREVIEW_LIMIT,
TEXT_PREVIEW_LIMIT,
decodeBase64,
encodeBase64,
2026-08-23 08:30:48 -07:00
extensionOf,
imageMimeFor,
looksBinary,
previewKind,
previewLimit,
} from "./filePreview";
describe("extensionOf", () => {
it("lowercases, and takes only the last segment", () => {
expect(extensionOf("Photo.PNG")).toBe("png");
expect(extensionOf("archive.tar.gz")).toBe("gz");
expect(extensionOf("/workspace/app/main.rs")).toBe("rs");
});
it("treats a leading dot as hidden, not as an extension", () => {
// `.gitignore` is a text file called `.gitignore`, not one of type "gitignore".
expect(extensionOf(".gitignore")).toBe("");
expect(extensionOf("Makefile")).toBe("");
});
});
describe("previewKind", () => {
it("recognises images by extension, with a MIME the Blob can use", () => {
expect(previewKind("logo.png")).toBe("image");
expect(imageMimeFor("logo.PNG")).toBe("image/png");
expect(imageMimeFor("photo.jpeg")).toBe("image/jpeg");
expect(imageMimeFor("icon.svg")).toBe("image/svg+xml");
expect(imageMimeFor("notes.txt")).toBeNull();
});
it("recognises known text extensions and conventional extensionless names", () => {
expect(previewKind("main.rs")).toBe("text");
expect(previewKind("config.yaml")).toBe("text");
expect(previewKind("Dockerfile")).toBe("text");
expect(previewKind("README")).toBe("text");
expect(previewKind(".gitignore")).toBe("text");
});
it("leaves anything else undecided rather than refusing it outright", () => {
// `unknown` means "read it and sniff the bytes" — a .bak of a config file
// should still preview.
expect(previewKind("dump.bak")).toBe("unknown");
expect(previewKind("app.wasm")).toBe("unknown");
});
});
describe("previewLimit", () => {
it("gives images the bigger budget, since they are what blows a text cap", () => {
expect(previewLimit("photo.jpg")).toBe(IMAGE_PREVIEW_LIMIT);
expect(previewLimit("notes.md")).toBe(TEXT_PREVIEW_LIMIT);
expect(previewLimit("mystery.bin")).toBe(TEXT_PREVIEW_LIMIT);
expect(IMAGE_PREVIEW_LIMIT).toBeGreaterThan(TEXT_PREVIEW_LIMIT);
});
});
describe("decodeBase64 / looksBinary", () => {
it("round-trips bytes that are not valid UTF-8", () => {
// The reason the backend returns base64 at all: these bytes must survive.
const bytes = decodeBase64(btoa("\xff\xd8\xff\xe0"));
expect(Array.from(bytes)).toEqual([0xff, 0xd8, 0xff, 0xe0]);
});
it("calls a NUL-bearing prefix binary and plain text text", () => {
expect(looksBinary(new Uint8Array([0x68, 0x69, 0x0a]))).toBe(false);
expect(looksBinary(new Uint8Array([0x68, 0x00, 0x69]))).toBe(true);
});
it("only sniffs the first 8 KB, so a NUL deep in a big file is ignored", () => {
const bytes = new Uint8Array(20000).fill(0x61);
bytes[9000] = 0;
expect(looksBinary(bytes)).toBe(false);
});
});
describe("encodeBase64", () => {
it("matches btoa on a small input", () => {
expect(encodeBase64(new Uint8Array([0xff, 0xd8, 0x00, 0x41]))).toBe(btoa("\xff\xd8\x00\x41"));
});
it("round-trips 1 MiB without overflowing the call stack", () => {
// Spreading a 1 MiB array into String.fromCharCode throws RangeError in V8.
const bytes = new Uint8Array(TEXT_PREVIEW_LIMIT);
for (let i = 0; i < bytes.length; i++) bytes[i] = (i * 31 + 7) & 0xff;
const back = decodeBase64(encodeBase64(bytes));
expect(back.length).toBe(bytes.length);
expect(back.every((b, i) => b === bytes[i])).toBe(true);
});
it("encodes an empty input as the empty string", () => {
expect(encodeBase64(new Uint8Array(0))).toBe("");
});
});