fix(chat): use stock Navigator Back at every screen width

This commit is contained in:
2026-10-24 12:36:17 +01:00
parent 0ecd4a8487
commit f22afabd94
16 changed files with 262 additions and 53 deletions
@@ -10,6 +10,7 @@ import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_screen.dart';
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
import '../native_back.dart';
ServerEndpoint webServer() => ServerEndpoint.resolve(
base: Uri.parse('http://127.0.0.1:8096/'),
@@ -188,6 +189,7 @@ void main() {
await replySessions(tester, channel);
await replySubscribe(tester, channel);
await replyHistory(tester, channel);
await tester.pumpAndSettle();
expect(provider.value.uri.path, '/chat/s1');
expect(find.byType(ChatThreadView), findsOneWidget);
@@ -234,10 +236,9 @@ void main() {
// The open chat takes over the app bar: one Back there, and no
// second header row under it.
expect(find.byKey(const Key('chat-thread-header')), findsNothing);
await tester.tap(find.byKey(const ValueKey('shell.back')));
await tester.pump();
// The bar hands back to the list on the next frame.
await tester.pump();
await tester.tap(findNativeBack());
// A real Navigator pop finishes its native reverse transition.
await tester.pumpAndSettle();
expect(provider.value.uri.path, '/chat');
expect(find.byType(ChatThreadView), findsNothing);
expect(find.byType(ChatSessionsScreen), findsOneWidget);
@@ -0,0 +1,158 @@
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();
},
);
}
}
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import '../native_back.dart';
import 'package:pipelines_app/application/app_router.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
@@ -122,7 +123,7 @@ void main() {
await tester.pumpAndSettle();
expect(find.text('Navigation dialog'), findsNothing);
expect(provider.value.uri.path, detailPath);
expect(find.byKey(const ValueKey('shell.back')), findsOneWidget);
expect(findNativeBack(), findsOneWidget);
if (predictive) {
await backGesture(tester, 'startBackGesture');
+6 -5
View File
@@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import '../native_back.dart';
import 'package:pipelines_app/application/app_route.dart';
import 'package:pipelines_app/application/app_router.dart';
import 'package:pipelines_app/application/pipelines_app.dart';
@@ -160,11 +161,11 @@ void main() {
await replyDetail(tester, channel, 1);
expect(find.text('Run 1'), findsWidgets);
expect(find.byKey(const ValueKey('shell.back')), findsOneWidget);
expect(findNativeBack(), findsOneWidget);
expect(provider.value.uri.path, '/runs/1');
expect(find.text('Pipelines — Not implemented'), findsNothing);
await tester.tap(find.byKey(const ValueKey('shell.back')));
await tester.tap(findNativeBack());
await tester.pump();
expect(provider.value.uri.path, '/runs');
expect(find.text('Deep-linked run'), findsWidgets);
@@ -267,7 +268,7 @@ void main() {
await tester.pump();
expect(provider.value.uri.path, '/runs/2');
await replyDetail(tester, channel, 2);
expect(find.byKey(const ValueKey('shell.back')), findsOneWidget);
expect(findNativeBack(), findsOneWidget);
// The engine's browser Back arrives as new route information.
await provider.didPushRouteInformation(
@@ -277,7 +278,7 @@ void main() {
// The bar returns to the tab's own after the closed run hands it back.
await tester.pump();
expect(find.text('Beta run'), findsWidgets);
expect(find.byKey(const ValueKey('shell.back')), findsNothing);
expect(findNativeBack(), findsNothing);
// Forward re-opens the same run.
await provider.didPushRouteInformation(
@@ -286,7 +287,7 @@ void main() {
await tester.pump();
await replyDetail(tester, channel, 2);
await tester.pump();
expect(find.byKey(const ValueKey('shell.back')), findsOneWidget);
expect(findNativeBack(), findsOneWidget);
});
testWidgets('a malformed boot path is corrected to /pipelines', (
+6 -5
View File
@@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_test/flutter_test.dart';
import '../native_back.dart';
import 'package:pipelines_app/application/app_route.dart';
import 'package:pipelines_app/application/app_router.dart';
import 'package:pipelines_app/application/pipelines_app.dart';
@@ -323,7 +324,7 @@ void main() {
expect(provider.value.uri.path, '/runs/2');
expect(find.byType(SearchScreen), findsNothing);
expect(find.byKey(const ValueKey('shell.back')), findsOneWidget);
expect(findNativeBack(), findsOneWidget);
expect(find.text('Run 2'), findsWidgets);
// The shell was never remounted, so the open run was not re-read.
expect(requestsOf(channel, 'get_run'), hasLength(1));
@@ -408,7 +409,7 @@ void main() {
expect(provider.value.uri.path, '/runs/7');
await replyRunDetail(tester, channel, 7, index: 1);
expect(find.byType(SearchScreen), findsNothing);
expect(find.byKey(const ValueKey('shell.back')), findsOneWidget);
expect(findNativeBack(), findsOneWidget);
expect(find.text('Run 7'), findsWidgets);
// One app socket carried the search and the run reads.
expect(sockets, 1);
@@ -650,8 +651,8 @@ void main() {
// leaves from the loading placeholder instead.
await openArtifactHit(tester, channel);
expect(provider.value.uri.path, '/runs/7');
expect(find.byKey(const ValueKey('shell.back')), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('shell.back')));
expect(findNativeBack(), findsOneWidget);
await tester.tap(findNativeBack());
await tester.pump();
await tester.pump();
expect(provider.value.uri.path, '/runs');
@@ -710,7 +711,7 @@ void main() {
await tester.tap(find.byKey(const Key('run-visit-back')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const ValueKey('shell.back')));
await tester.tap(findNativeBack());
await tester.pump();
await tester.pump();
expect(provider.value.uri.path, '/runs');