|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | + |
| 5 | +if (!common.hasCrypto) |
| 6 | + common.skip('missing crypto'); |
| 7 | + |
| 8 | +const assert = require('assert'); |
| 9 | +const { |
| 10 | + randomUUIDv7, |
| 11 | +} = require('crypto'); |
| 12 | + |
| 13 | +// Basic return type and format checks. |
| 14 | +{ |
| 15 | + const uuid = randomUUIDv7(); |
| 16 | + assert.strictEqual(typeof uuid, 'string'); |
| 17 | + assert.strictEqual(uuid.length, 36); |
| 18 | + |
| 19 | + // UUIDv7 format: xxxxxxxx-xxxx-7xxx-[89ab]xxx-xxxxxxxxxxxx |
| 20 | + assert.match( |
| 21 | + uuid, |
| 22 | + /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +// Check version and variant bits. |
| 27 | +{ |
| 28 | + const uuid = randomUUIDv7(); |
| 29 | + |
| 30 | + // Version 7: byte 6 high nibble should be 0x7. |
| 31 | + assert.strictEqual( |
| 32 | + Buffer.from(uuid.slice(14, 16), 'hex')[0] & 0xf0, 0x70, |
| 33 | + ); |
| 34 | + |
| 35 | + // Variant: byte 8 high 2 bits should be 0b10. |
| 36 | + assert.strictEqual( |
| 37 | + Buffer.from(uuid.slice(19, 21), 'hex')[0] & 0b1100_0000, 0b1000_0000, |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +// Uniqueness: generate many UUIDs and verify no duplicates. |
| 42 | +{ |
| 43 | + const seen = new Set(); |
| 44 | + for (let i = 0; i < 1000; i++) { |
| 45 | + const uuid = randomUUIDv7(); |
| 46 | + assert(!seen.has(uuid), `Duplicate UUID generated: ${uuid}`); |
| 47 | + seen.add(uuid); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Timestamp: the embedded timestamp should approximate Date.now(). |
| 52 | +{ |
| 53 | + const before = Date.now(); |
| 54 | + const uuid = randomUUIDv7(); |
| 55 | + const after = Date.now(); |
| 56 | + |
| 57 | + // Extract the 48-bit timestamp from the UUID. |
| 58 | + // Bytes 0-3 (chars 0-8) and bytes 4-5 (chars 9-13, skipping the dash). |
| 59 | + const hex = uuid.replace(/-/g, ''); |
| 60 | + const timestampHex = hex.slice(0, 12); // first 48 bits = 12 hex chars |
| 61 | + const timestamp = parseInt(timestampHex, 16); |
| 62 | + |
| 63 | + assert(timestamp >= before, `Timestamp ${timestamp} < before ${before}`); |
| 64 | + assert(timestamp <= after, `Timestamp ${timestamp} > after ${after}`); |
| 65 | +} |
| 66 | + |
| 67 | +// Monotonicity: UUIDs generated in sequence should sort lexicographically |
| 68 | +// within the same millisecond or across milliseconds. |
| 69 | +{ |
| 70 | + let prev = randomUUIDv7(); |
| 71 | + for (let i = 0; i < 100; i++) { |
| 72 | + const curr = randomUUIDv7(); |
| 73 | + // UUIDs with later timestamps must sort after earlier ones. |
| 74 | + // Within the same millisecond, ordering depends on random bits, |
| 75 | + // so we only assert >= on the timestamp portion. |
| 76 | + const prevTs = parseInt(prev.replace(/-/g, '').slice(0, 12), 16); |
| 77 | + const currTs = parseInt(curr.replace(/-/g, '').slice(0, 12), 16); |
| 78 | + assert(currTs >= prevTs, |
| 79 | + `Timestamp went backwards: ${currTs} < ${prevTs}`); |
| 80 | + prev = curr; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Ensure randomUUIDv7 takes no arguments (or ignores them gracefully). |
| 85 | +{ |
| 86 | + const uuid = randomUUIDv7(); |
| 87 | + assert.match( |
| 88 | + uuid, |
| 89 | + /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, |
| 90 | + ); |
| 91 | +} |
0 commit comments