import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/screen.dart'; import 'package:pipelines_app/shell/adaptive_shell.dart'; import 'package:pipelines_app/shell/destination.dart'; import '../../native_back.dart'; import 'conversation/chat_thread_fixtures.dart'; class _Gateway extends FakeChatGateway { @override Future call(String type, [JsonMap fields = const {}]) { if (type == 'chat_sessions') { return Future.value({ 'chat_sessions': [ {'id': 'a', 'title': 'First chat'}, {'id': 'b', 'title': 'Second chat'}, ], }); } if (type == 'chat_shells') return Future.value({'chat_shells': []}); return super.call(type, fields); } int get historyReads => calls.where((call) => call.type == 'chat_messages').length; } JsonMap _user(String id, String text) => chatMessage( id, 'user', parts: [ {'type': 'text', 'text': text}, ], ); /// A few frames, without settling: the phone pushes its page after a frame, /// and a pending read's indicator would never settle. Future _frames(WidgetTester tester) async { await tester.pump(); await tester.pump(); await tester.pump(const Duration(milliseconds: 100)); } Finder _shows(String text) => find.textContaining(text, findRichText: true); Future<_Gateway> _pumpShell(WidgetTester tester, Size size) async { SharedPreferences.setMockInitialValues({}); tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final gateway = _Gateway(); addTearDown(gateway.close); await tester.pumpWidget( MaterialApp( home: AdaptiveShell( destination: AppDestination.chat, destinationBodies: { AppDestination.chat: (_) => ChatScreen(gateway: gateway), }, ), ), ); await tester.pumpAndSettle(); return gateway; } void main() { for (final (label, size) in [ ('phone page', const Size(390, 844)), ('wide pane', const Size(1280, 800)), ]) { testWidgets('a reopened chat shows its last transcript at once and ' 'refreshes it from the server ($label)', (tester) async { final gateway = await _pumpShell(tester, size); final phone = size.width < 600; Future leave() async { if (phone) { await tester.tap(findNativeBack()); } else { gateway.history = (_) async => chatPage(const []); await tester.tap(_shows('Second chat').first); } await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('chat-thread-a')), findsNothing); gateway.history = null; } // First visit: nothing is cached, so the read is awaited. await tester.tap(_shows('First chat').first); await _frames(tester); expect(find.byKey(const Key('chat-thread-loading')), findsOneWidget); gateway.historyRequests.last.complete( chatPage([_user('u1', 'Earlier question'), _user('gone', 'Removed')]), ); await tester.pumpAndSettle(); expect(_shows('Earlier question'), findsOneWidget); await leave(); // Reopen: the cached transcript is drawn before the read replies, and // the conversation still re-subscribes and re-reads. final reads = gateway.historyReads; final subscribes = gateway.calls .where((call) => call.type == 'chat_subscribe') .length; await tester.tap(_shows('First chat').first); await _frames(tester); expect(find.byKey(const ValueKey('chat-thread-a')), findsOneWidget); expect(find.byKey(const Key('chat-thread-loading')), findsNothing); expect(_shows('Earlier question'), findsOneWidget); expect(_shows('Removed'), findsOneWidget); await tester.pumpAndSettle(); expect(gateway.historyReads, reads + 1); expect( gateway.calls.where((call) => call.type == 'chat_subscribe').length, subscribes + 1, ); // A live frame during the read is kept through the reply. gateway.emitEvent( chatTextFrame('Live reply while reopening', session: 'a'), ); await tester.pumpAndSettle(); expect(_shows('Live reply while reopening'), findsOneWidget); // The server's reply replaces the cached copy: a message it no longer // holds leaves, a new one arrives, the live frame stays. gateway.historyRequests.last.complete( chatPage([ _user('u1', 'Earlier question'), _user('u2', 'Sent from another client'), ]), ); await tester.pumpAndSettle(); expect(_shows('Removed'), findsNothing); expect(_shows('Sent from another client'), findsOneWidget); expect(_shows('Live reply while reopening'), findsOneWidget); // The refreshed transcript is what the next visit opens on. await leave(); await tester.tap(_shows('First chat').first); await _frames(tester); expect(find.byKey(const Key('chat-thread-loading')), findsNothing); expect(_shows('Sent from another client'), findsOneWidget); expect(_shows('Removed'), findsNothing); await tester.pumpWidget(const SizedBox.shrink()); }); } testWidgets('a chat whose read failed is not cached for the next visit', ( tester, ) async { final gateway = await _pumpShell(tester, const Size(390, 844)); gateway.history = (_) async => throw StateError('refused'); await tester.tap(_shows('First chat').first); await tester.pumpAndSettle(); await tester.tap(findNativeBack()); await tester.pumpAndSettle(); gateway.history = null; await tester.tap(_shows('First chat').first); await _frames(tester); expect(find.byKey(const Key('chat-thread-loading')), findsOneWidget); await tester.pumpWidget(const SizedBox.shrink()); }); }