/* eslint-disable prettier/prettier */ /* eslint-disable @typescript-eslint/no-floating-promises -- node тесты */ import * as test from 'node:test'; import * as assert from 'node:assert/strict'; import { escape, unescape, encodeHTMLEntities, decodeHTMLEntities, decodeHTMLEntitiesDeep, decodeHTMLFullEntities, } from '../escape.ts'; const outOfBoundsChar = String.fromCharCode(65533); const empty = [ [undefined, ''], [null, ''], ] as string[][]; // JS type check test.test('escape', async (t) => { const escapeTest = [ ...empty, ['Entities &<>\'"', 'Entities &<>'"'], ['&foo <> bar "fizz" l\'a', '&foo <> bar "fizz" l'a'], ]; await Promise.all( escapeTest.map( async ([input, expected]) => await t.test( `escape(${JSON.stringify(input)}) should equal ${JSON.stringify(expected)}`, () => { assert.deepEqual(escape(input), expected); }, ), ), ); }); test.test('unescape', async (t) => { const unescapeTest = [ ...empty, ['Entities &<>'"', 'Entities &<>\'"'], ['foo''bar', "foo''bar"], ]; await Promise.all( unescapeTest.map( async ([input, expected]) => await t.test( `unescape(${JSON.stringify(input)}) should equal ${JSON.stringify(expected)}`, () => { assert.deepEqual(unescape(input), expected); }, ), ), ); }); test.test('encodeHTMLEntities', async (t) => { const encodeTest = [ ...empty, ['a\n<>"\'&©∆℞😂\0\x01', 'a\n<>"'&©∆℞😂\x00'], ]; await Promise.all( encodeTest.map( async ([input, expected]) => await t.test( `encodeHTMLEntities(${JSON.stringify(input)}) should equal ${JSON.stringify(expected)}`, () => { assert.deepEqual(encodeHTMLEntities(input), expected); }, ), ), ); }); const decodeTests = [ ...empty, [ 'Null and invalid entities � �', `Null and invalid entities ${outOfBoundsChar} ${outOfBoundsChar}`, ], ['Показувати моє ім'я лише авторові', "Показувати моє ім'я лише авторові"], [ 'Երկիրը չգտնվեց', 'Երկիրը չգտնվեց', ], ['& & < < > > " "', '& & < < > > " "'], ['" "', '" "'], ['Привет!', 'Привет!'], ['HEX entities ആ ആ', 'HEX entities ആ ആ'], ['Emoji 😂 🧚', 'Emoji 😂 🧚'], ['a\n<>"'&©∆℞😂\x00', 'a\n<>"\'&©∆℞😂\0\x01'], ]; test.test('decodeHTMLEntities', async (t) => { await Promise.all( decodeTests.map( async ([input, expected]) => await t.test( `decodeHTMLEntities(${JSON.stringify(input)}) should equal ${JSON.stringify(expected)}`, () => { assert.deepEqual(decodeHTMLEntities(input), expected); }, ), ), ); }); test.test('decodeHTMLEntitiesDeep', async (t) => { const decodeTestsLoopEntered = { array: [ 'Երկիրը չգտնվեց', { objectInArray: 'Показувати моє ім'я лише авторові', }, ], object: { keyInObject: '& & < < > > " "', arrayInObject: ['a\n<>"'&©∆℞😂\x00'], }, string: 'https://vk.com/groups?act=events_my', number: 123, function: test, null: null, undefined: undefined, }; const decodeTestsLoopExpected = { array: [ 'Երկիրը չգտնվեց', { objectInArray: "Показувати моє ім'я лише авторові", }, ], object: { keyInObject: '& & < < > > " "', arrayInObject: ['a\n<>"\'&©∆℞😂\0\x01'], }, string: 'https://vk.com/groups?act=events_my', number: 123, function: test, null: null, undefined: undefined, }; await t.test('object input', () => { assert.deepEqual(decodeHTMLEntitiesDeep(decodeTestsLoopEntered), decodeTestsLoopExpected); }); await t.test('string input', () => { assert.deepEqual(decodeHTMLEntitiesDeep('ր'), 'ր'); }); await t.test('number input', () => { assert.deepEqual(decodeHTMLEntitiesDeep(1), 1); }); await t.test('null input', () => { assert.deepEqual(decodeHTMLEntitiesDeep(null), null); }); await t.test('undefined input', () => { assert.deepEqual(decodeHTMLEntitiesDeep(undefined), undefined); }); await t.test('map input', () => { assert.deepEqual(decodeHTMLEntitiesDeep({ 'Ե': true }), { Ե: true }); }); await t.test('boolean input', () => { assert.deepEqual(decodeHTMLEntitiesDeep(false), false); }); await t.test('function input', () => { assert.deepEqual(decodeHTMLEntitiesDeep(test), test); }); await t.test('array input', () => { assert.deepEqual(decodeHTMLEntitiesDeep(['Ե', 1]), ['Ե', 1]); }); }); test.test('decodeHTMLEntities', async (t) => { const decodeFullTests = [ ...decodeTests, ['& &', '& &'], ['text ⋛︀ blah', 'text \u22db\ufe00 blah'], ['Lambda = λ = λ ', 'Lambda = λ = λ '], ['&# &#x €43 © = ©f = ©', '&# &#x €43 © = ©f = ©'], ['& &', '& &'], ['&Pi Π', '&Pi Π'], ]; await Promise.all( decodeFullTests.map( async ([input, expected]) => await t.test( `decodeHTMLFullEntities(${JSON.stringify(input)}) should equal ${JSON.stringify(expected)}`, () => { assert.deepEqual(decodeHTMLFullEntities(input), expected); }, ), ), ); });