-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcuid2_test.go
More file actions
172 lines (147 loc) · 5.02 KB
/
cuid2_test.go
File metadata and controls
172 lines (147 loc) · 5.02 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package cuid2
import (
"testing"
)
// External Tests
func TestIsCuid(t *testing.T) {
testCases := map[string]bool{
Generate(): true, // Default
Generate() + Generate() + Generate(): false, // Too Long
"": false, // Too Short
"42": false, // Non-CUID
"aaaaDLL": false, // Capital letters
"yi7rqj1trke": true, // Valid
"-x!ha": false, // Invalid characters
"ab*%@#x": false, // Invalid characters
}
for testCase, expected := range testCases {
if IsCuid(testCase) != expected {
t.Fatalf("Expected IsCuid(%v) to be %v, but got %v", testCase, expected, !expected)
}
}
}
func TestGeneratingInvalidCuid(t *testing.T) {
_, err := Init(WithLength(64))
if err == nil {
t.Fatalf(
"Expected to receive an error for Init(WithLength(64)), but got nothing",
)
}
}
func TestDefaultCuidLength(t *testing.T) {
cuid := Generate()
if len(cuid) != DefaultIdLength {
t.Fatalf("Expected default Cuid length to be %v, but got %v", DefaultIdLength, len(cuid))
}
}
func TestGeneratingCuidWithCustomLength(t *testing.T) {
customLength := 16
generate, err := Init(WithLength(customLength))
if err != nil {
t.Fatalf("Expected to initialize cuid2 generator but received error = %v", err.Error())
}
cuid := generate()
if len(cuid) != customLength {
t.Fatalf("Expected to generate Cuid with a custom length of %v, but got %v", customLength, len(cuid))
}
}
func TestGeneratingCuidWithMaxLength(t *testing.T) {
generate, err := Init(WithLength(MaxIdLength))
if err != nil {
t.Fatalf("Expected to initialize cuid2 generator but received error = %v", err.Error())
}
cuid := generate()
if len(cuid) != MaxIdLength {
t.Fatalf("Expected to generate Cuid with a max length of %v, but got %v", MaxIdLength, cuid)
}
}
// Internal Tests
func TestSessionCounter(t *testing.T) {
var initialSessionCount int64 = 10
sessionCounter := NewSessionCounter(initialSessionCount)
expectedCounts := []int64{11, 12, 13, 14}
actualCounts := []int64{
sessionCounter.Increment(),
sessionCounter.Increment(),
sessionCounter.Increment(),
sessionCounter.Increment(),
}
for index, actualCount := range actualCounts {
expectedCount := expectedCounts[index]
if actualCount != expectedCount {
t.Error("Expected session counts to increment by one for each successive call")
t.Errorf("For an initial session count of %v, expected %v", initialSessionCount, expectedCounts)
t.Fatalf("Got %v", actualCounts)
}
}
}
func TestCreatingFingerprintWithEnvKeyString(t *testing.T) {
mockRandomFunc := func() float64 { return 0.124816 }
mockEnvStr := "KEY1KEY2"
expectedFingerprint := "0auhbh4kdn02a87je880p4dyw34ov0y6xm5ics64ezqi9gymln0jhf5goqp74qt977dm2or7mdw8ohgyawyou6ojbhgma1fcb"
fingerprint := createFingerprint(mockRandomFunc, mockEnvStr)
if fingerprint != expectedFingerprint {
t.Errorf("Fingerprint did not match expected value.\nGot: %s, Expected: %s", fingerprint, expectedFingerprint)
}
}
func TestCreatingFingerprintWithoutEnvKeyString(t *testing.T) {
mockRandomFunc := func() float64 { return 0.2481632 }
expectedFingerprint := "dzgrtvh0dz0agj9g7wy2ww2mc3g8ddogm550pug0qyvdl91ebq3sowoqzi7jyl35sznv4nne4dqbg4dl5c8yo9vfuf3znvojy"
fingerprint := createFingerprint(mockRandomFunc, "")
if fingerprint != expectedFingerprint {
t.Errorf("Fingerprint did not match expected value.\nGot: %s\nExpected: %s", fingerprint, expectedFingerprint)
}
}
func TestDeterminismOfGeneration(t *testing.T) {
testCases := []struct {
name string
length int
fingerprint string
counterStart int64
timeMs int64
randomFloat float64
expectedID string
expectedNextID string
}{
{
name: "Short ID with low random value",
length: 10,
fingerprint: "test-fingerprint",
counterStart: 0,
timeMs: 1751850060928,
randomFloat: 0.1,
expectedID: "c79ab4qwd8",
expectedNextID: "ctfxvev2em",
},
{
name: "Long ID with high random value",
length: 32,
fingerprint: "fruit-salad",
counterStart: 476782360,
timeMs: 1751850806018,
randomFloat: 0.8,
expectedID: "uhqvhs8l0q5ub01c37pgwfqak5az4l2n",
expectedNextID: "u2c7rvhbd6evwn1vj69bye7tj8e7ou2m",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
g := &cuidGenerator {
length: testCase.length,
counter: NewSessionCounter(testCase.counterStart),
fingerprint: testCase.fingerprint,
}
mockRandomFunc := func() float64 {
return testCase.randomFloat
}
firstID := g.generate(testCase.timeMs, mockRandomFunc)
if firstID != testCase.expectedID {
t.Errorf("First ID generated did not match expected.\nGot: %s, Expected: %s", firstID, testCase.expectedID)
}
secondID := g.generate(testCase.timeMs, mockRandomFunc)
if secondID != testCase.expectedNextID {
t.Errorf("Second ID generated did not match expected.\nGot: %s, Expected: %s", secondID, testCase.expectedNextID)
}
})
}
}