[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 stringify_js_1 = require("../stringify.js");
|
---|
| 7 | const v7_js_1 = require("../v7.js");
|
---|
| 8 | const RFC_V7 = '017f22e2-79b0-7cc3-98c4-dc0c0c07398f';
|
---|
| 9 | const RFC_V7_BYTES = (0, parse_js_1.default)('017f22e2-79b0-7cc3-98c4-dc0c0c07398f');
|
---|
| 10 | const RFC_MSECS = 0x17f22e279b0;
|
---|
| 11 | const RFC_SEQ = (0x0cc3 << 20) | (0x98c4dc >> 2);
|
---|
| 12 | const RFC_RANDOM = Uint8Array.of(0x10, 0x91, 0x56, 0xbe, 0xc4, 0xfb, 0x0c, 0xc3, 0x18, 0xc4, 0x6c, 0x0c, 0x0c, 0x07, 0x39, 0x8f);
|
---|
| 13 | (0, node_test_1.describe)('v7', () => {
|
---|
| 14 | (0, node_test_1.default)('subsequent UUIDs are different', () => {
|
---|
| 15 | const id1 = (0, v7_js_1.default)();
|
---|
| 16 | const id2 = (0, v7_js_1.default)();
|
---|
| 17 | assert.ok(id1 !== id2);
|
---|
| 18 | });
|
---|
| 19 | (0, node_test_1.default)('explicit options.random and options.msecs produces expected result', () => {
|
---|
| 20 | const id = (0, v7_js_1.default)({
|
---|
| 21 | random: RFC_RANDOM,
|
---|
| 22 | msecs: RFC_MSECS,
|
---|
| 23 | seq: RFC_SEQ,
|
---|
| 24 | });
|
---|
| 25 | assert.strictEqual(id, RFC_V7);
|
---|
| 26 | });
|
---|
| 27 | (0, node_test_1.default)('explicit options.rng produces expected result', () => {
|
---|
| 28 | const id = (0, v7_js_1.default)({
|
---|
| 29 | rng: () => RFC_RANDOM,
|
---|
| 30 | msecs: RFC_MSECS,
|
---|
| 31 | seq: RFC_SEQ,
|
---|
| 32 | });
|
---|
| 33 | assert.strictEqual(id, RFC_V7);
|
---|
| 34 | });
|
---|
| 35 | (0, node_test_1.default)('explicit options.msecs produces expected result', () => {
|
---|
| 36 | const id = (0, v7_js_1.default)({
|
---|
| 37 | msecs: RFC_MSECS,
|
---|
| 38 | });
|
---|
| 39 | assert.strictEqual(id.indexOf('017f22e2'), 0);
|
---|
| 40 | });
|
---|
| 41 | (0, node_test_1.default)('fills one UUID into a buffer as expected', () => {
|
---|
| 42 | const buffer = new Uint8Array(16);
|
---|
| 43 | const result = (0, v7_js_1.default)({
|
---|
| 44 | random: RFC_RANDOM,
|
---|
| 45 | msecs: RFC_MSECS,
|
---|
| 46 | seq: RFC_SEQ,
|
---|
| 47 | }, buffer);
|
---|
| 48 | (0, stringify_js_1.default)(buffer);
|
---|
| 49 | assert.deepEqual(buffer, RFC_V7_BYTES);
|
---|
| 50 | assert.strictEqual(buffer, result);
|
---|
| 51 | });
|
---|
| 52 | (0, node_test_1.default)('fills two UUIDs into a buffer as expected', () => {
|
---|
| 53 | const buffer = new Uint8Array(32);
|
---|
| 54 | (0, v7_js_1.default)({
|
---|
| 55 | random: RFC_RANDOM,
|
---|
| 56 | msecs: RFC_MSECS,
|
---|
| 57 | seq: RFC_SEQ,
|
---|
| 58 | }, buffer, 0);
|
---|
| 59 | (0, v7_js_1.default)({
|
---|
| 60 | random: RFC_RANDOM,
|
---|
| 61 | msecs: RFC_MSECS,
|
---|
| 62 | seq: RFC_SEQ,
|
---|
| 63 | }, buffer, 16);
|
---|
| 64 | const expected = new Uint8Array(32);
|
---|
| 65 | expected.set(RFC_V7_BYTES);
|
---|
| 66 | expected.set(RFC_V7_BYTES, 16);
|
---|
| 67 | assert.deepEqual(buffer, expected);
|
---|
| 68 | });
|
---|
| 69 | (0, node_test_1.default)('lexicographical sorting is preserved', () => {
|
---|
| 70 | let id;
|
---|
| 71 | let prior;
|
---|
| 72 | let msecs = RFC_MSECS;
|
---|
| 73 | for (let i = 0; i < 20000; ++i) {
|
---|
| 74 | if (i % 1500 === 0) {
|
---|
| 75 | msecs += 1;
|
---|
| 76 | }
|
---|
| 77 | id = (0, v7_js_1.default)({ msecs, seq: i });
|
---|
| 78 | if (prior !== undefined) {
|
---|
| 79 | assert.ok(prior < id, `${prior} < ${id}`);
|
---|
| 80 | }
|
---|
| 81 | prior = id;
|
---|
| 82 | }
|
---|
| 83 | });
|
---|
| 84 | (0, node_test_1.default)('can supply seq', () => {
|
---|
| 85 | let seq = 0x12345;
|
---|
| 86 | let uuid = (0, v7_js_1.default)({
|
---|
| 87 | msecs: RFC_MSECS,
|
---|
| 88 | seq,
|
---|
| 89 | });
|
---|
| 90 | assert.strictEqual(uuid.substr(0, 25), '017f22e2-79b0-7000-848d-1');
|
---|
| 91 | seq = 0x6fffffff;
|
---|
| 92 | uuid = (0, v7_js_1.default)({
|
---|
| 93 | msecs: RFC_MSECS,
|
---|
| 94 | seq,
|
---|
| 95 | });
|
---|
| 96 | assert.strictEqual(uuid.substring(0, 25), '017f22e2-79b0-76ff-bfff-f');
|
---|
| 97 | });
|
---|
| 98 | (0, node_test_1.default)('internal seq is reset upon timestamp change', () => {
|
---|
| 99 | (0, v7_js_1.default)({
|
---|
| 100 | msecs: RFC_MSECS,
|
---|
| 101 | seq: 0x6fffffff,
|
---|
| 102 | });
|
---|
| 103 | const uuid = (0, v7_js_1.default)({
|
---|
| 104 | msecs: RFC_MSECS + 1,
|
---|
| 105 | });
|
---|
| 106 | assert.ok(uuid.indexOf('fff') !== 15);
|
---|
| 107 | });
|
---|
| 108 | (0, node_test_1.default)('v7() state transitions', () => {
|
---|
| 109 | const tests = [
|
---|
| 110 | {
|
---|
| 111 | title: 'new time interval',
|
---|
| 112 | state: { msecs: 1, seq: 123 },
|
---|
| 113 | now: 2,
|
---|
| 114 | expected: {
|
---|
| 115 | msecs: 2,
|
---|
| 116 | seq: 0x6c318c4,
|
---|
| 117 | },
|
---|
| 118 | },
|
---|
| 119 | {
|
---|
| 120 | title: 'same time interval',
|
---|
| 121 | state: { msecs: 1, seq: 123 },
|
---|
| 122 | now: 1,
|
---|
| 123 | expected: {
|
---|
| 124 | msecs: 1,
|
---|
| 125 | seq: 124,
|
---|
| 126 | },
|
---|
| 127 | },
|
---|
| 128 | {
|
---|
| 129 | title: 'same time interval (sequence rollover)',
|
---|
| 130 | state: { msecs: 1, seq: 0xffffffff },
|
---|
| 131 | now: 1,
|
---|
| 132 | expected: {
|
---|
| 133 | msecs: 2,
|
---|
| 134 | seq: 0,
|
---|
| 135 | },
|
---|
| 136 | },
|
---|
| 137 | {
|
---|
| 138 | title: 'time regression',
|
---|
| 139 | state: { msecs: 2, seq: 123 },
|
---|
| 140 | now: 1,
|
---|
| 141 | expected: {
|
---|
| 142 | msecs: 2,
|
---|
| 143 | seq: 124,
|
---|
| 144 | },
|
---|
| 145 | },
|
---|
| 146 | {
|
---|
| 147 | title: 'time regression (sequence rollover)',
|
---|
| 148 | state: { msecs: 2, seq: 0xffffffff },
|
---|
| 149 | now: 1,
|
---|
| 150 | expected: {
|
---|
| 151 | msecs: 3,
|
---|
| 152 | seq: 0,
|
---|
| 153 | },
|
---|
| 154 | },
|
---|
| 155 | ];
|
---|
| 156 | for (const { title, state, now, expected } of tests) {
|
---|
| 157 | assert.deepStrictEqual((0, v7_js_1.updateV7State)(state, now, RFC_RANDOM), expected, `Failed: ${title}`);
|
---|
| 158 | }
|
---|
| 159 | });
|
---|
| 160 | (0, node_test_1.default)('flipping bits changes the result', () => {
|
---|
| 161 | const asBigInt = (buf) => buf.reduce((acc, v) => (acc << 8n) | BigInt(v), 0n);
|
---|
| 162 | const asNumber = (bits, data) => Number(BigInt.asUintN(bits, data));
|
---|
| 163 | const flip = (data, n) => data ^ (1n << BigInt(127 - n));
|
---|
| 164 | const optionsFrom = (data) => {
|
---|
| 165 | const ms = asNumber(48, data >> 80n);
|
---|
| 166 | const hi = asNumber(12, data >> 64n);
|
---|
| 167 | const lo = asNumber(20, data >> 42n);
|
---|
| 168 | const r = BigInt.asUintN(42, data);
|
---|
| 169 | return {
|
---|
| 170 | msecs: ms,
|
---|
| 171 | seq: (hi << 20) | lo,
|
---|
| 172 | random: Uint8Array.from([
|
---|
| 173 | ...Array(10).fill(0),
|
---|
| 174 | ...Array(6)
|
---|
| 175 | .fill(0)
|
---|
| 176 | .map((_, i) => asNumber(8, r >> (BigInt(i) * 8n)))
|
---|
| 177 | .reverse(),
|
---|
| 178 | ]),
|
---|
| 179 | };
|
---|
| 180 | };
|
---|
| 181 | const buf = new Uint8Array(16);
|
---|
| 182 | const data = asBigInt((0, v7_js_1.default)({}, buf));
|
---|
| 183 | const id = (0, stringify_js_1.default)(buf);
|
---|
| 184 | const reserved = [48, 49, 50, 51, 64, 65];
|
---|
| 185 | for (let i = 0; i < 128; ++i) {
|
---|
| 186 | if (reserved.includes(i)) {
|
---|
| 187 | continue;
|
---|
| 188 | }
|
---|
| 189 | const flipped = flip(data, i);
|
---|
| 190 | assert.strictEqual(asBigInt((0, v7_js_1.default)(optionsFrom(flipped), buf)).toString(16), flipped.toString(16), `Unequal uuids at bit ${i}`);
|
---|
| 191 | assert.notStrictEqual((0, stringify_js_1.default)(buf), id);
|
---|
| 192 | }
|
---|
| 193 | });
|
---|
| 194 | });
|
---|