import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_controller.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'package:pipelines_app/features/chat/conversation/chat_transcript_cache.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_response_pending.dart'; import 'chat_thread_fixtures.dart'; /// A session whose turn is already running when the client attaches: the /// server answers chat_subscribe and then replays the live `step started` /// marker, the only snapshot of the turn a reload or a reopened chat gets. FakeChatGateway _busySession() { final gateway = FakeChatGateway(); gateway.onSubscribe = () => Future.microtask(() => gateway.emitEvent(chatStepFrame('started'))); gateway.history = (_) async => chatPage([ chatMessage( 'u', 'user', parts: [ {'type': 'text', 'id': 'ut', 'text': 'Do the long task'}, ], ), ]); return gateway; } Future _pump(WidgetTester tester) async { await tester.pump(); await tester.pump(const Duration(milliseconds: 100)); await tester.pump(); } void main() { testWidgets('a thread opened on a busy session shows the activity at once ' 'and hides it when the turn ends', (tester) async { final gateway = _busySession(); addTearDown(gateway.close); await tester.pumpWidget( MaterialApp( home: Scaffold( body: ChatThreadView( gateway: gateway, sessionId: 's', onBack: () {}, drafts: MemoryChatDraftStore(), ), ), ), ); await _pump(tester); // No text, reasoning or tool frame has arrived: the replayed marker alone // draws the activity row and the Stop control. expect(find.text('Do the long task'), findsOneWidget); expect(find.byType(ChatResponsePending), findsOneWidget); expect(find.byKey(const Key('chat-turn-activity')), findsOneWidget); gateway.emitEvent(chatStepFrame('ended')); await tester.pumpAndSettle(); expect(find.byType(ChatResponsePending), findsNothing); await tester.pumpWidget(const SizedBox.shrink()); }); test( 'a reopened chat with a cached transcript is running after attach', () async { final gateway = _busySession(); final controller = ChatThreadController( gateway, 's', cached: ChatTranscriptSnapshot( messages: [chatMessage('u', 'user')], cursor: '', hasMore: false, ), ); addTearDown(() { controller.dispose(); gateway.close(); }); expect(controller.running, isFalse); await controller.load(); await pumpEventQueue(); expect(controller.running, isTrue); gateway.emitEvent(chatStepFrame('ended')); await pumpEventQueue(); expect(controller.running, isFalse); }, ); test('a resync on attach keeps the turn running through the re-subscribe ' 'replay', () async { final gateway = _busySession(); final controller = ChatThreadController(gateway, 's'); addTearDown(() { controller.dispose(); gateway.close(); }); await controller.load(); await pumpEventQueue(); expect(controller.running, isTrue); // A harness that cannot replay the turn's earlier deltas asks for a // resync; the held turn is settled and the re-subscribe proves it again. gateway.emitEvent({'session_id': 's', 'kind': 'resync'}); await pumpEventQueue(); expect( gateway.calls.where((call) => call.type == 'chat_subscribe').length, 2, ); expect(controller.running, isTrue); }); }