import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'chat_composer_bar_fixtures.dart'; void main() { late ComposerBarGateway gateway; late int sequence; String nextOperation() => 'op/${++sequence}'; setUp(() { gateway = ComposerBarGateway()..page = fixtureTurn(); sequence = 0; }); tearDown(() => gateway.close()); Future pumpComposer( WidgetTester tester, { bool cupertino = false, }) async { await tester.pumpWidget( composerThread( gateway, cupertino: cupertino, newOperationId: nextOperation, ), ); await tester.pumpAndSettle(); } Finder palette() => find.byKey(const Key('chat-command-palette')); Finder row(String name) => find.byKey(ValueKey('chat-command-$name')); testWidgets('a leading slash opens the filtered recorded command palette', ( tester, ) async { await pumpComposer(tester); await tester.enterText(find.byKey(const Key('chat-composer-field')), '/'); await tester.pump(); expect(palette(), findsOneWidget); for (final name in ['new', 'close', 'rename', 'stop', 'help']) { expect(row(name), findsOneWidget); } expect(find.text('/new'), findsOneWidget); expect( find.text('Start a fresh session in this directory'), findsOneWidget, ); await tester.enterText(find.byKey(const Key('chat-composer-field')), '/cl'); await tester.pump(); expect(row('close'), findsOneWidget); expect(row('new'), findsNothing); expect(row('rename'), findsNothing); expect(row('stop'), findsNothing); expect(row('help'), findsNothing); // Recognition is case-insensitive, and an argument or plain text hides it. await tester.enterText(find.byKey(const Key('chat-composer-field')), '/CL'); await tester.pump(); expect(row('close'), findsOneWidget); await tester.enterText( find.byKey(const Key('chat-composer-field')), '/close now', ); await tester.pump(); expect(palette(), findsNothing); await tester.enterText( find.byKey(const Key('chat-composer-field')), 'plain text', ); await tester.pump(); expect(palette(), findsNothing); }); testWidgets('Escape closes the palette until the draft is edited again', ( tester, ) async { await pumpComposer(tester); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/he'); await tester.pump(); expect(palette(), findsOneWidget); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(palette(), findsNothing); expect(fieldText(tester), '/he'); expect(gateway.promptBodies, isEmpty); await tester.enterText(field, '/hel'); await tester.pump(); expect(palette(), findsOneWidget); // Editing reopens even when the operator returns to the exact word that // was dismissed: the closure follows the edit, not the text. await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); await tester.enterText(field, '/h'); await tester.pump(); expect(palette(), findsOneWidget); await tester.enterText(field, '/he'); await tester.pump(); expect(palette(), findsOneWidget); }); testWidgets('an unknown command reports locally and never prompts', ( tester, ) async { await pumpComposer(tester); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/nope'); await tester.pump(); expect(palette(), findsNothing); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(gateway.promptBodies, isEmpty); expect(fieldText(tester), ''); expect( find.text( '“/nope” is not a command, so nothing was sent to the agent. ' 'Send it as a message, or cancel.', ), findsOneWidget, ); expect(find.text('Send as message'), findsOneWidget); expect(find.text('Cancel'), findsOneWidget); // The primary Send control takes the same command path, never the prompt. await tester.enterText(field, '/also unknown'); await tester.pump(); await tester.tap(find.byKey(const Key('chat-composer-primary'))); await tester.pumpAndSettle(); expect(gateway.promptBodies, isEmpty); expect(fieldText(tester), ''); expect( find.text( '“/also” is not a command, so nothing was sent to the agent. ' 'Send it as a message, or cancel.', ), findsOneWidget, ); await tester.tap(find.byKey(const Key('chat-command-cancel'))); await tester.pump(); expect(find.byKey(const Key('chat-composer-command-notice')), findsNothing); expect(gateway.promptBodies, isEmpty); }); testWidgets('Send as message is the only route an unknown command takes', ( tester, ) async { await pumpComposer(tester); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/nope'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(gateway.promptBodies, isEmpty); await tester.tap(find.byKey(const Key('chat-command-send-as-message'))); await tester.pumpAndSettle(); expect(gateway.promptBodies.single['text'], '/nope'); expect(fieldText(tester), ''); expect(find.byKey(const Key('chat-composer-command-notice')), findsNothing); }); testWidgets('a completed command never becomes a prompt', (tester) async { await pumpComposer(tester); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/cl'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(fieldText(tester), '/close'); expect(palette(), findsNothing); expect(gateway.promptBodies, isEmpty); // A second Enter runs the command: its confirm, never a prompt. await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(gateway.promptBodies, isEmpty); expect(fieldText(tester), ''); expect(find.text('Close chat?'), findsOneWidget); await tester.tap(find.text('Cancel')); await tester.pumpAndSettle(); expect(gateway.calls.where((call) => call.type == 'chat_close'), isEmpty); // An argument is validated locally, then consumed too. await tester.enterText(field, '/close now'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(gateway.promptBodies, isEmpty); expect(fieldText(tester), ''); expect(find.text('/close does not take an argument.'), findsOneWidget); }); testWidgets('/help consumes the draft and lists commands in recorded order', ( tester, ) async { await pumpComposer(tester); await tester.enterText( find.byKey(const Key('chat-composer-field')), '/help', ); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(fieldText(tester), ''); expect(gateway.promptBodies, isEmpty); expect( find.text( '/new — Start a fresh session in this directory\n' '/close — Finish this session for every client, keeping its history\n' '/rename — Rename this session\n' '/stop — Stop the turn that is running\n' '/help — List every command', ), findsOneWidget, ); expect(find.text('Command'), findsOneWidget); await tester.tap(find.byKey(const Key('chat-command-dismiss'))); await tester.pump(); expect(find.byKey(const Key('chat-composer-command-notice')), findsNothing); }); testWidgets('/stop without a running turn and extra arguments fail locally', ( tester, ) async { await pumpComposer(tester); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/stop'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(fieldText(tester), ''); expect(gateway.interruptBodies, isEmpty); expect(find.text('Command failed'), findsOneWidget); expect(find.text('No turn is running in this chat.'), findsOneWidget); await tester.enterText(field, '/help x'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(fieldText(tester), ''); expect(find.text('/help does not take an argument.'), findsOneWidget); expect(gateway.promptBodies, isEmpty); }); testWidgets('/stop interrupts the current session through the composer', ( tester, ) async { await pumpComposer(tester); gateway.emitEvent({ 'session_id': 's', 'kind': 'step', 'step': {'status': 'started', 'turn_id': 'turn', 'intermediate': false}, }); await tester.pumpAndSettle(); await tester.enterText( find.byKey(const Key('chat-composer-field')), '/stop', ); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(gateway.interruptBodies, [ <String, dynamic>{'session_id': 's'}, ]); expect(fieldText(tester), ''); expect(gateway.promptBodies, isEmpty); expect(find.text('Stopping the running turn…'), findsOneWidget); }); testWidgets('a command picked from the palette is never sent either', ( tester, ) async { await pumpComposer(tester); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/h'); await tester.pump(); await tester.tap(row('help')); await tester.pumpAndSettle(); expect(fieldText(tester), '/help'); await tester.tap(find.byKey(const Key('chat-composer-primary'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('chat-composer-primary'))); await tester.pumpAndSettle(); expect(gateway.promptBodies, isEmpty); expect(fieldText(tester), ''); expect(find.text('Command'), findsOneWidget); expect(find.textContaining('/help — List every command'), findsOneWidget); }); testWidgets('a row completes the command and never prompts', (tester) async { await pumpComposer(tester); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/cl'); await tester.pump(); await tester.tap(row('close')); await tester.pumpAndSettle(); expect(fieldText(tester), '/close'); expect(palette(), findsNothing); expect(gateway.promptBodies, isEmpty); }); testWidgets('Up/Down move the highlight and Enter completes it', ( tester, ) async { await pumpComposer(tester); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(fieldText(tester), '/close'); expect(palette(), findsNothing); expect(gateway.promptBodies, isEmpty); }); testWidgets('ArrowUp wraps the highlight to the last command', ( tester, ) async { await pumpComposer(tester); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(fieldText(tester), '/help'); expect(palette(), findsNothing); expect(gateway.promptBodies, isEmpty); }); testWidgets('the highlight resets when the filtered list shrinks', ( tester, ) async { await pumpComposer(tester); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); await tester.pump(); // `/r` leaves one row, so the stale third index drops back to the first; // widening to `/` must not restore it. await tester.enterText(field, '/r'); await tester.pump(); await tester.enterText(field, '/'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(fieldText(tester), '/new'); expect(palette(), findsNothing); expect(gateway.promptBodies, isEmpty); }); testWidgets( 'the Cupertino host reports an unknown command with its controls', (tester) async { await pumpComposer(tester, cupertino: true); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/nope'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); expect(gateway.promptBodies, isEmpty); await tester.tap(find.byKey(const Key('chat-command-send-as-message'))); await tester.pumpAndSettle(); expect(gateway.promptBodies.single['text'], '/nope'); expect(fieldText(tester), ''); }, ); testWidgets('Escape dismisses the local command report', (tester) async { await pumpComposer(tester); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/nope'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect( find.byKey(const Key('chat-composer-command-notice')), findsOneWidget, ); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(find.byKey(const Key('chat-composer-command-notice')), findsNothing); expect(gateway.promptBodies, isEmpty); }); testWidgets('the Cupertino host shows the same palette', (tester) async { await pumpComposer(tester, cupertino: true); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, '/re'); await tester.pump(); expect(palette(), findsOneWidget); expect(row('rename'), findsOneWidget); expect(find.text('/rename'), findsOneWidget); expect(find.text('Rename this session'), findsOneWidget); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(palette(), findsNothing); }); testWidgets('the palette fits a 320px phone at 2x text', (tester) async { tester.view.physicalSize = const Size(640, 1200); tester.view.devicePixelRatio = 2; tester.platformDispatcher.textScaleFactorTestValue = 2; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); await pumpComposer(tester, cupertino: true); await tester.enterText(find.byKey(const Key('chat-composer-field')), '/'); await tester.pump(); expect(tester.takeException(), isNull); expect(palette(), findsOneWidget); expect(row('new'), findsOneWidget); expect(row('help'), findsOneWidget); }); }