[79a0317] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | const assert = require("assert");
|
---|
| 4 | const node_test_1 = require("node:test");
|
---|
| 5 | const parse_js_1 = require("../parse.js");
|
---|
| 6 | const v1_js_1 = require("../v1.js");
|
---|
| 7 | const TIME = 1321644961388;
|
---|
| 8 | const RFC_V1 = 'c232ab00-9414-11ec-b3c8-9f68deced846';
|
---|
| 9 | const RFC_V1_BYTES = (0, parse_js_1.default)(RFC_V1);
|
---|
| 10 | const RFC_OPTIONS = {
|
---|
| 11 | msecs: 0x17f22e279b0,
|
---|
| 12 | nsecs: 0,
|
---|
| 13 | clockseq: 0x33c8,
|
---|
| 14 | node: Uint8Array.of(0x9f, 0x68, 0xde, 0xce, 0xd8, 0x46),
|
---|
| 15 | };
|
---|
| 16 | const RFC_RANDOM = Uint8Array.of(0, 0, 0, 0, 0, 0, 0, 0, RFC_OPTIONS.clockseq >> 8, RFC_OPTIONS.clockseq & 0xff, ...RFC_OPTIONS.node);
|
---|
| 17 | function compareV1TimeField(a, b) {
|
---|
| 18 | a = a.split('-').slice(0, 3).reverse().join('');
|
---|
| 19 | b = b.split('-').slice(0, 3).reverse().join('');
|
---|
| 20 | return a < b ? -1 : a > b ? 1 : 0;
|
---|
| 21 | }
|
---|
| 22 | (0, node_test_1.describe)('v1', () => {
|
---|
| 23 | (0, node_test_1.default)('v1 sort order (default)', () => {
|
---|
| 24 | const ids = [(0, v1_js_1.default)(), (0, v1_js_1.default)(), (0, v1_js_1.default)(), (0, v1_js_1.default)(), (0, v1_js_1.default)()];
|
---|
| 25 | const sorted = [...ids].sort(compareV1TimeField);
|
---|
| 26 | assert.deepEqual(ids, sorted);
|
---|
| 27 | });
|
---|
| 28 | (0, node_test_1.default)('v1 sort order (time option)', () => {
|
---|
| 29 | const ids = [
|
---|
| 30 | (0, v1_js_1.default)({ msecs: TIME - 10 * 3600 * 1000 }),
|
---|
| 31 | (0, v1_js_1.default)({ msecs: TIME - 1 }),
|
---|
| 32 | (0, v1_js_1.default)({ msecs: TIME }),
|
---|
| 33 | (0, v1_js_1.default)({ msecs: TIME + 1 }),
|
---|
| 34 | (0, v1_js_1.default)({ msecs: TIME + 28 * 24 * 3600 * 1000 }),
|
---|
| 35 | ];
|
---|
| 36 | const sorted = [...ids].sort(compareV1TimeField);
|
---|
| 37 | assert.deepEqual(ids, sorted);
|
---|
| 38 | });
|
---|
| 39 | (0, node_test_1.default)('v1(options)', () => {
|
---|
| 40 | assert.equal((0, v1_js_1.default)({ msecs: RFC_OPTIONS.msecs, random: RFC_RANDOM }), RFC_V1, 'minimal options');
|
---|
| 41 | assert.equal((0, v1_js_1.default)(RFC_OPTIONS), RFC_V1, 'full options');
|
---|
| 42 | });
|
---|
| 43 | (0, node_test_1.default)('v1(options) equality', () => {
|
---|
| 44 | assert.notEqual((0, v1_js_1.default)({ msecs: TIME }), (0, v1_js_1.default)({ msecs: TIME }), 'UUIDs with minimal options differ');
|
---|
| 45 | assert.equal((0, v1_js_1.default)(RFC_OPTIONS), (0, v1_js_1.default)(RFC_OPTIONS), 'UUIDs with full options are identical');
|
---|
| 46 | });
|
---|
| 47 | (0, node_test_1.default)('fills one UUID into a buffer as expected', () => {
|
---|
| 48 | const buffer = new Uint8Array(16);
|
---|
| 49 | const result = (0, v1_js_1.default)(RFC_OPTIONS, buffer);
|
---|
| 50 | assert.deepEqual(buffer, RFC_V1_BYTES);
|
---|
| 51 | assert.strictEqual(buffer, result);
|
---|
| 52 | });
|
---|
| 53 | (0, node_test_1.default)('fills two UUIDs into a buffer as expected', () => {
|
---|
| 54 | const buffer = new Uint8Array(32);
|
---|
| 55 | (0, v1_js_1.default)(RFC_OPTIONS, buffer, 0);
|
---|
| 56 | (0, v1_js_1.default)(RFC_OPTIONS, buffer, 16);
|
---|
| 57 | const expectedBuf = new Uint8Array(32);
|
---|
| 58 | expectedBuf.set(RFC_V1_BYTES);
|
---|
| 59 | expectedBuf.set(RFC_V1_BYTES, 16);
|
---|
| 60 | assert.deepEqual(buffer, expectedBuf);
|
---|
| 61 | });
|
---|
| 62 | (0, node_test_1.default)('v1() state transitions', () => {
|
---|
| 63 | const PRE_STATE = {
|
---|
| 64 | msecs: 10,
|
---|
| 65 | nsecs: 20,
|
---|
| 66 | clockseq: 0x1234,
|
---|
| 67 | node: Uint8Array.of(0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc),
|
---|
| 68 | };
|
---|
| 69 | const tests = [
|
---|
| 70 | {
|
---|
| 71 | title: 'initial state',
|
---|
| 72 | state: {},
|
---|
| 73 | now: 10,
|
---|
| 74 | expected: {
|
---|
| 75 | msecs: 10,
|
---|
| 76 | nsecs: 0,
|
---|
| 77 | clockseq: RFC_OPTIONS.clockseq,
|
---|
| 78 | node: RFC_OPTIONS.node,
|
---|
| 79 | },
|
---|
| 80 | },
|
---|
| 81 | {
|
---|
| 82 | title: 'same time interval',
|
---|
| 83 | state: { ...PRE_STATE },
|
---|
| 84 | now: PRE_STATE.msecs,
|
---|
| 85 | expected: {
|
---|
| 86 | ...PRE_STATE,
|
---|
| 87 | nsecs: 21,
|
---|
| 88 | },
|
---|
| 89 | },
|
---|
| 90 | {
|
---|
| 91 | title: 'new time interval',
|
---|
| 92 | state: { ...PRE_STATE },
|
---|
| 93 | now: PRE_STATE.msecs + 1,
|
---|
| 94 | expected: {
|
---|
| 95 | ...PRE_STATE,
|
---|
| 96 | msecs: PRE_STATE.msecs + 1,
|
---|
| 97 | nsecs: 0,
|
---|
| 98 | },
|
---|
| 99 | },
|
---|
| 100 | {
|
---|
| 101 | title: 'same time interval (nsecs overflow)',
|
---|
| 102 | state: { ...PRE_STATE, nsecs: 9999 },
|
---|
| 103 | now: PRE_STATE.msecs,
|
---|
| 104 | expected: {
|
---|
| 105 | ...PRE_STATE,
|
---|
| 106 | nsecs: 0,
|
---|
| 107 | clockseq: RFC_OPTIONS.clockseq,
|
---|
| 108 | node: RFC_OPTIONS.node,
|
---|
| 109 | },
|
---|
| 110 | },
|
---|
| 111 | {
|
---|
| 112 | title: 'time regression',
|
---|
| 113 | state: { ...PRE_STATE },
|
---|
| 114 | now: PRE_STATE.msecs - 1,
|
---|
| 115 | expected: {
|
---|
| 116 | ...PRE_STATE,
|
---|
| 117 | msecs: PRE_STATE.msecs - 1,
|
---|
| 118 | clockseq: RFC_OPTIONS.clockseq,
|
---|
| 119 | node: RFC_OPTIONS.node,
|
---|
| 120 | },
|
---|
| 121 | },
|
---|
| 122 | ];
|
---|
| 123 | for (const { title, state, now, expected } of tests) {
|
---|
| 124 | assert.deepStrictEqual((0, v1_js_1.updateV1State)(state, now, RFC_RANDOM), expected, `Failed: ${title}`);
|
---|
| 125 | }
|
---|
| 126 | });
|
---|
| 127 | });
|
---|