Files
pipelines/app/test/application/templates_wiring_test.dart
T

350 lines
11 KiB
Dart

import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/application/pipelines_app.dart';
import 'package:pipelines_app/application/templates_destination.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'package:pipelines_app/features/settings/settings_keys.dart';
import 'package:pipelines_app/features/templates/screen.dart';
import 'package:pipelines_app/features/templates/ui/template_create_wording.dart';
import 'package:pipelines_app/features/templates/ui/template_detail.dart'
show templateDetailBackKey;
import 'package:shared_preferences/shared_preferences.dart';
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
import '../features/pipelines/fixtures.dart';
import '../features/templates/ui/template_gallery_test_support.dart'
show GalleryGateway, galleryTemplates, pumpGallery, templatesReply;
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 < 100 && 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();
}
Future<void> pumpApp(
WidgetTester tester, {
required FakeChannel channel,
required PipelinesClient client,
required PlatformRouteInformationProvider provider,
required bool cupertino,
}) async {
tester.view.physicalSize = const Size(1100, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
if (cupertino) {
tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);
}
SharedPreferences.setMockInitialValues({});
await tester.pumpWidget(
PipelinesApp(
server: webServer(),
client: client,
cupertino: cupertino,
routeInformationProvider: provider,
),
);
await tester.pump();
}
Future<void> openSettings(
WidgetTester tester,
FakeChannel channel,
PlatformRouteInformationProvider provider,
) async {
await tester.tap(find.text('Settings').last);
await tester.pump();
await tester.pump();
expect(provider.value.uri.path, '/settings');
// Settings pings the shared socket; answer it so later reads are not queued.
await reply(tester, channel, 'ping', 1, {
'type': 'pong',
'build': {'version': 'probe'},
'time': 'fixture',
});
}
Future<void> openTemplates(
WidgetTester tester,
FakeChannel channel,
PlatformRouteInformationProvider provider, {
int listRead = 1,
}) async {
await tester.ensureVisible(find.byKey(SettingsKeys.templates));
await tester.tap(find.byKey(SettingsKeys.templates));
await tester.pump();
await tester.pump(const Duration(milliseconds: 600));
expect(provider.value.uri.path, '/templates');
await reply(tester, channel, 'list_templates', listRead, {
'type': 'templates',
'templates': [
{
'key': 'release',
'name': 'Release workflow',
'description': 'Ship a version',
'source': 'name: "release"\n',
'scheduled': true,
},
],
});
await reply(tester, channel, 'validate', listRead, {
'type': 'graph',
'graph': {'stages': <dynamic>[]},
});
}
void main() {
testWidgets('non-embedded TemplatesScreen keeps its own native bar', (
tester,
) async {
final gateway = GalleryGateway(
(type, fields) async => templatesReply(galleryTemplates),
);
await pumpGallery(tester, gateway);
expect(find.byType(AppBar), findsOneWidget);
await tester.tap(find.byKey(templateRowKey('release')));
await tester.pump();
expect(find.text('Definition source'), findsOneWidget);
expect(find.byKey(templatesBackKey), findsOneWidget);
expect(gateway.callsOf('list_templates'), hasLength(1));
});
for (final cupertino in [false, true]) {
testWidgets(
'Settings opens the embedded Templates page and Back restores it '
'($cupertino)',
(tester) async {
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 pumpApp(
tester,
channel: channel,
client: client,
provider: provider,
cupertino: cupertino,
);
await replyPipelines(tester, channel, fixture);
await openSettings(tester, channel, provider);
// An edit that only Settings owns must survive the Templates detour.
await tester.enterText(find.byType(EditableText).first, 'edited url');
await tester.pump();
expect(find.text('edited url'), findsOneWidget);
await openTemplates(tester, channel, provider);
final screen = tester.widget<TemplatesScreen>(
find.byType(TemplatesScreen),
);
expect(screen.embedded, isTrue);
expect(find.text('Release workflow'), findsOneWidget);
// One visible navigation bar: the Templates page owns it, the shell is
// preserved underneath.
expect(
find.byType(cupertino ? CupertinoNavigationBar : AppBar),
findsOneWidget,
);
if (cupertino) {
expect(find.byType(Material), findsNothing);
}
expect(sockets, 1);
expect(requestsOf(channel, 'list_templates'), hasLength(1));
await tester.tap(find.byKey(templateRowKey('release')));
await tester.pump();
expect(find.text('Definition source'), findsOneWidget);
// Detail Back returns to the gallery, then the page Back restores the
// same Settings state.
await tester.tap(find.byKey(templateDetailBackKey));
await tester.pump();
await tester.tap(find.byKey(templatesPageBackKey));
await tester.pumpAndSettle();
expect(provider.value.uri.path, '/settings');
expect(find.byType(TemplatesScreen), findsNothing);
expect(find.text('edited url'), findsOneWidget);
expect(find.text('Server URL'), findsOneWidget);
expect(sockets, 1);
expect(requestsOf(channel, 'ping'), hasLength(1));
// Browser Back from /templates follows the same close path.
await openTemplates(tester, channel, provider, listRead: 2);
expect(find.byType(TemplatesScreen), findsOneWidget);
await provider.didPushRouteInformation(
RouteInformation(uri: Uri.parse('/settings')),
);
await tester.pumpAndSettle();
expect(provider.value.uri.path, '/settings');
expect(find.byType(TemplatesScreen), findsNothing);
expect(find.text('edited url'), findsOneWidget);
expect(requestsOf(channel, 'list_templates'), hasLength(2));
expect(sockets, 1);
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump();
},
);
}
testWidgets('an accepted create re-reads Pipelines exactly once', (
tester,
) async {
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 pumpApp(
tester,
channel: channel,
client: client,
provider: provider,
cupertino: false,
);
await replyPipelines(tester, channel, fixture);
expect(
find.byKey(const Key('pipeline-row-41'), skipOffstage: false),
findsNothing,
);
await openSettings(tester, channel, provider);
await openTemplates(tester, channel, provider);
await tester.tap(find.byKey(templateRowKey('release')));
await tester.pump();
await tester.tap(find.byKey(templateCreateActionKey));
await tester.pump();
await tester.enterText(find.byKey(templateCreateNameKey), 'Probe release');
await tester.pump();
await tester.tap(find.byKey(templateCreateConfirmKey));
await tester.pump();
await reply(tester, channel, 'validate', 2, {
'type': 'graph',
'graph': {'stages': <dynamic>[]},
});
await reply(tester, channel, 'submit', 1, {
'type': 'pipeline',
'detail': {'id': 41, 'name': 'Probe release'},
});
expect(
find.text('Created "Probe release". It is in the Pipelines list.'),
findsOneWidget,
);
// The detail itself never adds a row: the hidden list is still the old one
// until the fresh server reply arrives.
final refreshes = await waitForCount(tester, channel, 'list_pipelines', 2);
expect(
find.byKey(const Key('pipeline-row-41'), skipOffstage: false),
findsNothing,
);
channel.incoming.add(
jsonEncode({
'id': refreshes[1]['id'],
'type': 'pipelines',
'pipelines': [
...fixture.summaries,
pipelineSummary(id: 41, name: 'Probe release'),
],
}),
);
await tester.pump();
await reply(tester, channel, 'list_runs', 2, {
'type': 'runs',
'runs': fixture.runs,
});
expect(
find.byKey(const Key('pipeline-row-41'), skipOffstage: false),
findsOneWidget,
);
await tester.tap(find.byKey(templateCreateCancelKey));
await tester.pump();
await tester.tap(find.byKey(templateDetailBackKey));
await tester.pump();
await tester.tap(find.byKey(templatesPageBackKey));
await tester.pumpAndSettle();
await tester.tap(find.text('Pipelines').last);
await tester.pumpAndSettle();
expect(provider.value.uri.path, '/pipelines');
expect(find.text('Probe release'), findsOneWidget);
// Only the accepted detail's invalidation re-read the list; showing it
// again adds no request.
expect(requestsOf(channel, 'list_pipelines'), hasLength(2));
expect(requestsOf(channel, 'list_runs'), hasLength(2));
// One validation derives the gallery count; the other belongs to Create.
expect(requestsOf(channel, 'validate'), hasLength(2));
expect(requestsOf(channel, 'submit'), hasLength(1));
expect(sockets, 1);
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump();
});
}