Files
suaveplan/packages/foundation/data/mime/tests/tree-shake.test.ts
2026-05-27 23:47:53 +01:00

53 lines
2.1 KiB
TypeScript

/**
* Tree-shake guard: verifies subpath imports work independently and the
* `sideEffects` allowlist is narrowly scoped (only the magic-byte
* signature registry is preserved).
*/
import { describe, expect, it } from "@suaveplan/testing/runner";
import pkg from "../package.json";
describe("tree-shake compatibility", () => {
it("package.json sideEffects allowlist is narrow", {
timeout: 5000,
}, () => {
// The magic-byte signature registry registers entries via the
// side-effecting `signatures/index.ts` module — keep it on the
// sideEffects allowlist so bundlers don't tree-shake the
// registration loop. Every OTHER module must be side-effect-free.
expect(pkg.sideEffects).toEqual([
"**/detection/from-magic-bytes/signatures/index.{ts,js}",
]);
});
it("has subpath exports for core modules", { timeout: 5000 }, () => {
const exports = pkg.exports as Record<string, unknown>;
expect(exports["."]).toBeDefined();
expect(exports["./content-type"]).toBeDefined();
expect(exports["./detection"]).toBeDefined();
expect(exports["./db"]).toBeDefined();
expect(exports["./charset"]).toBeDefined();
expect(exports["./errors"]).toBeDefined();
expect(exports["./schemas"]).toBeDefined();
});
});
describe("subpath import smoke test", () => {
it("can import from content-type subpath", { timeout: 30000 }, async () => {
// Dynamic import simulates tree-shaking consumer
const m = await import("../src/content-type/index.js");
expect(typeof m.parseContentType).toBe("function");
expect(typeof m.formatContentType).toBe("function");
});
it("can import from charset subpath", { timeout: 30000 }, async () => {
const m = await import("../src/charset/index.js");
expect(typeof m.sniffBom).toBe("function");
});
it("can import from db subpath", { timeout: 30000 }, async () => {
const m = await import("../src/db/index.js");
expect(typeof m.getMimeEntry).toBe("function");
expect(Array.isArray(m.BUILTIN_DB)).toBe(true);
});
});