Spaces:
Running
Running
File size: 1,554 Bytes
10d1fd4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import { describe, expect, it, vi } from "vitest";
interface MockNavigatorWithGpu {
gpu?: {
requestAdapter: ReturnType<typeof vi.fn>;
};
}
function importFresh() {
vi.resetModules();
return import("./webGpu");
}
function setupMockNavigator(mockNavigator: MockNavigatorWithGpu) {
global.navigator = { ...global.navigator, ...mockNavigator };
}
describe("webGpu module", () => {
it("detects WebGPU availability and F16 support when adapter resolves", async () => {
const mockNavigator: MockNavigatorWithGpu = {
gpu: {
requestAdapter: vi.fn().mockResolvedValue({
features: new Set(["shader-f16"]),
}),
},
};
setupMockNavigator(mockNavigator);
const mod = await importFresh();
expect(mod.isWebGPUAvailable).toBe(true);
expect(mod.isF16Supported).toBe(true);
});
it("sets isWebGPUAvailable false when requestAdapter rejects", async () => {
const mockNavigator: MockNavigatorWithGpu = {
gpu: {
requestAdapter: vi.fn().mockRejectedValue(new Error("fail")),
},
};
setupMockNavigator(mockNavigator);
const mod = await importFresh();
expect(mod.isWebGPUAvailable).toBe(false);
expect(mod.isF16Supported).toBe(false);
});
it("sets isWebGPUAvailable false when navigator has no gpu", async () => {
const mockNavigator: MockNavigatorWithGpu = {};
setupMockNavigator(mockNavigator);
const mod = await importFresh();
expect(mod.isWebGPUAvailable).toBe(false);
expect(mod.isF16Supported).toBe(false);
});
});
|