| 1 | import test from 'node:test' ; |
| 2 | import assert from 'node:assert/strict' ; |
| 3 | import * as api from '../src/index.js' ; |
| 4 | test('integer and pick helpers validate inputs and include both endpoints' , () => { |
| 5 | const rng = api.createRng('range' ); |
| 6 | assert.equal(typeof rng.int, 'function' ); |
| 7 | assert.deepEqual(new Set(Array.from({length: 100}, () => rng.int(-1, 1))), new Set([-1, 0, 1])); |
| 8 | assert.equal(rng.int(4, 4), 4); |
| 9 | assert.equal(rng.pick(['only' ]), 'only' ); |
| 10 | for (const seed of [null, undefined, 12, {}, [], new String('a' )]) assert.throws(() => api.createRng(seed), TypeError); |
| 11 | assert.throws(() => api.createRng('s' , 1), TypeError); |
| 12 | for (const args of [[2, 1], [0.1, 2], [0, Infinity], [-Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]]) assert.throws(() => rng.int(...args), RangeError); |
| 13 | assert.throws(() => rng.pick([]), RangeError); |
| 14 | assert.throws(() => rng.pick('abc' ), TypeError); |
| 15 | }); |
| 16 | const sequence = (seed, namespace) => { |
| 17 | const rng = api.createRng(seed, namespace); |
| 18 | return Array.from({length: 32}, () => rng.next()); |
| 19 | }; |
| 20 | test('fresh namespace streams are deterministic and independent' , () => { |
| 21 | assert.equal(typeof api.createRng, 'function' ); |
| 22 | const a = sequence('opaque key' , 'layout' ); |
| 23 | sequence('opaque key' , 'texture' ); |
| 24 | assert.deepEqual(sequence('opaque key' , 'layout' ), a); |
| 25 | assert.notDeepEqual(sequence('opaque key' , 'palette' ), a); |
| 26 | assert(a.every(n => n >= 0 && n < 1)); |
| 27 | }); |
| 28 | test('full unnormalized strings affect stream state' , () => { |
| 29 | const seeds = ['' , 'a' , 'A' , ' a' , 'a ' , 'é' , 'e\u0301' , '🌿你好' , 'f' .repeat(64), 'f' .repeat(63) + 'e' , 'x' .repeat(20000) + 'a' , 'x' .repeat(20000) + 'b' ]; |
| 30 | assert.equal(new Set(seeds.map(s => JSON.stringify(sequence(s)))).size, seeds.length); |
| 31 | assert.notDeepEqual(sequence('a' , 'bc' ), sequence('ab' , 'c' )); |
| 32 | }); |