Files
pipelines/app/test/features/chat/conversation/chat_response_pending_test.dart
T
alan 6dbfdc6e64 fix(chat): centre the pending dots on the activity text
The dots were period glyphs sitting on the baseline, so they read low
beside the label. They are now round dots sized from the surrounding
text, vertically centred with it and scaling with it.
2026-10-30 23:54:22 +00:00

236 lines
7.7 KiB
Dart

import 'dart:async';
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/conversation/chat_draft_store.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_response_pending.dart';
import 'chat_thread_fixtures.dart';
class _Gateway extends FakeChatGateway {
Completer<JsonMap> prompt = Completer();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'chat_prompt') return prompt.future;
return super.call(type, fields);
}
}
Future<void> _pump(WidgetTester tester) async {
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
await tester.pump();
}
Future<void> _host(
WidgetTester tester,
_Gateway gateway, {
bool cupertino = false,
}) async {
gateway.history = (_) async => chatPage([]);
final thread = ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
drafts: MemoryChatDraftStore(),
newOperationId: () => 'test-pending',
);
Widget animations(BuildContext context, Widget? child) => MediaQuery(
data: MediaQuery.of(context).copyWith(disableAnimations: false),
child: child!,
);
await tester.pumpWidget(
cupertino
? CupertinoApp(
builder: animations,
home: CupertinoPageScaffold(child: thread),
)
: MaterialApp(
builder: animations,
home: Scaffold(body: thread),
),
);
await tester.pumpAndSettle();
}
Future<void> _send(WidgetTester tester) async {
await tester.enterText(find.byKey(const Key('chat-composer-field')), 'Hello');
await tester.pump();
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await _pump(tester);
}
void main() {
for (final cupertino in [false, true]) {
testWidgets(
'response bubble exists before frames, through tools, then text '
'($cupertino)',
(tester) async {
final gateway = _Gateway();
addTearDown(gateway.close);
await _host(tester, gateway, cupertino: cupertino);
expect(find.byType(ChatResponsePending), findsNothing);
await _send(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
expect(
find.bySemanticsLabel(RegExp(r'Thinking.*expand for details')),
findsOneWidget,
);
expect(find.byType(ChatMessageView), findsNWidgets(2));
final opacities = tester
.widgetList<Opacity>(
find.descendant(
of: find.byType(ChatResponsePending),
matching: find.byType(Opacity),
),
)
.map((widget) => widget.opacity)
.toList();
await tester.pump(const Duration(milliseconds: 200));
expect(
tester
.widgetList<Opacity>(
find.descendant(
of: find.byType(ChatResponsePending),
matching: find.byType(Opacity),
),
)
.map((widget) => widget.opacity)
.toList(),
isNot(opacities),
);
gateway.prompt.complete({});
await _pump(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
gateway.emitEvent(chatStepFrame('running'));
gateway.emitEvent({
'session_id': 's',
'kind': 'reasoning',
'message_id': 'a',
'part': {'id': 'reasoning', 'type': 'reasoning'},
'delta': 'Thinking through this.',
});
await _pump(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
gateway.emitEvent({
'session_id': 's',
'kind': 'tool',
'message_id': 'a',
'part': {
'id': 'tool-1',
'type': 'tool',
'name': 'read',
'state': {'status': 'completed'},
},
});
await _pump(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
expect(find.textContaining('Thinking'), findsOneWidget);
expect(find.byType(ChatMessageView), findsNWidgets(2));
expect(
find.descendant(
of: find.byKey(const Key('chat-turn-activity')),
matching: find.byType(ChatResponsePending),
),
findsOneWidget,
);
gateway.emitEvent(chatTextFrame('Here is the answer.'));
await _pump(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
expect(find.text('Here is the answer.'), findsOneWidget);
expect(find.byKey(const Key('chat-turn-activity')), findsOneWidget);
gateway.emitEvent(chatStepFrame('ended'));
await tester.pumpAndSettle();
expect(find.byType(ChatResponsePending), findsNothing);
expect(find.text('Used 1 tool'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
},
);
}
for (final result in ['refused', 'queued', 'ended']) {
testWidgets(
'$result clears a response shell without leaving fake history',
(tester) async {
final gateway = _Gateway();
addTearDown(gateway.close);
await _host(tester, gateway);
await _send(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
if (result == 'refused') {
gateway.prompt.completeError(StateError('Send refused'));
} else {
gateway.prompt.complete({'chat_queued': result == 'queued'});
if (result == 'ended') gateway.emitEvent(chatStepFrame('ended'));
}
await tester.pumpAndSettle();
expect(find.byType(ChatResponsePending), findsNothing);
expect(
tester
.widgetList<ChatMessageView>(find.byType(ChatMessageView))
.where((view) => view.row['kind'] == 'assistant'),
isEmpty,
);
},
);
}
testWidgets('reduced motion keeps a static accessible ellipsis', (
tester,
) async {
await tester.pumpWidget(
const MaterialApp(
home: MediaQuery(
data: MediaQueryData(disableAnimations: true),
child: ChatResponsePending(),
),
),
);
await tester.pumpAndSettle();
// Three steady dots, all fully drawn, with no animation scheduled.
final opacities = tester
.widgetList<Opacity>(
find.descendant(
of: find.byType(ChatResponsePending),
matching: find.byType(Opacity),
),
)
.map((widget) => widget.opacity);
expect(opacities, [1, 1, 1]);
expect(find.bySemanticsLabel('Assistant is responding'), findsOneWidget);
expect(tester.binding.hasScheduledFrame, isFalse);
});
for (final cupertino in [false, true]) {
testWidgets('the pending dots sit on the activity text\'s middle '
'($cupertino)', (tester) async {
final gateway = _Gateway();
addTearDown(gateway.close);
await _host(tester, gateway, cupertino: cupertino);
await _send(tester);
final dots = tester.getRect(
find.byKey(const Key('chat-response-pending-dots')),
);
final label = tester.getRect(find.textContaining('Thinking'));
expect(dots.center.dy, closeTo(label.center.dy, 1));
expect(dots.height, lessThanOrEqualTo(label.height));
// Round dots, not baseline glyphs.
expect(
find.descendant(
of: find.byType(ChatResponsePending),
matching: find.text('.'),
),
findsNothing,
);
});
}
}