import 'dart:async'; 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 prompt = Completer(); @override Future call(String type, [JsonMap fields = const {}]) { if (type == 'chat_prompt') return prompt.future; return super.call(type, fields); } } Future _pump(WidgetTester tester) async { await tester.pump(); await tester.pump(const Duration(milliseconds: 100)); await tester.pump(); } Future _host(WidgetTester tester, _Gateway gateway) 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( MaterialApp( builder: animations, home: Scaffold(body: thread), ), ); await tester.pumpAndSettle(); } Future _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() { testWidgets( 'response bubble exists before frames, through tools, then text', (tester) async { final gateway = _Gateway(); addTearDown(gateway.close); await _host(tester, gateway); 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( 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( 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(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( 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); }); testWidgets('the pending dots sit on the activity text\'s middle ', ( tester, ) async { final gateway = _Gateway(); addTearDown(gateway.close); await _host(tester, gateway); 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, ); }); }