|
| 1 | +import { expect, test, describe } from "vitest"; |
| 2 | +import { deterministicUUID, generateUUIDFromHash } from "./utils"; |
| 3 | + |
| 4 | +const hash1 = |
| 5 | + "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"; |
| 6 | +const hash2 = |
| 7 | + "aa26b0aa4af7a749aa1a1aa3c10aa9923f611910772a473f1119a5a4940a0ab27ac115f1a0a1a5f14f11bc117fa67b143732c304cc5fa9aa1a6f57f50021a1ff"; |
| 8 | + |
| 9 | +describe("generateUUIDFromHash", () => { |
| 10 | + test("generates valid UUID v4 format", () => { |
| 11 | + const uuid = generateUUIDFromHash(hash1); |
| 12 | + |
| 13 | + // Check UUID format (8-4-4-4-12 characters) |
| 14 | + expect(uuid).toMatch( |
| 15 | + /^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/ |
| 16 | + ); |
| 17 | + }); |
| 18 | + |
| 19 | + test("generates consistent UUIDs for same input", () => { |
| 20 | + const uuid1 = generateUUIDFromHash(hash1); |
| 21 | + const uuid2 = generateUUIDFromHash(hash1); |
| 22 | + |
| 23 | + expect(uuid1).toBe(uuid2); |
| 24 | + }); |
| 25 | + |
| 26 | + test("sets correct version (5) in UUID", () => { |
| 27 | + const uuid = generateUUIDFromHash(hash1); |
| 28 | + expect(uuid.charAt(14)).toBe("5"); |
| 29 | + }); |
| 30 | + |
| 31 | + test("sets correct variant bits in UUID", () => { |
| 32 | + const uuid = generateUUIDFromHash(hash1); |
| 33 | + |
| 34 | + // The 19th character should be 8, 9, a, or b |
| 35 | + expect(uuid.charAt(19)).toMatch(/[89ab]/); |
| 36 | + }); |
| 37 | + |
| 38 | + test("generates different UUIDs for different inputs", () => { |
| 39 | + const uuid1 = generateUUIDFromHash(hash1); |
| 40 | + const uuid2 = generateUUIDFromHash(hash2); |
| 41 | + |
| 42 | + expect(uuid1).not.toBe(uuid2); |
| 43 | + }); |
| 44 | + |
| 45 | + test("throws error for invalid hash length", () => { |
| 46 | + expect(() => generateUUIDFromHash("123")).toThrowError(); |
| 47 | + }); |
| 48 | + |
| 49 | + test("throws error for non-hex characters", () => { |
| 50 | + const invalidHash = |
| 51 | + "qe26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"; // Contains non-hex chars |
| 52 | + |
| 53 | + expect(() => { |
| 54 | + generateUUIDFromHash(invalidHash); |
| 55 | + }).toThrowError(); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe("deterministicUUID", () => { |
| 60 | + test("generates consistent UUID for the same key", async () => { |
| 61 | + const key = "test-key"; |
| 62 | + const uuid1 = await deterministicUUID(key); |
| 63 | + const uuid2 = await deterministicUUID(key); |
| 64 | + expect(uuid1).toBe(uuid2); |
| 65 | + }); |
| 66 | + |
| 67 | + test("generates different UUIDs for different keys", async () => { |
| 68 | + const uuid1 = await deterministicUUID("test"); |
| 69 | + const uuid2 = await deterministicUUID("test2"); |
| 70 | + expect(uuid1).not.toBe(uuid2); |
| 71 | + }); |
| 72 | +}); |
0 commit comments