feat(app): render markdown in chat messages and artifact previews

This commit is contained in:
2026-10-22 00:26:36 +01:00
parent 0e323dde10
commit dc6b963170
8 changed files with 926 additions and 12 deletions
@@ -13,6 +13,7 @@ import 'package:pipelines_app/features/artifacts/ui/artifact_gallery_page.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_fetch.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_page.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_visit_list.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
import '../domain/artifact_fixtures.dart';
import 'artifact_visit_list_fixture.dart';
@@ -37,7 +38,7 @@ Future<void> _pumpViewer(
required String selected,
bool cupertino = false,
ArtifactFetch? fetch,
Widget Function(String)? markdownBuilder,
Widget Function(BuildContext, String)? markdownBuilder,
ArtifactExternalOpen? openExternal,
}) async {
final page = ArtifactViewerPage(
@@ -45,7 +46,8 @@ Future<void> _pumpViewer(
selectedIdentity: selected,
configuredEndpoint: _endpoint,
fetch: fetch ?? (_) async => ArtifactFetchResult(status: 200, bytes: _png),
markdownBuilder: markdownBuilder ?? (value) => SelectableText(value),
markdownBuilder:
markdownBuilder ?? (context, value) => SelectableText(value),
openExternal: openExternal ?? (_) async => true,
);
await tester.pumpWidget(
@@ -64,6 +66,54 @@ TextButton _textButton(WidgetTester tester, String label) {
);
}
const String _markdownBody =
'# Report\n'
'\n'
'A paragraph with **bold** and `code`.\n'
'\n'
'| Step | State |\n'
'| --- | --- |\n'
'| build | ok |\n'
'\n'
'```dart\n'
'final n = 1;\n'
'```';
/// Opens the gallery's markdown row so the default builder composes the real
/// viewer exactly as the run detail does.
Future<void> _openMarkdownFromGallery(
WidgetTester tester, {
required bool cupertino,
String name = 'notes.md',
String contentType = 'text/markdown',
}) async {
final page = ArtifactGalleryPage(
steps: <JsonMap>[
step(
stage: 'review',
visit: 1,
artifacts: [
artifact(
name,
url: '/artifacts/1/1/$name',
contentType: contentType,
sizeBytes: 4096,
),
],
),
],
configuredEndpoint: _endpoint,
fetch: (_) async =>
ArtifactFetchResult(status: 200, bytes: utf8.encode(_markdownBody)),
);
await tester.pumpWidget(
cupertino ? CupertinoApp(home: page) : MaterialApp(home: page),
);
await tester.pumpAndSettle();
await tester.tap(find.text(name));
await tester.pumpAndSettle();
}
void main() {
testWidgets('an image is previewed in-app without opening a tab', (
tester,
@@ -153,7 +203,7 @@ void main() {
selected: identity,
fetch: (_) async =>
ArtifactFetchResult(status: 200, bytes: utf8.encode('# Title')),
markdownBuilder: (value) => Text('MARKDOWN $value'),
markdownBuilder: (context, value) => Text('MARKDOWN $value'),
);
expect(find.text('MARKDOWN # Title'), findsOneWidget);
@@ -403,4 +453,66 @@ void main() {
expect(find.text('cupertino text'), findsOneWidget);
expect(find.byType(CupertinoButton), findsWidgets);
});
testWidgets('the gallery previews markdown through the chat renderer', (
tester,
) async {
await _openMarkdownFromGallery(tester, cupertino: false);
expect(find.byType(ArtifactViewerPage), findsOneWidget);
expect(find.byType(ChatMarkdownView), findsOneWidget);
// The subset renders as native blocks, not raw markers.
expect(find.text('Report'), findsOneWidget);
expect(find.text('build'), findsOneWidget);
expect(find.text('ok'), findsOneWidget);
expect(find.text('dart'), findsOneWidget);
expect(find.text('final n = 1;'), findsOneWidget);
expect(find.text('# Report'), findsNothing);
expect(find.text('A paragraph with bold and code.'), findsOneWidget);
// The recorded facts stay visible above the preview.
expect(find.text('1 of 1 · text/markdown'), findsOneWidget);
expect(find.text('text/markdown · 4.0\u00a0KiB'), findsOneWidget);
expect(find.text('review · visit 1'), findsOneWidget);
});
testWidgets('a .markdown name previews without a recorded MIME', (
tester,
) async {
await _openMarkdownFromGallery(
tester,
cupertino: false,
name: 'notes.markdown',
contentType: '',
);
expect(find.byType(ArtifactViewerPage), findsOneWidget);
expect(find.byType(ChatMarkdownView), findsOneWidget);
expect(find.text('Report'), findsOneWidget);
expect(find.text('dart'), findsOneWidget);
expect(find.text('1 of 1 · artifact'), findsOneWidget);
expect(find.text('type not recorded · 4.0\u00a0KiB'), findsOneWidget);
});
testWidgets('Cupertino markdown preview is native at 320px and 2x text', (
tester,
) async {
tester.view.physicalSize = const Size(640, 1200);
tester.view.devicePixelRatio = 2;
tester.platformDispatcher.textScaleFactorTestValue = 2;
tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);
await _openMarkdownFromGallery(tester, cupertino: true);
expect(tester.takeException(), isNull);
expect(find.byType(ChatMarkdownView), findsOneWidget);
expect(find.text('Report'), findsOneWidget);
expect(find.text('dart'), findsOneWidget);
expect(find.text('1 of 1 · text/markdown'), findsOneWidget);
// The viewer and its markdown add no Material control to a Cupertino host.
expect(find.byType(Material), findsNothing);
});
}
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
import 'chat_thread_fixtures.dart';
@@ -306,6 +307,179 @@ void main() {
);
}
}
// Assistant prose goes through the landed markdown renderer; the user's
// message keeps the server's characters byte-for-byte.
for (final cupertino in [false, true]) {
testWidgets('assistant history renders markdown while user text stays exact '
'(${cupertino ? 'cupertino' : 'material'})', (tester) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
const userText =
'Keep **these** characters [as-is](javascript:alert) <b>literal</b>.';
const answer =
'## Stored answer\n'
'\n'
'- first item\n'
'- second item\n'
'\n'
'```dart\n'
'final answer = 42;\n'
'```\n'
'\n'
'Literal <b>markup</b> and [unsafe](javascript:alert) stay inert.';
gateway.history = (_) async => chatPage([
chatMessage(
'u1',
'user',
parts: [
{'type': 'text', 'text': userText},
],
),
chatMessage(
'a1',
'assistant',
parts: [
{'type': 'text', 'text': answer},
],
),
]);
await tester.pumpWidget(
(cupertino ? cupertinoHost : host)(
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
),
);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(find.byType(ChatMarkdownView), findsOneWidget);
// Stored assistant prose is parsed into native blocks.
expect(find.text('Stored answer'), findsOneWidget);
expect(find.text('first item'), findsOneWidget);
expect(find.text('second item'), findsOneWidget);
// A fenced block keeps its language label and exact code.
expect(find.text('dart'), findsOneWidget);
expect(find.text('final answer = 42;'), findsOneWidget);
// The subset's raw markers never reach the screen.
expect(find.textContaining('## Stored answer'), findsNothing);
expect(find.textContaining('```'), findsNothing);
// HTML stays literal and an unsafe link keeps only its label.
expect(
find.text(
'Literal <b>markup</b> and unsafe stay inert.',
findRichText: true,
),
findsOneWidget,
);
expect(
find.textContaining('javascript:alert'),
findsOneWidget,
reason: 'only the user text keeps the unsafe target verbatim',
);
// The user message is the server's exact plain text, not markdown.
expect(find.text(userText), findsOneWidget);
expect(
find.text('Keep these characters as-is literal.', findRichText: true),
findsNothing,
);
// No Material control is introduced into the Cupertino host.
if (cupertino) {
expect(find.byType(Material), findsNothing);
}
});
}
testWidgets('a live assistant text frame renders formatted markdown', (
tester,
) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage(const []);
await tester.pumpWidget(
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-thread-empty')), findsOneWidget);
gateway.emitEvent(
chatTextFrame('### Live answer\n\n1. first\n2. second\n\n`live code`'),
);
await tester.pump();
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(find.byType(ChatMarkdownView), findsOneWidget);
expect(find.text('Live answer'), findsOneWidget);
expect(find.text('1.'), findsOneWidget);
expect(find.text('2.'), findsOneWidget);
expect(find.text('first'), findsOneWidget);
expect(find.text('second'), findsOneWidget);
expect(find.text('live code'), findsOneWidget);
expect(find.textContaining('### Live answer'), findsNothing);
expect(find.textContaining('`live code`'), findsNothing);
expect(find.text('Assistant is responding…'), findsOneWidget);
});
testWidgets('formatted assistant output fits 320px at 2x in dark Cupertino', (
tester,
) async {
tester.view.physicalSize = const Size(640, 1200);
tester.view.devicePixelRatio = 2;
tester.platformDispatcher.textScaleFactorTestValue = 2;
tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage([
chatMessage(
'a1',
'assistant',
parts: [
{
'type': 'text',
'text':
'## A long answer heading that wraps inside a narrow phone\n'
'\n'
'A paragraph long enough to wrap onto several lines without '
'clipping any words at twice the host text size.\n'
'\n'
'| Step | State |\n'
'| --- | --- |\n'
'| build | ok |\n'
'\n'
'```dart\n'
'final value = "a code line that scrolls sideways";\n'
'```',
},
],
),
]);
await tester.pumpWidget(
cupertinoHost(
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
),
);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(
find.text('A long answer heading that wraps inside a narrow phone'),
findsOneWidget,
);
expect(find.text('build'), findsOneWidget);
expect(find.text('dart'), findsOneWidget);
expect(find.byType(Material), findsNothing);
});
}
/// The paged-history fixture shape: a page of consecutive assistant messages,