Files
suaveplan/packages/media/fonts/font-subset/tests/property.test.ts
2026-05-27 23:47:53 +01:00

84 lines
2.6 KiB
TypeScript

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 }
);
});
});