import { describe, test, expect } from "bun:test"; import { matchCachedModels } from "../src/lib/modelCache"; // Pure URL matching (D-005 downloaded badge); the `caches` wrapper is // browser-only and exercised manually. const TINY = "onnx-community/whisper-tiny.en"; const BASE = "onnx-community/whisper-base"; function hubUrl(modelId: string, file: string): string { return `https://huggingface.co/${modelId}/resolve/main/${file}`; } describe("matchCachedModels", () => { test("matches models with cached .onnx weights", () => { const urls = [ hubUrl(TINY, "onnx/encoder_model_quantized.onnx"), hubUrl(TINY, "onnx/decoder_model_merged_quantized.onnx"), hubUrl(TINY, "tokenizer.json"), ]; expect(matchCachedModels(urls, [TINY, BASE])).toEqual(new Set([TINY])); }); test("configs/tokenizer alone do not count as downloaded", () => { const urls = [hubUrl(BASE, "tokenizer.json"), hubUrl(BASE, "config.json")]; expect(matchCachedModels(urls, [TINY, BASE])).toEqual(new Set()); }); test("ignores query strings when checking the .onnx extension", () => { const urls = [`${hubUrl(BASE, "onnx/encoder_model_q4.onnx")}?download=true`]; expect(matchCachedModels(urls, [BASE])).toEqual(new Set([BASE])); }); test("does not confuse '.en' variants with their multilingual sibling", () => { // "onnx-community/whisper-tiny.en/..." contains the substring // "whisper-tiny" but must only match the .en id thanks to the trailing /. const urls = [hubUrl(TINY, "onnx/encoder_model_quantized.onnx")]; expect( matchCachedModels(urls, [TINY, "onnx-community/whisper-tiny"]), ).toEqual(new Set([TINY])); }); test("empty inputs yield an empty set", () => { expect(matchCachedModels([], [TINY])).toEqual(new Set()); expect(matchCachedModels([hubUrl(TINY, "onnx/encoder_model.onnx")], [])).toEqual( new Set(), ); }); });