Files
pipelines/app/test/application/chat_scroll_back_test.dart
T

159 lines
5.8 KiB
Dart

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/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<JsonMap> 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() {
for (final width in [390.0, 840.0]) {
testWidgets(
'list → chat → top → bottom → one 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 = <String>[];
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,
cupertino: false,
onPopToChat: () => router.goTo(AppRoute.chat),
onPopToRuns: () {},
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<NavigatorState>(find.byType(Navigator));
expect(
navigator.canPop(),
isTrue,
reason: 'Chat must be pushed, not swapped into the main page',
);
expect(
tester.widget<BackButton>(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<ListView>(transcript).controller!;
await tester.drag(
transcript,
Offset(0, scroll.position.maxScrollExtent + 500),
);
await tester.pumpAndSettle();
expect(scroll.offset, closeTo(scroll.position.minScrollExtent, 1));
await tester.drag(
transcript,
Offset(0, -scroll.position.maxScrollExtent - 500),
);
await tester.pumpAndSettle();
expect(scroll.offset, closeTo(scroll.position.maxScrollExtent, 1));
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(find.text('Press back again to close'), findsNothing);
expect(exits, isEmpty);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.text('Press back again to close'), findsOneWidget);
expect(exits, isEmpty);
await tester.binding.handlePopRoute();
await tester.pump();
expect(exits, ['SystemNavigator.pop']);
await tester.pumpWidget(const SizedBox.shrink());
disposeRouter();
},
);
}
}