修复页面加载bug

This commit is contained in:
2026-05-29 17:09:44 +08:00
parent e36f28a668
commit d3bd3c7c50
3 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import vm from "node:vm";
import { randomUUIDPolyfillScript } from "@/lib/client/random-uuid-polyfill";
describe("client randomUUID polyfill", () => {
it("defines crypto.randomUUID when crypto is missing", () => {
const context = {};
vm.runInNewContext(randomUUIDPolyfillScript, context);
expect(context.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}$/
);
});
it("defines crypto.randomUUID when only getRandomValues is available", () => {
const context = {
crypto: {
getRandomValues(bytes: Uint8Array) {
for (let index = 0; index < bytes.length; index += 1) bytes[index] = index;
return bytes;
}
}
};
vm.runInNewContext(randomUUIDPolyfillScript, context);
expect(context.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}$/
);
});
});