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
@@ -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,