index.test.js
This file
Path
test/index.test.js
371 lines · 10.1 KiB · JavaScript
Last changed by
Fix non-integer size in customRandom() (#616)lostsignalsbefore adoption0b2cec5
History of this fileTake it
The walk stopped after 200 commits: lines older than cdc3edc point at it.
Limit ID even moreAndrey Sitnikbefore adoption7087969
line 1why, one step back1import { equal, match, notEqual, ok, throws } from 'node:assert'Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 2–31why, one step back2import { after, before, describe, test } from 'node:test'34import * as browser from '../index.browser.js'5import * as node from '../index.js'67for (let type of ['node', 'browser']) {8 let { customAlphabet, customRandom, nanoid, random, urlAlphabet } =9 type === 'node' ? node : browser1011 describe(type, () => {12 if (type === 'browser') {13 before(() => {14 Object.defineProperty(global, 'crypto', {15 configurable: true,16 value: {17 getRandomValues(array) {18 for (let i = 0; i < array.length; i++) {19 array[i] = Math.floor(Math.random() * 256)20 }21 return array22 }23 }24 })25 })2627 after(() => {28 Object.defineProperty(global, 'crypto', { value: undefined })29 })30 }31Fix testsAndrey Sitnikbefore adoption70bc11f
lines 32–35why, one step back32 test('is ready for 0 size', () => {33 equal(nanoid(0), '')34 })35Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 36–50why, one step back36 test(`generates URL-friendly IDs`, () => {37 for (let i = 0; i < 100; i++) {38 let id = nanoid()39 equal(id.length, 21)40 equal(typeof id, 'string')41 for (let char of id) {42 match(urlAlphabet, new RegExp(char, 'g'))43 }44 }45 })4647 test(`changes ID length`, () => {48 equal(nanoid(10).length, 10)49 })50Add edge case tests for large IDs and zero-size generation (#562)Mahmoud Hamdybefore adoption0e0ab5d
lines 51–58why, one step back51 test(`generates large IDs`, () => {52 let id = nanoid(1000)53 equal(id.length, 1000)54 for (let char of id) {55 match(urlAlphabet, new RegExp(char, 'g'))56 }57 })58Port amazing nope-id optimizations (#602)Andrey Sitnikbefore adoptionb1bb75f
lines 59–66why, one step back59 test(`generates IDs bigger than pool size`, () => {60 let id = nanoid(70000)61 equal(id.length, 70000)62 for (let char of id) {63 match(urlAlphabet, new RegExp(char, 'g'))64 }65 })66Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 67–86why, one step back67 test(`accepts string`, () => {68 equal(nanoid('10').length, 10)69 })7071 test(`has no collisions`, () => {72 let used = {}73 for (let i = 0; i < 50 * 1000; i++) {74 let id = nanoid()75 equal(used[id], undefined)76 used[id] = true77 }78 })7980 test(`avoids pool pollution, infinite loop`, () => {81 nanoid(2.1)82 let second = nanoid()83 let third = nanoid()84 notEqual(second, third)85 })86Limit ID even moreAndrey Sitnikbefore adoption7087969
lines 87–97why, one step back87 test('throws on negative or too big ID length', () => {88 throws(() => {89 nanoid(2147483648)90 }, /Wrong ID size|Invalid typed array length/)91 throws(() => {92 nanoid(-10)93 }, /Wrong ID size|Invalid typed array length/)94 let a = nanoid()95 let b = nanoid()96 notEqual(a, b)97 })Fix random pool breakAndrey Sitnikbefore adoptionbe7901a
line 98why, one step back98Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 99–124why, one step back99 test(`has flat distribution`, () => {100 let COUNT = 100 * 1000101 let LENGTH = nanoid().length102103 let chars = {}104 for (let i = 0; i < COUNT; i++) {105 let id = nanoid()106 for (let char of id) {107 if (!chars[char]) chars[char] = 0108 chars[char] += 1109 }110 }111112 equal(Object.keys(chars).length, urlAlphabet.length)113114 let max = 0115 let min = Number.MAX_SAFE_INTEGER116 for (let k in chars) {117 let distribution = (chars[k] * urlAlphabet.length) / (COUNT * LENGTH)118 if (distribution > max) max = distribution119 if (distribution < min) min = distribution120 }121 ok(max - min <= 0.05)122 })123124 if (type === 'node') {Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
line 125why, one step back125 test(`prevent collision with proxy number`, () => {Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 126–153why, one step back126 let makeProxyNumberToReproducePreviousID = () => {127 let step = 0128 return {129 valueOf() {130 // "if (!pool || pool.length < bytes) {"131 if (step === 0) {132 step++133 return 0134 }135 // "} else if (poolOffset + bytes > pool.length) {"136 if (step === 1) {137 step++138 return -Infinity139 }140 // "poolOffset += bytes"141 if (step === 2) {142 step++143 return 0144 }145146 return 21147 }148 }149 }150151 let id1 = nanoid()152 let id2 = nanoid(makeProxyNumberToReproducePreviousID())153 notEqual(id1, id2)Fix testsAndrey Sitnikbefore adoption70bc11f
line 154why, one step back154 })Port amazing nope-id optimizations (#602)Andrey Sitnikbefore adoptionb1bb75f
lines 155–157why, one step back155156 test(`recovers from crypto errors`, () => {157 nanoid(1)Remove unnecessary import for modern Node.jsAndrey Sitnikbefore adoption87f2821
lines 158–159why, one step back158 let original = crypto.getRandomValues159 crypto.getRandomValues = () => {Port amazing nope-id optimizations (#602)Andrey Sitnikbefore adoptionb1bb75f
lines 160–169why, one step back160 throw new Error('Entropy failure')161 }162 try {163 throws(() => {164 nanoid(65536)165 }, /Entropy failure/)166 throws(() => {167 random(131072)168 }, /Entropy failure/)169 } finally {Remove unnecessary import for modern Node.jsAndrey Sitnikbefore adoption87f2821
line 170why, one step back170 crypto.getRandomValues = originalPort amazing nope-id optimizations (#602)Andrey Sitnikbefore adoptionb1bb75f
lines 171–174why, one step back171 }172 equal(nanoid().length, 21)173 equal(random(10).length, 10)174 })Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
line 175why, one step back175 }Fix testsAndrey Sitnikbefore adoption70bc11f
line 176why, one step back176Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
lines 177–215why, one step back177 describe('customAlphabet', () => {178 test(`has options`, () => {179 let nanoidA = customAlphabet('a', 5)180 equal(nanoidA(), 'aaaaa')181 })182183 test(`has flat distribution`, () => {184 let COUNT = 50 * 1000185 let LENGTH = 30186 let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'187 let nanoid2 = customAlphabet(ALPHABET, LENGTH)188189 let chars = {}190 for (let i = 0; i < COUNT; i++) {191 let id = nanoid2()192 for (let char of id) {193 if (!chars[char]) chars[char] = 0194 chars[char] += 1195 }196 }197198 equal(Object.keys(chars).length, ALPHABET.length)199200 let max = 0201 let min = Number.MAX_SAFE_INTEGER202 for (let k in chars) {203 let distribution = (chars[k] * ALPHABET.length) / (COUNT * LENGTH)204 if (distribution > max) max = distribution205 if (distribution < min) min = distribution206 }207 ok(max - min <= 0.05)208 })209210 test(`changes size`, () => {211 let nanoidA = customAlphabet('a')212 equal(nanoidA(10), 'aaaaaaaaaa')213 })214215 test(`is ready for 0 size`, () => {Fix testsAndrey Sitnikbefore adoption70bc11f
lines 216–217why, one step back216 equal(customAlphabet('abc')(0), '')217 equal(customAlphabet('abc', 0)(0), '')Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
lines 218–219why, one step back218 equal(customAlphabet('')(0), '')219 equal(customAlphabet('', 0)(0), '')Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 220why, one step back220 })Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
line 221why, one step back221fix: size customRandom's byte request from the ID length (#612)00200200before adoptionccb0e6e
lines 222–230why, one step back222 test(`does not hang when default size is 0`, { timeout: 2000 }, () => {223 let nanoid2 = customAlphabet('абв', 0)224 let id = nanoid2(8)225 equal(id.length, 8)226 for (let char of id) {227 ok('абв'.includes(char))228 }229 })230Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
lines 231–239why, one step back231 test(`avoids pool pollution, infinite loop`, () => {232 let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'233 let nanoid2 = customAlphabet(ALPHABET)234 nanoid2(2.1)235 let second = nanoid2()236 let third = nanoid2()237 notEqual(second, third)238 })239Fix non-integer size in customRandom() (#616)lostsignalsbefore adoption0b2cec5
lines 240–250why, one step back240 test(`supports non-integer size`, () => {241 let nanoid2 = customAlphabet('0123456789abcdef')242 let ids = new Set()243 for (let i = 0; i < 10; i++) {244 let id = nanoid2(16.5)245 equal(id.length, 16)246 ids.add(id)247 }248 equal(ids.size, 10)249 })250Port amazing nope-id optimizations (#602)Andrey Sitnikbefore adoptionb1bb75f
lines 251–276why, one step back251 test(`supports multi-byte characters`, () => {252 let ALPHABET = 'абвгд'253 let nanoid2 = customAlphabet(ALPHABET, 10)254 for (let i = 0; i < 100; i++) {255 let id = nanoid2()256 equal(id.length, 10)257 for (let char of id) {258 ok(ALPHABET.includes(char))259 }260 }261 })262263 test(`supports alphabets bigger than 256 symbols`, () => {264 equal(customAlphabet('a'.repeat(300))(0), '')265 })266267 test(`supports non-string alphabets`, () => {268 let ALPHABET = ['a', 'b', 'c', 'd']269 let nanoid2 = customAlphabet(ALPHABET, 10)270 let id = nanoid2()271 equal(id.length, 10)272 for (let char of id) {273 ok(ALPHABET.includes(char))274 }275 })276Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
lines 277–304why, one step back277 if (type === 'node') {278 test(`does not fall in infinite loop`, () => {279 equal(customAlphabet('abc')(0), '')280 equal(customAlphabet('abc', 0)(0), '')281 })282 }283 })284285 describe('customRandom', () => {286 test(`supports generator`, () => {287 let sequence = [2, 255, 3, 7, 7, 7, 7, 7, 0, 1]288 function fakeRandom(size) {289 let bytes = []290 for (let i = 0; i < size; i += sequence.length) {291 bytes = bytes.concat(sequence.slice(0, size - i))292 }293 return bytes294 }295 let nanoid4 = customRandom('abcde', 4, fakeRandom)296 let nanoid18 = customRandom('abcde', 18, fakeRandom)297 equal(nanoid4(), 'cccc')298 equal(nanoid18(), 'acccccdcbacccccdcb')299 })300301 test(`is ready for 0 size`, () => {302 let nanoid0 = customRandom('abc', 5, size => new Uint8Array(size))303 equal(nanoid0(0), '')304 equal(customRandom('', 5, size => new Uint8Array(size))(0), '')fix: size customRandom's byte request from the ID length (#612)00200200before adoptionccb0e6e
lines 305–309why, one step back305 })306307 test(`does not hang when default size is 0`, { timeout: 2000 }, () => {308 let nanoid0 = customRandom('abc', 0, size => new Uint8Array(size).fill(1))309 equal(nanoid0(8), 'bbbbbbbb')Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
line 310why, one step back310 })Port amazing nope-id optimizations (#602)Andrey Sitnikbefore adoptionb1bb75f
lines 311–323why, one step back311312 test(`supports power-of-two alphabets`, () => {313 let sequence = [2, 255, 3, 7]314 function fakeRandom(size) {315 let bytes = []316 for (let i = 0; i < size; i += sequence.length) {317 bytes = bytes.concat(sequence.slice(0, size - i))318 }319 return bytes320 }321 let nanoid4 = customRandom('abcd', 4, fakeRandom)322 equal(nanoid4(), 'dddc')323 equal(nanoid4(0), '')Fix non-integer size in customRandom() (#616)lostsignalsbefore adoption0b2cec5
line 324why, one step back324 equal(nanoid4(4.5), 'dddc')Port amazing nope-id optimizations (#602)Andrey Sitnikbefore adoptionb1bb75f
line 325why, one step back325 })Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
lines 326–344why, one step back326 })327328 describe('urlAlphabet', () => {329 test(`is string`, () => {330 equal(typeof urlAlphabet, 'string')331 })332333 test(`has 64 symbols`, () => {334 equal(urlAlphabet.length, 64)335 })336337 test(`has no duplicates`, () => {338 for (let i = 0; i < urlAlphabet.length; i++) {339 equal(urlAlphabet.lastIndexOf(urlAlphabet[i]), i)340 }341 })342 })343344 describe('random', () => {Port amazing nope-id optimizations (#602)Andrey Sitnikbefore adoptionb1bb75f
lines 345–350why, one step back345 test(`throws on negative size`, () => {346 throws(() => {347 random(-1)348 }, /Wrong ID size|Invalid typed array length/)349 })350Structure tests with describe() instead of prefixAndrey Sitnikbefore adoption974f73b
lines 351–369why, one step back351 test(`generates small random buffers`, () => {352 for (let i = 0; i < urlAlphabet.length; i++) {353 equal(random(10).length, 10)354 }355 })356357 test(`generates random buffers`, () => {358 let numbers = {}359 let bytes = random(1000)360 equal(bytes.length, 1000)361 for (let byte of bytes) {362 if (!numbers[byte]) numbers[byte] = 0363 numbers[byte] += 1364 equal(typeof byte, 'number')365 ok(byte <= 255)366 ok(byte >= 0)367 }368 })369 })Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 370–371why, one step back370 })371}