test(webui): cover randomUUID entry shim fallback

Add a focused regression test for the non-secure-context WebUI entry shim so missing crypto.randomUUID no longer depends on manual verification.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Xubin Ren 2026-05-11 07:35:37 +00:00 committed by Xubin Ren
parent 5ea2c37325
commit 6d07aa6059

View File

@ -0,0 +1,42 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const render = vi.fn();
const createRoot = vi.fn(() => ({ render }));
vi.mock("react-dom/client", () => ({
default: { createRoot },
}));
vi.mock("@/App", () => ({
default: () => null,
}));
describe("main entry crypto shim", () => {
const originalRandomUUID = globalThis.crypto.randomUUID;
beforeEach(() => {
vi.resetModules();
createRoot.mockClear();
render.mockClear();
document.body.innerHTML = '<div id="root"></div>';
delete (globalThis.crypto as Crypto & { randomUUID?: Crypto["randomUUID"] }).randomUUID;
});
afterEach(() => {
Object.defineProperty(globalThis.crypto, "randomUUID", {
value: originalRandomUUID,
configurable: true,
});
document.body.innerHTML = "";
});
it("installs a randomUUID fallback when the browser omits it", async () => {
await import("../main");
expect(globalThis.crypto.randomUUID).toEqual(expect.any(Function));
expect(globalThis.crypto.randomUUID()).toMatch(
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/,
);
expect(createRoot).toHaveBeenCalledWith(document.getElementById("root"));
});
});