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/core/transport/connection_state.dart'; import 'package:pipelines_app/features/runs/actions/decision_action.dart'; import 'package:pipelines_app/features/runs/actions/decision_wording.dart'; typedef _Handler = Future Function(String type, JsonMap fields); class _FakeGateway implements ApiGateway { _FakeGateway(this.handler); final _Handler handler; final List<({String type, JsonMap fields})> calls = []; @override Future call(String type, [JsonMap fields = const {}]) { final copy = Map.from(fields); calls.add((type: type, fields: copy)); return handler(type, copy); } @override Stream get frames => const Stream.empty(); } JsonMap _waitingRun() => { 'id': 7, 'title': 'Report', 'status': 'waiting', 'current_stage': 'human', 'graph': { 'stages': [ { 'key': 'human', 'type': 'human', 'prompt': 'Graph prompt', 'edges': {'go': '#', 'skip': 'finish'}, }, ], }, 'steps': [ { 'sequence': 1, 'status': 'waiting', 'stage': 'human', 'output': { 'prompt': 'Ship it?', 'options': ['go', 'skip'], }, }, ], }; JsonMap _decided() => { ..._waitingRun(), 'status': 'succeeded', 'steps': const [], }; Widget _app(Widget child, {required bool cupertino, double textScale = 1}) { Widget media(BuildContext context, Widget? app) => MediaQuery( data: MediaQuery.of( context, ).copyWith(textScaler: TextScaler.linear(textScale)), child: app!, ); if (cupertino) { return CupertinoApp( builder: media, home: CupertinoPageScaffold(child: Center(child: child)), ); } return MaterialApp( builder: media, home: Scaffold(body: Center(child: child)), ); } Future _pump( WidgetTester tester, _FakeGateway gateway, List changed, { bool cupertino = false, double textScale = 1, }) async { await tester.pumpWidget( _app( HumanDecisionAction( run: _waitingRun(), gateway: gateway, onRunChanged: changed.add, ), cupertino: cupertino, textScale: textScale, ), ); } Future _chooseGo(WidgetTester tester, {String feedback = ''}) async { await tester.tap(find.byKey(decisionActionKey)); await tester.pumpAndSettle(); await tester.tap(find.text('go')); await tester.pump(); if (feedback.isNotEmpty) { await tester.enterText(find.byKey(decisionFeedbackKey), feedback); } } void main() { testWidgets('a refusal stays visible and keeps the selected draft', ( tester, ) async { const refusal = 'option "go" is not a valid outcome; choose one of: skip'; final gateway = _FakeGateway((type, fields) async { if (type == 'decide_run') throw const ProtocolError(refusal); expect(type, 'get_run'); return { 'run_detail': { ..._waitingRun(), 'status': 'failed', 'steps': const [], }, }; }); final changed = []; await _pump(tester, gateway, changed); await _chooseGo(tester, feedback: ' keep me '); await tester.tap(find.text(decisionConfirmLabel)); await tester.pumpAndSettle(); expect(gateway.calls.map((call) => call.type), ['decide_run', 'get_run']); expect(gateway.calls.last.fields, {'run_id': 7}); expect(find.text(refusal), findsOneWidget); expect(find.byType(AlertDialog), findsOneWidget); expect( tester .widget>(find.byType(RadioGroup)) .groupValue, 'go', ); expect( tester .widget(find.byKey(decisionFeedbackKey)) .controller ?.text, ' keep me ', ); expect(changed.single['status'], 'failed'); expect(find.byKey(decisionActionKey), findsNothing); }); testWidgets('a refusal is shown before the resync keeps the draft', ( tester, ) async { const refusal = 'option "go" is no longer valid'; final resync = Completer(); final gateway = _FakeGateway((type, fields) { if (type == 'decide_run') throw const ProtocolError(refusal); return resync.future; }); final changed = []; await _pump(tester, gateway, changed); await _chooseGo(tester, feedback: ' keep me '); await tester.tap(find.text(decisionConfirmLabel)); await tester.pump(); expect(find.text(refusal), findsOneWidget); expect(find.byType(AlertDialog), findsOneWidget); expect( tester .widget>(find.byType(RadioGroup)) .groupValue, 'go', ); expect( tester .widget(find.byKey(decisionFeedbackKey)) .controller ?.text, ' keep me ', ); expect( tester .widget( find.widgetWithText(TextButton, decisionCancelLabel), ) .onPressed, isNotNull, ); expect(changed, isEmpty); resync.complete({'run_detail': _decided()}); await tester.pumpAndSettle(); expect(find.text(refusal), findsOneWidget); expect( tester .widget( find.widgetWithText(FilledButton, decisionConfirmLabel), ) .onPressed, isNull, ); expect(changed.single['status'], 'succeeded'); }); testWidgets('a failed resync keeps the draft and allows a retry', ( tester, ) async { const refusal = 'server refused the decision'; final gateway = _FakeGateway((type, fields) async { if (type == 'decide_run') throw const ProtocolError(refusal); throw const NotConnectedError(); }); final changed = []; await _pump(tester, gateway, changed); await _chooseGo(tester, feedback: ' keep me '); await tester.tap(find.text(decisionConfirmLabel)); await tester.pumpAndSettle(); expect(gateway.calls.map((call) => call.type), ['decide_run', 'get_run']); expect(find.text(refusal), findsOneWidget); expect( tester .widget>(find.byType(RadioGroup)) .groupValue, 'go', ); expect( tester .widget( find.widgetWithText(FilledButton, decisionConfirmLabel), ) .onPressed, isNotNull, ); expect(changed, isEmpty); }); testWidgets('a late resync after disposal is ignored safely', (tester) async { final resync = Completer(); final gateway = _FakeGateway((type, fields) { if (type == 'decide_run') throw const ProtocolError('no longer waiting'); return resync.future; }); final changed = []; await _pump(tester, gateway, changed); await _chooseGo(tester); await tester.tap(find.text(decisionConfirmLabel)); await tester.pump(); expect(gateway.calls.map((call) => call.type), ['decide_run', 'get_run']); await tester.pumpWidget(const SizedBox()); resync.complete({'run_detail': _decided()}); await tester.pump(); expect(tester.takeException(), isNull); expect(changed, isEmpty); }); testWidgets('confirm cannot submit twice while the decision is in flight', ( tester, ) async { final reply = Completer(); final gateway = _FakeGateway((type, fields) => reply.future); final changed = []; await _pump(tester, gateway, changed); await _chooseGo(tester); final confirm = find.text(decisionConfirmLabel); await tester.tap(confirm); await tester.tap(confirm); await tester.pump(); expect( gateway.calls.where((call) => call.type == 'decide_run'), hasLength(1), ); expect(find.text(decisionSendingLabel), findsOneWidget); expect( tester .widget( find.widgetWithText(TextButton, decisionCancelLabel), ) .onPressed, isNull, ); reply.complete({'run_detail': _decided()}); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsNothing); expect(changed, hasLength(1)); }); testWidgets('a late reply after disposal is ignored safely', (tester) async { final reply = Completer(); final gateway = _FakeGateway((type, fields) => reply.future); final changed = []; await _pump(tester, gateway, changed); await _chooseGo(tester); await tester.tap(find.text(decisionConfirmLabel)); await tester.pump(); expect(gateway.calls, hasLength(1)); await tester.pumpWidget(const SizedBox()); reply.complete({'run_detail': _decided()}); await tester.pump(); expect(tester.takeException(), isNull); expect(changed, isEmpty); }); testWidgets('both prompts fit 2x text at a narrow width', (tester) async { tester.view.physicalSize = const Size(320, 480); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); for (final cupertino in [false, true]) { final gateway = _FakeGateway( (type, fields) async => {'run_detail': _decided()}, ); await _pump( tester, gateway, [], cupertino: cupertino, textScale: 2, ); await tester.tap(find.byKey(decisionActionKey)); await tester.pumpAndSettle(); expect(tester.takeException(), isNull, reason: 'cupertino=$cupertino'); expect(find.text(decisionWaitingTitle), findsOneWidget); expect(find.text('Ship it?'), findsOneWidget); expect( cupertino ? find.byType(CupertinoAlertDialog) : find.byType(AlertDialog), findsOneWidget, ); } }); }