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
The walk stopped after 200 commits: lines older than cdc3edc point at it.
Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 1–7why, one step back1import { equal, match, notEqual, ok } from 'node:assert'2import { describe, test } from 'node:test'34import { urlAlphabet } from '../index.js'5import { customAlphabet, nanoid } from '../non-secure/index.js'67describe('non secure', () => {Add edge case tests for large IDs and zero-size generation (#562)Mahmoud Hamdybefore adoption0e0ab5d
lines 8–11why, one step back8 test('is ready for 0 size', () => {9 equal(nanoid(0), '')10 })11Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 12–63why, one step back12 test('generates URL-friendly IDs', () => {13 for (let i = 0; i < 10; i++) {14 let id = nanoid()15 equal(id.length, 21)16 for (let char of id) {17 match(urlAlphabet, new RegExp(char, 'g'))18 }19 }20 })2122 test('changes ID length', () => {23 equal(nanoid(10).length, 10)24 })2526 test('accepts string', () => {27 equal(nanoid('10').length, 10)28 })2930 test('has no collisions', () => {31 let used = {}32 for (let i = 0; i < 100 * 1000; i++) {33 let id = nanoid()34 equal(used[id], undefined)35 used[id] = true36 }37 })3839 test('has flat distribution', () => {40 let COUNT = 100 * 100041 let LENGTH = nanoid().length4243 let chars = {}44 for (let i = 0; i < COUNT; i++) {45 let id = nanoid()46 for (let char of id) {47 if (!chars[char]) chars[char] = 048 chars[char] += 149 }50 }5152 equal(Object.keys(chars).length, urlAlphabet.length)5354 let max = 055 let min = Number.MAX_SAFE_INTEGER56 for (let k in chars) {57 let distribution = (chars[k] * urlAlphabet.length) / (COUNT * LENGTH)58 if (distribution > max) max = distribution59 if (distribution < min) min = distribution60 }61 ok(max - min <= 0.05)62 })63Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
line 64why, one step back64 test('avoids pool pollution, infinite loop', () => {Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 65–70why, one step back65 nanoid(2.1)66 let second = nanoid()67 let third = nanoid()68 notEqual(second, third)69 })70Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
lines 71–75why, one step back71 describe('customAlphabet', () => {72 test('is ready for 0 size', () => {73 equal(customAlphabet('abc')(0), '')74 equal(customAlphabet('abc', 0)(0), '')75 })Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 76why, one step back76Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
lines 77–80why, one step back77 test('has options', () => {78 let nanoidA = customAlphabet('a', 5)79 equal(nanoidA(), 'aaaaa')80 })Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 81why, one step back81Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
lines 82–83why, one step back82 test('has flat distribution', () => {83 let COUNT = 100 * 1000Stabilize testsAndrey Sitnikbefore adoption45aa324
line 84why, one step back84 let LENGTH = 30Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
lines 85–94why, one step back85 let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'86 let nanoid2 = customAlphabet(ALPHABET, LENGTH)8788 let chars = {}89 for (let i = 0; i < COUNT; i++) {90 let id = nanoid2()91 for (let char of id) {92 if (!chars[char]) chars[char] = 093 chars[char] += 194 }Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 95–96why, one step back95 }96Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
line 97why, one step back97 equal(Object.keys(chars).length, ALPHABET.length)Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 98why, one step back98Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
lines 99–107why, one step back99 let max = 0100 let min = Number.MAX_SAFE_INTEGER101 for (let k in chars) {102 let distribution = (chars[k] * ALPHABET.length) / (COUNT * LENGTH)103 if (distribution > max) max = distribution104 if (distribution < min) min = distribution105 }106 ok(max - min <= 0.05)107 })Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 108why, one step back108Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
lines 109–116why, one step back109 test('avoids pool pollution, infinite loop', () => {110 let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'111 let nanoid2 = customAlphabet(ALPHABET)112 nanoid2(2.1)113 let second = nanoid2()114 let third = nanoid2()115 notEqual(second, third)116 })Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 117–118why, one step back117 })118})fix(non-secure): clamp negative size with a smaller guard (#600)spokodevbefore adoption6de05d7
lines 119–128why, one step back119120test('does not hang on negative size (nanoid)', () => {121 equal(nanoid(-1), '')122 equal(nanoid(-100), '')123})124125test('does not hang on negative size (customAlphabet)', () => {126 equal(customAlphabet('abcdef')(-1), '')127 equal(customAlphabet('abcdef', -5)(), '')128})