import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import '../../native_back.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/screen.dart'; import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart'; import 'package:pipelines_app/ui/adaptive/segmented_bar.dart' show segmentedMoreKey; /// A chat destination gateway: one listed session whose harness offers models. class _ScreenGateway implements ApiGateway { final calls = <({String type, JsonMap fields})>[]; List sessions = [ { 'id': 's', 'title': 'Wire chat', 'backend': 'opencode', 'model': 'model-a', 'model_provider': 'p1', }, ]; JsonMap switchReply = const { 'chat_model': {'session_id': 's', 'provider': 'p2', 'model': 'model-b'}, }; @override Stream get frames => const Stream.empty(); @override Future call(String type, [JsonMap fields = const {}]) async { calls.add((type: type, fields: fields)); switch (type) { case 'chat_sessions': return {'chat_sessions': sessions}; case 'chat_subscribe': return {}; case 'chat_messages': return { 'chat_history': { 'messages': const [], 'next_cursor': '', 'has_more': false, }, }; case 'chat_models': return const { 'chat_models': [ {'id': 'model-a', 'provider': 'p1', 'name': 'Model Alpha'}, {'id': 'model-b', 'provider': 'p2', 'name': 'Model Beta'}, ], }; case 'chat_model': return switchReply; } return {}; } int count(String type) => calls.where((call) => call.type == type).length; } Future _pumpScreen( WidgetTester tester, _ScreenGateway gateway, { String? initialSessionId, }) async { // The phone layout: an open chat is its own page with the native Back. tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); await tester.pumpWidget( MaterialApp( theme: ThemeData(platform: TargetPlatform.android), home: Scaffold( body: ChatScreen(gateway: gateway, initialSessionId: initialSessionId), ), ), ); await tester.pumpAndSettle(); } /// A phone bar keeps the model in the More menu's settings sheet: opens it /// (a wide bar shows the control already). Future _openSettings(WidgetTester tester) async { if (find.byKey(const Key('chat-model')).evaluate().isNotEmpty) return; await tester.tap(find.byKey(segmentedMoreKey).hitTestable()); await tester.pumpAndSettle(); await tester.tap(find.text('Chat settings')); await tester.pumpAndSettle(); } /// Dismisses that sheet through its barrier, above the bar. Future _closeSettings(WidgetTester tester) async { await tester.tapAt(const Offset(5, 5)); await tester.pumpAndSettle(); } void main() { testWidgets('an accepted switch survives leaving and reopening the chat', ( tester, ) async { final gateway = _ScreenGateway(); await _pumpScreen(tester, gateway); await tester.tap(find.byKey(const Key('chat-row-s'))); await tester.pumpAndSettle(); await _openSettings(tester); expect(find.byTooltip('Change model (currently model-a)'), findsOneWidget); await tester.tap(find.byKey(const Key('chat-model'))); await tester.pumpAndSettle(); await tester.tap(find.text('Model Beta')); await tester.pumpAndSettle(); expect(find.byTooltip('Change model (currently model-b)'), findsOneWidget); await _closeSettings(tester); await tester.tap(findNativeBack()); await tester.pumpAndSettle(); expect(find.byTooltip('Change model (currently model-b)'), findsNothing); // The list still carries the row it read before the switch; reopening must // still show the model the server accepted. await tester.tap(find.byKey(const Key('chat-row-s'))); await tester.pumpAndSettle(); await _openSettings(tester); expect(find.byTooltip('Change model (currently model-b)'), findsOneWidget); expect(find.byTooltip('Change model (currently model-a)'), findsNothing); await tester.tap(find.byKey(const Key('chat-model'))); await tester.pumpAndSettle(); expect( find.descendant( of: find.byKey(const Key('chat-model-option-p2/model-b')), matching: find.byIcon(Icons.check), ), findsOneWidget, ); expect( find.descendant( of: find.byKey(const Key('chat-model-option-p1/model-a')), matching: find.byIcon(Icons.check), ), findsNothing, ); }); testWidgets('a routed chat resolves its row from the loaded list', ( tester, ) async { final gateway = _ScreenGateway(); await _pumpScreen(tester, gateway, initialSessionId: 's'); // The deep link opened before any row was tapped: the list's own read // supplies the title and the recorded model, with no second list request. expect(find.text('Wire chat'), findsOneWidget); await _openSettings(tester); expect(find.byTooltip('Change model (currently model-a)'), findsOneWidget); expect(find.byKey(const Key('chat-model')), findsOneWidget); expect(gateway.count('chat_sessions'), 1); await tester.tap(find.byKey(const Key('chat-model'))); await tester.pumpAndSettle(); expect(gateway.calls.last.type, 'chat_models'); expect(gateway.calls.last.fields, { 'chat_query': {'session_id': 's'}, }); await tester.tap(find.text('Model Beta')); await tester.pumpAndSettle(); expect(gateway.calls.last.fields, { 'chat_model': {'session_id': 's', 'provider': 'p2', 'model': 'model-b'}, }); expect(find.byTooltip('Change model (currently model-b)'), findsOneWidget); }); testWidgets( 'a routed chat on a harness without model switching stays plain', (tester) async { final gateway = _ScreenGateway() ..sessions = [ {'id': 's', 'title': 'Wire chat', 'backend': 'other'}, ]; await _pumpScreen(tester, gateway, initialSessionId: 's'); expect(find.text('Wire chat'), findsOneWidget); expect(find.byKey(const Key('chat-model')), findsNothing); expect(gateway.count('chat_models'), 0); }, ); for (final width in [390.0, 1200.0]) { testWidgets( 'a new route opens the target after Chat is already mounted on $width px', (tester) async { final gateway = _ScreenGateway() ..sessions = [ {'id': 'old', 'title': 'Old chat'}, {'id': 'target', 'title': 'Target chat'}, ]; tester.view.physicalSize = Size(width, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); await tester.pumpWidget( MaterialApp( home: Scaffold( body: ChatScreen(gateway: gateway, initialSessionId: 'old'), ), ), ); await tester.pumpAndSettle(); await tester.pumpWidget( MaterialApp( home: Scaffold( body: ChatScreen(gateway: gateway, initialSessionId: 'target'), ), ), ); await tester.pumpAndSettle(); final workspace = tester.widget( find.byType(ChatWorkspace), ); expect(workspace.controller.activePane?.sessionId, 'target'); expect( find.byKey(const ValueKey('chat-session-target')), findsOneWidget, ); }, ); } testWidgets( 'a repeated route reactivates its chat after another pane became active', (tester) async { final gateway = _ScreenGateway() ..sessions = [ {'id': 'a', 'title': 'Chat a'}, {'id': 'b', 'title': 'Chat b'}, ]; await _pumpScreen(tester, gateway, initialSessionId: 'a'); final workspace = tester.widget( find.byType(ChatWorkspace), ); workspace.controller.splitActive('b', ChatSplitAxis.horizontal); final b = workspace.controller.panes.firstWhere( (pane) => pane.sessionId == 'b', ); workspace.controller.activate(b.id); await tester.pump(); expect(workspace.controller.activePane?.sessionId, 'b'); await tester.pumpWidget( MaterialApp( home: Scaffold( body: ChatScreen(gateway: gateway, initialSessionId: 'a'), ), ), ); await tester.pumpAndSettle(); expect(workspace.controller.activePane?.sessionId, 'a'); }, ); }