import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/conversation/chat_mode_control.dart'; import 'package:pipelines_app/features/chat/screen.dart'; /// A chat destination gateway whose listing can change between reads, as it /// does when the server learns a row's mode after the row was first opened. class _ModeScreenGateway implements ApiGateway { final calls = <({String type, JsonMap fields})>[]; List sessions = [ _claude('s', 'Wire chat', mode: 'default', name: 'Ask for permission'), ]; /// The `chat_session` a `/new` replies with, for the chat `s`. JsonMap created = _claude('n1', 'New chat'); /// Holds list reads while set, so a test can look before the re-read lands. Completer? listGate; @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': final rows = [...sessions]; await listGate?.future; return {'chat_sessions': rows}; case 'chat_new_session': return { 'chat_session': {...created, 'previous_id': 's'}, }; case 'chat_messages': return { 'chat_history': { 'messages': const [], 'next_cursor': '', 'has_more': false, }, }; } return {}; } } JsonMap _claude( String id, String title, { String mode = '', String name = '', }) => { 'id': id, 'title': title, 'backend': 'claude', 'directory': '/w', if (mode.isNotEmpty) 'mode': mode, if (name.isNotEmpty) 'mode_name': name, }; /// `/new` is typed on a desktop keyboard, where Enter sends. final _desktop = TargetPlatformVariant.only(TargetPlatform.linux); final _modeControl = find.byKey(const Key('chat-mode')); Widget _app( _ModeScreenGateway gateway, { required TargetPlatform platform, String? initialSessionId, int syncToken = 0, }) => MaterialApp( theme: ThemeData(platform: platform), home: Scaffold( body: ChatScreen( gateway: gateway, initialSessionId: initialSessionId, syncToken: syncToken, ), ), ); Future _pump( WidgetTester tester, _ModeScreenGateway gateway, { required TargetPlatform platform, Size size = const Size(390, 844), String? initialSessionId, }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); await tester.pumpWidget( _app(gateway, platform: platform, initialSessionId: initialSessionId), ); await tester.pumpAndSettle(); } /// The mode the drawn control names, or null when the bar has none. A /// Cupertino phone bar draws only the shield, so read the control itself. String? _shown(WidgetTester tester) { if (_modeControl.evaluate().isEmpty) return null; final control = find.byType(ChatModeControl); expect(control, findsOneWidget); return tester.widget(control).modeName; } void main() { for (final platform in [TargetPlatform.android, TargetPlatform.iOS]) { final style = platform == TargetPlatform.iOS ? 'Cupertino' : 'Material'; testWidgets( '$style: an existing chat opened from the list shows its mode', (tester) async { final gateway = _ModeScreenGateway(); await _pump(tester, gateway, platform: platform); await tester.tap(find.byKey(const Key('chat-row-s'))); await tester.pumpAndSettle(); expect(_shown(tester), 'Ask for permission'); }, ); testWidgets( '$style: a mode the list learns after the chat opened appears in place', (tester) async { final gateway = _ModeScreenGateway() ..sessions = [_claude('s', 'Wire chat')]; await _pump(tester, gateway, platform: platform); await tester.tap(find.byKey(const Key('chat-row-s'))); await tester.pumpAndSettle(); expect(_shown(tester), isNull); // A reconnect re-reads the list; the open chat follows that row // without being closed and opened again. gateway.sessions = [ _claude( 's', 'Wire chat', mode: 'default', name: 'Ask for permission', ), ]; await tester.pumpWidget( _app(gateway, platform: platform, syncToken: 1), ); await tester.pumpAndSettle(); expect(_shown(tester), 'Ask for permission'); // Another device chose Auto; the next re-read keeps the bar in sync. gateway.sessions = [ _claude('s', 'Wire chat', mode: 'auto', name: 'Auto'), ]; await tester.pumpWidget( _app(gateway, platform: platform, syncToken: 2), ); await tester.pumpAndSettle(); expect(_shown(tester), 'Auto'); }, ); testWidgets('$style: a routed chat shows its mode once the list loads', ( tester, ) async { final gateway = _ModeScreenGateway(); await _pump(tester, gateway, platform: platform, initialSessionId: 's'); expect(_shown(tester), 'Ask for permission'); }); testWidgets('$style: switching chats shows each chat its own mode', ( tester, ) async { final gateway = _ModeScreenGateway() ..sessions = [ _claude( 's', 'Wire chat', mode: 'default', name: 'Ask for permission', ), _claude('t', 'Other chat', mode: 'auto', name: 'Auto'), {'id': 'u', 'title': 'Plain chat', 'backend': 'opencode'}, ]; // Wide: the list stays beside the open chat, so a row swaps it. await _pump( tester, gateway, platform: platform, size: const Size(1400, 900), ); await tester.tap(find.byKey(const Key('chat-row-s'))); await tester.pumpAndSettle(); expect(_shown(tester), 'Ask for permission'); await tester.tap(find.byKey(const Key('chat-row-t'))); await tester.pumpAndSettle(); expect(_shown(tester), 'Auto'); await tester.tap(find.byKey(const Key('chat-row-u'))); await tester.pumpAndSettle(); expect(_shown(tester), isNull); await tester.tap(find.byKey(const Key('chat-row-s'))); await tester.pumpAndSettle(); expect(_shown(tester), 'Ask for permission'); }); } Future runNew(WidgetTester tester) async { await tester.enterText( find.byKey(const Key('chat-composer-field')), '/new', ); await tester.pump(); // The first Enter may complete the palette row; the second runs it. await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pump(); final field = tester.widget( find.descendant( of: find.byKey(const Key('chat-composer-field')), matching: find.byType(EditableText), ), ); if (field.controller.text.isNotEmpty) { await tester.sendKeyEvent(LogicalKeyboardKey.enter); } await tester.pumpAndSettle(); } testWidgets( 'a new session whose reply names its mode shows it before the list re-reads', (tester) async { final gateway = _ModeScreenGateway() ..created = _claude('n1', 'New chat', mode: 'plan', name: 'Plan'); await _pump( tester, gateway, platform: TargetPlatform.linux, size: const Size(1400, 900), ); await tester.tap(find.byKey(const Key('chat-row-s'))); await tester.pumpAndSettle(); gateway.listGate = Completer(); await runNew(tester); expect(find.text('New chat'), findsWidgets); expect(_shown(tester), 'Plan'); gateway.listGate!.complete(); await tester.pumpAndSettle(); }, variant: _desktop, ); testWidgets( 'a new session whose reply lacks its mode shows it when the list re-reads', (tester) async { final gateway = _ModeScreenGateway(); await _pump( tester, gateway, platform: TargetPlatform.linux, size: const Size(1400, 900), ); await tester.tap(find.byKey(const Key('chat-row-s'))); await tester.pumpAndSettle(); gateway ..listGate = Completer() ..sessions = [_claude('n1', 'New chat', mode: 'plan', name: 'Plan')]; await runNew(tester); // The row is open on the /new reply, which names no mode. expect(find.text('New chat'), findsWidgets); expect(_shown(tester), isNull); gateway.listGate!.complete(); await tester.pumpAndSettle(); expect(_shown(tester), 'Plan'); }, variant: _desktop, ); }