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 native_js_1 = require("../native.js");
|
---|
6 | const v4_js_1 = require("../v4.js");
|
---|
7 | const randomBytesFixture = Uint8Array.of(0x10, 0x91, 0x56, 0xbe, 0xc4, 0xfb, 0xc1, 0xea, 0x71, 0xb4, 0xef, 0xe1, 0x67, 0x1c, 0x58, 0x36);
|
---|
8 | const expectedBytes = Uint8Array.of(16, 145, 86, 190, 196, 251, 65, 234, 177, 180, 239, 225, 103, 28, 88, 54);
|
---|
9 | (0, node_test_1.describe)('v4', () => {
|
---|
10 | (0, node_test_1.default)('subsequent UUIDs are different', () => {
|
---|
11 | const id1 = (0, v4_js_1.default)();
|
---|
12 | const id2 = (0, v4_js_1.default)();
|
---|
13 | assert.ok(id1 !== id2);
|
---|
14 | });
|
---|
15 | (0, node_test_1.default)('should uses native randomUUID() if no option is passed', async () => {
|
---|
16 | const mock = (await Promise.resolve().then(() => require('node:test'))).default.mock;
|
---|
17 | if (!mock) {
|
---|
18 | return;
|
---|
19 | }
|
---|
20 | const mockRandomUUID = mock.method(native_js_1.default, 'randomUUID');
|
---|
21 | assert.equal(mockRandomUUID.mock.callCount(), 0);
|
---|
22 | (0, v4_js_1.default)();
|
---|
23 | assert.equal(mockRandomUUID.mock.callCount(), 1);
|
---|
24 | mock.restoreAll();
|
---|
25 | });
|
---|
26 | (0, node_test_1.default)('should not use native randomUUID() if an option is passed', async () => {
|
---|
27 | const mock = (await Promise.resolve().then(() => require('node:test'))).default.mock;
|
---|
28 | if (!mock) {
|
---|
29 | return;
|
---|
30 | }
|
---|
31 | const mockRandomUUID = mock.method(native_js_1.default, 'randomUUID');
|
---|
32 | assert.equal(mockRandomUUID.mock.callCount(), 0);
|
---|
33 | (0, v4_js_1.default)({});
|
---|
34 | assert.equal(mockRandomUUID.mock.callCount(), 0);
|
---|
35 | mock.restoreAll();
|
---|
36 | });
|
---|
37 | (0, node_test_1.default)('explicit options.random produces expected result', () => {
|
---|
38 | const id = (0, v4_js_1.default)({ random: randomBytesFixture });
|
---|
39 | assert.strictEqual(id, '109156be-c4fb-41ea-b1b4-efe1671c5836');
|
---|
40 | });
|
---|
41 | (0, node_test_1.default)('explicit options.rng produces expected result', () => {
|
---|
42 | const id = (0, v4_js_1.default)({ rng: () => randomBytesFixture });
|
---|
43 | assert.strictEqual(id, '109156be-c4fb-41ea-b1b4-efe1671c5836');
|
---|
44 | });
|
---|
45 | (0, node_test_1.default)('fills one UUID into a buffer as expected', () => {
|
---|
46 | const buffer = new Uint8Array(16);
|
---|
47 | const result = (0, v4_js_1.default)({ random: randomBytesFixture }, buffer);
|
---|
48 | assert.deepEqual(buffer, expectedBytes);
|
---|
49 | assert.strictEqual(buffer, result);
|
---|
50 | });
|
---|
51 | (0, node_test_1.default)('fills two UUIDs into a buffer as expected', () => {
|
---|
52 | const buffer = new Uint8Array(32);
|
---|
53 | (0, v4_js_1.default)({ random: randomBytesFixture }, buffer, 0);
|
---|
54 | (0, v4_js_1.default)({ random: randomBytesFixture }, buffer, 16);
|
---|
55 | const expectedBuf = new Uint8Array(32);
|
---|
56 | expectedBuf.set(expectedBytes);
|
---|
57 | expectedBuf.set(expectedBytes, 16);
|
---|
58 | assert.deepEqual(buffer, expectedBuf);
|
---|
59 | });
|
---|
60 | });
|
---|