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/actions/retry_action.dart'; typedef RetryHandler = Future Function(String type, JsonMap fields); /// A fake gateway that records every request and never owns a socket. class FakeRetryGateway implements ApiGateway { FakeRetryGateway(this.handler); final RetryHandler 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(); Iterable<({String type, JsonMap fields})> get retries => calls.where((call) => call.type == 'retry_run'); } JsonMap failedRun({ num id = 7, bool executing = false, String status = 'failed', String title = 'Nightly build', }) => {'id': id, 'title': title, 'status': status, 'executing': executing}; JsonMap retriedRun({num id = 8}) => failedRun(id: id, status: 'running', executing: true, title: 'Retry of 7'); /// Owns the run like a run-detail screen would, so the callback's returned /// server detail is what the action renders next. class RetryHost extends StatefulWidget { const RetryHost({ super.key, required this.gateway, required this.initial, required this.changed, }); final FakeRetryGateway gateway; final JsonMap initial; final List changed; @override State createState() => _RetryHostState(); } class _RetryHostState extends State { late JsonMap _run = widget.initial; @override Widget build(BuildContext context) { return RetryRunAction( run: _run, gateway: widget.gateway, onRunChanged: (run) { widget.changed.add(run); setState(() => _run = run); }, ); } } Widget retryApp(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 pumpRetry( WidgetTester tester, { required FakeRetryGateway gateway, required List changed, bool cupertino = false, double textScale = 1, JsonMap? run, }) async { await tester.pumpWidget( retryApp( RetryHost( gateway: gateway, initial: run ?? failedRun(), changed: changed, ), cupertino: cupertino, textScale: textScale, ), ); }