Separate chunked reply paragraphs with an empty-line gap
Consecutive text parts of an agent turn (including folded messages) and markdown blocks within a part are now separated by ~0.9x the scaled body font size; a heading stays 8px from what it introduces. Adds a ?paras=1 chunked reply to the composer probe and gap tests at 1x/2x.
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
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/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_bubble.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
|
||||
|
||||
/// A reply that arrived in chunks: two folded messages, the first with two
|
||||
/// text parts and a tool call, the last part holding two markdown paragraphs.
|
||||
final JsonMap _chunked = transcriptRows([
|
||||
{
|
||||
'id': 'a1',
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{'type': 'reasoning', 'text': 'Thinking it over'},
|
||||
{'type': 'text', 'text': 'First chunk.'},
|
||||
{
|
||||
'type': 'tool',
|
||||
'tool': 'shell',
|
||||
'state': {
|
||||
'status': 'completed',
|
||||
'input': {'command': 'ls'},
|
||||
},
|
||||
},
|
||||
{'type': 'text', 'text': 'Second chunk.'},
|
||||
],
|
||||
},
|
||||
{
|
||||
'id': 'a2',
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{'type': 'text', 'text': 'Third paragraph.\n\nFourth paragraph.'},
|
||||
],
|
||||
},
|
||||
]).single;
|
||||
|
||||
Widget _host({
|
||||
required bool cupertino,
|
||||
required double scale,
|
||||
ChatMessageTextBuilder? textBuilder,
|
||||
}) {
|
||||
final body = ListView(
|
||||
children: [ChatMessageView(row: _chunked, textBuilder: textBuilder)],
|
||||
);
|
||||
Widget scaled(BuildContext context, Widget? child) => MediaQuery(
|
||||
data: MediaQuery.of(context).copyWith(textScaler: TextScaler.linear(scale)),
|
||||
child: child!,
|
||||
);
|
||||
if (cupertino) {
|
||||
return CupertinoApp(
|
||||
builder: scaled,
|
||||
home: CupertinoPageScaffold(child: body),
|
||||
);
|
||||
}
|
||||
return MaterialApp(
|
||||
theme: ThemeData(platform: TargetPlatform.android),
|
||||
builder: scaled,
|
||||
home: Scaffold(body: body),
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
for (final cupertino in [false, true]) {
|
||||
for (final scale in [1.0, 2.0]) {
|
||||
for (final markdown in [false, true]) {
|
||||
final label =
|
||||
'${cupertino ? 'cupertino' : 'material'}, ${scale}x'
|
||||
'${markdown ? ', markdown' : ''}';
|
||||
|
||||
testWidgets('chunks and paragraphs read as paragraphs ($label)', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(800, 2400);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
await tester.pumpWidget(
|
||||
_host(
|
||||
cupertino: cupertino,
|
||||
scale: scale,
|
||||
textBuilder: markdown ? chatMarkdownText : null,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final context = tester.element(find.byType(ChatMessageView));
|
||||
final fontSize = chatMessageTextStyle(context, cupertino)!.fontSize!;
|
||||
final line = fontSize * scale;
|
||||
Rect rect(String text) => tester.getRect(
|
||||
find.textContaining(text, findRichText: true).first,
|
||||
);
|
||||
double gap(String above, String below) =>
|
||||
rect(below).top - rect(above).bottom;
|
||||
|
||||
// Consecutive text parts, across a tool call and a folded message.
|
||||
expect(gap('First chunk.', 'Second chunk.'), greaterThan(0.8 * line));
|
||||
expect(gap('First chunk.', 'Second chunk.'), lessThan(1.5 * line));
|
||||
expect(
|
||||
gap('Second chunk.', 'Third paragraph.'),
|
||||
greaterThan(0.8 * line),
|
||||
);
|
||||
if (markdown) {
|
||||
// Paragraph blocks inside one markdown part.
|
||||
expect(
|
||||
gap('Third paragraph.', 'Fourth paragraph.'),
|
||||
greaterThan(0.8 * line),
|
||||
);
|
||||
expect(
|
||||
gap('Third paragraph.', 'Fourth paragraph.'),
|
||||
lessThan(1.5 * line),
|
||||
);
|
||||
}
|
||||
|
||||
// The activity line keeps its small lead-in, and nothing pads the
|
||||
// footer away from the last paragraph.
|
||||
final activity = tester.getRect(
|
||||
find.byKey(const Key('chat-turn-activity')),
|
||||
);
|
||||
expect(rect('First chunk.').top - activity.bottom, lessThan(line));
|
||||
final last = markdown ? 'Fourth paragraph.' : 'Third paragraph.';
|
||||
final footer = tester.getRect(
|
||||
find.byKey(const Key('chat-message-footer')),
|
||||
);
|
||||
expect(footer.top - rect(last).bottom, lessThan(0.5 * line));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
testWidgets('a heading stays close to the paragraph it introduces', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
const MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ChatMarkdownView(text: '## Plan\nFirst step.\n\nSecond step.'),
|
||||
),
|
||||
),
|
||||
);
|
||||
Rect rect(String text) =>
|
||||
tester.getRect(find.textContaining(text, findRichText: true).first);
|
||||
final headingGap = rect('First step.').top - rect('Plan').bottom;
|
||||
final paragraphGap = rect('Second step.').top - rect('First step.').bottom;
|
||||
expect(headingGap, lessThan(paragraphGap));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user