import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:pipelines_app/application/app_route.dart'; import 'package:pipelines_app/application/app_router.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/conversation/chat_transcript_scroll_view.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/theme/app_theme.dart'; import '../features/chat/conversation/chat_thread_fixtures.dart'; import '../native_back.dart'; class _Gateway extends FakeChatGateway { @override Future call(String type, [JsonMap fields = const {}]) { if (type == 'chat_sessions') { return Future.value({ 'chat_sessions': [ {'id': 's', 'title': 'Scroll and return'}, ], }); } if (type == 'chat_shells') return Future.value({'chat_shells': []}); return super.call(type, fields); } } void main() { // Wide windows keep the chat beside its list instead of pushing a page; that // layout is covered by test/features/chat/chat_list_detail_test.dart. for (final predictive in [false, true]) { const width = 390.0; testWidgets( 'list → chat → top → bottom → one ${predictive ? 'predictive gesture' : 'system Back'} pops a real route at $width', (tester) async { SharedPreferences.setMockInitialValues({}); tester.view.physicalSize = Size(width, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final exits = []; tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( SystemChannels.platform, (call) async { if (call.method == 'SystemNavigator.pop') exits.add(call.method); return null; }, ); addTearDown( () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( SystemChannels.platform, null, ), ); final gateway = _Gateway() ..history = (_) async => chatPage([ for (var i = 0; i < 30; i++) chatMessage( 'm$i', i.isEven ? 'user' : 'assistant', parts: [ { 'type': 'text', 'text': 'Message $i. ${List.filled(12 + i % 5, 'Recorded text that wraps across the screen.').join(' ')}', }, ], ), ]); addTearDown(gateway.close); late final AppRouterDelegate router; router = AppRouterDelegate( initial: AppRoute.chat, builder: (_, route) => AdaptiveShell( destination: AppDestination.chat, destinationBodies: { AppDestination.chat: (_) => ChatScreen( gateway: gateway, initialSessionId: route.chatSessionId, onSessionSelected: (id) => router.goTo(AppRoute.chatSession(id)), onBack: () => router.goTo(AppRoute.chat), ), }, ), ); var routerDisposed = false; void disposeRouter() { if (routerDisposed) return; routerDisposed = true; router.dispose(); } addTearDown(disposeRouter); final provider = PlatformRouteInformationProvider( initialRouteInformation: RouteInformation(uri: Uri.parse('/chat')), ); addTearDown(provider.dispose); await tester.pumpWidget( MaterialApp.router( theme: appLightTheme(), routerDelegate: router, routeInformationParser: const AppRouteParser(), routeInformationProvider: provider, ), ); await tester.pumpAndSettle(); await tester.tap(find.text('Scroll and return')); await tester.pumpAndSettle(); final navigator = tester.state(find.byType(Navigator)); expect( navigator.canPop(), isTrue, reason: 'Chat must be pushed, not swapped into the main page', ); expect( find.byWidgetPredicate( (widget) => widget is PopScope && !widget.canPop, ), findsNothing, reason: 'The main-screen Back interceptor must not block an open chat', ); expect( tester.widget(findNativeBack()).onPressed, isNull, reason: 'Use the stock Navigator Back action without a chat override', ); final transcript = find.byKey(const ValueKey('chat-thread-s')); final scroll = tester .widget(transcript) .controller; // A long transcript is bottom-anchored: older rows sit above offset 0. final span = scroll.position.maxScrollExtent - scroll.position.minScrollExtent; await tester.drag(transcript, Offset(0, span + 500)); await tester.pumpAndSettle(); expect(scroll.offset, closeTo(scroll.position.minScrollExtent, 1)); await tester.drag(transcript, Offset(0, -span - 500)); await tester.pumpAndSettle(); expect(scroll.offset, closeTo(scroll.position.maxScrollExtent, 1)); // A continuously animated processing control must not keep Back open. gateway.emitEvent(chatStepFrame('running')); await tester.pump(); await tester.pump(); if (predictive) { final route = ModalRoute.of(tester.element(transcript))!; await _backGesture(tester, 'startBackGesture', progress: 0); await tester.pump(); expect(route.navigator!.userGestureInProgress, isTrue); await _backGesture( tester, 'updateBackGestureProgress', progress: 0.6, ); await tester.pump(const Duration(milliseconds: 16)); await _backGesture(tester, 'commitBackGesture'); } else { await tester.binding.handlePopRoute(); } await tester.pump(); await tester.pump(const Duration(milliseconds: 500)); await tester.pump(); expect(router.route, AppRoute.chat); expect(find.text('Scroll and return'), findsOneWidget); expect(find.byKey(const ValueKey('chat-thread-s')), findsNothing); expect(exits, isEmpty); // At the main screen Back is the platform's: no exit prompt. await tester.binding.handlePopRoute(); await tester.pump(); expect(find.text('Press back again to close'), findsNothing); expect(exits, ['SystemNavigator.pop']); await tester.pumpWidget(const SizedBox.shrink()); disposeRouter(); }, ); } } Future _backGesture( WidgetTester tester, String method, { double? progress, }) => tester.binding.defaultBinaryMessenger.handlePlatformMessage( 'flutter/backgesture', const StandardMethodCodec().encodeMethodCall( MethodCall( method, progress == null ? null : { 'touchOffset': [5 + 300 * progress, 400], 'progress': progress, 'swipeEdge': 0, }, ), ), (_) {}, );