import { describe, expect, it, vi } from 'vitest';
import { htmlToPlainText } from '../html-to-text';
describe('htmlToPlainText', () => {
it('preserves line breaks from
within a paragraph', () => {
expect(htmlToPlainText('
Line 1.
Line 2.
Line 1.
Line 2.
Paragraph 2.
', { paragraphSpacing: true }) ).toBe('Line 1.\nLine 2.\n\nParagraph 2.'); }); it('without paragraphSpacing, separates paragraphs with a single newline', () => { expect(htmlToPlainText('A
B
')).toBe('A\nB'); }); it('renders links as text when the text equals the href', () => { expect( htmlToPlainText('') ).toBe('alice@example.com'); }); it('renders an @mention as just its text', () => { expect( htmlToPlainText('') ).toBe("Hi @Dana see usVisit our site
')).toBe( 'Visit our siteHello world
')).toBe('Hello world'); }); it('caps consecutive blank lines at one', () => { expect(htmlToPlainText('A
B
', { paragraphSpacing: true })).toBe( 'A\n\nB' ); }); it('returns an empty string for empty or whitespace-only HTML', () => { expect(htmlToPlainText('')).toBe(''); expect(htmlToPlainText('')).toBe(''); }); it('handles nested lists as separate lines', () => { expect(htmlToPlainText('
Hi
--
User A
')).toBe('Hi\n-- \nUser A'); }); describe('mailMarkup', () => { // The text part of a reply showed the quoted original as ordinary text, // and list items without their bullets. const mail = { paragraphSpacing: true, mailMarkup: true }; it('prefixes quoted text with "> "', () => { const html = 'Sure.
On Sun, B wrote:
Hi A,
can you?
', mail)).toBe('> a\n>\n> > b'); }); // Every quote level used to serialise its innerHTML and parse it again, so // Reply on a long thread of nested quotes did quadratic work. it('parses a deeply nested quote once', () => { const depth = 300; const html = 'a
b
'.repeat(depth) + ''.repeat(depth); const parse = vi.spyOn(DOMParser.prototype, 'parseFromString'); try { expect(htmlToPlainText(html, mail)).toBe(`${'> '.repeat(depth)}deep`); expect(parse).toHaveBeenCalledTimes(1); } finally { parse.mockRestore(); } }); it('marks list items', () => { expect(htmlToPlainText('deep
' + '
a