Files
pipelines/app/test/features/chat/ui/transcript/chat_markdown_test.dart
T
alan a5afc55a9d feat(app): render native transcript markdown
Port the web markdown subset to a pure Dart parser and a native
Material/Cupertino block renderer, with a caller seam for assistant text
parts and safe http(s) link activation.

- parser: entities, paragraphs, H1-H6, fenced code, quotes, lists, tables,
  rules, inline code/emphasis/links; entity overflow stays literal
- renderer: host text roles, inline/fenced code fills, table header fill,
  heading level and list/columnHeader semantics, horizontal code/table
  scrolling, native selection
- tests cover parser safety, both hosts, the seam and real taps
- probe plus note record the web evidence and remaining limits
2026-10-21 23:34:33 +01:00

215 lines
7.9 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('&amp;lt; &quot;x&quot; &#39; &#x27;'),
'&lt; "x" \' \'',
);
expect(decodeHtmlEntities('&amp;amp;'), '&amp;');
expect(decodeHtmlEntities('&nbsp;&hellip;&mdash;'), '\u00a0\u2026\u2014');
});
test('keeps unknown and malformed references as written', () {
expect(
decodeHtmlEntities('&bogus; &amp &#xZZ; &#0;'),
'&bogus; &amp &#xZZ; &#0;',
);
});
test('an overflowing numeric reference stays literal and never throws', () {
const overflow = '&#99999999999999999999; &#xFFFFFFFFFFFFFFFFFFFFFFFF;';
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('&#x110000; &#1114112;'),
'&#x110000; &#1114112;',
);
expect(decodeHtmlEntities('&#00000000065; &#x000041;'), '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(bullets.items, ['one', 'two']);
final ordered = blocks[2] as MarkdownList;
expect(ordered.ordered, isTrue);
expect(ordered.items, ['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 retained rather than dropped', () {
final blocks = parseMarkdown('- outer\n - inner\n continuation');
final list = blocks.first as MarkdownList;
expect(list.items, ['outer', 'inner']);
expect((blocks.last as MarkdownParagraph).text, 'continuation');
});
test('a link with an empty target stays literal', () {
const source = '[label]()';
expect(_visible(parseInline(source)), source);
expect(parseInline(source).whereType<MarkdownLink>(), isEmpty);
});
});
}