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