From 3196ba63d56f3817d198a4e6070329b44d3c93cf Mon Sep 17 00:00:00 2001 From: SnapOtter Date: Tue, 9 Jun 2026 23:04:40 +0800 Subject: [PATCH] refactor: export verifyBundleModels with unit tests --- apps/api/src/lib/feature-status.ts | 2 +- tests/unit/features/feature-status.test.ts | 42 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/apps/api/src/lib/feature-status.ts b/apps/api/src/lib/feature-status.ts index bdc32e7c..38a1d177 100644 --- a/apps/api/src/lib/feature-status.ts +++ b/apps/api/src/lib/feature-status.ts @@ -370,7 +370,7 @@ export function recoverInterruptedInstalls(): void { // ── Feature states (composite view) ───────────────────────────────────── -function verifyBundleModels(bundleId: string): string | null { +export function verifyBundleModels(bundleId: string): string | null { const manifest = readManifest(); if (!manifest) return null; diff --git a/tests/unit/features/feature-status.test.ts b/tests/unit/features/feature-status.test.ts index 9a49448b..4114b381 100644 --- a/tests/unit/features/feature-status.test.ts +++ b/tests/unit/features/feature-status.test.ts @@ -524,3 +524,45 @@ describe("Composite state - getFeatureStates", () => { } }); }); + +describe("verifyBundleModels", () => { + it("returns null when all models exist and meet minSize", () => { + writeTestManifest({ + "background-removal": { + models: [{ id: "u2net", path: "u2net.onnx", minSize: 10 }], + }, + }); + writeFileSync(join(modelsDir, "u2net.onnx"), Buffer.alloc(1024)); + expect(mod.verifyBundleModels("background-removal")).toBeNull(); + }); + + it("returns error string when model file is missing", () => { + writeTestManifest({ + "background-removal": { + models: [{ id: "u2net", path: "u2net.onnx" }], + }, + }); + const result = mod.verifyBundleModels("background-removal"); + expect(result).toBe("Missing model file: u2net.onnx"); + }); + + it("returns error string when model file is undersized", () => { + writeTestManifest({ + "background-removal": { + models: [{ id: "u2net", path: "u2net.onnx", minSize: 1000 }], + }, + }); + writeFileSync(join(modelsDir, "u2net.onnx"), Buffer.alloc(10)); + const result = mod.verifyBundleModels("background-removal"); + expect(result).toContain("undersized"); + }); + + it("returns null when manifest is missing", () => { + expect(mod.verifyBundleModels("background-removal")).toBeNull(); + }); + + it("returns null when bundle not in manifest", () => { + writeTestManifest({ "some-other-bundle": { models: [] } }); + expect(mod.verifyBundleModels("background-removal")).toBeNull(); + }); +});