Files
2026-05-27 23:47:53 +01:00

86 lines
3.1 KiB
TypeScript

import { describe, expect, it } from "@suaveplan/testing/runner";
import * as fc from "fast-check";
import type { AudioClipInput } from "../src/index.js";
import { AudioSpritePacker, NullAudioAdapter } from "../src/index.js";
describe("property", () => {
it("manifest entry count equals clip count", {
timeout: 30000,
}, async () => {
await fc.assert(
fc.asyncProperty(
fc.array(fc.string({ minLength: 1, maxLength: 20 }), {
minLength: 1,
maxLength: 50,
}),
async (names) => {
const packer = new AudioSpritePacker({
adapter: new NullAudioAdapter(),
});
const clips: AudioClipInput[] = names.map((name, i) => ({
name,
data: new Uint8Array([i % 256]),
}));
const sprite = await packer.pack(clips);
expect(sprite.manifest.entries).toHaveLength(clips.length);
}
),
{ numRuns: 30 }
);
});
it("unpack returns correct byteLength", { timeout: 30000 }, async () => {
await fc.assert(
fc.asyncProperty(
fc.array(fc.string({ minLength: 1, maxLength: 20 }), {
minLength: 1,
maxLength: 20,
}),
async (names) => {
const packer = new AudioSpritePacker({
adapter: new NullAudioAdapter(),
});
const clips: AudioClipInput[] = names.map((name, i) => ({
name,
data: new Uint8Array([i % 256]),
}));
const sprite = await packer.pack(clips);
for (const entry of sprite.manifest.entries) {
const slice = packer.unpack(sprite, entry.name);
expect(slice.length).toBe(entry.byteLength);
}
}
),
{ numRuns: 20 }
);
});
it("silence padding increases total duration", {
timeout: 30000,
}, async () => {
await fc.assert(
fc.asyncProperty(
fc.integer({ min: 0, max: 500 }),
async (paddingMs) => {
const packer = new AudioSpritePacker({
adapter: new NullAudioAdapter(),
});
const clips: AudioClipInput[] = [
{ name: "a", data: new Uint8Array([1]) },
{ name: "b", data: new Uint8Array([2]) },
];
const sprite = await packer.pack(clips, {
silencePaddingMs: paddingMs,
});
if (paddingMs > 0) {
expect(
sprite.manifest.totalDurationSeconds
).toBeGreaterThan(0);
}
}
),
{ numRuns: 20 }
);
});
});