From dbad090b5f3043cb597230199c24ed8f7b294a74 Mon Sep 17 00:00:00 2001
From: Shlok Khemani <khemanishlok@gmail.com>
Date: Sun, 22 Dec 2024 13:06:22 +0530
Subject: [PATCH 1/2] chore: Add UUID tests and fix version 5 bits

---
 packages/core/src/tests/uuid.test.ts | 108 +++++++++++++++++++++++++++
 packages/core/src/uuid.ts            |   4 +-
 2 files changed, 111 insertions(+), 1 deletion(-)
 create mode 100644 packages/core/src/tests/uuid.test.ts

diff --git a/packages/core/src/tests/uuid.test.ts b/packages/core/src/tests/uuid.test.ts
new file mode 100644
index 00000000000..5a09d78eb61
--- /dev/null
+++ b/packages/core/src/tests/uuid.test.ts
@@ -0,0 +1,108 @@
+import { beforeEach, describe, expect, it } from "vitest";
+import { stringToUuid } from "../uuid";
+import type { UUID } from "../types";
+
+describe("UUID Module", () => {
+    // Helper function to generate test strings
+    const generateTestString = (): string =>
+        Math.random().toString(36).substring(7);
+
+    // Test data setup
+    let testString: string;
+    let testNumber: number;
+
+    beforeEach(() => {
+        testString = generateTestString();
+        testNumber = Math.floor(Math.random() * 1000);
+    });
+
+    describe("stringToUuid", () => {
+        it("should generate a valid UUID matching the standard format", () => {
+            const result = stringToUuid(testString) as UUID;
+            expect(result).toMatch(
+                /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
+            );
+        });
+
+        it("should generate consistent UUIDs for identical inputs", () => {
+            const input = testString;
+            const uuid1 = stringToUuid(input) as UUID;
+            const uuid2 = stringToUuid(input) as UUID;
+            expect(uuid1).toBe(uuid2);
+        });
+
+        it("should generate unique UUIDs for different inputs", () => {
+            const input1 = testString;
+            const input2 = generateTestString();
+            const uuid1 = stringToUuid(input1) as UUID;
+            const uuid2 = stringToUuid(input2) as UUID;
+            expect(uuid1).not.toBe(uuid2);
+        });
+
+        describe("input handling", () => {
+            it("should convert number inputs to strings correctly", () => {
+                const numberUuid = stringToUuid(testNumber) as UUID;
+                const stringUuid = stringToUuid(testNumber.toString()) as UUID;
+                expect(numberUuid).toBe(stringUuid);
+            });
+
+            it("should throw TypeError for invalid input types", () => {
+                expect(() => stringToUuid(undefined as any)).toThrow(TypeError);
+                expect(() => stringToUuid(null as any)).toThrow(TypeError);
+                expect(() => stringToUuid({} as any)).toThrow(TypeError);
+            });
+
+            it("should handle empty string input", () => {
+                const result = stringToUuid("") as UUID;
+                expect(result).toMatch(
+                    /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
+                );
+            });
+
+            it("should handle Unicode characters and emojis consistently", () => {
+                const unicodeInput = "Hello δΈ–η•Œ! 🌍";
+                const result1 = stringToUuid(unicodeInput) as UUID;
+                const result2 = stringToUuid(unicodeInput) as UUID;
+                expect(result1).toBe(result2);
+                expect(result1).toMatch(
+                    /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
+                );
+            });
+        });
+
+        describe("UUID version and variant bits", () => {
+            it("should set correct version bits (version 5)", () => {
+                const uuid = stringToUuid(testString) as UUID;
+                const versionChar = uuid.split("-")[2][0];
+                expect(versionChar).toBe("5");
+            });
+
+            it("should set correct variant bits (RFC4122)", () => {
+                const uuid = stringToUuid(testString) as UUID;
+                const variantByte = parseInt(
+                    uuid.split("-")[3].slice(0, 2),
+                    16
+                );
+                expect(variantByte >= 0x80 && variantByte <= 0xbf).toBe(true);
+            });
+        });
+
+        describe("encoding handling", () => {
+            it("should handle URL-unsafe characters", () => {
+                const urlUnsafeInput = "test?query=value&param=123";
+                const result = stringToUuid(urlUnsafeInput) as UUID;
+                expect(result).toMatch(
+                    /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
+                );
+            });
+
+            it("should handle very long inputs", () => {
+                const longInput = "a".repeat(1000);
+                const result = stringToUuid(longInput) as UUID;
+                expect(result).toMatch(
+                    /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
+                );
+            });
+        });
+    });
+});
diff --git a/packages/core/src/uuid.ts b/packages/core/src/uuid.ts
index 2227eca2132..414c34f459c 100644
--- a/packages/core/src/uuid.ts
+++ b/packages/core/src/uuid.ts
@@ -37,11 +37,13 @@ export function stringToUuid(target: string | number): UUID {
         hashBuffer[i / 2] = parseInt(hash.slice(i, i + 2), 16);
     }
 
+    hashBuffer[6] = (hashBuffer[6] & 0x0f) | 0x50;
+
     return (_uint8ArrayToHex(hashBuffer.slice(0, 4)) +
         "-" +
         _uint8ArrayToHex(hashBuffer.slice(4, 6)) +
         "-" +
-        _uint8ToHex(hashBuffer[6] & 0x0f) +
+        _uint8ToHex(hashBuffer[6]) +
         _uint8ToHex(hashBuffer[7]) +
         "-" +
         _uint8ToHex((hashBuffer[8] & 0x3f) | 0x80) +

From 5cce789e971a6d7d8e4d6c789e6346b726abaf94 Mon Sep 17 00:00:00 2001
From: Monil Patel <monilpat@gmail.com>
Date: Sat, 11 Jan 2025 13:27:30 -0800
Subject: [PATCH 2/2] Update uuid.ts

update so just adding test
---
 packages/core/src/uuid.ts | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/packages/core/src/uuid.ts b/packages/core/src/uuid.ts
index 414c34f459c..2227eca2132 100644
--- a/packages/core/src/uuid.ts
+++ b/packages/core/src/uuid.ts
@@ -37,13 +37,11 @@ export function stringToUuid(target: string | number): UUID {
         hashBuffer[i / 2] = parseInt(hash.slice(i, i + 2), 16);
     }
 
-    hashBuffer[6] = (hashBuffer[6] & 0x0f) | 0x50;
-
     return (_uint8ArrayToHex(hashBuffer.slice(0, 4)) +
         "-" +
         _uint8ArrayToHex(hashBuffer.slice(4, 6)) +
         "-" +
-        _uint8ToHex(hashBuffer[6]) +
+        _uint8ToHex(hashBuffer[6] & 0x0f) +
         _uint8ToHex(hashBuffer[7]) +
         "-" +
         _uint8ToHex((hashBuffer[8] & 0x3f) | 0x80) +