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_gear.dart'; final _gearFinder = find.byKey(const Key('chat-background-tasks-gear')); final _rotationFinder = find.byKey(const Key('chat-background-tasks-rotation')); RotationTransition _rotation(WidgetTester tester) => tester.widget(_rotationFinder); Widget _gear(int running, {Brightness brightness = Brightness.light}) => MaterialApp( theme: ThemeData(colorSchemeSeed: Colors.indigo, brightness: brightness), home: Scaffold( body: Center(child: ChatTaskGear(running: running)), ), ); void _reducedMotion(bool reduced) => TestWidgetsFlutterBinding.ensureInitialized() .platformDispatcher .accessibilityFeaturesTestValue = FakeAccessibilityFeatures( disableAnimations: reduced, ); class _TasksGateway implements ApiGateway { final _frames = StreamController.broadcast(); List running = const []; @override Stream get frames => _frames.stream; @override Future 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 {}; } void close() => _frames.close(); } void main() { tearDown(() => _reducedMotion(false)); testWidgets('a running task shows the outlined gear with no count on it', ( tester, ) async { _reducedMotion(false); await tester.pumpWidget(_gear(2)); expect( find.descendant(of: _gearFinder, matching: find.byType(Text)), findsNothing, ); expect(find.byType(Badge), findsNothing); expect( find.descendant( of: _gearFinder, matching: find.byIcon(Icons.settings_outlined), ), findsOneWidget, ); expect( find.descendant( of: _gearFinder, matching: find.byIcon(Icons.layers_outlined), ), findsNothing, ); expect(tester.binding.transientCallbackCount, greaterThan(0)); await tester.pumpWidget(const SizedBox.shrink()); expect(tester.binding.transientCallbackCount, 0); }); testWidgets('the running gear turns linearly, one turn per four seconds', ( tester, ) async { _reducedMotion(false); await tester.pumpWidget(_gear(1)); expect(_rotation(tester).turns.value, closeTo(0, 1e-9)); await tester.pump(const Duration(seconds: 1)); expect(_rotation(tester).turns.value, closeTo(0.25, 1e-3)); await tester.pump(const Duration(seconds: 1)); expect(_rotation(tester).turns.value, closeTo(0.5, 1e-3)); await tester.pump(const Duration(seconds: 2)); expect(_rotation(tester).turns.value, closeTo(0, 1e-3)); expect(ChatTaskGear.turnDuration, const Duration(seconds: 4)); }); testWidgets('an idle gear is static with no animation', (tester) async { _reducedMotion(false); await tester.pumpWidget(_gear(0)); expect( find.descendant( of: _gearFinder, matching: find.byIcon(Icons.settings_outlined), ), findsOneWidget, ); expect(tester.binding.transientCallbackCount, 0); await tester.pump(const Duration(seconds: 1)); expect(_rotation(tester).turns.value, closeTo(0, 1e-9)); }); testWidgets('stopping the last task halts and resets the turn', ( tester, ) async { _reducedMotion(false); await tester.pumpWidget(_gear(2)); await tester.pump(const Duration(seconds: 1)); expect(_rotation(tester).turns.value, greaterThan(0)); await tester.pumpWidget(_gear(0)); expect(tester.binding.transientCallbackCount, 0); expect(_rotation(tester).turns.value, closeTo(0, 1e-9)); }); testWidgets('reduced motion never rotates', (tester) async { _reducedMotion(true); await tester.pumpWidget(_gear(3)); expect(tester.binding.transientCallbackCount, 0); await tester.pump(const Duration(seconds: 2)); expect(_rotation(tester).turns.value, closeTo(0, 1e-9)); expect(tester.binding.transientCallbackCount, 0); }); testWidgets('every state keeps the same square size', (tester) async { _reducedMotion(false); for (final running in [0, 1, 12]) { await tester.pumpWidget(_gear(running)); expect(tester.getSize(_gearFinder), const Size(16, 16)); } }); testWidgets('the composer button shows the gear and its tooltip', ( tester, ) async { _reducedMotion(false); final gateway = _TasksGateway(); addTearDown(gateway.close); gateway.running = const [ {'id': 'sh1', 'status': 'running', 'command': 'go test ./...'}, {'id': 'sh2', 'status': 'running', 'command': 'make'}, ]; await tester.pumpWidget( MaterialApp( home: Scaffold( body: ChatBackgroundTasks(gateway: gateway, sessionId: 's'), ), ), ); // The gear turns forever while tasks run, so settle by pumping only. await tester.pump(); await tester.pump(); expect(find.text('2 tasks'), findsOneWidget); expect(find.byTooltip('Background tasks (2 running)'), findsOneWidget); expect( find.descendant(of: _gearFinder, matching: find.text('2')), findsNothing, ); expect(tester.binding.transientCallbackCount, greaterThan(0)); }); }