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

417 lines
13 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/application/app_route.dart';
import 'package:pipelines_app/application/app_router.dart';
import 'package:pipelines_app/application/pipelines_app.dart';
import 'package:pipelines_app/application/search_destination.dart';
import 'package:pipelines_app/application/server_endpoint.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'package:pipelines_app/features/search/search_screen.dart';
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
ServerEndpoint webServer() => ServerEndpoint.resolve(
base: Uri.parse('http://127.0.0.1:8096/'),
web: true,
);
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();
Map<String, dynamic>? requestOf(
FakeChannel channel,
String type, {
int index = 0,
}) {
final requests = requestsOf(channel, type);
return index < requests.length ? requests[index] : null;
}
Future<Map<String, dynamic>> waitForRequest(
WidgetTester tester,
FakeChannel channel,
String type, {
int index = 0,
}) async {
for (var attempt = 0; attempt < 80; attempt++) {
final request = requestOf(channel, type, index: index);
if (request != null) return request;
await tester.pump(const Duration(milliseconds: 5));
}
fail('Timed out waiting for $type #$index');
}
Future<void> reply(
WidgetTester tester,
FakeChannel channel,
String type, {
required Map<String, dynamic> body,
int index = 0,
}) async {
final request = await waitForRequest(tester, channel, type, index: index);
channel.incoming.add(jsonEncode({'id': request['id'], ...body}));
await tester.pump();
await tester.pump();
}
Future<void> replyRunList(WidgetTester tester, FakeChannel channel) => reply(
tester,
channel,
'list_runs',
body: {
'type': 'runs',
'runs': [
{'id': 2, 'name': 'Preserved run', 'status': 'running'},
],
},
);
Future<void> replyRunDetail(
WidgetTester tester,
FakeChannel channel,
int id, {
int index = 0,
}) => reply(
tester,
channel,
'get_run',
index: index,
body: {
'type': 'run',
'run_detail': {'id': id, 'title': 'Run $id', 'status': 'running'},
},
);
JsonMap runHit(int id) => {
'kind': 'run',
'target': 'run:$id',
'run': {
'id': id,
'pipeline_name': 'Preserved run',
'status': 'running',
'workspace': '/workspace/demo',
'created_at': '2026-10-21T12:00:00Z',
},
};
Future<void> pumpApp(
WidgetTester tester, {
required FakeChannel channel,
required PipelinesClient client,
required PlatformRouteInformationProvider provider,
required bool cupertino,
}) async {
await tester.pumpWidget(
PipelinesApp(
server: webServer(),
client: client,
cupertino: cupertino,
routeInformationProvider: provider,
),
);
await tester.pump();
}
void main() {
testWidgets(
'the shell action opens focused Search and Close keeps the open run',
(tester) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final channel = FakeChannel()..readyCompleter.complete();
var sockets = 0;
final client = PipelinesClient(
channelFactory: (_) {
sockets++;
return channel;
},
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/runs/2')),
);
addTearDown(provider.dispose);
await pumpApp(
tester,
channel: channel,
client: client,
provider: provider,
cupertino: false,
);
await replyRunList(tester, channel);
await replyRunDetail(tester, channel, 2);
expect(find.text('Back to list'), findsOneWidget);
// The shell's existing action opens Search without adding a fifth tab.
await tester.tap(find.byKey(const ValueKey('shell.search')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 600));
await tester.pump(const Duration(milliseconds: 100));
expect(provider.value.uri.path, '/search');
expect(find.byType(SearchScreen), findsOneWidget);
expect(find.byKey(const ValueKey('search.field')), findsOneWidget);
final editable = tester.widget<EditableText>(
find.descendant(
of: find.byKey(const ValueKey('search.field')),
matching: find.byType(EditableText),
),
);
expect(editable.focusNode.hasFocus, isTrue);
expect(find.byType(CloseButton), findsOneWidget);
// Closing is the native dialog action, not a second router or modal.
await tester.tap(find.byType(CloseButton));
await tester.pump();
await tester.pump(const Duration(milliseconds: 400));
await tester.pump(const Duration(milliseconds: 400));
expect(provider.value.uri.path, '/runs/2');
expect(find.byType(SearchScreen), findsNothing);
expect(find.text('Back to list'), findsOneWidget);
expect(find.text('Run 2'), findsWidgets);
// The shell was never remounted, so the open run was not re-read.
expect(requestsOf(channel, 'get_run'), hasLength(1));
expect(requestsOf(channel, 'list_runs'), hasLength(1));
expect(sockets, 1);
// Escape is the browser panel's close shortcut on this page too.
await tester.tap(find.byKey(const ValueKey('shell.search')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 600));
await tester.pump(const Duration(milliseconds: 100));
expect(provider.value.uri.path, '/search');
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pump();
await tester.pump(const Duration(milliseconds: 400));
expect(provider.value.uri.path, '/runs/2');
expect(find.text('Back to list'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump();
},
);
testWidgets('a result opens through the existing run route over one socket', (
tester,
) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final channel = FakeChannel()..readyCompleter.complete();
var sockets = 0;
final client = PipelinesClient(
channelFactory: (_) {
sockets++;
return channel;
},
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/runs/2')),
);
addTearDown(provider.dispose);
await pumpApp(
tester,
channel: channel,
client: client,
provider: provider,
cupertino: false,
);
await replyRunList(tester, channel);
await replyRunDetail(tester, channel, 2);
await tester.tap(find.byKey(const ValueKey('shell.search')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 600));
await tester.pump(const Duration(milliseconds: 100));
await tester.enterText(
find.byKey(const ValueKey('search.field')),
'release',
);
await tester.pump(const Duration(milliseconds: 300));
final request = await waitForRequest(tester, channel, 'search');
expect(request['search'], {'query': 'release', 'kind': '', 'limit': 0});
channel.incoming.add(
jsonEncode({
'id': request['id'],
'type': 'search_results',
'search_results': {
'query': 'release',
'kind': '',
'results': [runHit(7)],
'truncated': false,
'scanned_steps': 4,
'scanned_bytes': 512,
'cap': 50,
'scan_bound_bytes': 1 << 20,
},
}),
);
await tester.pump();
await tester.pump();
expect(find.text('run #7 · Preserved run'), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('search.result.run:7')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 400));
await tester.pump(const Duration(milliseconds: 400));
// The hit map's own run id uses the route Runs already owns.
expect(provider.value.uri.path, '/runs/7');
await replyRunDetail(tester, channel, 7, index: 1);
expect(find.byType(SearchScreen), findsNothing);
expect(find.text('Back to list'), findsOneWidget);
expect(find.text('Run 7'), findsWidgets);
// One app socket carried the search and the run reads.
expect(sockets, 1);
expect(requestsOf(channel, 'search'), hasLength(1));
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump();
});
testWidgets('Cupertino uses its native bar action and Back', (tester) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);
final channel = FakeChannel()..readyCompleter.complete();
var sockets = 0;
final client = PipelinesClient(
channelFactory: (_) {
sockets++;
return channel;
},
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/runs')),
);
addTearDown(provider.dispose);
await pumpApp(
tester,
channel: channel,
client: client,
provider: provider,
cupertino: true,
);
await replyRunList(tester, channel);
expect(find.text('Preserved run'), findsWidgets);
await tester.tap(find.byKey(const ValueKey('shell.search')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 600));
await tester.pump(const Duration(milliseconds: 100));
expect(provider.value.uri.path, '/search');
expect(find.byKey(const ValueKey('search.field')), findsOneWidget);
expect(find.byType(Material), findsNothing);
final cupertinoField = tester.widget<EditableText>(
find.descendant(
of: find.byKey(const ValueKey('search.field')),
matching: find.byType(EditableText),
),
);
expect(cupertinoField.focusNode.hasFocus, isTrue);
await tester.tap(find.text('Cancel'));
await tester.pump();
await tester.pump(const Duration(milliseconds: 400));
await tester.pump(const Duration(milliseconds: 400));
expect(provider.value.uri.path, '/runs');
expect(find.byKey(const ValueKey('search.field')), findsNothing);
expect(find.text('Preserved run'), findsWidgets);
expect(sockets, 1);
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump();
});
test(
'the hit helper never guesses a route for pipeline or malformed hits',
() {
expect(searchHitRunId(runHit(3)), 3);
expect(
searchHitRunId({
'kind': 'step',
'step': {'run_id': 8, 'sequence': 2},
}),
8,
);
expect(
searchHitRunId({
'kind': 'field',
'field': {'run_id': '9', 'sequence': 1},
}),
9,
);
expect(
searchHitRunId({
'kind': 'pipeline',
'pipeline': {'id': 4, 'name': 'Alpha'},
}),
isNull,
);
expect(searchHitRunId({'kind': 'artifact', 'artifact': {}}), isNull);
expect(
searchHitRunId({
'kind': 'run',
'run': {'id': 0},
}),
isNull,
);
},
);
test(
'openSearch keeps the previous route and system Back restores it',
() async {
final router = AppRouterDelegate(
builder: (context, route) => const SizedBox.shrink(),
onPopToRuns: () {},
onPopToChat: () {},
cupertino: false,
initial: AppRoute.run(3),
);
addTearDown(router.dispose);
router.openSearch();
expect(router.route, AppRoute.search);
expect(router.searchOpen, isTrue);
expect(await router.popRoute(), isTrue);
expect(router.route, AppRoute.run(3));
expect(router.searchOpen, isFalse);
},
);
test('the /search route is total and survives the parser', () {
expect(AppRoute.parse('/search'), AppRoute.search);
expect(
AppRoute.parse('http://127.0.0.1:8096/search?q=plan'),
AppRoute.search,
);
expect(AppRoute.parse('/search/2'), AppRoute.pipelines);
expect(AppRoute.search.path, '/search');
});
}