1 | import * as assert from 'assert';
|
---|
2 | import test, { describe } from 'node:test';
|
---|
3 | import v1ToV6 from '../v1ToV6.js';
|
---|
4 | import v6 from '../v6.js';
|
---|
5 | import v6ToV1 from '../v6ToV1.js';
|
---|
6 | describe('v6', () => {
|
---|
7 | const V1_ID = 'f1207660-21d2-11ef-8c4f-419efbd44d48';
|
---|
8 | const V6_ID = '1ef21d2f-1207-6660-8c4f-419efbd44d48';
|
---|
9 | const fullOptions = {
|
---|
10 | msecs: 0x133b891f705,
|
---|
11 | nsecs: 0x1538,
|
---|
12 | clockseq: 0x385c,
|
---|
13 | node: Uint8Array.of(0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10),
|
---|
14 | };
|
---|
15 | const EXPECTED_BYTES = Uint8Array.of(0x1e, 0x11, 0x22, 0xbd, 0x94, 0x28, 0x68, 0x88, 0xb8, 0x5c, 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10);
|
---|
16 | test('default behavior', () => {
|
---|
17 | const id = v6();
|
---|
18 | assert.ok(/[0-9a-f]{8}-[0-9a-f]{4}-6[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/.test(id), 'id is valid v6 UUID');
|
---|
19 | });
|
---|
20 | test('default behavior (binary type)', () => {
|
---|
21 | const buffer = new Uint8Array(16);
|
---|
22 | const result = v6(fullOptions, buffer);
|
---|
23 | assert.deepEqual(buffer, EXPECTED_BYTES);
|
---|
24 | assert.strictEqual(buffer, result);
|
---|
25 | });
|
---|
26 | test('all options', () => {
|
---|
27 | const id = v6(fullOptions);
|
---|
28 | assert.equal(id, '1e1122bd-9428-6888-b85c-61cd3cbb3210');
|
---|
29 | });
|
---|
30 | test('sort by creation time', () => {
|
---|
31 | const ids = [];
|
---|
32 | for (let i = 0; i < 5; i++) {
|
---|
33 | ids.push(v6({ msecs: i * 1000 }));
|
---|
34 | }
|
---|
35 | assert.deepEqual(ids, ids.slice().sort());
|
---|
36 | });
|
---|
37 | test('creating at array offset', () => {
|
---|
38 | const buffer = new Uint8Array(32);
|
---|
39 | v6(fullOptions, buffer, 0);
|
---|
40 | v6(fullOptions, buffer, 16);
|
---|
41 | const expectedBuf = new Uint8Array(32);
|
---|
42 | expectedBuf.set(EXPECTED_BYTES, 0);
|
---|
43 | expectedBuf.set(EXPECTED_BYTES, 16);
|
---|
44 | assert.deepEqual(buffer, expectedBuf);
|
---|
45 | });
|
---|
46 | test('v1 -> v6 conversion', () => {
|
---|
47 | const id = v1ToV6(V1_ID);
|
---|
48 | assert.equal(id, V6_ID);
|
---|
49 | });
|
---|
50 | test('v6 -> v1 conversion', () => {
|
---|
51 | const id = v6ToV1(V6_ID);
|
---|
52 | assert.equal(id, V1_ID);
|
---|
53 | });
|
---|
54 | });
|
---|