import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/runs/ui/run_more_menu.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/core/transport/connection_state.dart'; import 'package:pipelines_app/features/runs/actions/title_wording.dart'; import 'package:pipelines_app/features/runs/screen.dart'; JsonMap _run(int id, String title) => { 'id': id, 'title': title, 'name': 'fixture', 'status': 'running', }; JsonMap _detail(int id, String title) => { ..._run(id, title), 'input': {'prompt': 'the stored prompt'}, 'steps': const [], }; /// Records the exact rename request and answers only when the test releases it, /// so an optimistic title would be visible before the server reply. class _Gateway implements ApiGateway { final framesController = StreamController.broadcast(); final saves = <({JsonMap fields, Completer reply})>[]; final titles = {1: 'Nightly build', 2: 'Other run'}; @override Stream get frames => framesController.stream; @override Future call(String type, [JsonMap fields = const {}]) { switch (type) { case 'list_runs': return Future.value({ 'runs': [ for (final entry in titles.entries) _run(entry.key, entry.value), ], }); case 'get_run': final id = (fields['run_id'] as num).toInt(); return Future.value({'run_detail': _detail(id, titles[id]!)}); case 'set_run_title': final reply = Completer(); saves.add((fields: Map.from(fields), reply: reply)); return reply.future; } return Future.error(UnsupportedError(type)); } void accept(String title) { final save = saves.removeAt(0); final id = (save.fields['run_id'] as num).toInt(); titles[id] = title; save.reply.complete({'run_detail': _detail(id, title)}); } void refuse(Object error) => saves.removeAt(0).reply.completeError(error); } Future<_Gateway> _mount( WidgetTester tester, { required bool cupertino, int initialRunId = 1, }) async { final gateway = _Gateway(); final screen = RunsScreen(gateway: gateway, initialRunId: initialRunId); await tester.pumpWidget( cupertino ? CupertinoApp( home: CupertinoPageScaffold(child: SafeArea(child: screen)), ) : MaterialApp(home: Scaffold(body: screen)), ); await tester.pumpAndSettle(); return gateway; } String _fieldText(WidgetTester tester, {required bool cupertino}) => cupertino ? tester .widget(find.byKey(runTitleFieldKey)) .controller! .text : tester.widget(find.byKey(runTitleFieldKey)).controller!.text; /// Rename lives in the run detail's more menu. Future _openRename(WidgetTester tester) async { await tester.tap(find.byKey(runMoreMenuKey)); await tester.pumpAndSettle(); await tester.tap(find.text(runTitleActionLabel).last); await tester.pumpAndSettle(); } void main() { testWidgets('material detail renames through the shared gateway and folds ' 'only the server reply into the detail and list', (tester) async { final gateway = await _mount(tester, cupertino: false); expect(find.text('Nightly build'), findsOneWidget); expect(find.byKey(runMoreMenuKey), findsOneWidget); expect(gateway.saves, isEmpty); await _openRename(tester); expect(find.byType(AlertDialog), findsOneWidget); expect(_fieldText(tester, cupertino: false), 'Nightly build'); await tester.enterText(find.byKey(runTitleFieldKey), 'Draft only'); await tester.pump(); expect(gateway.saves, isEmpty); await tester.tap(find.byKey(runTitleCancelKey)); await tester.pumpAndSettle(); expect(gateway.saves, isEmpty); expect(find.text('Nightly build'), findsOneWidget); await _openRename(tester); await tester.enterText(find.byKey(runTitleFieldKey), ' Release build '); await tester.tap(find.byKey(runTitleSaveKey)); await tester.pump(); expect(gateway.saves.single.fields, { 'run_id': 1, 'title': ' Release build ', }); // Still the stored title: nothing is applied before the server replies. expect(find.text('Nightly build'), findsOneWidget); expect(find.text('Release build'), findsNothing); gateway.accept('Release build'); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsNothing); expect(find.text('Release build'), findsOneWidget); expect(find.text('Nightly build'), findsNothing); await tester.tap(find.text('Back to list')); await tester.pump(); expect(find.text('Release build'), findsOneWidget); }); testWidgets('cupertino detail shows the native rename entry point', ( tester, ) async { final gateway = await _mount(tester, cupertino: true); expect(find.byKey(runMoreMenuKey), findsOneWidget); await _openRename(tester); expect(find.byType(CupertinoAlertDialog), findsOneWidget); expect(find.byType(AlertDialog), findsNothing); await tester.enterText(find.byKey(runTitleFieldKey), 'Release build'); await tester.tap(find.byKey(runTitleSaveKey)); await tester.pump(); expect(gateway.saves.single.fields, { 'run_id': 1, 'title': 'Release build', }); gateway.accept('Release build'); await tester.pumpAndSettle(); expect(find.byType(CupertinoAlertDialog), findsNothing); expect(find.text('Release build'), findsOneWidget); }); testWidgets('a refused rename keeps the typed title and the stored detail', ( tester, ) async { final gateway = await _mount(tester, cupertino: false); await _openRename(tester); await tester.enterText(find.byKey(runTitleFieldKey), ' locked '); await tester.tap(find.byKey(runTitleSaveKey)); await tester.pump(); gateway.refuse(const ProtocolError('rename refused by the server')); await tester.pumpAndSettle(); expect(find.text('rename refused by the server'), findsOneWidget); expect(_fieldText(tester, cupertino: false), ' locked '); expect(find.byType(AlertDialog), findsOneWidget); expect(find.text('Nightly build'), findsOneWidget); }); }