71 lines
2.3 KiB
TypeScript
71 lines
2.3 KiB
TypeScript
import { describe, expect, it } from "@suaveplan/testing/runner";
|
|
import { generateUuid, uint8ArrayToUuid, uuidToUint8Array } from "./uuid.js";
|
|
|
|
describe("uuid round-trip", () => {
|
|
it("round-trips a known UUID", { timeout: 5000 }, () => {
|
|
const uuid = "550e8400-e29b-41d4-a716-446655440000";
|
|
expect(uint8ArrayToUuid(uuidToUint8Array(uuid))).toBe(uuid);
|
|
});
|
|
|
|
it("uuidToUint8Array produces 16 bytes", { timeout: 5000 }, () => {
|
|
expect(
|
|
uuidToUint8Array("550e8400-e29b-41d4-a716-446655440000")
|
|
).toHaveLength(16);
|
|
});
|
|
|
|
it("throws on invalid UUID", { timeout: 5000 }, () => {
|
|
expect(() => uuidToUint8Array("not-a-uuid")).toThrow();
|
|
});
|
|
|
|
it("throws on UUID with wrong structure", { timeout: 5000 }, () => {
|
|
expect(() =>
|
|
uuidToUint8Array("550e8400e29b41d4a716446655440000")
|
|
).toThrow();
|
|
});
|
|
|
|
it("uint8ArrayToUuid throws on non-16-byte array", {
|
|
timeout: 5000,
|
|
}, () => {
|
|
expect(() => uint8ArrayToUuid(new Uint8Array(15))).toThrow();
|
|
});
|
|
|
|
it("uint8ArrayToUuid throws on empty array", { timeout: 5000 }, () => {
|
|
expect(() => uint8ArrayToUuid(new Uint8Array(0))).toThrow();
|
|
});
|
|
|
|
it("round-trips all-zeros UUID", { timeout: 5000 }, () => {
|
|
const uuid = "00000000-0000-0000-0000-000000000000";
|
|
expect(uint8ArrayToUuid(uuidToUint8Array(uuid))).toBe(uuid);
|
|
});
|
|
|
|
it("round-trips all-fs UUID", { timeout: 5000 }, () => {
|
|
const uuid = "ffffffff-ffff-ffff-ffff-ffffffffffff";
|
|
expect(uint8ArrayToUuid(uuidToUint8Array(uuid))).toBe(uuid);
|
|
});
|
|
});
|
|
|
|
describe("generateUuid", () => {
|
|
it("generates a valid UUID v4 string", { timeout: 5000 }, () => {
|
|
const uuid = generateUuid();
|
|
expect(uuid).toMatch(
|
|
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
|
|
);
|
|
});
|
|
|
|
it("generates unique values", { timeout: 5000 }, () => {
|
|
expect(generateUuid()).not.toBe(generateUuid());
|
|
});
|
|
|
|
it("output is lowercase", { timeout: 5000 }, () => {
|
|
const uuid = generateUuid();
|
|
expect(uuid).toBe(uuid.toLowerCase());
|
|
});
|
|
|
|
it("generated UUID can round-trip through uint8Array", {
|
|
timeout: 5000,
|
|
}, () => {
|
|
const uuid = generateUuid();
|
|
expect(uint8ArrayToUuid(uuidToUint8Array(uuid))).toBe(uuid);
|
|
});
|
|
});
|