Skip to content

Modular logistics and supply chain operating system (LSOS)

test(console): cover the randomUUID polyfill's validators and entropy-cache path

Ronald A. Richardsonf79ad033e781
diff --git a/console/tests/unit/initializers/initialize-random-uuid-polyfill-test.js b/console/tests/unit/initializers/initialize-random-uuid-polyfill-test.js
index 7e0df29a..1aca6ac2 100644
--- a/console/tests/unit/initializers/initialize-random-uuid-polyfill-test.js
+++ b/console/tests/unit/initializers/initialize-random-uuid-polyfill-test.js
@@ -32,3 +32,63 @@ module('Unit | Initializer | initialize-random-uuid-polyfill', function (hooks)
assert.strictEqual(window.crypto.randomUUID, native, 'the native implementation is not replaced');
});
});
+
+/**
+ * The polyfill is a port of Node's crypto.randomUUID, including its argument validators and
+ * its entropy-cache option. Chrome supplies a native randomUUID, so the polyfill only runs
+ * once the native one is removed — which is what makes these paths reachable at all.
+ */
+module('Unit | Initializer | initialize-random-uuid-polyfill | options', function (hooks) {
+ hooks.beforeEach(function () {
+ this.original = window.crypto.randomUUID;
+ window.crypto.randomUUID = undefined;
+ initialize();
+ this.randomUUID = window.crypto.randomUUID;
+ });
+
+ hooks.afterEach(function () {
+ window.crypto.randomUUID = this.original;
+ });
+
+ test('an explicit empty options object is accepted', function (assert) {
+ const uuid = this.randomUUID({});
+
+ assert.strictEqual(uuid.length, 36);
+ });
+
+ test('disabling the entropy cache still produces distinct valid uuids', function (assert) {
+ // Takes the un-buffered path, which allocates its own 16-byte buffer on first use.
+ const first = this.randomUUID({ disableEntropyCache: true });
+ const second = this.randomUUID({ disableEntropyCache: true });
+
+ assert.ok(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(first), `a valid v4 uuid (${first})`);
+ assert.notStrictEqual(first, second, 'successive un-buffered calls differ');
+ });
+
+ test('a non-boolean disableEntropyCache is rejected', function (assert) {
+ assert.throws(() => this.randomUUID({ disableEntropyCache: 'yes' }), /options.disableEntropyCache variable is not of type boolean/);
+
+ try {
+ this.randomUUID({ disableEntropyCache: 1 });
+ } catch (error) {
+ assert.strictEqual(error.code, 'ERR_INVALID_ARG_TYPE', 'it carries Node’s error code');
+ assert.true(error instanceof TypeError);
+ }
+ });
+
+ test('options that are not a plain object are rejected', function (assert) {
+ assert.throws(() => this.randomUUID(null), /options variable is not of type Object/, 'null is not an options object');
+ assert.throws(() => this.randomUUID([]), /options variable is not of type Object/, 'nor is an array');
+ assert.throws(() => this.randomUUID('nope'), /options variable is not of type Object/, 'nor is a string');
+ });
+
+ test('the batched path recycles its buffer across many calls', function (assert) {
+ // kBatchSize is 128; going past it forces the batch counter to wrap and refill.
+ const uuids = new Set();
+ for (let i = 0; i < 130; i++) {
+ uuids.add(this.randomUUID());
+ }
+
+ assert.strictEqual(uuids.size, 130, 'every uuid across a full batch and beyond is distinct');
+ });
+});