aero-mail/tests/compose-content.test.ts
2026-11-17 16:36:25 +07:00

36 lines
2.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { test } from 'node:test'
import assert from 'node:assert/strict'
import { JSDOM } from 'jsdom'
import createDOMPurify from 'dompurify'
import { sanitizeEditorHtml } from '../src/renderer/src/utils/compose-html.ts'
import { appendReplySignature } from '../src/renderer/src/utils/reply-signature.ts'
const window = new JSDOM('').window
const purifier = createDOMPurify(window)
test('сохраняет выделение, размер, списки и ссылки без картинок', (): void => {
const result: string = sanitizeEditorHtml('<p><span style="font-size:20px;color:red">Размер</span><b>Важно</b> <i>курсив</i> <u>линия</u><font size="5" color="#ff0000">крупно</font></p><ul><li>Пункт</li></ul><a href="https://example.org">Ссылка</a>', window.document, purifier)
assert.match(result, /<b>Важно<\/b>/)
assert.match(result, /<i>курсив<\/i>/)
assert.match(result, /font-size: 24px/)
assert.match(result, /font-size: 20px/)
assert.match(result, /<ul><li>Пункт<\/li><\/ul>/)
assert.match(result, /href="https:\/\/example.org"/)
})
test('очищает вставляемый HTML от скриптов, внешних картинок и опасных стилей', (): void => {
const result: string = sanitizeEditorHtml('<script>alert(1)</script><img src="https://tracker.example/img" onerror="alert(1)"><a href="javascript:alert(1)">X</a><span style="background:url(https://tracker.example);position:fixed;color:red">Текст</span>', window.document, purifier)
assert.doesNotMatch(result, /script|onerror|tracker|javascript|position|background/)
assert.match(result, /Текст/)
})
test('сохраняет встроенную картинку для последующего преобразования в CID', (): void => {
const result: string = sanitizeEditorHtml('<img src="data:image/png;base64,aGVsbG8=" alt="Картинка">', window.document, purifier)
assert.match(result, /data:image\/png;base64,aGVsbG8=/)
})
test('добавляет подпись перед будущей цитатой и оставляет пустую подпись отключённой', (): void => {
assert.equal(appendReplySignature('Добрый день.', 'С уважением,\nИван'), 'Добрый день.\n\n-- \nС уважением,\nИван')
assert.equal(appendReplySignature('', 'Иван'), '\n\n-- \nИван')
assert.equal(appendReplySignature('Ответ', ' '), 'Ответ')
})