-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.test.ts
More file actions
122 lines (105 loc) · 4.28 KB
/
Copy pathindex.test.ts
File metadata and controls
122 lines (105 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { expect } from "@std/expect";
import { describe, it } from "@std/testing/bdd";
import { uuid58Encode, Uuid58EncodeError, uuid58EncodeSafe } from "./encode.ts";
import { uuid58Decode, Uuid58DecodeError, uuid58DecodeSafe } from "./decode.ts";
import { uuid58 } from "./generate.ts";
const samples = [
"00000000-0000-0000-0000-000000000000", // All zeros
"ffffffff-ffff-ffff-ffff-ffffffffffff", // All ff
"f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf", // Random
"123e4567-e89b-12d3-a456-426614174000", // RFC 4122 example
"00112233-4455-6677-8899-aabbccddeeff", // Contains leading 00 bytes
];
describe("uuid58Encode / uuid58Decode round-trip", () => {
it("encodes then decodes back to the identical UUID (case-insensitive)", () => {
for (const uuid of samples) {
const short = uuid58Encode(uuid);
const restored = uuid58Decode(short);
expect(restored).toBe(uuid.toLowerCase());
}
});
it("returns a Base58 string of ~22 chars for non-degenerate UUIDs", () => {
const uuid = "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf";
const short = uuid58Encode(uuid);
expect(short.length).toBe(22);
});
it("throws on malformed UUID input", () => {
expect(() => uuid58Encode("not-a-uuid")).toThrow(Uuid58EncodeError);
expect(() => uuid58Encode("12345678-1234")).toThrow(Uuid58EncodeError);
});
it("throws on malformed Base58 input", () => {
expect(() => uuid58Decode("O0lI")).toThrow(Uuid58DecodeError); // Contains forbidden characters
expect(() => uuid58Decode("$$$")).toThrow(Uuid58DecodeError);
});
it("throws on Base58 strings that are too short", () => {
expect(() => uuid58Decode("UoWww8DGaVGLtea7zU7p")).toThrow(
Uuid58DecodeError,
);
});
it("throws on Base58 strings that are too long", () => {
expect(() => uuid58Decode("1111UoWww8DGaVGLtea7zU7p")).toThrow(
Uuid58DecodeError,
);
});
});
describe("uuid58EncodeSafe / uuid58DecodeSafe", () => {
it("encodes and decodes correctly (safe)", () => {
for (const uuid of samples) {
const encoded = uuid58EncodeSafe(uuid);
expect(encoded).not.toBeInstanceOf(Uuid58EncodeError);
if (encoded instanceof Uuid58EncodeError) throw encoded;
const decoded = uuid58DecodeSafe(encoded);
expect(decoded).not.toBeInstanceOf(Uuid58DecodeError);
if (decoded instanceof Uuid58DecodeError) throw decoded;
expect(decoded).toBe(uuid.toLowerCase());
}
});
it("returns Uuid58EncodeError on malformed UUID input (safe)", () => {
expect(uuid58EncodeSafe("not-a-uuid")).toBeInstanceOf(Uuid58EncodeError);
expect(uuid58EncodeSafe("12345678-1234")).toBeInstanceOf(Uuid58EncodeError);
});
it("returns Uuid58DecodeError on malformed Base58 input (safe)", () => {
expect(uuid58DecodeSafe("O0lI")).toBeInstanceOf(Uuid58DecodeError);
expect(uuid58DecodeSafe("$$$")).toBeInstanceOf(Uuid58DecodeError);
});
});
describe("uuid58", () => {
it("round-trips", () => {
const short = uuid58();
expect(uuid58Encode(uuid58Decode(short))).toBe(short);
});
it("generates valid UUID v4 with variant 1", () => {
const short = uuid58();
const decoded = uuid58Decode(short);
// Check UUID format (8-4-4-4-12 pattern)
expect(decoded).toMatch(
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,
);
// Check version 4 (13th character should be '4')
expect(decoded[14]).toBe("4");
// Check variant 1 (17th character should be '8', '9', 'a', or 'b')
expect(["8", "9", "a", "b"]).toContain(decoded[19]);
});
});
describe("uuid58Encode is case-insensitive and ignores hyphens", () => {
const uuidVariants = [
"f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf",
"F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF",
"f4b247fd1f8745d4aa061c6fc0a8dfaf",
"F4B247FD1F8745D4AA061C6FC0A8DFAF",
];
it("returns the same Base58 string for all case/hyphen variants", () => {
const encodedSet = new Set(uuidVariants.map(uuid58Encode));
expect(encodedSet.size).toBe(1);
});
it("safe version also returns the same Base58 string for all case/hyphen variants", () => {
const encodedSet = new Set(
uuidVariants.map((uuid) => {
const result = uuid58EncodeSafe(uuid);
if (result instanceof Uuid58EncodeError) throw result;
return result;
}),
);
expect(encodedSet.size).toBe(1);
});
});