Files
Rosea/packages/ui/test/letter-parse.test.ts
T
mnovak a200832cfb [ui] LetterSlot: Add the page for A Letter from the Gardener, shown once the author writes it
docs/LETTER.md holds slots as comments only, so the player sees no change
until the author writes. The app bundles the letter at build time, a small
Markdown subset renders without {@html}, only https: and tel: become links,
and lint:copy now reads the letter. Review found turbo replaying a stale
build after the letter changed; docs/LETTER.md is now in its cache key.
2026-11-01 15:39:17 +01:00

79 lines
3.3 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { parseLetter } from '../src/letter/parse.js';
describe('parseLetter', () => {
it('reads a file of only comments as an empty letter', () => {
const scaffold = '<!--\n A note for the author.\n-->\n\n<!-- Slot: one -->\n<!-- Slot: two -->\n';
expect(parseLetter(scaffold)).toEqual([]);
expect(parseLetter('')).toEqual([]);
expect(parseLetter(' \n\n \r\n')).toEqual([]);
});
it('reads headings, paragraphs joined across lines, and lists', () => {
const md = '# Why\n\nOne line\nand the next.\n\n## How\n- first\n* second\n\nLast.';
expect(parseLetter(md)).toEqual([
{ kind: 'heading', level: 2, inlines: [{ kind: 'text', text: 'Why' }] },
{ kind: 'paragraph', inlines: [{ kind: 'text', text: 'One line and the next.' }] },
{ kind: 'heading', level: 3, inlines: [{ kind: 'text', text: 'How' }] },
{ kind: 'list', items: [[{ kind: 'text', text: 'first' }], [{ kind: 'text', text: 'second' }]] },
{ kind: 'paragraph', inlines: [{ kind: 'text', text: 'Last.' }] }
]);
});
it('maps every heading below the first level to h3', () => {
expect(parseLetter('### Deep\n\n###### Deeper')).toEqual([
{ kind: 'heading', level: 3, inlines: [{ kind: 'text', text: 'Deep' }] },
{ kind: 'heading', level: 3, inlines: [{ kind: 'text', text: 'Deeper' }] }
]);
});
it('reads CRLF files the same as LF', () => {
expect(parseLetter('# A\r\n\r\nB\r\nC')).toEqual(parseLetter('# A\n\nB\nC'));
});
it('reads emphasis and links inside a paragraph', () => {
expect(parseLetter('A *quiet* door: [call](tel:+420 123 456) or [read](https://example.org/x).')).toEqual([
{
kind: 'paragraph',
inlines: [
{ kind: 'text', text: 'A ' },
{ kind: 'em', text: 'quiet' },
{ kind: 'text', text: ' door: ' },
{ kind: 'link', text: 'call', href: 'tel:+420 123 456' },
{ kind: 'text', text: ' or ' },
{ kind: 'link', text: 'read', href: 'https://example.org/x' },
{ kind: 'text', text: '.' }
]
}
]);
});
it('reads a phone number with brackets as one link', () => {
expect(parseLetter('[call](tel:+420 (2) 123)')).toEqual([
{ kind: 'paragraph', inlines: [{ kind: 'link', text: 'call', href: 'tel:+420 (2) 123' }] }
]);
});
it('keeps only the label of a link whose scheme is not https: or tel:', () => {
for (const href of ['javascript:alert(1)', 'data:text/html,x', 'http://example.org', '/relative', 'JavaScript:x']) {
expect(parseLetter(`[label](${href})`), href).toEqual([
{ kind: 'paragraph', inlines: [{ kind: 'text', text: 'label' }] }
]);
}
});
it('never produces markup: raw HTML stays text', () => {
const blocks = parseLetter('<script>alert(1)</script>\n\n<b onclick="x">bold</b>');
expect(blocks).toEqual([
{ kind: 'paragraph', inlines: [{ kind: 'text', text: '<script>alert(1)</script>' }] },
{ kind: 'paragraph', inlines: [{ kind: 'text', text: '<b onclick="x">bold</b>' }] }
]);
});
it('drops a comment in the middle of a paragraph and keeps the words around it', () => {
expect(parseLetter('Before <!-- a note --> after.')).toEqual([
{ kind: 'paragraph', inlines: [{ kind: 'text', text: 'Before after.' }] }
]);
});
});