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/screen.dart'; import 'package:pipelines_app/shell/adaptive_shell.dart'; import 'package:pipelines_app/shell/destination.dart'; import 'package:pipelines_app/ui/layout/app_list_row.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'conversation/chat_thread_fixtures.dart'; /// Server recency order: the list shows Starred by name (apple, Zed), then /// Chats by directory (Recent, Second in /w; Other in /v), then Closed folded. const _rows = [ {'id': 'r', 'title': 'Recent', 'directory': '/w'}, {'id': 'd', 'title': 'Other', 'directory': '/v'}, {'id': 'c', 'title': 'Done', 'directory': '/w', 'closed_at': 1}, {'id': 's', 'title': 'Second', 'directory': '/w'}, {'id': 'z', 'title': 'Zed', 'directory': '/w', 'starred': true}, {'id': 'a', 'title': 'apple', 'directory': '/v', 'starred': true}, ]; class _Gateway extends FakeChatGateway { _Gateway(this.rows); final List rows; @override Future call(String type, [JsonMap fields = const {}]) { if (type == 'chat_sessions') { return Future.value({ 'chat_sessions': [ for (final row in rows) {...row}, ], }); } if (type == 'chat_shells') return Future.value({'chat_shells': []}); return super.call(type, fields); } } /// The app's routing: the route names the open chat; selecting one replaces /// the route, Back returns to the list. Future> _pump( WidgetTester tester, Size size, { List rows = _rows, }) async { SharedPreferences.setMockInitialValues({}); tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final gateway = _Gateway(rows)..history = (_) async => chatPage(const []); addTearDown(gateway.close); final route = ValueNotifier(null); addTearDown(route.dispose); await tester.pumpWidget( MaterialApp( home: AdaptiveShell( destination: AppDestination.chat, destinationBodies: { AppDestination.chat: (_) => ValueListenableBuilder( valueListenable: route, builder: (context, id, _) => ChatScreen( gateway: gateway, initialSessionId: id, onSessionSelected: (next) => route.value = next, onBack: () => route.value = null, ), ), }, ), ), ); await tester.pumpAndSettle(); return route; } Finder _thread(String id) => find.byKey(ValueKey('chat-thread-$id')); final _next = find.byKey(const Key('chat-next')); bool _rowSelected(WidgetTester tester, String id) => tester.widget(find.byKey(Key('chat-row-$id'))).selected; Future _pressNext(WidgetTester tester) async { await tester.tap(_next); await tester.pumpAndSettle(); } void main() { testWidgets('wide: Next chat walks the list order, wraps, syncs the row', ( tester, ) async { final route = await _pump(tester, const Size(1280, 800)); await tester.tap(find.byKey(const Key('chat-row-a'))); await tester.pumpAndSettle(); expect(_thread('a'), findsOneWidget); expect( tester .widget( find.descendant(of: _next, matching: find.byType(IconButton)), ) .tooltip, 'Next chat', ); final visited = []; for (var i = 0; i < 5; i++) { await _pressNext(tester); visited.add(route.value!); expect(_thread(route.value!), findsOneWidget); expect(_rowSelected(tester, route.value!), isTrue); } // Closed is folded, so Done is skipped; after the last row comes the // first again. expect(visited, ['z', 'r', 's', 'd', 'a']); expect(_rowSelected(tester, 'd'), isFalse); final navigator = tester.state( find.byType(Navigator).first, ); expect(navigator.canPop(), isFalse); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('phone: Next chat replaces the open page, never stacks one', ( tester, ) async { final route = await _pump(tester, const Size(390, 844)); await tester.tap(find.text('Second')); await tester.pumpAndSettle(); expect(_thread('s'), findsOneWidget); await _pressNext(tester); expect(route.value, 'd'); expect(_thread('d'), findsOneWidget); expect(_thread('s'), findsNothing); await _pressNext(tester); expect(route.value, 'a', reason: 'wraps past the last row'); // One Back leaves the chat: no earlier chat page waits beneath it. await tester.pageBack(); await tester.pumpAndSettle(); expect(route.value, isNull); expect(_thread('a'), findsNothing); final navigator = tester.state( find.byType(Navigator).first, ); expect(navigator.canPop(), isFalse); expect(_rowSelected(tester, 'a'), isFalse); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('only one chat to show hides Next chat', (tester) async { await _pump( tester, const Size(390, 844), rows: const [ {'id': 'r', 'title': 'Recent', 'directory': '/w'}, {'id': 'c', 'title': 'Done', 'directory': '/w', 'closed_at': 1}, ], ); await tester.tap(find.text('Recent')); await tester.pumpAndSettle(); expect(_thread('r'), findsOneWidget); expect(_next, findsNothing); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('unfolding Closed offers Next chat and includes its rows', ( tester, ) async { final route = await _pump( tester, const Size(1280, 800), rows: const [ {'id': 'r', 'title': 'Recent', 'directory': '/w'}, {'id': 'c', 'title': 'Done', 'directory': '/w', 'closed_at': 1}, ], ); await tester.tap(find.text('Recent')); await tester.pumpAndSettle(); expect(_next, findsNothing); await tester.tap(find.text('Closed')); await tester.pumpAndSettle(); expect(_next, findsOneWidget); await _pressNext(tester); expect(route.value, 'c'); await tester.pumpWidget(const SizedBox.shrink()); }); }