99f7edc0ec
A list ended at the first line that was not an item, so a numbered list with nested bullets, wrapped lines or blank lines between items became one-item lists that each showed 1. Lists now nest (an item holds the blocks indented under it), continue across blank lines, join wrapped lines, and number from their first item. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
274 lines
10 KiB
Dart
274 lines
10 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown.dart';
|
|
|
|
String _visible(List<MarkdownInline> spans) => spans
|
|
.map(
|
|
(span) => switch (span) {
|
|
MarkdownPlain(text: final text) => text,
|
|
MarkdownInlineCode(text: final text) => text,
|
|
MarkdownStrong(spans: final inner) => _visible(inner),
|
|
MarkdownEmphasis(spans: final inner) => _visible(inner),
|
|
MarkdownLink(spans: final inner) => _visible(inner),
|
|
},
|
|
)
|
|
.join();
|
|
|
|
void main() {
|
|
group('entities', () {
|
|
test('decodes named and numeric references exactly once', () {
|
|
expect(
|
|
decodeHtmlEntities('&lt; "x" ' ''),
|
|
'< "x" \' \'',
|
|
);
|
|
expect(decodeHtmlEntities('&amp;'), '&');
|
|
expect(decodeHtmlEntities(' …—'), '\u00a0\u2026\u2014');
|
|
});
|
|
|
|
test('keeps unknown and malformed references as written', () {
|
|
expect(
|
|
decodeHtmlEntities('&bogus; & &#xZZ; �'),
|
|
'&bogus; & &#xZZ; �',
|
|
);
|
|
});
|
|
|
|
test('an overflowing numeric reference stays literal and never throws', () {
|
|
const overflow = '� �';
|
|
expect(decodeHtmlEntities(overflow), overflow);
|
|
final blocks = parseMarkdown(overflow);
|
|
expect((blocks.single as MarkdownParagraph).text, overflow);
|
|
});
|
|
|
|
test('numeric references beyond the Unicode range stay literal', () {
|
|
expect(
|
|
decodeHtmlEntities('� �'),
|
|
'� �',
|
|
);
|
|
expect(decodeHtmlEntities('A A'), 'A A');
|
|
});
|
|
});
|
|
|
|
group('blocks', () {
|
|
test('paragraphs join wrapped lines and keep trailing text', () {
|
|
final blocks = parseMarkdown('one line\nsecond line\n\nnext');
|
|
expect(blocks, hasLength(2));
|
|
expect((blocks[0] as MarkdownParagraph).text, 'one line second line');
|
|
expect((blocks[1] as MarkdownParagraph).text, 'next');
|
|
});
|
|
|
|
test('headings cover levels one to six and closing hashes', () {
|
|
final blocks = parseMarkdown(
|
|
'# one\n## two\n### three####\n#### four\n##### five\n###### six\n'
|
|
'####### seven\n#nospace',
|
|
);
|
|
final headings = blocks.whereType<MarkdownHeading>().toList();
|
|
expect(
|
|
[for (final heading in headings) heading.level],
|
|
[1, 2, 3, 4, 5, 6],
|
|
);
|
|
expect(
|
|
[for (final heading in headings) heading.text],
|
|
['one', 'two', 'three', 'four', 'five', 'six'],
|
|
);
|
|
expect(
|
|
(blocks[headings.length] as MarkdownParagraph).text,
|
|
'####### seven #nospace',
|
|
);
|
|
});
|
|
|
|
test('fenced code keeps verbatim whitespace and the language', () {
|
|
final blocks = parseMarkdown(
|
|
'```dart\n final x = 1;\n\n print(x);\n```\nafter',
|
|
);
|
|
final code = blocks.first as MarkdownCode;
|
|
expect(code.language, 'dart');
|
|
expect(code.code, ' final x = 1;\n\n print(x);');
|
|
expect(markdownCodeLabel(code.language), 'dart');
|
|
expect((blocks.last as MarkdownParagraph).text, 'after');
|
|
});
|
|
|
|
test('an unnamed fence is labelled code and unclosed code is kept', () {
|
|
final blocks = parseMarkdown('~~~\nbody');
|
|
final code = blocks.single as MarkdownCode;
|
|
expect(code.language, '');
|
|
expect(code.code, 'body');
|
|
expect(markdownCodeLabel(code.language), 'code');
|
|
});
|
|
|
|
test('quotes, bullet lists and ordered lists keep their content', () {
|
|
final blocks = parseMarkdown(
|
|
'> quoted **line**\n> second\n\n- one\n* two\n\n1. first\n2) second',
|
|
);
|
|
expect(blocks, hasLength(3));
|
|
expect((blocks[0] as MarkdownQuote).lines, ['quoted **line**', 'second']);
|
|
final bullets = blocks[1] as MarkdownList;
|
|
expect(bullets.ordered, isFalse);
|
|
expect(_texts(bullets), ['one', 'two']);
|
|
final ordered = blocks[2] as MarkdownList;
|
|
expect(ordered.ordered, isTrue);
|
|
expect(_texts(ordered), ['first', 'second']);
|
|
});
|
|
|
|
test('thematic rules need three identical markers', () {
|
|
final blocks = parseMarkdown('---\n***\n___\n- - -');
|
|
expect(blocks.whereType<MarkdownRule>(), hasLength(3));
|
|
expect(blocks.last, isA<MarkdownList>());
|
|
});
|
|
|
|
test('a table requires a delimiter row and fits rows to the header', () {
|
|
final blocks = parseMarkdown(
|
|
'| a | b |\n| --- | :---: |\n| 1 | 2 |\n| 3 |',
|
|
);
|
|
final table = blocks.single as MarkdownTable;
|
|
expect(table.header, ['a', 'b']);
|
|
expect(table.rows, [
|
|
['1', '2'],
|
|
['3', ''],
|
|
]);
|
|
|
|
final plain = parseMarkdown('a | b\nnope');
|
|
expect(plain.single, isA<MarkdownParagraph>());
|
|
expect((plain.single as MarkdownParagraph).text, 'a | b nope');
|
|
});
|
|
});
|
|
|
|
group('inline', () {
|
|
test('code, bold and italic spans keep plain text', () {
|
|
final spans = parseInline('a `x = 1` **bold** *italic* __also__');
|
|
expect(_visible(spans), 'a x = 1 bold italic also');
|
|
expect(spans.whereType<MarkdownInlineCode>().single.text, 'x = 1');
|
|
expect(spans.whereType<MarkdownStrong>(), hasLength(2));
|
|
expect(spans.whereType<MarkdownEmphasis>(), hasLength(1));
|
|
});
|
|
|
|
test('underscore emphasis never splits snake_case', () {
|
|
final spans = parseInline('snake_case_name and _real italic_ and _x');
|
|
expect(_visible(spans), 'snake_case_name and real italic and _x');
|
|
expect(spans.whereType<MarkdownEmphasis>().single, isNotNull);
|
|
expect(
|
|
spans.whereType<MarkdownPlain>().first.text,
|
|
'snake_case_name and ',
|
|
);
|
|
expect(spans.whereType<MarkdownPlain>().last.text, ' and _x');
|
|
});
|
|
|
|
test('unclosed markers stay plain text', () {
|
|
expect(_visible(parseInline('**open and `tick')), '**open and `tick');
|
|
});
|
|
|
|
test('links and bare http(s) URLs become link spans', () {
|
|
final spans = parseInline(
|
|
'See [docs](https://example.com/a) or https://example.com/b.',
|
|
);
|
|
final links = spans.whereType<MarkdownLink>().toList();
|
|
expect(links, hasLength(2));
|
|
expect(links[0].url, 'https://example.com/a');
|
|
expect(_visible(links[0].spans), 'docs');
|
|
expect(links[1].url, 'https://example.com/b');
|
|
expect(_visible(spans), 'See docs or https://example.com/b.');
|
|
});
|
|
|
|
test('only http and https targets are safe to activate', () {
|
|
expect(isSafeMarkdownLink('https://example.com'), isTrue);
|
|
expect(isSafeMarkdownLink('HTTP://example.com'), isTrue);
|
|
expect(isSafeMarkdownLink('http://example.com'), isTrue);
|
|
for (final unsafe in [
|
|
'javascript:alert(1)',
|
|
'data:text/html,<h1>x</h1>',
|
|
'ftp://example.com',
|
|
'//example.com',
|
|
'/artifacts/a.txt',
|
|
]) {
|
|
expect(isSafeMarkdownLink(unsafe), isFalse, reason: unsafe);
|
|
}
|
|
});
|
|
});
|
|
|
|
group('constructs outside the subset', () {
|
|
test('HTML stays literal text and is never interpreted', () {
|
|
const source = '<b>bold?</b> <script>alert(1)</script>';
|
|
final spans = parseInline(source);
|
|
expect(spans.whereType<MarkdownPlain>().single.text, source);
|
|
expect(_visible(spans), source);
|
|
});
|
|
|
|
test('strikethrough and reference links stay visible as text', () {
|
|
const source = '~~old~~ and [label][ref] and [missing]';
|
|
final blocks = parseMarkdown(source);
|
|
expect((blocks.single as MarkdownParagraph).text, source);
|
|
expect(_visible(parseInline(source)), source);
|
|
});
|
|
|
|
test('an indented nested item is kept under its parent', () {
|
|
final blocks = parseMarkdown('- outer\n - inner\n continuation');
|
|
final list = blocks.single as MarkdownList;
|
|
expect(_texts(list), ['outer']);
|
|
final inner = list.items.single.children.single as MarkdownList;
|
|
expect(_texts(inner), ['inner continuation']);
|
|
});
|
|
|
|
test('numbered items keep counting around nested bullets', () {
|
|
final blocks = parseMarkdown(
|
|
'1. First\n - detail a\n - detail b\n2. Second\n - detail c\n'
|
|
'3. Third',
|
|
);
|
|
final list = blocks.single as MarkdownList;
|
|
expect(list.ordered, isTrue);
|
|
expect(_texts(list), ['First', 'Second', 'Third']);
|
|
expect(_texts(list.items.first.children.single as MarkdownList), [
|
|
'detail a',
|
|
'detail b',
|
|
]);
|
|
});
|
|
|
|
test('a loose list stays one list across blank lines', () {
|
|
final list =
|
|
parseMarkdown('1. one\n\n2. two\n\n\n3. three').single
|
|
as MarkdownList;
|
|
expect(_texts(list), ['one', 'two', 'three']);
|
|
});
|
|
|
|
test('an ordered list starts at its first number', () {
|
|
final list = parseMarkdown('3. three\n4. four').single as MarkdownList;
|
|
expect(list.start, 3);
|
|
expect(parseMarkdown('- a').single, isA<MarkdownList>());
|
|
});
|
|
|
|
test('a wrapped line and an indented paragraph stay in their item', () {
|
|
final list =
|
|
parseMarkdown(
|
|
'1. starts here\nand wraps\n\n later paragraph\n2. next',
|
|
).single
|
|
as MarkdownList;
|
|
expect(_texts(list), ['starts here and wraps', 'next']);
|
|
expect(
|
|
(list.items.first.children.single as MarkdownParagraph).text,
|
|
'later paragraph',
|
|
);
|
|
});
|
|
|
|
test('a list ends at text back at the margin after a blank line', () {
|
|
final blocks = parseMarkdown('1. one\n2. two\n\nAfter the list.');
|
|
expect(blocks, hasLength(2));
|
|
expect((blocks.last as MarkdownParagraph).text, 'After the list.');
|
|
});
|
|
|
|
test('nested content is decoded once', () {
|
|
final list =
|
|
parseMarkdown('- a\n - &lt;b&gt;').single as MarkdownList;
|
|
expect(_texts(list.items.single.children.single as MarkdownList), [
|
|
'<b>',
|
|
]);
|
|
});
|
|
|
|
test('a link with an empty target stays literal', () {
|
|
const source = '[label]()';
|
|
expect(_visible(parseInline(source)), source);
|
|
expect(parseInline(source).whereType<MarkdownLink>(), isEmpty);
|
|
});
|
|
});
|
|
}
|
|
|
|
List<String> _texts(MarkdownList list) => [
|
|
for (final item in list.items) item.text,
|
|
];
|