import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/application/app_session.dart'; import 'package:pipelines_app/application/chat_destination.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import '../core/transport/pipelines_client_test.dart' show FakeChannel; import 'chat_registration_test.dart' show replyHistory, replyShells, replySessions, replySubscribe, requestOf, waitForRequest, webServer; Widget host(AppSession session, {String? initialSessionId}) => MaterialApp( home: Scaffold( body: ChatDestination( session: session, server: webServer(), initialSessionId: initialSessionId, ), ), ); void main() { testWidgets('nothing is requested before the shared socket is ready', ( tester, ) async { final channel = FakeChannel(); final client = PipelinesClient(channelFactory: (_) => channel); final server = webServer(); final session = AppSession(endpoint: server.uri, client: client); addTearDown(session.dispose); final starting = session.start(); await tester.pumpWidget(host(session, initialSessionId: 's1')); expect(find.text('Connecting to Pipelines…'), findsOneWidget); expect(find.byType(ChatThreadView), findsNothing); expect(channel.outgoing, isEmpty); channel.readyCompleter.complete(); await starting; }); testWidgets('a reconnect resyncs the open session without losing it', ( tester, ) async { final first = FakeChannel()..readyCompleter.complete(); final second = FakeChannel()..readyCompleter.complete(); var sockets = 0; final client = PipelinesClient( channelFactory: (_) => sockets++ == 0 ? first : second, backoffBase: const Duration(milliseconds: 10), ); final server = webServer(); final session = AppSession(endpoint: server.uri, client: client); addTearDown(session.dispose); await session.start(); await tester.pumpWidget(host(session, initialSessionId: 's1')); await tester.pump(); await replySessions(tester, first); await replySubscribe(tester, first); await replyHistory(tester, first); expect(find.text('The stored reply stays readable.'), findsOneWidget); first.incoming.addError(StateError('network drop')); await tester.pump(); expect(find.byType(ChatThreadView), findsOneWidget); expect(find.text('The stored reply stays readable.'), findsOneWidget); // The mounted session list re-reads on reconnect: a session created while // the socket was gone must not stay invisible until Refresh. Requests are // serialized, so this reply also lets the thread's resync proceed. final relisted = await waitForRequest(tester, second, 'chat_sessions'); second.incoming.add( jsonEncode({ 'id': relisted['id'], 'type': 'chat_sessions', 'chat_sessions': [ {'id': 's1', 'title': 'Wire chat'}, {'id': 's2', 'title': 'Created while offline'}, ], }), ); await tester.pump(); // The task bar re-reads its snapshot on the new socket ahead of the // thread's own resubscribe; both wait behind the serialized list reply. await replyShells(tester, second); final resubscribe = await waitForRequest(tester, second, 'chat_subscribe'); second.incoming.add( jsonEncode({'id': resubscribe['id'], 'type': 'chat_subscribe'}), ); await tester.pump(); await replyHistory( tester, second, text: 'Refreshed after reconnect.', shells: false, ); expect(find.byType(ChatThreadView), findsOneWidget); expect(find.text('Refreshed after reconnect.'), findsOneWidget); expect(find.text('The stored reply stays readable.'), findsNothing); expect(requestOf(second, 'chat_messages')!['chat_query'], { 'session_id': 's1', 'limit': 200, 'cursor': '', }); expect( find.text('Created while offline', skipOffstage: false), findsOneWidget, ); }); }