import { describe, it, expect } from '@jest/globals'; import { escapeHtmlTags, isEscapeExempt, prepareText } from '../../../services/cells/text.js'; import type { ImportEntity } from '../../../services/types.js'; describe('escapeHtmlTags (D-23): what REST escapes, and nothing else', () => { it.each([ ['bold', '<b>bold</b>'], [ 'link', '<a href="x?a=1&b=2">link</a>' ], ['', '<script>alert(1)</script>'], ['', '<IMG src=x onerror=alert(1)>'], ['A C', 'A <b> C'], ['x

', '<p\nclass="a">x</p>'] ])('escapes the tags in %j', (input, expected) => { expect(escapeHtmlTags(input)).toBe(expected); }); it.each([ ['Tom & Jerry'], ['a < b and c > d'], ['x 3'], ['"quoted" and \'single\''], ['

a tag name with a digit is not matched by REST either

'], ['
'], ['<>'], ['< b >'], [''] ])('leaves %j alone', (input) => { expect(escapeHtmlTags(input)).toBe(input); }); it('escapes every tag, also next to text that is not a tag', () => { expect(escapeHtmlTags('1 < 2 and 3 > 2')).toBe( '1 < 2 <i>and</i> 3 > 2' ); }); it('stays fast on a cell of many "" (the plain expression takes 0.7 s)', () => { const cell = '${cell}`)).toBe(`>${cell}`); expect(Date.now() - start).toBeLessThan(200); }); }); describe('isEscapeExempt (ALL-22)', () => { it.each<[ImportEntity, string, boolean]>([ ['product', 'description', true], ['category', 'description', true], ['product', 'metafield:custom.material', true], ['category', 'metafield:custom.banner', true], ['customer', 'metafield:custom.vip', true], ['customer', 'password', true], ['customer', 'password_hash', true], ['product', 'short_description', false], ['product', 'name', false], ['product', 'meta_description', false], ['product', 'attr:color', false], ['product', 'redirect_from', false], ['category', 'meta_description', false], ['customer', 'full_name', false], ['customer', 'description', false], ['customerAddress', 'address_line_1', false], ['customerAddress', 'extra', false], ['product', 'password', false] ])('%s / %s: %s', (entity, column, exempt) => { expect(isEscapeExempt(entity, column)).toBe(exempt); }); }); describe('prepareText (§ 5.1)', () => { it('trims a cell', () => { expect(prepareText(' Blue tee \t', 'product', 'name')).toBe('Blue tee'); }); it.each([[''], [' '], ['\t\n'], [' ']])( 'gives null for the blank cell %j: not provided', (cell) => { expect(prepareText(cell, 'product', 'name')).toBeNull(); expect(prepareText(cell, 'product', 'description')).toBeNull(); } ); it('escapes a cell, after trimming', () => { expect(prepareText(' Sale ', 'product', 'name')).toBe( '<b>Sale</b>' ); expect(prepareText('x', 'product', 'short_description')).toBe( '<i>x</i>' ); }); it('does not escape an exempt column', () => { expect(prepareText('

Soft

', 'product', 'description')).toBe( '

Soft

' ); expect(prepareText('

x

', 'category', 'metafield:custom.note')).toBe( '

x

' ); expect(prepareText('ac', 'customer', 'password')).toBe('ac'); }); it('keeps the spaces of a password and trims everything else', () => { expect(prepareText(' secret ', 'customer', 'password')).toBe( ' secret ' ); expect(prepareText(' $2a$10$x ', 'customer', 'password_hash')).toBe( '$2a$10$x' ); }); it('treats a password of only spaces as a value, and an empty one as blank', () => { expect(prepareText(' ', 'customer', 'password')).toBe(' '); expect(prepareText('', 'customer', 'password')).toBeNull(); }); });