import 'dart:async'; 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/features/chat/stack/chat_stack_entry.dart'; import 'package:pipelines_app/features/chat/stack/mobile_chat_navigation.dart'; import 'package:pipelines_app/features/chat/workspace/chat_peek_header.dart'; import 'package:pipelines_app/shell/adaptive_shell.dart'; import 'package:pipelines_app/shell/destination.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../conversation/chat_thread_fixtures.dart'; const _rows = [ {'id': 'a', 'title': 'Alpha', 'directory': '/w', 'color': 'teal'}, {'id': 'b', 'title': 'Beta', 'directory': '/w'}, {'id': 'c', 'title': 'Gamma', 'directory': '/v'}, ]; class _Gateway extends FakeChatGateway { @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); } } Future _pump(WidgetTester tester, Size size) async { SharedPreferences.setMockInitialValues({}); unawaited( MobileChatNavigationController.instance.setValue( MobileChatNavigation.chatStack, ), ); tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final gateway = _Gateway()..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(); } Finder _thread(String id) => find.byKey(ValueKey('chat-thread-$id')); final _stack = find.byKey(const Key('chat-stack')); void main() { final navigation = MobileChatNavigationController.instance; tearDown(() { unawaited(navigation.setValue(MobileChatNavigation.quickSwitch)); }); testWidgets('the setting swaps the peek for the stack, live', (tester) async { await _pump(tester, const Size(390, 844)); await tester.tap(find.byKey(const Key('chat-row-a'))); await tester.pumpAndSettle(); expect(_thread('a'), findsOneWidget); expect(_stack.hitTestable(), findsOneWidget); expect(find.bySemanticsLabel(chatStackLabel(1)), findsOneWidget); // Add chat picks a chat that is not open and opens it. await tester.tap(_stack); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('chat-stack-button-add'))); await tester.pumpAndSettle(); await tester.tap(find.text('Beta').last); await tester.pumpAndSettle(); expect(_thread('b').hitTestable(), findsOneWidget); expect(find.bySemanticsLabel(chatStackLabel(2)), findsOneWidget); // The stack replaces the peek. expect(find.byType(ChatPeekHeader), findsNothing); // A chat in the column opens as its list row would. await tester.tap(_stack); await tester.pumpAndSettle(); await tester.tap(find.text('Alpha').last); await tester.pumpAndSettle(); expect(_thread('a').hitTestable(), findsOneWidget); expect(_stack.hitTestable(), findsOneWidget); // Back to Quick switch: the peek returns, the stack leaves. unawaited(navigation.setValue(MobileChatNavigation.quickSwitch)); await tester.pumpAndSettle(); expect(_stack, findsNothing); expect(find.byType(ChatPeekHeader), findsOneWidget); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('long press removes a chat, Undo restores it', (tester) async { await _pump(tester, const Size(390, 844)); await tester.tap(find.byKey(const Key('chat-row-a'))); await tester.pumpAndSettle(); await tester.tap(_stack); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('chat-stack-button-add'))); await tester.pumpAndSettle(); await tester.tap(find.text('Gamma').last); await tester.pumpAndSettle(); expect(find.bySemanticsLabel(chatStackLabel(2)), findsOneWidget); await tester.tap(_stack); await tester.pumpAndSettle(); await tester.longPress(find.text('Alpha').last); await tester.pumpAndSettle(); expect(find.text('Removed Alpha from open chats'), findsOneWidget); expect(find.bySemanticsLabel(chatStackLabel(1)), findsOneWidget); await tester.tap(find.text('Undo')); await tester.pumpAndSettle(); // Gamma stays on screen; Alpha is back in the stack. expect(_thread('c').hitTestable(), findsOneWidget); expect(find.bySemanticsLabel(chatStackLabel(2)), findsOneWidget); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('a wide window keeps its panes and shows no stack', ( tester, ) async { 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(_stack, findsNothing); await tester.pumpWidget(const SizedBox.shrink()); }); }