|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { |
| 5 | + createEntropyGenerator, |
| 6 | + type EntropyGenerator, |
| 7 | + generateEntropy, |
| 8 | + InvalidEntropyLengthError, |
| 9 | +} from "../src/entropy/entropyGenerator.ts"; |
| 10 | + |
| 11 | +const allowed = [16, 20, 24, 28, 32]; |
| 12 | + |
| 13 | +test("generateEntropy returns allowed lengths", () => { |
| 14 | + for (const length of allowed) { |
| 15 | + const entropy = generateEntropy(length); |
| 16 | + assert.equal(entropy.length, length); |
| 17 | + } |
| 18 | +}); |
| 19 | + |
| 20 | +test("generateEntropy rejects invalid lengths", () => { |
| 21 | + assert.throws(() => generateEntropy(15), InvalidEntropyLengthError); |
| 22 | + assert.throws(() => generateEntropy(33), InvalidEntropyLengthError); |
| 23 | +}); |
| 24 | + |
| 25 | +test("EntropyGenerator allows deterministic output in tests", () => { |
| 26 | + const fixed = Uint8Array.from({ length: 16 }, (_, i) => i); |
| 27 | + const generator = createEntropyGenerator(() => fixed); |
| 28 | + const entropy = generator.generate(16); |
| 29 | + assert.equal(entropy.length, 16); |
| 30 | + assert.deepEqual(Array.from(entropy), Array.from(fixed)); |
| 31 | +}); |
| 32 | + |
| 33 | +// Validate the interface shape used by callers |
| 34 | +const _typecheck: EntropyGenerator = { |
| 35 | + generate: (bytes) => new Uint8Array(bytes), |
| 36 | +}; |
| 37 | +void _typecheck; |
0 commit comments