Files
file-tools/tests/file/core.test.ts
T

56 lines
1.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { manifestCsv, manifestJson } from "../../src/file/manifest";
import {
detectKnownSignature,
extensionMismatch,
} from "../../src/file/signatures";
import {
extractStrings,
looksLikeText,
shannonEntropy,
} from "../../src/file/text";
describe("file signatures", () => {
it("detects PNG without claiming parser validation", () =>
expect(
detectKnownSignature(
Uint8Array.of(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a),
),
).toMatchObject({ extension: "png", mime: "image/png" }));
it("accounts for container extension aliases", () => {
expect(extensionMismatch("book.epub", "zip")).toBeUndefined();
expect(extensionMismatch("image.txt", "png")).toMatch(/does not match/u);
});
});
describe("bounded text helpers", () => {
it("extracts ASCII and UTF-16LE strings", () =>
expect(
extractStrings(
Uint8Array.from([65, 66, 67, 68, 0, 90, 0, 89, 0, 88, 0, 87, 0]),
),
).toEqual(expect.arrayContaining(["ABCD", "ZYXW"])));
it("distinguishes NUL-containing binary data", () =>
expect(looksLikeText(Uint8Array.of(65, 0, 66))).toBe(false));
it("measures a uniform byte distribution", () =>
expect(
shannonEntropy(Uint8Array.from({ length: 256 }, (_, index) => index)),
).toBeCloseTo(8));
});
describe("manifests", () => {
const records = [
{
name: "=formula",
path: "=formula",
size: 1,
type: "text/plain",
lastModified: 0,
},
];
it("is deterministic", () =>
expect(manifestJson(records)).toBe(manifestJson(records)));
it("neutralises spreadsheet-leading formulas", () =>
expect(manifestCsv(records)).toContain("'=formula"));
});