Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than cdc3edc point at it.

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 1–7why, one step back
1import { 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 back
8 test('is ready for 0 size', () => {
9 equal(nanoid(0), '')10 })11

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 12–63why, one step back
12 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 })63

Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b

line 64why, one step back
64 test('avoids pool pollution, infinite loop', () => {

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 65–70why, one step back
65 nanoid(2.1)
66 let second = nanoid()67 let third = nanoid()68 notEqual(second, third)69 })70

Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b

lines 71–75why, one step back
71 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 back
76

Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b

lines 77–80why, one step back
77 test('has options', () => {
78 let nanoidA = customAlphabet('a', 5)79 equal(nanoidA(), 'aaaaa')80 })

Update size limitAndrey Sitnikbefore adoptioncdc3edc

line 81why, one step back
81

Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b

lines 82–83why, one step back
82 test('has flat distribution', () => {
83 let COUNT = 100 * 1000

Stabilize testsAndrey Sitnikbefore adoption45aa324

line 84why, one step back
84 let LENGTH = 30

Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b

lines 85–94why, one step back
85 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 back
95 }
96

Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b

line 97why, one step back
97 equal(Object.keys(chars).length, ALPHABET.length)

Update size limitAndrey Sitnikbefore adoptioncdc3edc

line 98why, one step back
98

Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b

lines 99–107why, one step back
99 let max = 0
100 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 back
108

Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b

lines 109–116why, one step back
109 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 back
117 })
118})

fix(non-secure): clamp negative size with a smaller guard (#600)spokodevbefore adoption6de05d7

lines 119–128why, one step back
119
120test('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})