import 'dart:async'; import 'package:flutter/cupertino.dart'; 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/runs/screen.dart'; class _Gateway implements ApiGateway { final framesController = StreamController.broadcast(); final listRequests = >[]; final detailRequests = >>{}; @override Stream get frames => framesController.stream; @override Future call(String type, [JsonMap fields = const {}]) { final request = Completer(); if (type == 'list_runs') { listRequests.add(request); } else if (type == 'get_run') { final id = fields['run_id'] as num; detailRequests.putIfAbsent(id, () => []).add(request); } else { request.completeError(UnsupportedError(type)); } return request.future; } void replyList(int index, List runs) => listRequests[index].complete({'runs': runs}); void replyDetail(num id, int index, JsonMap detail) => detailRequests[id]![index].complete({'run_detail': detail}); void failDetail(num id, int index, Object error) => detailRequests[id]![index].completeError(error); } const _runs = [ {'id': 1, 'title': 'Alpha run', 'name': 'alpha', 'status': 'running'}, {'id': 2, 'title': 'Beta run', 'name': 'beta', 'status': 'failed'}, ]; Widget _materialScreen( _Gateway gateway, { int? initialRunId, int? initialPipelineId, int syncToken = 0, ValueChanged? onRunSelected, ValueChanged? onBack, ValueChanged? onPipelineFilterChanged, }) => MaterialApp( home: Scaffold( body: RunsScreen( gateway: gateway, initialRunId: initialRunId, initialPipelineId: initialPipelineId, syncToken: syncToken, onRunSelected: onRunSelected, onBack: onBack, onPipelineFilterChanged: onPipelineFilterChanged, ), ), ); Future _mountList(WidgetTester tester, _Gateway gateway) async { await tester.pump(); gateway.replyList(0, _runs); await tester.pump(); await tester.pump(); } void main() { late _Gateway gateway; setUp(() => gateway = _Gateway()); tearDown(() => gateway.framesController.close()); testWidgets('initial run opens a pending detail with Back available', ( tester, ) async { await tester.pumpWidget(_materialScreen(gateway, initialRunId: 1)); await _mountList(tester, gateway); expect(gateway.detailRequests[1], hasLength(1)); expect(find.text('Loading run details…'), findsOneWidget); expect(find.text('Back to list'), findsOneWidget); expect(find.text('Alpha run'), findsNothing); await tester.tap(find.text('Back to list')); await tester.pump(); expect(find.text('Alpha run'), findsOneWidget); expect(find.text('Beta run'), findsOneWidget); // A reply that lands after Back must not reopen the previous detail. gateway.replyDetail(1, 0, {'id': 1, 'title': 'Alpha run'}); await tester.pump(); expect(find.text('Alpha run'), findsOneWidget); expect(find.text('Loading run details…'), findsNothing); }); testWidgets('initial run renders its detail after the reply', (tester) async { await tester.pumpWidget(_materialScreen(gateway, initialRunId: 1)); await _mountList(tester, gateway); gateway.replyDetail(1, 0, { 'id': 1, 'title': 'Alpha run', 'status': 'succeeded', 'input': {'prompt': 'initial prompt'}, }); await tester.pump(); expect(find.text('Alpha run'), findsOneWidget); expect(find.text('Input'), findsOneWidget); expect(find.text('Loading run details…'), findsNothing); }); testWidgets('detail failure keeps Back and returns to the list', ( tester, ) async { await tester.pumpWidget(_materialScreen(gateway, initialRunId: 2)); await _mountList(tester, gateway); gateway.failDetail(2, 0, StateError('server unavailable')); await tester.pump(); expect(find.textContaining('server unavailable'), findsOneWidget); expect(find.text('Back to list'), findsOneWidget); await tester.tap(find.text('Back to list')); await tester.pump(); expect(find.text('Alpha run'), findsOneWidget); expect(find.textContaining('server unavailable'), findsNothing); }); testWidgets('selecting a list row is driven by the controller selection', ( tester, ) async { await tester.pumpWidget(_materialScreen(gateway)); await _mountList(tester, gateway); await tester.tap(find.text('Beta run')); await tester.pump(); expect(gateway.detailRequests[2], hasLength(1)); gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'}); await tester.pump(); expect(find.text('Beta run'), findsOneWidget); expect(find.text('Back to list'), findsOneWidget); }); testWidgets('selection and Back report route changes to the host', ( tester, ) async { int? selected; int? backedPipeline; await tester.pumpWidget( _materialScreen( gateway, onRunSelected: (id) => selected = id, onBack: (id) => backedPipeline = id, ), ); await _mountList(tester, gateway); await tester.tap(find.text('Beta run')); await tester.pump(); expect(selected, 2); gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'}); await tester.pump(); await tester.tap(find.text('Back to list')); await tester.pump(); expect(backedPipeline, isNull); expect(find.text('Alpha run'), findsOneWidget); }); testWidgets( 'pipeline route shows its chip and clearing reports the URL change', (tester) async { int? changedPipeline = 3; await tester.pumpWidget( _materialScreen( gateway, initialPipelineId: 3, onPipelineFilterChanged: (id) => changedPipeline = id, ), ); await _mountList(tester, gateway); expect(find.text('Pipeline 3'), findsOneWidget); expect(changedPipeline, 3); await tester.tap(find.byTooltip('Clear filter: Pipeline 3')); await tester.pump(); expect(changedPipeline, isNull); expect(find.text('Pipeline 3'), findsNothing); }, ); testWidgets('run detail keeps the pipeline filter for Back routing', ( tester, ) async { int? backPipeline; await tester.pumpWidget( _materialScreen( gateway, initialPipelineId: 3, onBack: (id) => backPipeline = id, ), ); await _mountList(tester, gateway); await tester.pumpWidget( _materialScreen( gateway, initialRunId: 2, onBack: (id) => backPipeline = id, ), ); await tester.pump(); gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'}); await tester.pump(); await tester.tap(find.text('Back to list')); await tester.pump(); expect(backPipeline, 3); }); testWidgets('a route change opens and closes the named run', (tester) async { await tester.pumpWidget(_materialScreen(gateway, initialRunId: 2)); await _mountList(tester, gateway); expect(gateway.detailRequests[2], hasLength(1)); gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'}); await tester.pump(); expect(find.text('Beta run'), findsOneWidget); await tester.pumpWidget(_materialScreen(gateway, initialRunId: 1)); await tester.pump(); expect(gateway.detailRequests[1], hasLength(1)); gateway.replyDetail(1, 0, {'id': 1, 'title': 'Alpha run'}); await tester.pump(); expect(find.text('Alpha run'), findsOneWidget); await tester.pumpWidget(_materialScreen(gateway)); await tester.pump(); expect(find.text('Alpha run'), findsOneWidget); expect(find.text('Beta run'), findsOneWidget); expect(find.text('Back to list'), findsNothing); }); testWidgets('a reconnect token refreshes the open detail in place', ( tester, ) async { await tester.pumpWidget(_materialScreen(gateway, initialRunId: 2)); await _mountList(tester, gateway); gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'}); await tester.pump(); expect(find.text('Beta run'), findsOneWidget); await tester.pumpWidget( _materialScreen(gateway, initialRunId: 2, syncToken: 1), ); await tester.pump(); expect(gateway.listRequests, hasLength(2)); gateway.replyList(1, _runs); await tester.pump(); await tester.pump(); expect(gateway.detailRequests[2], hasLength(2)); gateway.replyDetail(2, 1, { 'id': 2, 'title': 'Beta run', 'status': 'succeeded', }); await tester.pump(); expect(find.text('Beta run'), findsOneWidget); expect(find.text('Back to list'), findsOneWidget); }); testWidgets( 'Cupertino host keeps exactly one navigation bar while pending', (tester) async { await tester.pumpWidget( CupertinoApp( home: CupertinoPageScaffold( navigationBar: const CupertinoNavigationBar(middle: Text('Runs')), child: SafeArea( child: RunsScreen(gateway: gateway, initialRunId: 1), ), ), ), ); await _mountList(tester, gateway); expect(find.byType(CupertinoPageScaffold), findsOneWidget); expect(find.byType(CupertinoNavigationBar), findsOneWidget); expect(find.text('Loading run details…'), findsOneWidget); expect(find.byType(CupertinoActivityIndicator), findsOneWidget); expect(find.text('Back to list'), findsOneWidget); }, variant: TargetPlatformVariant.only(TargetPlatform.iOS), ); testWidgets('on a phone an open run is its own page and Back returns', ( tester, ) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); int? selected; var backs = 0; await tester.pumpWidget( _materialScreen( gateway, onRunSelected: (id) => selected = id, onBack: (_) => backs++, ), ); await _mountList(tester, gateway); await tester.tap(find.text('Beta run')); await tester.pump(); expect(selected, 2); gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'}); await tester.pumpAndSettle(); // The run covers the list as a page with its own Back. expect(find.text('Alpha run'), findsNothing); expect(find.byKey(const ValueKey('shell.back')), findsOneWidget); await tester.tap(find.byKey(const ValueKey('shell.back'))); await tester.pumpAndSettle(); expect(backs, 1); expect(find.text('Alpha run'), findsOneWidget); }); }