import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import test from "node:test"; import vm from "node:vm"; const appSource = await readFile(new URL("../app.js", import.meta.url), "utf8"); const helperStart = appSource.indexOf("function createUuid"); const helperEnd = appSource.indexOf("\n\nfunction mapApiOwner", helperStart); assert.notEqual(helperStart, -1, "createUuid helper should exist"); assert.notEqual(helperEnd, -1, "createUuid helper should end before API mappers"); function loadCreateUuid(windowObject) { const context = { window: windowObject }; vm.runInNewContext(`${appSource.slice(helperStart, helperEnd)}\nglobalThis.createUuid = createUuid;`, context); return context.createUuid; } test("UUID generation falls back when crypto.randomUUID is unavailable", () => { let next = 0; const createUuid = loadCreateUuid({ crypto: { getRandomValues(bytes) { for (let index = 0; index < bytes.length; index += 1) { bytes[index] = next; next = (next + 17) % 256; } return bytes; } } }); assert.match( createUuid(), /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/ ); }); test("UUID generation uses native randomUUID when it is available", () => { const createUuid = loadCreateUuid({ crypto: { randomUUID() { return "00000000-0000-4000-8000-000000000123"; } } }); assert.equal(createUuid(), "00000000-0000-4000-8000-000000000123"); });