Animate chat composer while turns run

Replace the running Stop button with an animated composer border and task spinner. Add Escape, slash-command, and touch-menu stopping paths with focused widget coverage, including reduced-motion behavior.
This commit is contained in:
2026-11-12 02:24:46 +00:00
parent e4f3ebdcab
commit f3a469c356
14 changed files with 997 additions and 300 deletions
@@ -22,7 +22,7 @@ void main() {
tearDown(() => gateway.close());
testWidgets(
'Stop stays beside Send while running and interrupts this session',
'a running turn shows no stop button; Send stays and nothing interrupts',
(tester) async {
await tester.pumpWidget(
composerThread(gateway, newOperationId: nextOperation),
@@ -35,18 +35,9 @@ void main() {
await tester.pump();
expect(find.byTooltip(chatSendLabel), findsOneWidget);
expect(find.byTooltip(chatStopLabel), findsOneWidget);
final button = tester.widget<IconButton>(
find.byKey(const Key('chat-composer-stop')),
);
final scheme = Theme.of(
tester.element(find.byKey(const Key('chat-composer-stop'))),
).colorScheme;
expect(button.style?.backgroundColor?.resolve({}), scheme.error);
await tester.tap(find.byKey(const Key('chat-composer-stop')));
await tester.pumpAndSettle();
expect(gateway.interruptBodies.single, {'session_id': 's'});
expect(find.byKey(const Key('chat-composer-stop')), findsNothing);
expect(find.byTooltip(chatStopLabel), findsNothing);
expect(gateway.interruptBodies, isEmpty);
expect(gateway.promptBodies, isEmpty);
},
);
@@ -190,7 +181,7 @@ void main() {
reply.complete({'chat_queued': true});
await tester.pumpAndSettle();
expect(fieldText(tester), 'Newer draft');
expect(find.byKey(const Key('chat-composer-stop')), findsOneWidget);
expect(find.byKey(const Key('chat-composer-stop')), findsNothing);
expect(gateway.interruptBodies, isEmpty);
},
);
@@ -213,7 +204,7 @@ void main() {
text,
);
await tester.pump();
expect(find.byKey(const Key('chat-composer-stop')), findsOneWidget);
expect(find.byKey(const Key('chat-composer-stop')), findsNothing);
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle();
expect(fieldText(tester), '');
@@ -228,7 +219,7 @@ void main() {
'op/2',
]);
expect(gateway.interruptBodies, isEmpty);
expect(find.byKey(const Key('chat-composer-stop')), findsOneWidget);
expect(find.byKey(const Key('chat-composer-stop')), findsNothing);
});
testWidgets('the composer wraps at 320 logical px with 2x text', (
@@ -267,6 +258,6 @@ void main() {
expect(tester.takeException(), isNull);
expect(find.byKey(const Key('chat-composer')), findsOneWidget);
expect(find.byKey(const Key('chat-composer-primary')), findsOneWidget);
expect(find.byKey(const Key('chat-composer-stop')), findsOneWidget);
expect(find.byKey(const Key('chat-composer-stop')), findsNothing);
});
}
@@ -0,0 +1,161 @@
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 'chat_composer_bar_fixtures.dart';
import 'chat_thread_fixtures.dart';
Finder get _border => find.byKey(const Key('chat-composer-running-border'));
Future<ComposerBarGateway> _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<void> _startTurn(WidgetTester tester, ComposerBarGateway gateway) async {
gateway.emitEvent(chatStepFrame('running'));
await tester.pump();
await tester.pump();
}
Future<void> _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('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);
});
}
@@ -0,0 +1,169 @@
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_escape_stop.dart';
void main() {
group('ChatEscapeStopTracker', () {
test('a second press inside the window completes a pair', () {
final tracker = ChatEscapeStopTracker();
final start = DateTime(2026, 11, 12, 10);
expect(tracker.press(start), isFalse);
expect(
tracker.press(start.add(const Duration(milliseconds: 900))),
isTrue,
);
});
test('a press after the window starts a new pair', () {
final tracker = ChatEscapeStopTracker();
final start = DateTime(2026, 11, 12, 10);
expect(tracker.press(start), isFalse);
expect(tracker.press(start.add(const Duration(seconds: 2))), isFalse);
expect(
tracker.press(start.add(const Duration(seconds: 2, milliseconds: 500))),
isTrue,
);
});
test('reset forgets a pending first press', () {
final tracker = ChatEscapeStopTracker();
final start = DateTime(2026, 11, 12, 10);
tracker.press(start);
tracker.reset();
expect(
tracker.press(start.add(const Duration(milliseconds: 100))),
isFalse,
);
});
});
group('ChatEscapeStop', () {
late DateTime now;
late int stops;
setUp(() {
now = DateTime(2026, 11, 12, 10);
stops = 0;
});
Widget host({required bool running, Widget? child}) => MaterialApp(
home: Scaffold(
body: ChatEscapeStop(
running: running,
onStop: () => stops++,
clock: () => now,
child: child ?? const TextField(key: Key('field')),
),
),
);
testWidgets('a single Escape shows the hint and does not stop', (
tester,
) async {
await tester.pumpWidget(host(running: true));
await tester.tap(find.byKey(const Key('field')));
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pump();
expect(stops, 0);
expect(find.text(chatEscapeStopHint), findsOneWidget);
});
testWidgets('two Escapes within a second stop the turn once', (
tester,
) async {
await tester.pumpWidget(host(running: true));
await tester.tap(find.byKey(const Key('field')));
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
now = now.add(const Duration(milliseconds: 400));
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pump();
expect(stops, 1);
expect(find.text(chatEscapeStopHint), findsNothing);
});
testWidgets('two Escapes more than a second apart do not stop', (
tester,
) async {
await tester.pumpWidget(host(running: true));
await tester.tap(find.byKey(const Key('field')));
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
now = now.add(const Duration(milliseconds: 1500));
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pump();
expect(stops, 0);
});
testWidgets('an idle chat ignores Escape', (tester) async {
await tester.pumpWidget(host(running: false));
await tester.tap(find.byKey(const Key('field')));
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pump();
expect(stops, 0);
expect(find.text(chatEscapeStopHint), findsNothing);
});
testWidgets('focus on the transcript counts the same as the composer', (
tester,
) async {
await tester.pumpWidget(
host(
running: true,
child: const Focus(
autofocus: true,
child: SizedBox(width: 10, height: 10),
),
),
);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pump();
expect(stops, 1);
});
testWidgets('an Escape a descendant already handled never counts', (
tester,
) async {
await tester.pumpWidget(
host(
running: true,
child: Focus(
autofocus: true,
onKeyEvent: (_, event) =>
event.logicalKey == LogicalKeyboardKey.escape
? KeyEventResult.handled
: KeyEventResult.ignored,
child: const SizedBox(width: 10, height: 10),
),
),
);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pump();
expect(stops, 0);
expect(find.text(chatEscapeStopHint), findsNothing);
});
testWidgets('a held Escape is one press, not a pair', (tester) async {
await tester.pumpWidget(
host(
running: true,
child: const Focus(
autofocus: true,
child: SizedBox(width: 10, height: 10),
),
),
);
await tester.pump();
await tester.sendKeyDownEvent(LogicalKeyboardKey.escape);
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.escape);
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.escape);
await tester.sendKeyUpEvent(LogicalKeyboardKey.escape);
await tester.pump();
expect(stops, 0);
});
});
}
@@ -1,174 +0,0 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/conversation/chat_stop_icon.dart';
import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart';
import 'package:pipelines_app/features/chat/conversation/chat_composer_controller.dart';
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
import 'package:pipelines_app/ui/motion/motion.dart';
import 'chat_composer_bar_fixtures.dart';
class _CountPaint extends CustomPainter {
_CountPaint(this.onPaint);
final VoidCallback onPaint;
@override
void paint(Canvas canvas, Size size) => onPaint();
@override
bool shouldRepaint(_CountPaint oldDelegate) => false;
}
void main() {
setUp(() {
TestWidgetsFlutterBinding.ensureInitialized()
.platformDispatcher
.accessibilityFeaturesTestValue = const FakeAccessibilityFeatures(
disableAnimations: false,
);
});
tearDown(() {
TestWidgetsFlutterBinding.ensureInitialized()
.platformDispatcher
.accessibilityFeaturesTestValue = const FakeAccessibilityFeatures(
disableAnimations: true,
);
});
testWidgets('rotation repaints only the icon, not the surrounding chat', (
tester,
) async {
var paints = 0;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: CustomPaint(
painter: _CountPaint(() => paints++),
child: const ChatStopIcon(child: Icon(Icons.stop)),
),
),
),
);
final before = paints;
await tester.pump(const Duration(seconds: 1));
await tester.pump(const Duration(seconds: 1));
expect(paints, before);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets(
'Stop rotates linearly and continuously once every eight seconds',
(tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(body: ChatStopIcon(child: Icon(Icons.stop))),
),
);
RotationTransition rotation() =>
tester.widget(find.byKey(const Key('chat-stop-rotation')));
await tester.pump(const Duration(seconds: 2));
expect(rotation().turns.value, closeTo(0.25, 0.001));
await tester.pump(const Duration(seconds: 2));
expect(rotation().turns.value, closeTo(0.5, 0.001));
await tester.pump(const Duration(seconds: 8));
expect(rotation().turns.value, closeTo(0.5, 0.001));
await tester.pumpWidget(const SizedBox.shrink());
expect(tester.binding.transientCallbackCount, 0);
},
);
testWidgets('reduced motion stops/reset rotation and resumes when enabled', (
tester,
) async {
final reduce = ValueNotifier<bool>(true);
addTearDown(reduce.dispose);
await tester.pumpWidget(
MaterialApp(
home: ValueListenableBuilder<bool>(
valueListenable: reduce,
builder: (context, value, _) => MediaQuery(
data: MediaQuery.of(context).copyWith(disableAnimations: value),
child: const ChatStopIcon(child: Icon(Icons.stop)),
),
),
),
);
RotationTransition rotation() =>
tester.widget(find.byKey(const Key('chat-stop-rotation')));
await tester.pumpAndSettle();
expect(rotation().turns.value, 0);
expect(tester.binding.transientCallbackCount, 0);
reduce.value = false;
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(rotation().turns.value, closeTo(0.125, 0.001));
reduce.value = true;
await tester.pumpAndSettle();
expect(rotation().turns.value, 0);
expect(tester.binding.transientCallbackCount, 0);
});
testWidgets('TickerMode silences hidden Stop animation', (tester) async {
await tester.pumpWidget(
const MaterialApp(
home: TickerMode(
enabled: false,
child: ChatStopIcon(child: Icon(Icons.stop)),
),
),
);
await tester.pumpAndSettle();
expect(tester.binding.transientCallbackCount, 0);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets(
'rotating Stop keeps its target and interrupt action; Send stays available',
(tester) async {
final gateway = ComposerBarGateway();
addTearDown(gateway.close);
final composer = ChatComposerController(
gateway,
's',
drafts: MemoryChatDraftStore(),
);
addTearDown(composer.dispose);
final running = ValueNotifier<bool>(true);
addTearDown(running.dispose);
final bar = ValueListenableBuilder<bool>(
valueListenable: running,
builder: (_, value, _) =>
ChatComposerBar(composer: composer, running: value),
);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(body: Center(child: bar)),
),
);
// The composer pops in first; Stop's target is measured once it has.
await tester.pump(Motion.tabEntranceDuration);
final stop = find.byKey(const Key('chat-composer-stop'));
final target = tester.getRect(stop);
await tester.pump(const Duration(seconds: 3));
expect(tester.getRect(stop), target);
expect(find.byKey(const Key('chat-composer-primary')), findsOneWidget);
expect(
tester
.widget<RotationTransition>(
find.byKey(const Key('chat-stop-rotation')),
)
.turns
.value,
greaterThan(0),
);
await tester.tap(stop);
await tester.pump();
await tester.pump();
expect(gateway.interruptBodies, [
{'session_id': 's'},
]);
running.value = false;
await tester.pumpAndSettle();
expect(find.byType(ChatStopIcon), findsNothing);
expect(tester.binding.transientCallbackCount, 0);
},
);
}
@@ -0,0 +1,122 @@
import 'dart:async';
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/conversation/chat_background_tasks.dart';
import 'package:pipelines_app/features/chat/conversation/chat_task_spinner.dart';
RotationTransition _rotation(WidgetTester tester) =>
tester.widget<RotationTransition>(
find.byKey(const Key('chat-background-tasks-spinner')),
);
Widget _spinner(bool spinning) => MaterialApp(
home: Scaffold(
body: ChatTaskSpinner(
spinning: spinning,
child: const Icon(Icons.layers_outlined, size: 16),
),
),
);
void _reducedMotion(bool reduced) =>
TestWidgetsFlutterBinding.ensureInitialized()
.platformDispatcher
.accessibilityFeaturesTestValue = FakeAccessibilityFeatures(
disableAnimations: reduced,
);
class _TasksGateway implements ApiGateway {
final _frames = StreamController<JsonMap>.broadcast();
List<JsonMap> running = const [];
@override
Stream<JsonMap> get frames => _frames.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
if (type == 'chat_shells') return {'chat_shells': running};
if (type == 'chat_automations') {
return {
'chat_automations': {'status': 'unavailable'},
};
}
return <String, dynamic>{};
}
void close() => _frames.close();
}
void main() {
tearDown(() => _reducedMotion(false));
testWidgets('turns once every period while spinning', (tester) async {
_reducedMotion(false);
await tester.pumpWidget(_spinner(true));
expect(_rotation(tester).turns.value, 0);
await tester.pump(ChatTaskSpinner.period ~/ 4);
expect(_rotation(tester).turns.value, closeTo(0.25, 0.01));
// Past one full lap the turn starts again from the top.
await tester.pump(
ChatTaskSpinner.period ~/ 4 * 3 + const Duration(milliseconds: 100),
);
expect(_rotation(tester).turns.value, closeTo(0.04, 0.01));
await tester.pumpWidget(const SizedBox.shrink());
expect(tester.binding.transientCallbackCount, 0);
});
testWidgets('stays still when not spinning, and stops when it stops', (
tester,
) async {
_reducedMotion(false);
await tester.pumpWidget(_spinner(false));
await tester.pump(const Duration(seconds: 2));
expect(_rotation(tester).turns.value, 0);
expect(tester.binding.transientCallbackCount, 0);
await tester.pumpWidget(_spinner(true));
await tester.pump(const Duration(milliseconds: 625));
expect(_rotation(tester).turns.value, greaterThan(0));
await tester.pumpWidget(_spinner(false));
await tester.pump();
expect(_rotation(tester).turns.value, 0);
await tester.pump(const Duration(seconds: 2));
expect(_rotation(tester).turns.value, 0);
expect(tester.binding.transientCallbackCount, 0);
});
testWidgets('reduced motion keeps the icon static even while spinning', (
tester,
) async {
_reducedMotion(true);
await tester.pumpWidget(_spinner(true));
await tester.pump(const Duration(seconds: 1));
expect(_rotation(tester).turns.value, 0);
expect(tester.binding.transientCallbackCount, 0);
});
testWidgets('the tasks icon rotates only while a task runs', (tester) async {
_reducedMotion(false);
final gateway = _TasksGateway();
addTearDown(gateway.close);
gateway.running = const [
{'id': 'sh1', 'status': 'running', 'command': 'go test ./...'},
];
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ChatBackgroundTasks(gateway: gateway, sessionId: 's'),
),
),
);
// The spinning icon repeats forever, so settle by pumping, never by waiting.
await tester.pump();
await tester.pump();
expect(find.text('1 task'), findsOneWidget);
final before = _rotation(tester).turns.value;
await tester.pump(const Duration(milliseconds: 300));
expect(_rotation(tester).turns.value, greaterThan(before));
});
}
@@ -0,0 +1,46 @@
import 'package:flutter_test/flutter_test.dart';
import 'chat_composer_bar_fixtures.dart';
import 'chat_thread_fixtures.dart';
void main() {
Future<ComposerBarGateway> 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;
}
testWidgets('the More menu offers Stop while a turn runs, for touch use', (
tester,
) async {
final gateway = await pumpThread(tester);
gateway.emitEvent(chatStepFrame('running'));
await tester.pump();
await tester.pump();
await tester.tap(find.byTooltip('More actions').hitTestable().first);
await tester.pumpAndSettle();
await tester.tap(find.text('Stop'));
await tester.pump();
await tester.pump();
expect(gateway.interruptBodies, [
{'session_id': 's'},
]);
expect(gateway.promptBodies, isEmpty);
});
testWidgets('the More menu has no Stop once the turn has ended', (
tester,
) async {
final gateway = await pumpThread(tester);
await tester.tap(find.byTooltip('More actions').hitTestable().first);
await tester.pumpAndSettle();
expect(find.text('Stop'), findsNothing);
expect(gateway.interruptBodies, isEmpty);
});
}