Files
pipelines/app/test/features/pipelines/ui/run_pipeline_launch_test.dart
T

680 lines
23 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:flutter/cupertino.dart';
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/core/transport/connection_state.dart';
import 'package:pipelines_app/features/pipelines/screen.dart';
import 'package:pipelines_app/features/pipelines/pending_run_store.dart';
import 'package:pipelines_app/features/pipelines/run_pipeline_intent.dart';
import 'package:pipelines_app/features/pipelines/ui/run_pipeline_wording.dart';
import 'package:pipelines_app/features/schedules/ui/editor/schedule_editor_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>[]},
'chat_directories' => {
'type': 'chat_directories',
'chat_directories': ['/work/recent', 'relative', '/work/other'],
},
'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() {
TestWidgetsFlutterBinding.ensureInitialized();
SharedPreferences.setMockInitialValues({});
testWidgets('pending launch intent is scoped to its server endpoint', (
tester,
) async {
const intent = RunPipelineIntent(
pipelineId: 1,
version: 2,
prompt: 'pending',
workspace: '/work/alpha',
notifyMode: RunNotifyMode.notify,
requestKey: 'endpoint-intent',
);
SharedPreferences.setMockInitialValues({
pendingRunStorageKey('ws://server-a/connect', 1, 2): jsonEncode(
intent.toJson(),
),
});
final fixture = PipelineFixture();
final gateway = launchGateway(fixture, onLaunch: (_) => noRun());
await tester.pumpWidget(
MaterialApp(
home: PipelinesScreen(
gateway: gateway,
endpoint: 'ws://server-b/connect',
),
),
);
await tester.pumpAndSettle();
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
expect(find.text(runPipelineCheckRetryLabel), findsNothing);
await tester.tap(find.byKey(runPipelineCancelKey));
await tester.pumpAndSettle();
await tester.pumpWidget(
MaterialApp(
home: PipelinesScreen(
gateway: gateway,
endpoint: 'ws://server-a/connect',
),
),
);
await tester.pumpAndSettle();
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
expect(find.text(runPipelineCheckRetryLabel), findsOneWidget);
});
testWidgets(
'workspace directory suggestions load on open and selection is local',
(tester) async {
final fixture = PipelineFixture();
final gateway = launchGateway(fixture, onLaunch: (_) => acceptedRun(76));
await pumpScreen(tester, gateway, <int>[]);
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
expect(gateway.callsOf('chat_directories'), hasLength(1));
expect(find.text(runPipelineDirectoriesLoading), findsNothing);
expect(
tester
.widget<TextField>(find.byKey(runPipelineWorkspaceKey))
.controller
?.text,
'/work/recent',
);
await tester.tap(find.byKey(runPipelineWorkspaceSuggestionsKey));
await tester.pumpAndSettle();
expect(find.text('/work/other'), findsOneWidget);
expect(find.text('relative'), findsNothing);
await tester.tap(find.text('/work/other'));
await tester.pumpAndSettle();
expect(
tester
.widget<TextField>(find.byKey(runPipelineWorkspaceKey))
.controller
?.text,
'/work/other',
);
expect(gateway.callsOf('run_pipeline'), isEmpty);
expect(gateway.callsOf('chat_directories'), hasLength(1));
},
);
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(runPipelineOutputFormatLabel), findsOneWidget);
expect(find.text(runPipelineOutputFormatHelper), 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(runPipelineOutputFormatKey));
await tester.pumpAndSettle();
await tester.tap(find.text('HTML report').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',
'output_format': 'html',
});
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);
expect(
tester
.widget<DropdownButtonFormField<dynamic>>(
find.byKey(runPipelineOutputFormatKey),
)
.onChanged,
isNull,
);
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('an unknown launch survives a reload with the same key', (
tester,
) async {
SharedPreferences.setMockInitialValues({});
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(87);
},
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);
// A reload: the screen and its controller are gone; only the device
// store remains.
await tester.pumpWidget(const SizedBox());
await pumpScreen(tester, gateway, navigated);
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
expect(find.text(runPipelineCheckRetryLabel), findsOneWidget);
await tester.tap(find.byKey(runPipelineConfirmKey));
await tester.pumpAndSettle();
expect(keys, hasLength(2));
expect(keys[1], keys[0]);
expect(navigated, [87]);
});
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);
});
testWidgets('Schedule… leaves the run dialog for the schedule editor', (
tester,
) async {
final fixture = PipelineFixture();
var launches = 0;
final gateway = launchGateway(
fixture,
onLaunch: (_) {
launches++;
return acceptedRun(9);
},
);
await pumpScreen(tester, gateway, []);
await openDetail(tester);
await tester.tap(find.byKey(runPipelineActionKey));
await tester.pumpAndSettle();
await fillFields(tester, prompt: 'Nightly review');
await tester.tap(find.byKey(const Key('run-pipeline-schedule')));
await tester.pumpAndSettle();
// The editor is the next page, seeded with the typed prompt; no run left.
expect(find.byKey(runPipelineConfirmKey), findsNothing);
expect(find.byKey(scheduleEditorSubmitKey), findsOneWidget);
expect(find.text('Nightly review'), findsWidgets);
expect(launches, 0);
});
}