18 lines
1.0 KiB
JavaScript
18 lines
1.0 KiB
JavaScript
'use strict';
|
|
const test = require('node:test'), assert = require('node:assert/strict');
|
|
const fs = require('node:fs'), path = require('node:path'), vm = require('node:vm');
|
|
const crypto = require('node:crypto');
|
|
const source = fs.readFileSync(path.join(__dirname, '../public/collections.js'), 'utf8').split('(async () => {')[0];
|
|
test('Collection IDs use the native secure-context API when available', () => {
|
|
const context = { crypto: { randomUUID: () => 'native-uuid' } };
|
|
vm.runInNewContext(source, context);
|
|
assert.equal(context.collectionId(), 'native-uuid');
|
|
});
|
|
test('Collection IDs work on HTTP without randomUUID and remain unique valid UUIDs', () => {
|
|
const context = { crypto: { getRandomValues: values => crypto.randomFillSync(values) } };
|
|
vm.runInNewContext(source, context);
|
|
const ids = Array.from({ length: 200 }, () => context.collectionId());
|
|
assert.equal(new Set(ids).size, ids.length);
|
|
for (const id of ids) assert.match(id, /^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/);
|
|
});
|