non-secure.test.js
This file
Path
test/non-secure.test.js
128 lines · 3.2 KiB · JavaScript
Last changed by
Stabilize testsAndrey Sitnikbefore adoption45aa324
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129import { equal, match, notEqual, ok } from 'node:assert'import { describe, test } from 'node:test'
import { urlAlphabet } from '../index.js'import { customAlphabet, nanoid } from '../non-secure/index.js'
describe('non secure', () => { test('is ready for 0 size', () => { equal(nanoid(0), '') })
test('generates URL-friendly IDs', () => { for (let i = 0; i < 10; i++) { let id = nanoid() equal(id.length, 21) for (let char of id) { match(urlAlphabet, new RegExp(char, 'g')) } } })
test('changes ID length', () => { equal(nanoid(10).length, 10) })
test('accepts string', () => { equal(nanoid('10').length, 10) })
test('has no collisions', () => { let used = {} for (let i = 0; i < 100 * 1000; i++) { let id = nanoid() equal(used[id], undefined) used[id] = true } })
test('has flat distribution', () => { let COUNT = 100 * 1000 let LENGTH = nanoid().length
let chars = {} for (let i = 0; i < COUNT; i++) { let id = nanoid() for (let char of id) { if (!chars[char]) chars[char] = 0 chars[char] += 1 } }
equal(Object.keys(chars).length, urlAlphabet.length)
let max = 0 let min = Number.MAX_SAFE_INTEGER for (let k in chars) { let distribution = (chars[k] * urlAlphabet.length) / (COUNT * LENGTH) if (distribution > max) max = distribution if (distribution < min) min = distribution } ok(max - min <= 0.05) })
test('avoids pool pollution, infinite loop', () => { nanoid(2.1) let second = nanoid() let third = nanoid() notEqual(second, third) })
describe('customAlphabet', () => { test('is ready for 0 size', () => { equal(customAlphabet('abc')(0), '') equal(customAlphabet('abc', 0)(0), '') })
test('has options', () => { let nanoidA = customAlphabet('a', 5) equal(nanoidA(), 'aaaaa') })
test('has flat distribution', () => { let COUNT = 100 * 1000 let LENGTH = 30 let ALPHABET = 'abcdefghijklmnopqrstuvwxyz' let nanoid2 = customAlphabet(ALPHABET, LENGTH)
let chars = {} for (let i = 0; i < COUNT; i++) { let id = nanoid2() for (let char of id) { if (!chars[char]) chars[char] = 0 chars[char] += 1 } }
equal(Object.keys(chars).length, ALPHABET.length)
let max = 0 let min = Number.MAX_SAFE_INTEGER for (let k in chars) { let distribution = (chars[k] * ALPHABET.length) / (COUNT * LENGTH) if (distribution > max) max = distribution if (distribution < min) min = distribution } ok(max - min <= 0.05) })
test('avoids pool pollution, infinite loop', () => { let ALPHABET = 'abcdefghijklmnopqrstuvwxyz' let nanoid2 = customAlphabet(ALPHABET) nanoid2(2.1) let second = nanoid2() let third = nanoid2() notEqual(second, third) }) })})
test('does not hang on negative size (nanoid)', () => { equal(nanoid(-1), '') equal(nanoid(-100), '')})
test('does not hang on negative size (customAlphabet)', () => { equal(customAlphabet('abcdef')(-1), '') equal(customAlphabet('abcdef', -5)(), '')})