103 lines
3.2 KiB
TypeScript
103 lines
3.2 KiB
TypeScript
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
|
|
);
|
|
}
|
|
});
|
|
});
|