Initial Commit

This commit is contained in:
2026-05-27 23:47:53 +01:00
parent fbc70f96e6
commit 9b28db724f
18103 changed files with 1600554 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
import { describe, expect, it } from "@suaveplan/testing/runner";
import {
createFontSubsetter,
detectFontFormat,
FONT_SUBSET_CODES,
type FontSubsetError,
NullFontSubsetAdapter,
parseUnicodeRanges,
} from "../src/index.js";
// Minimal TTF magic bytes + padding
const TTF_DATA = new Uint8Array([
0x00,
0x01,
0x00,
0x00,
...new Array(96).fill(0),
]);
const WOFF2_DATA = new Uint8Array([
0x77,
0x4f,
0x46,
0x32,
...new Array(96).fill(0),
]);
describe("Full pipeline with NullFontSubsetAdapter", () => {
it("probes and compresses TTF → WOFF2", { timeout: 30000 }, async () => {
const adapter = new NullFontSubsetAdapter();
const compressor = createFontSubsetter({ adapter });
const meta = await compressor.probe(TTF_DATA);
expect(meta.format).toBe("ttf");
const result = await compressor.compress(TTF_DATA, {
outputFormat: "woff2",
});
expect(result.format).toBe("woff2");
expect(result.mimeType).toBe("font/woff2");
expect(result.originalSize).toBe(TTF_DATA.byteLength);
expect(result.ratio).toBeGreaterThan(0);
});
it("compresses with subset options", { timeout: 30000 }, async () => {
const adapter = new NullFontSubsetAdapter();
const compressor = createFontSubsetter({ adapter });
const result = await compressor.compress(TTF_DATA, {
subset: {
unicodeRanges: ["U+0020-007F"],
characters: "Hello World",
},
outputFormat: "woff",
stripHinting: true,
stripMetadata: true,
features: ["kern", "liga"],
});
expect(result.format).toBe("woff");
});
it("detects WOFF2 format from magic bytes", { timeout: 5000 }, () => {
expect(detectFontFormat(WOFF2_DATA)).toBe("woff2");
});
it("parseUnicodeRanges returns correct codepoints for ASCII", {
timeout: 5000,
}, () => {
const codepoints = parseUnicodeRanges(["U+0020-007F"]);
expect(codepoints.size).toBe(96);
expect(codepoints.has(0x20)).toBe(true);
expect(codepoints.has(0x7f)).toBe(true);
});
it("end-to-end with multiple compress calls", {
timeout: 30000,
}, async () => {
const adapter = new NullFontSubsetAdapter();
const compressor = createFontSubsetter({ adapter });
for (const fmt of ["ttf", "otf", "woff", "woff2"] as const) {
const result = await compressor.compress(TTF_DATA, {
outputFormat: fmt,
});
expect(result.format).toBe(fmt);
}
});
it("throws ENCODE_FAILED when aborted mid-flight", {
timeout: 5000,
}, async () => {
const ctrl = new AbortController();
ctrl.abort();
const adapter = new NullFontSubsetAdapter();
const compressor = createFontSubsetter({ adapter });
try {
await compressor.compress(TTF_DATA, undefined, ctrl.signal);
expect.fail("should have thrown");
} catch (e) {
expect((e as FontSubsetError).code).toBe(
FONT_SUBSET_CODES.ENCODE_FAILED
);
}
});
});

View File

@@ -0,0 +1,83 @@
import { describe, it } from "@suaveplan/testing/runner";
import * as fc from "fast-check";
import {
createFontSubsetter,
NullFontSubsetAdapter,
parseUnicodeRanges,
} from "../src/index.js";
const TTF_DATA = new Uint8Array([
0x00,
0x01,
0x00,
0x00,
...new Array(96).fill(0),
]);
describe("Property: ratio is positive and bounded", () => {
it("ratio is always > 0 for any outputFormat", {
timeout: 30000,
}, async () => {
const adapter = new NullFontSubsetAdapter();
const compressor = createFontSubsetter({ adapter });
await fc.assert(
fc.asyncProperty(
fc.constantFrom("ttf", "otf", "woff", "woff2" as const),
async (fmt) => {
const result = await compressor.compress(TTF_DATA, {
outputFormat: fmt,
});
return result.ratio > 0;
}
),
{ numRuns: 20 }
);
});
it("compressedSize equals originalSize for NullAdapter (no-op)", {
timeout: 30000,
}, async () => {
const adapter = new NullFontSubsetAdapter();
const compressor = createFontSubsetter({ adapter });
await fc.assert(
fc.asyncProperty(
fc.uint8Array({ minLength: 100, maxLength: 1000 }),
async (data) => {
const patched = new Uint8Array(data);
patched[0] = 0x00;
patched[1] = 0x01;
patched[2] = 0x00;
patched[3] = 0x00;
const result = await compressor.compress(patched);
return (
result.originalSize === patched.byteLength &&
result.compressedSize === patched.byteLength
);
}
),
{ numRuns: 20 }
);
});
});
describe("Property: parseUnicodeRanges", () => {
it("all codepoints in range U+0000-FFFF are correctly included", {
timeout: 30000,
}, () => {
fc.assert(
fc.property(fc.integer({ min: 0, max: 0xfffd }), (start) => {
const end = start + 2;
const startHex = start
.toString(16)
.toUpperCase()
.padStart(4, "0");
const endHex = end.toString(16).toUpperCase().padStart(4, "0");
const set = parseUnicodeRanges([`U+${startHex}-${endHex}`]);
return set.has(start) && set.has(end) && set.size === 3;
}),
{ numRuns: 30 }
);
});
});