-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.cjs
More file actions
35 lines (30 loc) · 1.04 KB
/
index.test.cjs
File metadata and controls
35 lines (30 loc) · 1.04 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
const generate = require('./index.cjs');
const { createSnowflakeGenerator } = require('./index.cjs');
const { test, expect } = require('@jest/globals');
const EPOCH = 1420070400000n;
test('generate() returns a numeric string parseable to BigInt', () => {
const id = generate();
expect(typeof id).toBe('string');
expect(/^[0-9]+$/.test(id)).toBe(true);
const n = BigInt(id);
expect(n > 0n).toBe(true);
});
test('consecutive IDs are monotonically increasing', () => {
const a = BigInt(generate());
const b = BigInt(generate());
expect(b > a).toBe(true);
});
test('workerId is encoded correctly', () => {
const gen = createSnowflakeGenerator({ workerId: 7 });
const id = BigInt(gen());
const worker = (id >> 12n) & 0x3ffn;
expect(worker).toBe(7n);
});
test('timestamp portion decodes to a recent time', () => {
const id = BigInt(generate());
const ts = (id >> 22n) + EPOCH;
const now = BigInt(Date.now());
// timestamp should be within the last minute
expect(ts <= now).toBe(true);
expect(now - ts < 60000n).toBe(true);
});