Implementation of TypeID in Lua
1local uuid7 = require("uuid7")
2
3describe("UUID7", function()
4 it("generates a valid UUIDv7 integer array", function()
5 local bytes = uuid7.as_table()
6
7 assert.are.equal(16, #bytes)
8
9 -- all values are valid bytes (0-255)
10 for _, byte in ipairs(bytes) do
11 assert.is_true(byte >= 0 and byte <= 255)
12 end
13
14 -- version, 7th byte
15 assert.are.equal(7, (bytes[7] >> 4) & 0xf)
16
17 -- variant, 9th byte
18 assert.are.equal(2, (bytes[9] >> 6) & 0x3)
19 end)
20
21 it("converts a byte table to a valid UUID string", function()
22 local bytes = {
23 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x70, 0x08,
24 0x80, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10
25 }
26
27 local uuid_str = uuid7.to_string(bytes)
28
29 assert.are.equal("01020304-0506-7008-800a-0b0c0d0e0f10", uuid_str)
30 end)
31
32 it("generates a valid UUIDv7 string", function()
33 local uuid_str = uuid7.generate()
34
35 -- Check format: 8-4-4-4-12 (36 chars with hyphens)
36 assert.are.equal(36, #uuid_str)
37 assert.is_true(uuid_str:match("^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$") ~= nil)
38
39 -- Check version (position 15, should be 7)
40 assert.are.equal("7", uuid_str:sub(15, 15))
41
42 -- Check variant (position 20, should be 8, 9, a, or b)
43 assert.is_true(uuid_str:sub(20, 20):match("[89ab]") ~= nil)
44 end)
45end)