import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/pipelines/ui/run_pipeline_wording.dart'; import 'package:pipelines_app/features/runs/screen.dart'; import '../../pipelines/fixtures.dart'; JsonMap _acceptedRun(int id) => { 'type': 'run', 'run_detail': {'id': id, 'status': 'running', 'title': 'Run $id'}, }; /// Pipelines for the picker: the fixture's three active ones plus one archived /// pipeline, which must never be offered for a new run. List> _pipelines(PipelineFixture fixture) => [ ...fixture.summaries, { ...pipelineSummary(id: 4, name: 'Retired build'), 'archived_at': '2026-10-01T10:00:00', }, ]; FakeGateway _gateway(PipelineFixture fixture, {required int launchedRunId}) => FakeGateway( (type, fields) => switch (type) { 'list_runs' => runListReply(fixture.runs), 'list_pipelines' => pipelineListReply(_pipelines(fixture)), 'get_pipeline' => pipelineDetailReply( fixture.detailFor( fields['pipeline_id'] as int, fields['version'] as int, )!, ), 'chat_directories' => { 'type': 'chat_directories', 'chat_directories': ['/work/recent'], }, 'run_pipeline' => _acceptedRun(launchedRunId), _ => errorReply('unexpected $type'), }, ); /// Pumps the Runs screen. With [withNavigationBar] the screen sits in a /// scaffold with `extendBody` and a bottom NavigationBar, as the shell does on /// a phone, so the body scrolls under the bar. Future _pumpRuns( WidgetTester tester, FakeGateway gateway, { required Size size, required List selected, bool withNavigationBar = false, }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final screen = RunsScreen(gateway: gateway, onRunSelected: selected.add); await tester.pumpWidget( MaterialApp( home: withNavigationBar ? Scaffold( extendBody: true, body: screen, bottomNavigationBar: NavigationBar( selectedIndex: 0, onDestinationSelected: (_) {}, destinations: const [ NavigationDestination( icon: Icon(Icons.play_arrow), label: 'Runs', ), NavigationDestination( icon: Icon(Icons.settings), label: 'Settings', ), ], ), ) : Scaffold(body: screen), ), ); await tester.pumpAndSettle(); } void main() { TestWidgetsFlutterBinding.ensureInitialized(); SharedPreferences.setMockInitialValues({}); for (final size in const [Size(430, 900), Size(1100, 900)]) { testWidgets('New run is labelled on the Runs list at ${size.width} wide', ( tester, ) async { final gateway = _gateway(PipelineFixture(), launchedRunId: 91); await _pumpRuns(tester, gateway, size: size, selected: []); final button = find.byKey(const Key('runs-new-run')); expect(button, findsOneWidget); expect( find.descendant(of: button, matching: find.text('New run')), findsOneWidget, ); }); } testWidgets('New run clears the bottom bar and opens the picker on a phone', ( tester, ) async { final gateway = _gateway(PipelineFixture(), launchedRunId: 91); await _pumpRuns( tester, gateway, size: const Size(430, 900), selected: [], withNavigationBar: true, ); final button = find.byKey(const Key('runs-new-run')); final bar = find.byType(NavigationBar); expect(button, findsOneWidget); // The button sits wholly above the bar, not beneath it. expect( tester.getRect(button).bottom, lessThanOrEqualTo(tester.getRect(bar).top), ); // A tap at the button reaches the button, not the bar behind it. await tester.tap(button); await tester.pumpAndSettle(); expect(find.byKey(const Key('new-run-search')), findsOneWidget); }); testWidgets( 'New run picks an active pipeline, starts it and opens the new run', (tester) async { final fixture = PipelineFixture(); final gateway = _gateway(fixture, launchedRunId: 91); final selected = []; await _pumpRuns( tester, gateway, size: const Size(1100, 900), selected: selected, ); await tester.tap(find.byKey(const Key('runs-new-run'))); await tester.pumpAndSettle(); // The picker lists active pipelines only; the archived one is withheld. expect(find.byKey(const ValueKey('new-run-pipeline-1')), findsOneWidget); expect(find.byKey(const ValueKey('new-run-pipeline-2')), findsOneWidget); expect(find.byKey(const ValueKey('new-run-pipeline-4')), findsNothing); // Search narrows the list by name. await tester.enterText(find.byKey(const Key('new-run-search')), 'beta'); await tester.pump(); expect(find.byKey(const ValueKey('new-run-pipeline-1')), findsNothing); expect(find.byKey(const ValueKey('new-run-pipeline-2')), findsOneWidget); await tester.tap(find.byKey(const ValueKey('new-run-pipeline-2'))); await tester.pumpAndSettle(); // The chosen pipeline's latest version is read, then the start dialog // opens for it: the same flow as the pipeline's own Run action. expect(gateway.callsOf('get_pipeline').single.fields, { 'pipeline_id': 2, 'version': 0, }); await tester.enterText( find.byKey(runPipelinePromptKey), 'Ship the beta.', ); await tester.enterText(find.byKey(runPipelineWorkspaceKey), '/work/beta'); await tester.pump(); await tester.tap(find.byKey(runPipelineConfirmKey)); await tester.pumpAndSettle(); final launch = gateway.callsOf('run_pipeline').single.fields; expect(launch['pipeline_id'], 2); expect(launch['version'], 1); expect(selected, [91]); }, ); testWidgets('dismissing the picker starts nothing', (tester) async { final gateway = _gateway(PipelineFixture(), launchedRunId: 91); final selected = []; await _pumpRuns( tester, gateway, size: const Size(430, 900), selected: selected, ); await tester.tap(find.byKey(const Key('runs-new-run'))); await tester.pumpAndSettle(); await tester.tapAt(const Offset(4, 4)); await tester.pumpAndSettle(); expect(find.byKey(const Key('new-run-search')), findsNothing); expect(gateway.callsOf('get_pipeline'), isEmpty); expect(gateway.callsOf('run_pipeline'), isEmpty); expect(selected, isEmpty); }); }