Files
whisper_web/tests/transformers-runtime.test.ts
T

58 lines
2.6 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
/**
* Tripwires for the patched, pinned Transformers.js runtime (D-WS1a).
* `@huggingface/transformers` carries a `bun patch` (D-027/D-028) keyed by
* its exact version; a bump that forgets to regenerate the patch would leave
* the app working but silently bring back the mobile OOM. These checks fail
* the fast suite instead.
*/
const ROOT = join(import.meta.dir, "..");
const PKG_DIR = join(ROOT, "node_modules/@huggingface/transformers");
const PINNED_VERSION = "4.3.2";
function read(path: string): string {
return readFileSync(path, "utf8");
}
/** Source text of `async function <name>(…) { … }` in a bundled file. */
function functionSource(source: string, name: string): string {
const start = source.indexOf(`async function ${name}(`);
if (start < 0) throw new Error(`${name} not found`);
const end = source.indexOf("\nfunction ", start + 1);
const endAsync = source.indexOf("\nasync function ", start + 1);
const stops = [end, endAsync].filter((i) => i > 0);
return source.slice(start, stops.length ? Math.min(...stops) : undefined);
}
describe("@huggingface/transformers runtime", () => {
test("installed version is the pinned 4.3.2", () => {
const installed = JSON.parse(read(join(PKG_DIR, "package.json"))).version;
expect(installed).toBe(PINNED_VERSION);
});
test("patchedDependencies points at an existing patch for the installed version", () => {
const rootPkg = JSON.parse(read(join(ROOT, "package.json")));
const patchPath = rootPkg.patchedDependencies?.[`@huggingface/transformers@${PINNED_VERSION}`];
expect(typeof patchPath).toBe("string");
expect(existsSync(join(ROOT, patchPath))).toBe(true);
});
test("web bundle's constructSessions carries the sequential-load patch (D-027/D-028)", () => {
const body = functionSource(read(join(PKG_DIR, "dist/transformers.web.js")), "constructSessions");
expect(body).toContain("options?.sequentialSessionLoad");
expect(body).toContain("Promise.all(");
expect(body).toContain("for (const name of Object.keys(names))");
});
test("Whisper generate still takes the seek loop when max_new_tokens is unset (D-021, D-WS1b)", () => {
// The worker's `max_new_tokens: 440` exists only to dodge this branch.
// If upstream changes the condition, re-measure D-021 before touching it.
const source = read(join(PKG_DIR, "src/models/whisper/modeling_whisper.js"));
expect(source).toContain("if (generation_config.return_timestamps && !kwargs.max_new_tokens)");
});
});