import { afterEach, expect, test, vi } from "vitest" import { assertUuid, generateUuidV4, generateUuidV4FromUrl, generateUuidV7, getUuidVersion, isUuid, } from "#Source/identifier/index.ts" afterEach(() => { vi.restoreAllMocks() }) test("isUuid validates UUID input", () => { expect(isUuid("550e8400-e29b-41d4-a716-446655440000")).toBe(true) expect(isUuid("550E8400-E29B-41D4-A716-446655440000")).toBe(true) expect(isUuid("123e4567-e89b-12d3-a456-426614174000")).toBe(true) expect(isUuid("not-a-uuid")).toBe(false) expect(isUuid("550e8400e29b41d4a716446655440000")).toBe(false) expect(isUuid("550e8400-e29b-91d4-a716-446655440000")).toBe(false) expect(isUuid("550e8400-e29b-41d4-c716-446655440000")).toBe(false) }) test("assertUuid throws on malformed input", () => { expect(() => assertUuid("550e8400-e29b-41d4-a716-446655440000")).not.toThrow() expect(() => assertUuid("not-a-uuid")).toThrow(TypeError) expect(() => assertUuid("550e8400e29b41d4a716446655440000")).toThrow(TypeError) }) test("getUuidVersion returns UUID version and throws on malformed input", () => { expect(getUuidVersion("550e8400-e29b-41d4-a716-446655440000")).toBe(4) expect(getUuidVersion("123e4567-e89b-12d3-a456-426614174000")).toBe(1) expect(() => getUuidVersion("not-a-uuid")).toThrow(TypeError) }) test("generateUuid returns RFC 4122 version-4 UUID format", () => { const UUID_V4_REGEXP = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i const value1 = generateUuidV4() const value2 = generateUuidV4() expect(value1).toMatch(UUID_V4_REGEXP) expect(value2).toMatch(UUID_V4_REGEXP) expect(value1).not.toBe(value2) }) test("generateUuidV4FromUrl extracts UUID from object URL and revokes URL", () => { const mockedUuid = "1e34c01e-ab7c-4e60-b6f3-a6101e8c0d2d" const mockedObjectUrl = `blob:https://www.cigaret.world/${mockedUuid}` const createObjectUrlSpy = vi.spyOn(URL, "createObjectURL").mockReturnValue(mockedObjectUrl) const revokeObjectUrlSpy = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => { return undefined }) const value = generateUuidV4FromUrl() expect(value).toBe(mockedUuid) expect(isUuid(value)).toBe(true) expect(createObjectUrlSpy).toHaveBeenCalledTimes(1) expect(revokeObjectUrlSpy).toHaveBeenCalledTimes(1) expect(revokeObjectUrlSpy).toHaveBeenCalledWith(mockedObjectUrl) }) test("generateUuidV7 returns UUIDv7 format and is monotonic within the same millisecond", () => { const UUID_V7_REGEXP = /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i vi.spyOn(Date, "now").mockReturnValue(1_700_000_000_000) vi.spyOn(crypto, "getRandomValues").mockImplementation((buffer) => { if (buffer === null) { return buffer } const target = buffer as Uint8Array target.fill(0) return buffer }) const value1 = generateUuidV7() const value2 = generateUuidV7() const value3 = generateUuidV7() expect(value1).toMatch(UUID_V7_REGEXP) expect(value2).toMatch(UUID_V7_REGEXP) expect(value3).toMatch(UUID_V7_REGEXP) expect(value1 < value2).toBe(true) expect(value2 < value3).toBe(true) })