Setting the file. One moment. UUID Test · Clickhouse Js Node Rowbinary · ClickHouse/agent-skills · Skills Docs(opens in a new tab)
tests/UUID.test.ts
TypeScript·119 lines·4 KB
7
return
new
Cursor
(
await
query
(
`SELECT ${
expr
} FORMAT RowBinary`
));
8}
9
10describe("readUUID", () => {
11 it("returns the raw 16 bytes and formats them (per-half byte order)", async () => {
12 const r = await reader("toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0')");
13 const bytes = readUUID(r);
14 expect(r.pos).toBe(16);
15 // Wire layout: each LE UInt64 half is byte-reversed vs the text form.
16 expect(bytes).toEqual(
17 Buffer.from([
18 0xe7, 0x11, 0xb3, 0x5c, 0x04, 0xc4, 0xf0, 0x61, 0xa0, 0xdb, 0xd3, 0x6a,
19 0x00, 0xa6, 0x7b, 0x90,
20 ]),
21 );
22 expect(formatUUID(bytes)).toBe("61f0c404-5cb3-11e7-907b-a6006ad3dba0");
23 });
24
25 it("formats the nil UUID", async () => {
26 expect(
27 formatUUID(
28 readUUID(
29 await reader("toUUID('00000000-0000-0000-0000-000000000000')"),
30 ),
31 ),
32 ).toBe("00000000-0000-0000-0000-000000000000");
33 });
34
35 // Smallest non-zero value: exercises zero-padding of leading hex digits.
36 it("zero-pads leading digits", async () => {
37 expect(
38 formatUUID(
39 readUUID(
40 await reader("toUUID('00000000-0000-0000-0000-000000000001')"),
41 ),
42 ),
43 ).toBe("00000000-0000-0000-0000-000000000001");
44 });
45
46 it("formats the all-ones UUID", async () => {
47 expect(
48 formatUUID(
49 readUUID(
50 await reader("toUUID('ffffffff-ffff-ffff-ffff-ffffffffffff')"),
51 ),
52 ),
53 ).toBe("ffffffff-ffff-ffff-ffff-ffffffffffff");
54 });
55
56 // The fast formatter: formatUUIDTable must match formatUUID exactly on the
57 // same raw bytes (lookup table instead of BigInt).
58 describe("formatUUIDTable (fast lookup-table formatter)", () => {
59 it("matches formatUUID for a typical value", async () => {
60 const b = readUUID(
61 await reader("toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0')"),
62 );
63 expect(formatUUIDTable(b)).toBe("61f0c404-5cb3-11e7-907b-a6006ad3dba0");
64 expect(formatUUIDTable(b)).toBe(formatUUID(b));
65 });
66
67 it("zero-pads leading digits and formats the nil UUID", async () => {
68 expect(
69 formatUUIDTable(
70 readUUID(
71 await reader("toUUID('00000000-0000-0000-0000-000000000001')"),
72 ),
73 ),
74 ).toBe("00000000-0000-0000-0000-000000000001");
75 expect(
76 formatUUIDTable(
77 readUUID(
78 await reader("toUUID('00000000-0000-0000-0000-000000000000')"),
79 ),
80 ),
81 ).toBe("00000000-0000-0000-0000-000000000000");
82 });
83
84 it("reuses the shared output buffer across calls without corruption", async () => {
85 const a = formatUUIDTable(
86 readUUID(
87 await reader("toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0')"),
88 ),
89 );
90 const b = formatUUIDTable(
91 readUUID(
92 await reader("toUUID('ffffffff-ffff-ffff-ffff-ffffffffffff')"),
93 ),
94 );
95 expect(a).toBe("61f0c404-5cb3-11e7-907b-a6006ad3dba0"); // earlier result is a copied string, not clobbered
96 expect(b).toBe("ffffffff-ffff-ffff-ffff-ffffffffffff");
97 });
98 });
99
100 describe("advance() edge cases", () => {
101 it("throws NeedMoreData for every incomplete prefix (0 .. full.length-1)", async () => {
102 const full = await query(
103 "SELECT toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0') FORMAT RowBinary",
104 );
105 for (let len = 0; len < full.length; len++) {
106 const r = new Cursor(full.subarray(0, len));
107 let thrown: unknown;
108 try {
109 readUUID(r);
110 } catch (e) {
111 thrown = e;
112 }
113 expect(thrown, `prefix length ${len} of ${full.length}`).toBe(
114 NeedMoreData,
115 );
116 }
117 });
118 });
119});