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'; /// 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 { await tester.pumpWidget( MaterialApp( theme: ThemeData(platform: TargetPlatform.android), home: Scaffold( body: ChatScreen(gateway: gateway, initialSessionId: initialSessionId), ), ), ); 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(); expect(find.text('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.text('model-b'), findsOneWidget); await tester.tap(find.byKey(const Key('chat-back'))); await tester.pumpAndSettle(); expect(find.text('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(); expect(find.text('model-b'), findsOneWidget); expect(find.text('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); expect(find.text('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.text('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); }, ); }