Files
pipelines/app/test/features/chat/conversation/chat_task_spinner_test.dart
T
alan f3a469c356 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.
2026-11-12 02:24:46 +00:00

123 lines
4.0 KiB
Dart

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));
});
}