Skip to content

Commit

Permalink
chore: rename fixture vars
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Jul 16, 2024
1 parent 475eb8b commit bb80de6
Showing 1 changed file with 55 additions and 72 deletions.
127 changes: 55 additions & 72 deletions src/test/v7.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,38 @@ import * as assert from 'assert';
import test, { describe } from 'node:test';
import { Version7Options } from '../_types.js';
import parse from '../parse.js';
import stringify, { unsafeStringify } from '../stringify.js';
import stringify from '../stringify.js';
import v7, { updateV7State } from '../v7.js';

/**
* fixture bit layout:
* ref: https://www.rfc-editor.org/rfc/rfc9562.html#name-example-of-a-uuidv7-value
*
* expectedBytes was calculated using this script:
* https://gist.github.com/d5382ac3a1ce4ba9ba40a90d9da8cbf1
*
* -------------------------------
* field bits value
* -------------------------------
* unix_ts_ms 48 0x17F22E279B0
* ver 4 0x7
* rand_a 12 0xCC3
* var 2 b10
* rand_b 62 b01, 0x8C4DC0C0C07398F
* -------------------------------
* total 128
* -------------------------------
* final: 017f22e2-79b0-7cc3-98c4-dc0c0c07398f
*/
// Fixture values for testing with the rfc v7 UUID example:
// https://www.rfc-editor.org/rfc/rfc9562.html#name-example-of-a-uuidv7-value
const RFC_V7 = '017f22e2-79b0-7cc3-98c4-dc0c0c07398f';
const RFC_V7_BYTES = parse('017f22e2-79b0-7cc3-98c4-dc0c0c07398f');
const RFC_MSECS = 0x17f22e279b0;

describe('v7', () => {
const msecsFixture = 0x17f22e279b0;
const seqFixture = 0x661b189b;

const randomBytesFixture = Uint8Array.of(
0x10,
0x91,
0x56,
0xbe,
0xc4,
0xfb,
0x0c,
0xc3,
0x18,
0xc4,
0xdc,
0x0c,
0x0c,
0x07,
0x39,
0x8f
);

const expectedBytes = parse('017f22e2-79b0-7cc3-98c4-dc0c0c07398f');
// `seq` and `random` values needed to create the above RFC uuid. These are
// specific to our implementation here, and are not part of the RFC.
const RFC_SEQ = 0x661b189b;
const RFC_RANDOM = Uint8Array.of(
0x10,
0x91,
0x56,
0xbe,
0xc4,
0xfb,
0x0c,
0xc3,
0x18,
0xc4,
0xdc,
0x0c,
0x0c,
0x07,
0x39,
0x8f
);

describe('v7', () => {
test('subsequent UUIDs are different', () => {
const id1 = v7();
const id2 = v7();
Expand All @@ -59,25 +42,25 @@ describe('v7', () => {

test('explicit options.random and options.msecs produces expected result', () => {
const id = v7({
random: randomBytesFixture,
msecs: msecsFixture,
seq: seqFixture,
random: RFC_RANDOM,
msecs: RFC_MSECS,
seq: RFC_SEQ,
});
assert.strictEqual(id, '017f22e2-79b0-7cc3-98c4-dc0c0c07398f');
assert.strictEqual(id, RFC_V7);
});

test('explicit options.rng produces expected result', () => {
const id = v7({
rng: () => randomBytesFixture,
msecs: msecsFixture,
seq: seqFixture,
rng: () => RFC_RANDOM,
msecs: RFC_MSECS,
seq: RFC_SEQ,
});
assert.strictEqual(id, '017f22e2-79b0-7cc3-98c4-dc0c0c07398f');
assert.strictEqual(id, RFC_V7);
});

test('explicit options.msecs produces expected result', () => {
const id = v7({
msecs: msecsFixture,
msecs: RFC_MSECS,
});
assert.strictEqual(id.indexOf('017f22e2'), 0);
});
Expand All @@ -86,15 +69,15 @@ describe('v7', () => {
const buffer = new Uint8Array(16);
const result = v7(
{
random: randomBytesFixture,
msecs: msecsFixture,
seq: seqFixture,
random: RFC_RANDOM,
msecs: RFC_MSECS,
seq: RFC_SEQ,
},
buffer
);
stringify(buffer);

assert.deepEqual(unsafeStringify(buffer), unsafeStringify(expectedBytes));
assert.deepEqual(buffer, RFC_V7_BYTES);
assert.strictEqual(buffer, result);
});

Expand All @@ -103,25 +86,25 @@ describe('v7', () => {

v7(
{
random: randomBytesFixture,
msecs: msecsFixture,
seq: seqFixture,
random: RFC_RANDOM,
msecs: RFC_MSECS,
seq: RFC_SEQ,
},
buffer,
0
);
v7(
{
random: randomBytesFixture,
msecs: msecsFixture,
seq: seqFixture,
random: RFC_RANDOM,
msecs: RFC_MSECS,
seq: RFC_SEQ,
},
buffer,
16
);
const expected = new Uint8Array(32);
expected.set(expectedBytes);
expected.set(expectedBytes, 16);
expected.set(RFC_V7_BYTES);
expected.set(RFC_V7_BYTES, 16);
assert.deepEqual(buffer, expected);
});

Expand All @@ -132,7 +115,7 @@ describe('v7', () => {
test('lexicographical sorting is preserved', () => {
let id;
let prior;
let msecs = msecsFixture;
let msecs = RFC_MSECS;
for (let i = 0; i < 20000; ++i) {
if (i % 1500 === 0) {
// every 1500 runs increment msecs so seq is
Expand Down Expand Up @@ -202,22 +185,22 @@ describe('v7', () => {
},
];
for (const { state, now, expected } of tests) {
assert.deepStrictEqual(updateV7State(state, now, randomBytesFixture), expected);
assert.deepStrictEqual(updateV7State(state, now, RFC_RANDOM), expected);
}
});

test('can supply seq', () => {
let seq = 0x12345;
let uuid = v7({
msecs: msecsFixture,
msecs: RFC_MSECS,
seq,
});

assert.strictEqual(uuid.substr(0, 25), '017f22e2-79b0-7000-891a-2');

seq = 0x6fffffff;
uuid = v7({
msecs: msecsFixture,
msecs: RFC_MSECS,
seq,
});

Expand All @@ -226,12 +209,12 @@ describe('v7', () => {

test('internal seq is reset upon timestamp change', () => {
v7({
msecs: msecsFixture,
msecs: RFC_MSECS,
seq: 0x6fffffff,
});

const uuid = v7({
msecs: msecsFixture + 1,
msecs: RFC_MSECS + 1,
});

assert.ok(uuid.indexOf('fff') !== 15);
Expand Down

0 comments on commit bb80de6

Please sign in to comment.