summaryrefslogtreecommitdiffhomepage
path: root/src/app/uuid.test.ts
blob: bd8e3063c913baff8304cd668e027d55c0b38510 (plain)
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
import { describe, expect, it } from "vitest";
import { randomId } from "./uuid";

const V4_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

describe("randomId", () => {
	it("returns a v4-shaped uuid", () => {
		const id = randomId();
		expect(id).toMatch(V4_RE);
	});

	it("returns distinct values across calls", () => {
		const ids = new Set<string>();
		for (let i = 0; i < 200; i++) {
			ids.add(randomId());
		}
		expect(ids.size).toBe(200);
	});

	it("works without crypto.randomUUID (getRandomValues branch)", () => {
		const origRandomUUID = crypto.randomUUID;
		try {
			// Remove randomUUID so the getRandomValues branch is taken
			delete (crypto as { randomUUID?: () => string }).randomUUID;
			const id = randomId();
			expect(id).toMatch(V4_RE);
		} finally {
			crypto.randomUUID = origRandomUUID;
		}
	});
});