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'; final _spinnerFinder = find.byKey(const Key('chat-background-tasks-spinner')); CircularProgressIndicator _ring(WidgetTester tester) => tester.widget( find.descendant( of: _spinnerFinder, matching: find.byType(CircularProgressIndicator), ), ); Widget _spinner(int running, {Brightness brightness = Brightness.light}) => MaterialApp( theme: ThemeData(colorSchemeSeed: Colors.indigo, brightness: brightness), home: Scaffold( body: Center(child: ChatTaskSpinner(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 an indeterminate primary ring with the count', (tester) async { _reducedMotion(false); await tester.pumpWidget(_spinner(2)); final ring = _ring(tester); expect(ring.value, isNull); expect(ring.strokeWidth, 2); expect( ring.color, ThemeData(colorSchemeSeed: Colors.indigo).colorScheme.primary, ); expect( find.descendant(of: _spinnerFinder, matching: find.text('2')), findsOneWidget, ); expect( find.descendant( of: _spinnerFinder, matching: find.byIcon(Icons.layers_outlined), ), findsNothing, ); // The ring keeps turning: time advances the animation. await tester.pump(const Duration(milliseconds: 300)); expect(tester.binding.transientCallbackCount, greaterThan(0)); await tester.pumpWidget(const SizedBox.shrink()); expect(tester.binding.transientCallbackCount, 0); }, ); testWidgets('dark theme uses the dark primary for the ring', (tester) async { _reducedMotion(false); await tester.pumpWidget(_spinner(1, brightness: Brightness.dark)); expect( _ring(tester).color, ThemeData( colorSchemeSeed: Colors.indigo, brightness: Brightness.dark, ).colorScheme.primary, ); }); testWidgets('no running task shows the static icon with no ring', ( tester, ) async { _reducedMotion(false); await tester.pumpWidget(_spinner(0)); expect( find.descendant( of: _spinnerFinder, matching: find.byType(CircularProgressIndicator), ), findsNothing, ); expect( find.descendant( of: _spinnerFinder, matching: find.byIcon(Icons.layers_outlined), ), findsOneWidget, ); 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(_spinner(running)); expect(tester.getSize(_spinnerFinder), const Size(16, 16)); } }); testWidgets('reduced motion holds one static ring segment while running', ( tester, ) async { _reducedMotion(true); await tester.pumpWidget(_spinner(3)); expect(_ring(tester).value, ChatTaskSpinner.reducedSegment); await tester.pump(const Duration(seconds: 1)); expect(_ring(tester).value, ChatTaskSpinner.reducedSegment); expect(tester.binding.transientCallbackCount, 0); }); testWidgets('the composer button shows the count ring 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 ring repeats forever, so settle by pumping, never by waiting. await tester.pump(); await tester.pump(); expect(find.text('2 tasks'), findsOneWidget); expect(find.byTooltip('Background tasks (2 running)'), findsOneWidget); expect(_ring(tester).value, isNull); expect( find.descendant(of: _spinnerFinder, matching: find.text('2')), findsOneWidget, ); }); }