Files
pipelines/app/test/application/templates_wiring_test.dart
T
alan 9e218f755a Consolidate startup and shell on Material
Remove alternate application hosts, page scaffolds, connection controls and icon metadata while preserving Material routing. Isolate navigation fixtures from native update plugins and retain separate startup-update regressions.
2026-11-11 20:11:17 +00:00

369 lines
13 KiB
Dart

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/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';
import '../features/settings/settings_topic_test_helper.dart';
import 'package:pipelines_app/ui/adaptive/segmented_bar.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);
SharedPreferences.setMockInitialValues({});
await tester.pumpWidget(
PipelinesApp(
server: webServer(),
client: client,
// This fixture tests template routing, not native update plugins.
androidSupported: false,
cupertino: false,
routeInformationProvider: provider,
),
);
await tester.pump();
}
Future<void> openSettings(
WidgetTester tester,
FakeChannel channel,
PlatformRouteInformationProvider provider,
) async {
await tester.tap(find.byKey(const Key('pipelines-open-settings')));
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',
});
// Hidden notification settings do not load during this navigation flow.
expect(requestsOf(channel, 'notification_settings'), isEmpty);
await selectSettingsTopic(tester, 'Connection');
}
Future<void> openTemplates(
WidgetTester tester,
FakeChannel channel,
PlatformRouteInformationProvider provider, {
int listRead = 1,
}) async {
await selectSettingsTopic(tester, 'Tools');
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(SegmentedBar), 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));
});
testWidgets(
'Settings opens the embedded Templates page and Back restores it',
(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);
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(SegmentedBar), findsOneWidget);
expect(sockets, 1);
expect(requestsOf(channel, 'list_templates'), hasLength(1));
// Refresh sits in the bar's actions pill, not in the body.
Finder inBar(Key key) => find.descendant(
of: find.byType(SegmentedBar),
matching: find.byKey(key),
);
expect(inBar(templatesRefreshKey), findsOneWidget);
expect(find.byKey(templatesRefreshKey), findsOneWidget);
expect(inBar(templatesPageBackKey), findsOneWidget);
await tester.tap(find.byKey(templateRowKey('release')));
await tester.pump();
await tester.pump();
expect(find.text('Definition source'), findsOneWidget);
// One Back at a time, in the bar's round leading piece: the open
// template's Back replaces the page's.
expect(inBar(templateDetailBackKey), findsOneWidget);
expect(find.byKey(templateDetailBackKey), findsOneWidget);
expect(find.byKey(templatesPageBackKey), findsNothing);
// Detail Back returns to the gallery, then the page Back restores the
// same Settings state.
await tester.tap(find.byKey(templateDetailBackKey));
await tester.pump();
// The gallery hands the bar back to the page's Back on the next frame.
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.byKey(SettingsKeys.templates), findsOneWidget);
await selectSettingsTopic(tester, 'Connection');
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.byKey(SettingsKeys.templates), findsOneWidget);
await selectSettingsTopic(tester, 'Connection');
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();
// The gallery hands the bar back to the page's Back on the next frame.
await tester.pump();
await tester.tap(find.byKey(templatesPageBackKey));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const ValueKey('shell.destination.pipelines')));
await tester.pumpAndSettle();
expect(provider.value.uri.path, '/settings');
// A tab retap preserves Settings; explicit Back returns to Pipelines.
await tester.tap(find.byKey(const Key('shell.back')));
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();
});
}