Files
file-tools/tests/file/core.test.ts
T
zemion 91dc9bc2f7
Verify / verify (push) Canceled after 0s
Release File Tools 0.2.0
2026-09-02 08:24:07 +02:00

160 lines
4.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { manifestCsv, manifestJson } from "../../src/file/manifest";
import {
checksumManifest,
joinBlobs,
planBatchRename,
splitBlob,
} from "../../src/file/operations";
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"));
it("reports truthful inspection coverage and per-file failures", () => {
const text = manifestJson([
{
...records[0]!,
inspectionStatus: "inspected",
inspection: {
evidence: [],
entropy: 0,
sampleBytes: 0,
strings: [],
findings: [],
},
},
{
...records[0]!,
path: "broken.bin",
inspectionStatus: "error",
inspectionError: "Timed out",
},
]);
const manifest = JSON.parse(text) as {
inspectionCoverage: { inspected: number; failed: number };
files: Array<{
inspectionStatus: string;
inspectionError: string | null;
}>;
};
expect(manifest.inspectionCoverage).toMatchObject({
inspected: 1,
failed: 1,
});
expect(
manifest.files.find((file) => file.inspectionStatus === "error"),
).toMatchObject({ inspectionError: "Timed out" });
expect(
manifestCsv([{ ...records[0]!, inspectionStatus: "cancelled" }]),
).toContain('"cancelled"');
});
});
describe("bounded file operations", () => {
it("plans sanitized collision-safe rename downloads deterministically", () => {
const files = [
{ name: "first.txt", blob: new Blob(["one"]) },
{ name: "second.txt", blob: new Blob(["two"]) },
];
const plan = planBatchRename(files, "same.{ext}");
expect(plan.map((item) => item.filename)).toEqual([
"same.txt",
"same (2).txt",
]);
expect(plan.map((item) => item.originalName)).toEqual([
"first.txt",
"second.txt",
]);
});
it("splits and rejoins byte-exact Blob parts with an offset manifest", async () => {
const source = new Blob([new Uint8Array(150_000).map((_, index) => index)]);
const split = splitBlob(source, "payload.bin", 65_536);
expect(split.parts.map((part) => part.size)).toEqual([
65_536, 65_536, 18_928,
]);
expect(split.manifest.parts[1]).toMatchObject({ offset: 65_536, index: 2 });
const joined = joinBlobs(split.parts.map((part) => part.blob));
expect(new Uint8Array(await joined.arrayBuffer())).toEqual(
new Uint8Array(await source.arrayBuffer()),
);
});
it("refuses unbounded split/join and emits sorted checksum files", () => {
expect(() => splitBlob(new Blob(["x"]), "x", 1)).toThrow(/Chunk size/u);
expect(() =>
joinBlobs([new Blob(["abc"]), new Blob(["def"])], "", 5),
).toThrow(/exceeds/u);
const output = checksumManifest(
[
{
name: "b",
path: "b",
size: 0,
type: "",
lastModified: 0,
sha256: "b".repeat(64),
},
{
name: "a",
path: "a",
size: 0,
type: "",
lastModified: 0,
sha256: "a".repeat(64),
},
],
"sha256",
);
expect(output.split("\n")[0]).toBe(`${"a".repeat(64)} *a`);
});
});