import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/conversation/chat_command_palette.dart'; import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart'; import 'package:pipelines_app/features/chat/conversation/chat_escape_stop.dart'; import 'package:pipelines_app/features/chat/conversation/composer_focus_ring.dart'; import 'package:pipelines_app/features/chat/conversation/composer_running_border.dart'; import 'chat_composer_bar_fixtures.dart'; import 'chat_thread_fixtures.dart'; Finder get _border => find.byKey(const Key('chat-composer-running-border')); Future _pumpThread(WidgetTester tester) async { final gateway = ComposerBarGateway()..page = fixtureTurn(); addTearDown(gateway.close); await tester.pumpWidget( composerThread(gateway, newOperationId: () => 'op/1'), ); await tester.pumpAndSettle(); return gateway; } /// Starts a turn. The border repeats for as long as it runs, so the test /// settles by pumping a frame, never by waiting for the tree to go quiet. Future _startTurn(WidgetTester tester, ComposerBarGateway gateway) async { gateway.emitEvent(chatStepFrame('running')); await tester.pump(); await tester.pump(); } Future _endTurn(WidgetTester tester, ComposerBarGateway gateway) async { gateway.emitEvent(chatStepFrame('ended')); await tester.pump(); await tester.pump(); } void _reducedMotion(bool reduced) => TestWidgetsFlutterBinding.ensureInitialized() .platformDispatcher .accessibilityFeaturesTestValue = FakeAccessibilityFeatures( disableAnimations: reduced, ); void main() { tearDown(() => _reducedMotion(false)); testWidgets('the border travels while a turn runs and is gone after it', ( tester, ) async { _reducedMotion(false); final gateway = await _pumpThread(tester); expect(_border, findsNothing); await _startTurn(tester, gateway); expect(_border, findsOneWidget); expect(tester.binding.transientCallbackCount, greaterThan(0)); await _endTurn(tester, gateway); expect(_border, findsNothing); expect(tester.binding.transientCallbackCount, 0); }); testWidgets('a focused field draws the highlight thicker than its ring', ( tester, ) async { _reducedMotion(false); final gateway = await _pumpThread(tester); await _startTurn(tester, gateway); ComposerRunningBorder border() => tester.widget( find.byType(ComposerRunningBorder), ); tester .widgetList(find.byType(EditableText)) .firstWhere((field) => field.focusNode.debugLabel == 'chat-composer') .focusNode .requestFocus(); await tester.pump(); await tester.pump(); expect(border().focused, isTrue); expect( ComposerRunningBorder.focusedStrokeWidth, greaterThan(ComposerFocusRing.ringWidth), ); FocusManager.instance.primaryFocus?.unfocus(); await tester.pump(); await tester.pump(); expect(border().focused, isFalse); await _endTurn(tester, gateway); }); testWidgets('reduced motion draws the static border and never ticks', ( tester, ) async { _reducedMotion(true); final gateway = await _pumpThread(tester); await _startTurn(tester, gateway); expect(_border, findsOneWidget); expect(tester.binding.transientCallbackCount, 0); await tester.pump(const Duration(seconds: 2)); expect(tester.binding.transientCallbackCount, 0); }); testWidgets('no stop button appears while a turn runs', (tester) async { final gateway = await _pumpThread(tester); await _startTurn(tester, gateway); expect(find.byKey(const Key('chat-composer-stop')), findsNothing); expect(find.byKey(const Key('chat-composer-primary')), findsOneWidget); }); testWidgets('/stop sent in the composer stops the running turn', ( tester, ) async { final gateway = await _pumpThread(tester); await _startTurn(tester, gateway); await tester.enterText( find.byKey(const Key('chat-composer-field')), '/stop', ); await tester.pump(); // A draft that already names /stop runs on the first send. await tester.tap(find.byKey(const Key('chat-composer-primary'))); await tester.pump(); await tester.pump(); expect(gateway.interruptBodies, [ {'session_id': 's'}, ]); expect(gateway.promptBodies, isEmpty); expect(find.text(chatStopCommandNotice), findsOneWidget); }); testWidgets('two Escapes from the composer stop the turn', (tester) async { final gateway = await _pumpThread(tester); await _startTurn(tester, gateway); await tester.tap(find.byKey(const Key('chat-composer-field'))); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(gateway.interruptBodies, isEmpty); expect(find.text(chatEscapeStopHint), findsOneWidget); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); await tester.pump(); expect(gateway.interruptBodies, [ {'session_id': 's'}, ]); }); testWidgets('a single Escape does not stop the turn', (tester) async { final gateway = await _pumpThread(tester); await _startTurn(tester, gateway); await tester.tap(find.byKey(const Key('chat-composer-field'))); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); await tester.pump(const Duration(seconds: 2)); expect(gateway.interruptBodies, isEmpty); }); testWidgets('Escape closes the command palette first and does not count', ( tester, ) async { final gateway = await _pumpThread(tester); await _startTurn(tester, gateway); await tester.enterText(find.byKey(const Key('chat-composer-field')), '/'); await tester.pump(); expect(find.byType(ChatCommandPalette), findsOneWidget); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(find.byType(ChatCommandPalette), findsNothing); expect(gateway.interruptBodies, isEmpty); // The palette's Escape was not the first of a pair; this one is. await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(gateway.interruptBodies, isEmpty); }); testWidgets('Escape does nothing while no turn runs', (tester) async { final gateway = await _pumpThread(tester); await tester.tap(find.byKey(const Key('chat-composer-field'))); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); expect(gateway.interruptBodies, isEmpty); expect(find.text(chatEscapeStopHint), findsNothing); }); }