Skip to content

Commit 8fc6d25

Browse files
authored
Merge pull request #1362 from shlokkhemani/add-uuid-tests-and-fix-version
chore: Add UUID tests and fix version 5 bits
2 parents b92df53 + 5cce789 commit 8fc6d25

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

packages/core/src/tests/uuid.test.ts

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { beforeEach, describe, expect, it } from "vitest";
2+
import { stringToUuid } from "../uuid";
3+
import type { UUID } from "../types";
4+
5+
describe("UUID Module", () => {
6+
// Helper function to generate test strings
7+
const generateTestString = (): string =>
8+
Math.random().toString(36).substring(7);
9+
10+
// Test data setup
11+
let testString: string;
12+
let testNumber: number;
13+
14+
beforeEach(() => {
15+
testString = generateTestString();
16+
testNumber = Math.floor(Math.random() * 1000);
17+
});
18+
19+
describe("stringToUuid", () => {
20+
it("should generate a valid UUID matching the standard format", () => {
21+
const result = stringToUuid(testString) as UUID;
22+
expect(result).toMatch(
23+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
24+
);
25+
});
26+
27+
it("should generate consistent UUIDs for identical inputs", () => {
28+
const input = testString;
29+
const uuid1 = stringToUuid(input) as UUID;
30+
const uuid2 = stringToUuid(input) as UUID;
31+
expect(uuid1).toBe(uuid2);
32+
});
33+
34+
it("should generate unique UUIDs for different inputs", () => {
35+
const input1 = testString;
36+
const input2 = generateTestString();
37+
const uuid1 = stringToUuid(input1) as UUID;
38+
const uuid2 = stringToUuid(input2) as UUID;
39+
expect(uuid1).not.toBe(uuid2);
40+
});
41+
42+
describe("input handling", () => {
43+
it("should convert number inputs to strings correctly", () => {
44+
const numberUuid = stringToUuid(testNumber) as UUID;
45+
const stringUuid = stringToUuid(testNumber.toString()) as UUID;
46+
expect(numberUuid).toBe(stringUuid);
47+
});
48+
49+
it("should throw TypeError for invalid input types", () => {
50+
expect(() => stringToUuid(undefined as any)).toThrow(TypeError);
51+
expect(() => stringToUuid(null as any)).toThrow(TypeError);
52+
expect(() => stringToUuid({} as any)).toThrow(TypeError);
53+
});
54+
55+
it("should handle empty string input", () => {
56+
const result = stringToUuid("") as UUID;
57+
expect(result).toMatch(
58+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
59+
);
60+
});
61+
62+
it("should handle Unicode characters and emojis consistently", () => {
63+
const unicodeInput = "Hello 世界! 🌍";
64+
const result1 = stringToUuid(unicodeInput) as UUID;
65+
const result2 = stringToUuid(unicodeInput) as UUID;
66+
expect(result1).toBe(result2);
67+
expect(result1).toMatch(
68+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
69+
);
70+
});
71+
});
72+
73+
describe("UUID version and variant bits", () => {
74+
it("should set correct version bits (version 5)", () => {
75+
const uuid = stringToUuid(testString) as UUID;
76+
const versionChar = uuid.split("-")[2][0];
77+
expect(versionChar).toBe("5");
78+
});
79+
80+
it("should set correct variant bits (RFC4122)", () => {
81+
const uuid = stringToUuid(testString) as UUID;
82+
const variantByte = parseInt(
83+
uuid.split("-")[3].slice(0, 2),
84+
16
85+
);
86+
expect(variantByte >= 0x80 && variantByte <= 0xbf).toBe(true);
87+
});
88+
});
89+
90+
describe("encoding handling", () => {
91+
it("should handle URL-unsafe characters", () => {
92+
const urlUnsafeInput = "test?query=value&param=123";
93+
const result = stringToUuid(urlUnsafeInput) as UUID;
94+
expect(result).toMatch(
95+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
96+
);
97+
});
98+
99+
it("should handle very long inputs", () => {
100+
const longInput = "a".repeat(1000);
101+
const result = stringToUuid(longInput) as UUID;
102+
expect(result).toMatch(
103+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
104+
);
105+
});
106+
});
107+
});
108+
});

0 commit comments

Comments
 (0)