feat(app): start a pipeline from the detail view

Native Run action opens the web's prompt/workspace/notify dialog, sends the exact run_pipeline body against the pinned resolved version with one request key per confirmed attempt, keeps refusals in the dialog, reconciles a lost reply by key, and opens only the server-named run through the existing run route with one fresh Runs list read.

Adds a shared failure-reason helper so operators never see Dart runtime prefixes, and records the slice boundary in notes/P3-run.md.
This commit is contained in:
2026-10-22 02:07:49 +01:00
parent 92706a290e
commit 53a8f668ca
24 changed files with 2644 additions and 19 deletions
@@ -0,0 +1,177 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/application/pipelines_app.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'package:pipelines_app/features/pipelines/ui/run_pipeline_wording.dart';
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
import '../features/pipelines/fixtures.dart';
import 'pipelines_app_test.dart' show webServer;
import 'pipelines_replies.dart';
List<Map<String, dynamic>> requestsOf(FakeChannel channel, String type) =>
channel.outgoing
.map((frame) => jsonDecode(frame as String) as Map<String, dynamic>)
.where((frame) => frame['type'] == type)
.toList();
Future<List<Map<String, dynamic>>> waitForCount(
WidgetTester tester,
FakeChannel channel,
String type,
int count,
) async {
for (
var attempt = 0;
attempt < 150 && requestsOf(channel, type).length < count;
attempt++
) {
await tester.pump(const Duration(milliseconds: 10));
}
final requests = requestsOf(channel, type);
expect(
requests.length,
greaterThanOrEqualTo(count),
reason: 'Timed out waiting for $type #$count',
);
return requests;
}
Future<void> reply(
WidgetTester tester,
FakeChannel channel,
String type,
int count,
Map<String, dynamic> body,
) async {
final requests = await waitForCount(tester, channel, type, count);
channel.incoming.add(jsonEncode({'id': requests[count - 1]['id'], ...body}));
await tester.pump();
await tester.pump();
}
void main() {
testWidgets('an accepted launch opens the returned run and re-reads the '
'runs list exactly once', (tester) async {
tester.view.physicalSize = const Size(1100, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final fixture = PipelineFixture();
final channel = FakeChannel()..readyCompleter.complete();
var sockets = 0;
final client = PipelinesClient(
channelFactory: (_) {
sockets++;
return channel;
},
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/pipelines')),
);
addTearDown(provider.dispose);
await tester.pumpWidget(
PipelinesApp(
server: webServer(),
client: client,
cupertino: false,
routeInformationProvider: provider,
),
);
await tester.pump();
await replyPipelines(tester, channel, fixture);
// The pipeline browser's own last-run join read is the boot list read.
expect(requestsOf(channel, 'list_runs'), hasLength(1));
await tester.tap(find.byKey(const Key('pipeline-row-1')));
await tester.pump();
await replyPipelineDetail(tester, channel, fixture);
await reply(tester, channel, 'list_schedules', 1, {
'type': 'schedules',
'schedules': <dynamic>[],
});
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(runPipelinePromptKey),
'Review the change.',
);
await tester.enterText(find.byKey(runPipelineWorkspaceKey), '/work/alpha');
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pump();
final launches = await waitForCount(tester, channel, 'run_pipeline', 1);
final launch = launches.single;
expect(launch['pipeline_id'], 1);
expect(launch['version'], 2);
expect(launch['workspace'], '/work/alpha');
expect(launch['input'], {'prompt': 'Review the change.'});
expect(launch.containsKey('notify_mode'), isFalse);
channel.incoming.add(
jsonEncode({
'id': launch['id'],
'type': 'run_started',
'run_detail': {
'id': 77,
'pipeline_id': 1,
'title': 'Started from the browser',
'status': 'running',
},
}),
);
await tester.pump();
await tester.pump();
expect(provider.value.uri.path, '/runs/77');
// The Runs destination mounted after the accepted launch and performed the
// one fresh list read; no row came from the run reply.
final lists = await waitForCount(tester, channel, 'list_runs', 2);
expect(lists, hasLength(2));
channel.incoming.add(
jsonEncode({
'id': lists[1]['id'],
'type': 'runs',
'runs': [
...fixture.runs,
{
'id': 77,
'pipeline_id': 1,
'title': 'Started from the browser',
'status': 'running',
},
],
}),
);
final detail = await waitForCount(tester, channel, 'get_run', 1);
expect(detail.single['run_id'], 77);
channel.incoming.add(
jsonEncode({
'id': detail.single['id'],
'type': 'run',
'run_detail': {
'id': 77,
'pipeline_id': 1,
'title': 'Started from the browser',
'status': 'running',
},
}),
);
await tester.pump();
await tester.pumpAndSettle();
expect(find.text('Started from the browser'), findsWidgets);
expect(provider.value.uri.path, '/runs/77');
expect(requestsOf(channel, 'list_runs'), hasLength(2));
expect(requestsOf(channel, 'run_pipeline'), hasLength(1));
expect(sockets, 1);
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump();
});
}
+38
View File
@@ -0,0 +1,38 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/error_reason.dart';
import 'package:pipelines_app/core/transport/connection_state.dart';
void main() {
const fallback = 'The server did not do it.';
test('runtime diagnostics are reduced to the reason itself', () {
expect(
describeErrorReason(StateError('Connection closed'), fallback: fallback),
'Connection closed',
);
expect(
describeErrorReason(Exception('Connection closed'), fallback: fallback),
'Connection closed',
);
});
test('protocol and repository messages stay verbatim', () {
expect(
describeErrorReason(
const ProtocolError('workspace is not allowed'),
fallback: fallback,
),
'workspace is not allowed',
);
expect(
describeErrorReason(' spaced reason ', fallback: fallback),
'spaced reason',
);
});
test('a failure with no text uses the honest fallback', () {
expect(describeErrorReason('', fallback: fallback), fallback);
expect(describeErrorReason(StateError(''), fallback: fallback), fallback);
expect(describeErrorReason('Bad state: ', fallback: fallback), fallback);
});
}
@@ -58,6 +58,11 @@ Future<void> _pumpScreen(
Future<void> _openGraphTab(WidgetTester tester, int pipelineId) async {
await tester.tap(find.byKey(Key('pipeline-row-$pipelineId')));
await tester.pumpAndSettle();
// The detail header scrolls when content-driven title/tab height needs it
// (the header gained the native Run action), so reach the tab like an
// operator would instead of requiring it to fit without scrolling.
await tester.ensureVisible(find.text('Graph'));
await tester.pumpAndSettle();
await tester.tap(find.text('Graph'));
await tester.pump();
await tester.pump();
@@ -0,0 +1,507 @@
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/pipelines/screen.dart';
import 'package:pipelines_app/features/pipelines/ui/run_pipeline_wording.dart';
import '../fixtures.dart';
typedef LaunchHandler = FutureOr<JsonMap> Function(JsonMap fields);
typedef LookupHandler = FutureOr<JsonMap> Function(JsonMap fields);
FakeGateway launchGateway(
PipelineFixture fixture, {
required LaunchHandler onLaunch,
LookupHandler? onLookup,
}) => FakeGateway(
(type, fields) => switch (type) {
'list_pipelines' => pipelineListReply(fixture.summaries),
'list_runs' => runListReply(fixture.runs),
'get_pipeline' => pipelineDetailReply(
fixture.detailFor(
fields['pipeline_id'] as int,
fields['version'] as int,
)!,
),
'list_schedules' => {'type': 'schedules', 'schedules': <dynamic>[]},
'run_pipeline' => onLaunch(fields),
'get_run' when onLookup != null => onLookup(fields),
_ => errorReply('unexpected $type'),
},
);
JsonMap acceptedRun(int id) => {
'type': 'run',
'run_detail': {'id': id, 'status': 'running', 'title': 'Run $id'},
};
JsonMap noRun() => {'type': 'run', 'run_detail': null};
Future<void> pumpScreen(
WidgetTester tester,
FakeGateway gateway,
List<int> navigated, {
bool cupertino = false,
}) async {
tester.view.physicalSize = const Size(1100, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final screen = PipelinesScreen(gateway: gateway, onRunStarted: navigated.add);
await tester.pumpWidget(
cupertino ? CupertinoApp(home: screen) : MaterialApp(home: screen),
);
await tester.pumpAndSettle();
}
Future<void> openDetail(WidgetTester tester) async {
await tester.tap(find.byKey(const Key('pipeline-row-1')));
await tester.pumpAndSettle();
}
Future<void> fillFields(
WidgetTester tester, {
String prompt = 'Review the change.',
String workspace = '/work/alpha',
}) async {
await tester.enterText(find.byKey(runPipelinePromptKey), prompt);
await tester.enterText(find.byKey(runPipelineWorkspaceKey), workspace);
await tester.pump();
}
void main() {
testWidgets('the detail Run action sends the web request for the pinned '
'version and opens only the reply run', (tester) async {
final fixture = PipelineFixture();
final navigated = <int>[];
final gateway = launchGateway(
fixture,
onLaunch: (fields) => acceptedRun(77),
);
await pumpScreen(tester, gateway, navigated);
await openDetail(tester);
expect(find.byKey(runPipelineActionKey), findsOneWidget);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
expect(find.text('Run Alpha review'), findsOneWidget);
expect(find.text(runPipelinePromptLabel), findsOneWidget);
expect(find.text(runPipelineWorkspaceLabel), findsOneWidget);
expect(find.text(runPipelineNotifyLabel), findsOneWidget);
expect(find.text(runPipelineCancelLabel), findsOneWidget);
expect(
find.descendant(
of: find.byType(AlertDialog),
matching: find.text(runPipelineConfirmLabel),
),
findsOneWidget,
);
expect(gateway.callsOf('run_pipeline'), isEmpty);
await fillFields(tester, prompt: ' Review the change. ');
await tester.enterText(
find.byKey(runPipelineWorkspaceKey),
'/work/alpha ',
);
await tester.tap(find.byKey(runPipelineNotifyKey));
await tester.pumpAndSettle();
await tester.tap(find.text('Silent').last);
await tester.pumpAndSettle();
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
final fields = gateway.callsOf('run_pipeline').single.fields;
expect(fields, {
'pipeline_id': 1,
'version': 2,
'request_key': fields['request_key'],
'workspace': '/work/alpha',
'input': {'prompt': 'Review the change.'},
'notify_mode': 'off',
});
expect(fields['request_key'], startsWith('app-run-1-2-'));
expect('${fields['request_key']}'.length, lessThanOrEqualTo(128));
expect(navigated, [77]);
expect(find.byType(AlertDialog), findsNothing);
});
testWidgets('a historical version is pinned and Pipeline default omits '
'notify_mode', (tester) async {
final fixture = PipelineFixture();
final navigated = <int>[];
final gateway = launchGateway(
fixture,
onLaunch: (fields) => acceptedRun(78),
);
await pumpScreen(tester, gateway, navigated);
await openDetail(tester);
await tester.tap(find.byKey(const Key('pipeline-version-1')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
await fillFields(tester);
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
final fields = gateway.callsOf('run_pipeline').single.fields;
expect(fields['pipeline_id'], 1);
expect(fields['version'], 1);
expect(fields['request_key'], startsWith('app-run-1-1-'));
expect(fields.containsKey('notify_mode'), isFalse);
expect(fields.containsKey('title'), isFalse);
expect(fields.containsKey('output_format'), isFalse);
expect(navigated, [78]);
});
testWidgets(
'prompt and workspace validation match the web and send nothing',
(tester) async {
final fixture = PipelineFixture();
final navigated = <int>[];
final gateway = launchGateway(
fixture,
onLaunch: (fields) => acceptedRun(79),
);
await pumpScreen(tester, gateway, navigated);
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(find.text(runPipelinePromptRequired), findsOneWidget);
expect(gateway.callsOf('run_pipeline'), isEmpty);
await fillFields(tester, prompt: 'Do work', workspace: 'relative/path');
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(find.text(runPipelineWorkspaceAbsolute), findsOneWidget);
expect(gateway.callsOf('run_pipeline'), isEmpty);
expect(navigated, isEmpty);
// The browser checks the field as typed, so a leading space is not an
// absolute path even though the sent workspace is trimmed.
await fillFields(tester, prompt: 'Do work', workspace: ' /work/alpha');
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(find.text(runPipelineWorkspaceAbsolute), findsOneWidget);
expect(gateway.callsOf('run_pipeline'), isEmpty);
expect(navigated, isEmpty);
await fillFields(tester, prompt: 'Do work', workspace: '/work/alpha ');
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(gateway.callsOf('run_pipeline'), hasLength(1));
expect(
gateway.callsOf('run_pipeline').single.fields['workspace'],
'/work/alpha',
);
expect(navigated, [79]);
},
);
testWidgets('a double confirmation sends exactly one request', (
tester,
) async {
final fixture = PipelineFixture();
final navigated = <int>[];
final pending = Completer<JsonMap>();
final gateway = launchGateway(fixture, onLaunch: (_) => pending.future);
await pumpScreen(tester, gateway, navigated);
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
await fillFields(tester);
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pump();
expect(find.text(runPipelineSendingLabel), findsOneWidget);
expect(gateway.callsOf('run_pipeline'), hasLength(1));
pending.complete(acceptedRun(80));
await tester.pumpAndSettle();
expect(gateway.callsOf('run_pipeline'), hasLength(1));
expect(navigated, [80]);
});
testWidgets('a server refusal keeps the dialog with its own reason and a '
'corrected attempt uses a fresh key', (tester) async {
final fixture = PipelineFixture();
final navigated = <int>[];
var attempts = 0;
final keys = <String>[];
final gateway = launchGateway(
fixture,
onLaunch: (fields) {
attempts++;
keys.add(fields['request_key'] as String);
if (attempts == 1) {
throw const ProtocolError('workspace is not allowed');
}
return acceptedRun(81);
},
onLookup: (_) => noRun(),
);
await pumpScreen(tester, gateway, navigated);
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
await fillFields(tester);
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text('workspace is not allowed'), findsOneWidget);
expect(find.text(runPipelineCheckRetryLabel), findsNothing);
expect(navigated, isEmpty);
// The refusal was proven not admitted, so the fields unlock and a new
// deliberate attempt mints its own key.
await tester.enterText(
find.byKey(runPipelineWorkspaceKey),
'/work/allowed',
);
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(keys, hasLength(2));
expect(keys[0], isNot(equals(keys[1])));
expect(navigated, [81]);
expect(find.byType(AlertDialog), findsNothing);
});
testWidgets('a lost reply is honest and the same key reconciles before one '
'resend', (tester) async {
final fixture = PipelineFixture();
final navigated = <int>[];
final keys = <String>[];
final lookups = <String>[];
var attempts = 0;
final gateway = launchGateway(
fixture,
onLaunch: (fields) {
attempts++;
keys.add(fields['request_key'] as String);
if (attempts == 1) throw StateError('Connection closed');
return acceptedRun(82);
},
onLookup: (fields) {
lookups.add(fields['request_key'] as String);
return noRun();
},
);
await pumpScreen(tester, gateway, navigated);
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
await fillFields(tester);
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.textContaining('Admission may be unknown'), findsOneWidget);
expect(find.textContaining('Bad state'), findsNothing);
expect(
find.text('Connection closed$runPipelineUnknownSuffix'),
findsOneWidget,
);
expect(find.text(runPipelineCheckRetryLabel), findsOneWidget);
expect(navigated, isEmpty);
// The fields stay locked while admission is unknown.
final prompt = tester.widget<TextField>(find.byKey(runPipelinePromptKey));
expect(prompt.enabled, isFalse);
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(lookups, hasLength(1));
expect(lookups.single, keys.first);
expect(keys, hasLength(2));
expect(keys[1], keys[0]);
expect(navigated, [82]);
expect(find.byType(AlertDialog), findsNothing);
});
testWidgets('an admitted run found by key opens without a second launch', (
tester,
) async {
final fixture = PipelineFixture();
final navigated = <int>[];
var attempts = 0;
final gateway = launchGateway(
fixture,
onLaunch: (fields) {
attempts++;
throw StateError('Connection closed');
},
onLookup: (_) => acceptedRun(83),
);
await pumpScreen(tester, gateway, navigated);
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
await fillFields(tester);
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(attempts, 1);
expect(gateway.callsOf('run_pipeline'), hasLength(1));
expect(navigated, [83]);
});
testWidgets('a refusal whose by-key read finds an admitted run opens it', (
tester,
) async {
final fixture = PipelineFixture();
final navigated = <int>[];
final gateway = launchGateway(
fixture,
onLaunch: (_) => throw const ProtocolError('workspace is not allowed'),
onLookup: (_) => acceptedRun(85),
);
await pumpScreen(tester, gateway, navigated);
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
await fillFields(tester);
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(gateway.callsOf('run_pipeline'), hasLength(1));
expect(navigated, [85]);
expect(find.byType(AlertDialog), findsNothing);
});
testWidgets('closing and reopening an unknown launch restores check/retry', (
tester,
) async {
final fixture = PipelineFixture();
final navigated = <int>[];
final keys = <String>[];
var attempts = 0;
final gateway = launchGateway(
fixture,
onLaunch: (fields) {
attempts++;
keys.add(fields['request_key'] as String);
if (attempts == 1) throw StateError('Connection closed');
return acceptedRun(86);
},
onLookup: (_) => noRun(),
);
await pumpScreen(tester, gateway, navigated);
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
await fillFields(tester);
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(find.text(runPipelineCheckRetryLabel), findsOneWidget);
// Cancel only closes the dialog; the unresolved intent stays with it.
await tester.tap(find.byKey(runPipelineCancelKey));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
expect(find.text(runPipelineUnknownAdmission), findsOneWidget);
expect(find.text(runPipelineCheckRetryLabel), findsOneWidget);
expect(
tester
.widget<TextField>(find.byKey(runPipelinePromptKey))
.controller
?.text,
'Review the change.',
);
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(keys, hasLength(2));
expect(keys[1], keys[0]);
expect(navigated, [86]);
});
testWidgets('the Cupertino host shows the same native prompt and words', (
tester,
) async {
final fixture = PipelineFixture();
final navigated = <int>[];
final gateway = launchGateway(
fixture,
onLaunch: (fields) => acceptedRun(84),
);
await pumpScreen(tester, gateway, navigated, cupertino: true);
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
expect(find.text('Run Alpha review'), findsOneWidget);
expect(find.text(runPipelinePromptLabel), findsOneWidget);
expect(find.text(runPipelineWorkspaceLabel), findsOneWidget);
expect(find.text(runPipelineNotifyLabel), findsOneWidget);
expect(
find.descendant(
of: find.byType(CupertinoAlertDialog),
matching: find.text(runPipelineConfirmLabel),
),
findsOneWidget,
);
expect(find.text(runPipelineCancelLabel), findsOneWidget);
expect(gateway.callsOf('run_pipeline'), isEmpty);
await tester.tap(find.byKey(runPipelineCancelKey));
await tester.pumpAndSettle();
expect(find.byType(CupertinoAlertDialog), findsNothing);
expect(gateway.callsOf('run_pipeline'), isEmpty);
});
testWidgets('Cupertino field labels and helper use theme label roles', (
tester,
) async {
final fixture = PipelineFixture();
final gateway = launchGateway(
fixture,
onLaunch: (fields) => acceptedRun(87),
);
await pumpScreen(tester, gateway, <int>[], cupertino: true);
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
final context = tester.element(find.byType(CupertinoAlertDialog));
final theme = CupertinoTheme.of(context);
final labelStyle = theme.textTheme.textStyle;
for (final label in [
runPipelinePromptLabel,
runPipelineWorkspaceLabel,
runPipelineNotifyLabel,
]) {
final style = tester.widget<Text>(find.text(label)).style;
expect(style?.color, labelStyle.color, reason: label);
expect(style?.fontSize, labelStyle.fontSize, reason: label);
}
final helper = tester
.widget<Text>(find.text(runPipelineWorkspaceHelper))
.style;
expect(
helper?.color,
CupertinoColors.secondaryLabel.resolveFrom(context),
);
expect(
helper?.fontSize,
theme.textTheme.tabLabelTextStyle.fontSize,
);
});
}
@@ -0,0 +1,70 @@
import 'dart:async';
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 listRequests = <Completer<JsonMap>>[];
final detailRequests = <num>[];
@override
Stream<JsonMap> get frames => const Stream.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'list_runs') {
final request = Completer<JsonMap>();
listRequests.add(request);
return request.future;
}
if (type == 'get_run') {
detailRequests.add(fields['run_id'] as num);
return Completer<JsonMap>().future;
}
return Future.error(UnsupportedError(type));
}
}
Widget _screen(_Gateway gateway, int token) => MaterialApp(
home: Scaffold(
body: RunsScreen(gateway: gateway, listRefreshToken: token),
),
);
void main() {
testWidgets('an accepted launch invalidates a mounted list with one fresh '
'read and no detail read', (tester) async {
final gateway = _Gateway();
await tester.pumpWidget(_screen(gateway, 0));
await tester.pump();
expect(gateway.listRequests, hasLength(1));
gateway.listRequests[0].complete({
'runs': [
{'id': 1, 'title': 'Old run', 'status': 'succeeded'},
],
});
await tester.pump();
await tester.pump();
expect(find.text('Old run'), findsOneWidget);
await tester.pumpWidget(_screen(gateway, 1));
await tester.pump();
expect(gateway.listRequests, hasLength(2));
// No row is invented while the fresh read is outstanding.
expect(find.text('New run'), findsNothing);
gateway.listRequests[1].complete({
'runs': [
{'id': 2, 'title': 'New run', 'status': 'running'},
{'id': 1, 'title': 'Old run', 'status': 'succeeded'},
],
});
await tester.pump();
await tester.pump();
expect(find.text('New run'), findsOneWidget);
expect(gateway.listRequests, hasLength(2));
expect(gateway.detailRequests, isEmpty);
});
}