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
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/semantics.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
|
||||
|
||||
import 'chat_markdown_test_support.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Material headings keep a non-increasing hierarchy', (
|
||||
tester,
|
||||
) async {
|
||||
await phoneViewport(tester);
|
||||
await tester.pumpWidget(
|
||||
markdownHost(const ChatMarkdownView(text: markdownHeadings)),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
|
||||
final context = tester.element(find.byType(ChatMarkdownView));
|
||||
final text = Theme.of(context).textTheme;
|
||||
final roles = [
|
||||
text.headlineSmall,
|
||||
text.titleLarge,
|
||||
text.titleMedium,
|
||||
text.titleMedium,
|
||||
text.titleMedium,
|
||||
text.titleMedium,
|
||||
];
|
||||
var previousSize = double.infinity;
|
||||
for (var level = 1; level <= 6; level += 1) {
|
||||
final label = markdownHeadingLabels[level - 1];
|
||||
final heading = textWidget(tester, label);
|
||||
final role = roles[level - 1]!;
|
||||
expect(heading.style?.fontSize, role.fontSize, reason: label);
|
||||
expect(heading.style?.fontWeight, role.fontWeight, reason: label);
|
||||
expect(
|
||||
heading.style!.fontSize!,
|
||||
lessThanOrEqualTo(previousSize),
|
||||
reason: label,
|
||||
);
|
||||
previousSize = heading.style!.fontSize!;
|
||||
final semantics = tester
|
||||
.widgetList<Semantics>(
|
||||
find.ancestor(
|
||||
of: find.text(label),
|
||||
matching: find.byType(Semantics),
|
||||
),
|
||||
)
|
||||
.singleWhere((widget) => widget.properties.headingLevel != null);
|
||||
expect(semantics.properties.headingLevel, level, reason: label);
|
||||
}
|
||||
final paragraph = textWidget(tester, 'A plain paragraph.');
|
||||
expect(paragraph.style, isNull);
|
||||
expect(roles.last!.fontWeight, isNot(text.bodyMedium?.fontWeight));
|
||||
});
|
||||
|
||||
testWidgets('Cupertino headings stay on title roles above body text', (
|
||||
tester,
|
||||
) async {
|
||||
await phoneViewport(tester);
|
||||
await tester.pumpWidget(
|
||||
markdownHost(
|
||||
const ChatMarkdownView(text: markdownHeadings),
|
||||
cupertino: true,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
|
||||
final context = tester.element(find.byType(ChatMarkdownView));
|
||||
final theme = CupertinoTheme.of(context).textTheme;
|
||||
final roles = [
|
||||
theme.navLargeTitleTextStyle,
|
||||
theme.navTitleTextStyle,
|
||||
theme.navTitleTextStyle,
|
||||
theme.navTitleTextStyle,
|
||||
theme.navTitleTextStyle,
|
||||
theme.navTitleTextStyle,
|
||||
];
|
||||
var previousSize = double.infinity;
|
||||
for (var level = 1; level <= 6; level += 1) {
|
||||
final label = markdownHeadingLabels[level - 1];
|
||||
final heading = textWidget(tester, label);
|
||||
final role = roles[level - 1];
|
||||
expect(heading.style?.fontSize, role.fontSize, reason: label);
|
||||
expect(heading.style?.fontWeight, role.fontWeight, reason: label);
|
||||
expect(
|
||||
heading.style!.fontSize!,
|
||||
lessThanOrEqualTo(previousSize),
|
||||
reason: label,
|
||||
);
|
||||
previousSize = heading.style!.fontSize!;
|
||||
expect(
|
||||
heading.style!.fontWeight,
|
||||
isNot(theme.textStyle.fontWeight),
|
||||
reason: label,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('inline code carries the host code surface on both hosts', (
|
||||
tester,
|
||||
) async {
|
||||
await phoneViewport(tester);
|
||||
for (final cupertino in [false, true]) {
|
||||
await tester.pumpWidget(
|
||||
markdownHost(
|
||||
const ChatMarkdownView(
|
||||
text: 'Use `inline_code()` in the middle of this sentence.',
|
||||
),
|
||||
cupertino: cupertino,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
final context = tester.element(find.byType(ChatMarkdownView));
|
||||
final fill = cupertino
|
||||
? CupertinoColors.tertiarySystemFill.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.surfaceContainerHighest;
|
||||
final spans = spansOf(tester, 'Use');
|
||||
final code = spans.firstWhere((span) => span.text == 'inline_code()');
|
||||
final plain = spans.firstWhere(
|
||||
(span) => span.text == ' in the middle of this sentence.',
|
||||
);
|
||||
expect(code.style?.backgroundColor, fill, reason: '$cupertino');
|
||||
expect(plain.style?.backgroundColor, isNull, reason: '$cupertino');
|
||||
expect(code.style?.fontSize, isNull, reason: '$cupertino');
|
||||
expect(code.style?.fontFamily, isNull, reason: '$cupertino');
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('a table header carries its own surface and column role', (
|
||||
tester,
|
||||
) async {
|
||||
await phoneViewport(tester);
|
||||
for (final cupertino in [false, true]) {
|
||||
await tester.pumpWidget(
|
||||
markdownHost(
|
||||
const ChatMarkdownView(
|
||||
text: '| Name | Value |\n| --- | --- |\n| a | 1 |',
|
||||
),
|
||||
cupertino: cupertino,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
final context = tester.element(find.byType(ChatMarkdownView));
|
||||
final fill = cupertino
|
||||
? CupertinoColors.tertiarySystemFill.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.surfaceContainerHigh;
|
||||
final table = tester.widget<Table>(find.byType(Table));
|
||||
final decoration = table.children.first.decoration;
|
||||
expect(decoration, isA<BoxDecoration>(), reason: '$cupertino');
|
||||
expect((decoration! as BoxDecoration).color, fill, reason: '$cupertino');
|
||||
final headers = tester
|
||||
.widgetList<Semantics>(find.byType(Semantics))
|
||||
.where(
|
||||
(widget) => widget.properties.role == SemanticsRole.columnHeader,
|
||||
);
|
||||
expect(headers, hasLength(2), reason: '$cupertino');
|
||||
expect(
|
||||
tester
|
||||
.widgetList<Semantics>(find.byType(Semantics))
|
||||
.where((widget) => widget.properties.header == true),
|
||||
isEmpty,
|
||||
reason: '$cupertino',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('semantics expose heading levels, lists and column headers', (
|
||||
tester,
|
||||
) async {
|
||||
final handle = tester.ensureSemantics();
|
||||
await phoneViewport(tester);
|
||||
await tester.pumpWidget(
|
||||
markdownHost(const ChatMarkdownView(text: markdownSample)),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final heading = tester.getSemantics(find.text('Second'));
|
||||
expect(heading.getSemanticsData().headingLevel, 2);
|
||||
|
||||
final item = tester.getSemantics(
|
||||
find
|
||||
.byWidgetPredicate(
|
||||
(widget) =>
|
||||
widget is Semantics &&
|
||||
widget.properties.role == SemanticsRole.listItem,
|
||||
)
|
||||
.first,
|
||||
);
|
||||
expect(item.getSemanticsData().role, SemanticsRole.listItem);
|
||||
expect(item.parent?.getSemanticsData().role, SemanticsRole.list);
|
||||
|
||||
final header = tester.getSemantics(
|
||||
find
|
||||
.byWidgetPredicate(
|
||||
(widget) =>
|
||||
widget is Semantics &&
|
||||
widget.properties.role == SemanticsRole.columnHeader,
|
||||
)
|
||||
.first,
|
||||
);
|
||||
expect(header.getSemanticsData().role, SemanticsRole.columnHeader);
|
||||
expect(tester.takeException(), isNull);
|
||||
handle.dispose();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
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(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);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
/// One representative message used by the markdown renderer tests.
|
||||
const markdownSample = '''
|
||||
# Title
|
||||
Paragraph with **bold**, *italic*, `inline` and snake_case_name.
|
||||
## Second
|
||||
> quote one
|
||||
> quote two
|
||||
- bullet one
|
||||
- bullet two
|
||||
1. first
|
||||
2. second
|
||||
| Name | Value |
|
||||
| --- | --- |
|
||||
| a | 1 |
|
||||
```dart
|
||||
const answer = 42;
|
||||
keep spaces
|
||||
```
|
||||
---
|
||||
[ok](https://example.com/ok) [**bold link**](https://example.com/bold) [bad](javascript:alert) https://example.com/auto
|
||||
<b>literal</b> ~~gone~~
|
||||
''';
|
||||
|
||||
/// All six heading levels plus a body paragraph.
|
||||
const markdownHeadingLabels = [
|
||||
'Level one',
|
||||
'Level two',
|
||||
'Level three',
|
||||
'Level four',
|
||||
'Level five',
|
||||
'Level six',
|
||||
];
|
||||
|
||||
const markdownHeadings = '''
|
||||
# Level one
|
||||
## Level two
|
||||
### Level three
|
||||
#### Level four
|
||||
##### Level five
|
||||
###### Level six
|
||||
A plain paragraph.
|
||||
''';
|
||||
|
||||
/// Mounts a markdown renderer in a dark host at 2x text scaling.
|
||||
Widget markdownHost(Widget child, {bool cupertino = false}) {
|
||||
Widget builder(BuildContext context, Widget? body) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: const TextScaler.linear(2)),
|
||||
child: body!,
|
||||
);
|
||||
if (cupertino) {
|
||||
return CupertinoApp(
|
||||
theme: const CupertinoThemeData(brightness: Brightness.dark),
|
||||
builder: builder,
|
||||
home: CupertinoPageScaffold(
|
||||
child: SafeArea(child: SingleChildScrollView(child: child)),
|
||||
),
|
||||
);
|
||||
}
|
||||
return MaterialApp(
|
||||
theme: ThemeData(
|
||||
brightness: Brightness.dark,
|
||||
platform: TargetPlatform.android,
|
||||
),
|
||||
builder: builder,
|
||||
home: Scaffold(
|
||||
body: SafeArea(child: SingleChildScrollView(child: child)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> phoneViewport(WidgetTester tester) async {
|
||||
tester.view.physicalSize = const Size(390, 844);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
}
|
||||
|
||||
String textOf(Text widget) =>
|
||||
widget.data ?? widget.textSpan?.toPlainText() ?? '';
|
||||
|
||||
String allText(WidgetTester tester) =>
|
||||
tester.widgetList<Text>(find.byType(Text)).map(textOf).join('\n');
|
||||
|
||||
Text textWidget(WidgetTester tester, String text) => tester
|
||||
.widgetList<Text>(find.byType(Text))
|
||||
.firstWhere((widget) => textOf(widget) == text);
|
||||
|
||||
Iterable<TextSpan> _spansBelow(InlineSpan root) sync* {
|
||||
if (root is! TextSpan) return;
|
||||
yield root;
|
||||
for (final child in root.children ?? const <InlineSpan>[]) {
|
||||
yield* _spansBelow(child);
|
||||
}
|
||||
}
|
||||
|
||||
List<TextSpan> spansOf(WidgetTester tester, String text) {
|
||||
final widget = tester
|
||||
.widgetList<Text>(find.byType(Text))
|
||||
.firstWhere((widget) => textOf(widget).contains(text));
|
||||
return _spansBelow(widget.textSpan!).toList();
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
|
||||
|
||||
import 'chat_markdown_test_support.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Material renders every block with host theme roles', (
|
||||
tester,
|
||||
) async {
|
||||
await phoneViewport(tester);
|
||||
final opened = <String>[];
|
||||
await tester.pumpWidget(
|
||||
markdownHost(
|
||||
ChatMarkdownView(text: markdownSample, onOpenLink: opened.add),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
|
||||
final context = tester.element(find.byType(ChatMarkdownView));
|
||||
final theme = Theme.of(context);
|
||||
final heading = tester
|
||||
.widgetList<Text>(find.byType(Text))
|
||||
.firstWhere((widget) => textOf(widget) == 'Title');
|
||||
expect(heading.style?.fontSize, theme.textTheme.headlineSmall?.fontSize);
|
||||
|
||||
final all = allText(tester);
|
||||
for (final text in [
|
||||
'Title',
|
||||
'Second',
|
||||
'quote one',
|
||||
'quote two',
|
||||
'bullet one',
|
||||
'bullet two',
|
||||
'first',
|
||||
'second',
|
||||
'const answer = 42;\n keep spaces',
|
||||
'literal',
|
||||
'~~gone~~',
|
||||
]) {
|
||||
expect(all, contains(text));
|
||||
}
|
||||
|
||||
final paragraph = spansOf(tester, 'Paragraph with');
|
||||
final bold = paragraph.firstWhere(
|
||||
(span) => span.children != null && span.toPlainText() == 'bold',
|
||||
);
|
||||
final italic = paragraph.firstWhere(
|
||||
(span) => span.children != null && span.toPlainText() == 'italic',
|
||||
);
|
||||
expect(bold.style?.fontWeight, FontWeight.bold);
|
||||
expect(italic.style?.fontStyle, FontStyle.italic);
|
||||
final inlineCode = paragraph.firstWhere(
|
||||
(span) => span.toPlainText() == 'inline',
|
||||
);
|
||||
expect(
|
||||
inlineCode.style?.backgroundColor,
|
||||
theme.colorScheme.surfaceContainerHighest,
|
||||
);
|
||||
final plain = paragraph.firstWhere(
|
||||
(span) => span.toPlainText() == ' and snake_case_name.',
|
||||
);
|
||||
expect(plain.style?.fontStyle, isNull);
|
||||
|
||||
expect(find.text('\u2022'), findsNWidgets(2));
|
||||
expect(find.text('1.'), findsOneWidget);
|
||||
expect(find.text('2.'), findsOneWidget);
|
||||
expect(find.text('dart'), findsOneWidget);
|
||||
|
||||
final table = tester.widget<Table>(find.byType(Table));
|
||||
expect(table.children, hasLength(2));
|
||||
expect(table.children.first.children, hasLength(2));
|
||||
expect(all, contains('a'));
|
||||
|
||||
final code = find.byWidgetPredicate(
|
||||
(widget) =>
|
||||
widget is Text &&
|
||||
widget.data == 'const answer = 42;\n keep spaces',
|
||||
);
|
||||
expect(code, findsOneWidget);
|
||||
final scroll = find.ancestor(
|
||||
of: code,
|
||||
matching: find.byWidgetPredicate(
|
||||
(widget) =>
|
||||
widget is SingleChildScrollView &&
|
||||
widget.scrollDirection == Axis.horizontal,
|
||||
),
|
||||
);
|
||||
expect(scroll, findsOneWidget);
|
||||
expect(find.byType(Divider), findsOneWidget);
|
||||
|
||||
final links = spansOf(tester, 'https://example.com/auto');
|
||||
final ok = links.firstWhere((span) => span.text == 'ok');
|
||||
final bad = links.firstWhere((span) => span.text == 'bad');
|
||||
final auto = links.firstWhere(
|
||||
(span) => span.text == 'https://example.com/auto',
|
||||
);
|
||||
expect(ok.recognizer, isA<TapGestureRecognizer>());
|
||||
expect(bad.recognizer, isNull);
|
||||
expect(bad.style?.decoration, isNull);
|
||||
expect(auto.recognizer, isA<TapGestureRecognizer>());
|
||||
final boldLink = links.firstWhere((span) => span.text == 'bold link');
|
||||
expect(boldLink.recognizer, isA<TapGestureRecognizer>());
|
||||
expect(
|
||||
links.any(
|
||||
(span) =>
|
||||
span.children != null &&
|
||||
span.toPlainText() == 'bold link' &&
|
||||
span.style?.fontWeight == FontWeight.bold,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
(ok.recognizer! as TapGestureRecognizer).onTap!();
|
||||
expect(opened, ['https://example.com/ok']);
|
||||
});
|
||||
|
||||
testWidgets('Cupertino renders the same blocks in a dark host', (
|
||||
tester,
|
||||
) async {
|
||||
await phoneViewport(tester);
|
||||
await tester.pumpWidget(
|
||||
markdownHost(ChatMarkdownView(text: markdownSample), cupertino: true),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
|
||||
final context = tester.element(find.byType(ChatMarkdownView));
|
||||
final heading = tester
|
||||
.widgetList<Text>(find.byType(Text))
|
||||
.firstWhere((widget) => textOf(widget) == 'Title');
|
||||
expect(
|
||||
heading.style?.fontSize,
|
||||
CupertinoTheme.of(context).textTheme.navLargeTitleTextStyle.fontSize,
|
||||
);
|
||||
final all = allText(tester);
|
||||
expect(all, contains('quote one'));
|
||||
expect(all, contains('const answer = 42;'));
|
||||
expect(find.text('code'), findsNothing);
|
||||
expect(find.text('dart'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('a safe link responds to a real tap on both hosts', (
|
||||
tester,
|
||||
) async {
|
||||
await phoneViewport(tester);
|
||||
final opened = <String>[];
|
||||
for (final cupertino in [false, true]) {
|
||||
opened.clear();
|
||||
await tester.pumpWidget(
|
||||
markdownHost(
|
||||
ChatMarkdownView(
|
||||
text: '[ok](https://example.com/ok)',
|
||||
onOpenLink: opened.add,
|
||||
),
|
||||
cupertino: cupertino,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tapAt(
|
||||
tester.getTopLeft(find.byType(Text)) + const Offset(6, 6),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(opened, ['https://example.com/ok'], reason: '$cupertino');
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('an overflowing entity renders literally instead of failing', (
|
||||
tester,
|
||||
) async {
|
||||
await phoneViewport(tester);
|
||||
const text =
|
||||
'value � and � stay';
|
||||
await tester.pumpWidget(markdownHost(ChatMarkdownView(text: text)));
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(allText(tester), contains(text));
|
||||
});
|
||||
|
||||
testWidgets('the text builder seam renders assistant markdown', (
|
||||
tester,
|
||||
) async {
|
||||
await phoneViewport(tester);
|
||||
final row = transcriptRows([
|
||||
{
|
||||
'id': 'a1',
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{'type': 'text', 'text': '# Title\n\nA **bold** answer.'},
|
||||
],
|
||||
},
|
||||
]).single;
|
||||
await tester.pumpWidget(
|
||||
markdownHost(ChatMessageView(row: row, textBuilder: chatMarkdownText)),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(ChatMarkdownView), findsOneWidget);
|
||||
final all = allText(tester);
|
||||
expect(all, contains('Title'));
|
||||
expect(all, isNot(contains('#')));
|
||||
expect(all, isNot(contains('**')));
|
||||
expect(all, contains('bold'));
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user