26 lines
962 B
JavaScript
26 lines
962 B
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { browserUuid } from "../src/lib/browser-uuid.ts";
|
|
|
|
test("native UUID generation keeps the crypto receiver", () => {
|
|
const source = {
|
|
randomUUID() { assert.equal(this, source); return "native-id"; },
|
|
getRandomValues() { assert.fail("native generation should be used"); },
|
|
};
|
|
assert.equal(browserUuid(source), "native-id");
|
|
});
|
|
|
|
test("older browsers generate RFC 4122 version 4 UUIDs using secure random bytes", () => {
|
|
const source = { getRandomValues(bytes) {
|
|
assert.equal(this, source);
|
|
assert.equal(bytes.length, 16);
|
|
for (let i = 0; i < bytes.length; i++) bytes[i] = i;
|
|
return bytes;
|
|
} };
|
|
assert.equal(browserUuid(source), "00010203-0405-4607-8809-0a0b0c0d0e0f");
|
|
});
|
|
|
|
test("missing secure randomness never falls back to a guessable identifier", () => {
|
|
assert.throws(() => browserUuid({}), /Secure browser identifiers are unavailable/);
|
|
});
|