-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.test.js
138 lines (121 loc) · 3.91 KB
/
index.test.js
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
const test = require('node:test')
const assert = require('node:assert').strict
const kuuid = require('./index.js')
const sleep = async (ms) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms)
})
}
test('should return 32 character id', function () {
const x = kuuid.id()
assert.strictEqual(x.length, 32)
})
test('should return ids that sort correctly', async function (done) {
//this.timeout(30000)
const ids = []
for(let i = 0; i < 20; i++) {
ids.push(kuuid.id())
await sleep(1100)
}
const j1 = JSON.stringify(ids)
ids.sort()
const j2 = JSON.stringify(ids)
// make sure sorting has had no effect i.e. they were sorted already
assert.strictEqual(j1, j2)
})
test('should return ids that sort correctly - reverse mode', async function (done) {
//this.timeout(30000)
const ids = []
for(let i = 0; i < 20; i++) {
ids.push(kuuid.idr())
await sleep(1100)
}
const j1 = JSON.stringify(ids)
ids.sort().reverse()
const j2 = JSON.stringify(ids)
// make sure sorting has had no effect i.e. they were sorted already
assert.strictEqual(j1, j2)
})
test('should return unique ids', function () {
//this.timeout(30000)
const ids = []
for (let i = 0; i < 10000; i++) {
const k = kuuid.id()
assert.strictEqual(-1, ids.indexOf(k))
ids.push(k)
}
})
test('should take a user-supplied timestamp', function () {
const id = kuuid.id('2018-01-01T00:00:00.000Z')
const prefix1 = id.substring(0, 8)
const prefix2 = kuuid.prefix('2018-01-01T00:00:00.000Z')
assert.strictEqual(prefix1, '001eVnWK')
assert.strictEqual(prefix2, prefix1)
})
test('should generate prefix for the epoch', function () {
const prefix = kuuid.prefix('1970-01-01T00:00:00.000Z')
assert.strictEqual(prefix, '00000000')
})
test('should generate prefix for the epoch + 1 year', function () {
const prefix = kuuid.prefix('1971-01-01T00:00:00.000Z')
assert.strictEqual(prefix, '00028JxA')
})
test('should generate prefix for the epoch + 10 year', function () {
const prefix = kuuid.prefix('1980-01-01T00:00:00.000Z')
assert.strictEqual(prefix, '000LLwUi')
})
test('should generate prefix for the epoch + 50 year', function () {
const prefix = kuuid.prefix('2020-01-01T00:00:00.000Z')
assert.strictEqual(prefix, '001imRQe')
})
test('should generate prefix for the epoch + 200 year', function () {
const prefix = kuuid.prefix('2170-01-01T00:00:00.000Z')
assert.strictEqual(prefix, '006t88C8')
})
test('should generate prefix with a numerical offset', function () {
const prefix = kuuid.prefix(1514764800000)
assert.strictEqual(prefix, '001eVnWK')
})
test('should generate reverse prefix for the epoch', function () {
const prefix = kuuid.prefixReverse('1970-01-01T00:00:00Z')
assert.strictEqual(prefix, 'zzzzzzzz')
})
test('should generate reverse prefix for the epoch + 1', function () {
const prefix = kuuid.prefixReverse('1970-01-01T00:00:01Z')
assert.strictEqual(prefix, 'zzzzzzzy')
})
test('should generate random data', function () {
//this.timeout(30000)
const ids = []
for (let i = 0; i < 10000; i++) {
const k = kuuid.rand()
assert.strictEqual(-1, ids.indexOf(k))
ids.push(k)
}
})
test('should return ids that sort correctly - short', async function (done) {
// this.timeout(30000)
const ids = []
for(let i = 0; i < 20; i++) {
ids.push(kuuid.ids())
await sleep(1100)
}
const j1 = JSON.stringify(ids)
ids.sort()
const j2 = JSON.stringify(ids)
// make sure sorting has had no effect i.e. they were sorted already
assert.strictEqual(j1, j2)
})
test('should return ids that sort correctly - short reverse mode', async function (done) {
// this.timeout(30000)
const ids = []
for(let i = 0; i < 20; i++) {
ids.push(kuuid.idsr())
await sleep(1100)
}
const j1 = JSON.stringify(ids)
ids.sort().reverse()
const j2 = JSON.stringify(ids)
// make sure sorting has had no effect i.e. they were sorted already
assert.strictEqual(j1, j2)
})