import { describe, it, expect } from '@jest/globals';
import { createHash } from 'crypto';
import { sanitizeRawHtml } from '../../../../../lib/util/sanitizeHtml.js';
import { descriptionToRows } from '../../../services/cells/description.js';
import type { EditorRow } from '../../../services/cells/description.js';
const NOW = 1_760_000_000_000;
const deps = {
now: NOW,
sha256Hex: (text: string) => createHash('sha256').update(text).digest('hex')
};
const convert = (text: string): EditorRow[] => {
const rows = descriptionToRows(text, deps);
if (rows === null) throw new Error('expected rows');
return rows;
};
const blocksOf = (text: string) => convert(text)[0].columns[0].data.blocks;
describe('descriptionToRows: golden examples (§ 5.7)', () => {
it('turns one line of plain text into one row, one column, one paragraph', () => {
// d88ab6ab = the first 8 digits of the SHA-256 of "Soft cotton tee".
expect(descriptionToRows('Soft cotton tee', deps)).toEqual([
{
id: 'rd88ab6ab',
size: 1,
className: 'md:grid-cols-1',
columns: [
{
id: 'cd88ab6ab',
size: 1,
data: {
time: NOW,
version: '2.31.0',
blocks: [
{
id: 'bd88ab6ab-0',
type: 'paragraph',
data: { text: 'Soft cotton tee' }
}
]
}
}
]
}
]);
});
it('turns blank-line-separated text into one paragraph block each', () => {
expect(
descriptionToRows('First paragraph.\n\nSecond paragraph.', deps)
).toEqual([
{
id: 'r8b3fc5d6',
size: 1,
className: 'md:grid-cols-1',
columns: [
{
id: 'c8b3fc5d6',
size: 1,
data: {
time: NOW,
version: '2.31.0',
blocks: [
{
id: 'b8b3fc5d6-0',
type: 'paragraph',
data: { text: 'First paragraph.' }
},
{
id: 'b8b3fc5d6-1',
type: 'paragraph',
data: { text: 'Second paragraph.' }
}
]
}
}
]
}
]);
});
it('turns HTML into one raw block, as it is', () => {
expect(descriptionToRows('
Hello world
', deps)).toEqual([
{
id: 'r41821239',
size: 1,
className: 'md:grid-cols-1',
columns: [
{
id: 'c41821239',
size: 1,
data: {
time: NOW,
version: '2.31.0',
blocks: [
{
id: 'b41821239-0',
type: 'raw',
data: { html: 'Hello world
' }
}
]
}
}
]
}
]);
});
it('turns a single line break inside a paragraph into
', () => {
expect(blocksOf('Line one\nLine two')).toEqual([
{
id: 'b6991ce0a-0',
type: 'paragraph',
data: { text: 'Line one
Line two' }
}
]);
});
});
describe('descriptionToRows: plain text', () => {
const paragraphs = (text: string) =>
blocksOf(text).map((block) => (block.data as { text: string }).text);
it('escapes & < > because the storefront renders a paragraph as HTML', () => {
expect(paragraphs('Tom & Jerry')).toEqual(['Tom & Jerry']);
expect(paragraphs('A d')).toEqual(['a < b and c > d']);
expect(paragraphs('5 > 3 & 2 < 4')).toEqual(['5 > 3 & 2 < 4']);
});
it('does not escape a quote, and does not escape twice', () => {
expect(paragraphs('He said "hi" & left')).toEqual([
'He said "hi" & left'
]);
expect(paragraphs('&')).toEqual(['&']);
});
it.each([
['CRLF', 'One\r\n\r\nTwo\r\nthree'],
['CR', 'One\r\rTwo\rthree'],
['LF', 'One\n\nTwo\nthree'],
['mixed', 'One\r\n\nTwo\nthree']
])('reads %s line breaks the same way', (_name, text) => {
expect(paragraphs(text)).toEqual(['One', 'Two
three']);
});
it('reads two or more blank lines, and lines with only spaces, as one separator', () => {
expect(paragraphs('One\n\n\n\nTwo')).toEqual(['One', 'Two']);
expect(paragraphs('One\n \t \nTwo')).toEqual(['One', 'Two']);
expect(paragraphs('One\n\n \n\nTwo')).toEqual(['One', 'Two']);
});
it('trims every line, and keeps the order of the lines', () => {
expect(paragraphs(' One \n Two\t\n\n Three ')).toEqual([
'One
Two',
'Three'
]);
});
it('keeps non-Latin text and emoji', () => {
expect(paragraphs('Áo thun nam\n\n日本語 👕')).toEqual([
'Áo thun nam',
'日本語 👕'
]);
});
it('numbers the blocks in order, with the same prefix', () => {
const blocks = blocksOf('a\n\nb\n\nc');
const h = blocks[0].id.split('-')[0];
expect(blocks.map((block) => block.id)).toEqual([
`${h}-0`,
`${h}-1`,
`${h}-2`
]);
});
});
describe('descriptionToRows: HTML', () => {
it.each([
['Soft
'],
['
'],
['Line one
Line two'],
['Title
\n'],
['text link text'],
['
'],
['x'],
['']
])('takes %j for HTML: one raw block, with the text as it is', (text) => {
const blocks = blocksOf(text);
expect(blocks).toHaveLength(1);
expect(blocks[0]).toMatchObject({ type: 'raw', data: { html: text } });
});
it.each([
['a < b and c > d'],
['x'],
['< p >'],
['<3 you >'],
['Price > 5']
])('takes %j for plain text, not for HTML', (text) => {
expect(blocksOf(text)[0].type).toBe('paragraph');
});
it('keeps the line breaks of HTML as they are', () => {
expect(blocksOf('a
\r\n\r\nb
')[0]).toMatchObject({
data: { html: 'a
\r\n\r\nb
' }
});
});
it('is cleaned by the service afterwards: the output has the shape sanitizeRawHtml walks', () => {
const rows = convert(
'Hi
'
);
sanitizeRawHtml(rows as never);
const html = (rows[0].columns[0].data.blocks[0].data as { html: string })
.html;
expect(html).toContain('Hi
');
expect(html).not.toContain('