import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown.dart'; String _visible(List 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().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(), hasLength(3)); expect(blocks.last, isA()); }); 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()); 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().single.text, 'x = 1'); expect(spans.whereType(), hasLength(2)); expect(spans.whereType(), 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().single, isNotNull); expect( spans.whereType().first.text, 'snake_case_name and ', ); expect(spans.whereType().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().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,

x

', '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 = 'bold? '; final spans = parseInline(source); expect(spans.whereType().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(), isEmpty); }); }); }