feat(app): compose live Runs with deep links and reconnect retention

This commit is contained in:
2026-10-21 17:26:36 +01:00
parent 38b7a849a7
commit 35a8aab772
34 changed files with 3038 additions and 128 deletions
+52
View File
@@ -0,0 +1,52 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/application/app_route.dart';
void main() {
test('parses each destination and a run detail', () {
expect(AppRoute.parse('/pipelines'), AppRoute.pipelines);
expect(AppRoute.parse('/runs'), AppRoute.runs);
expect(AppRoute.parse('/runs/12'), AppRoute.run(12));
expect(AppRoute.parse('/chat'), AppRoute.chat);
expect(AppRoute.parse('/settings'), AppRoute.settings);
});
test('normalises slashes and accepts a full URL, query and fragment', () {
expect(AppRoute.parse('/runs/'), AppRoute.runs);
expect(AppRoute.parse('/pipelines///'), AppRoute.pipelines);
expect(
AppRoute.parse('http://127.0.0.1:8096/runs/7?action=decide#top'),
AppRoute.run(7),
);
expect(AppRoute.parse('/runs/7/'), AppRoute.run(7));
});
test('a malformed or unknown location is the default Pipelines route', () {
for (final path in [
'',
'/',
'/runs/abc',
'/runs/0',
'/runs/-1',
'/runs/1/extra',
'/pipelines/2',
'/chat/session',
'/unknown',
'/runs/%E0%A4%A',
]) {
expect(AppRoute.parse(path), AppRoute.pipelines, reason: path);
}
});
test('serialises every route back to a parsable path', () {
for (final route in [
AppRoute.pipelines,
AppRoute.runs,
AppRoute.run(3),
AppRoute.chat,
AppRoute.settings,
]) {
expect(AppRoute.parse(route.path), route);
}
expect(AppRoute.run(3).path, '/runs/3');
});
}
+148
View File
@@ -0,0 +1,148 @@
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/application/app_session.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
Future<void> flush() => Future<void>.delayed(Duration.zero);
Future<void> waitFor(bool Function() condition, String reason) async {
for (var attempt = 0; attempt < 200; attempt++) {
if (condition()) return;
await Future<void>.delayed(const Duration(milliseconds: 5));
}
fail('Timed out: $reason');
}
List<String> types(FakeChannel channel) => [
for (final frame in channel.outgoing)
(jsonDecode(frame as String) as Map<String, dynamic>)['type'] as String,
];
void main() {
test(
'start subscribes once, stop unsubscribes, and the app owns the socket',
() async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final session = AppSession(
endpoint: Uri.parse('ws://example.test/connect'),
client: client,
);
await session.start();
await flush();
expect(session.connected, isTrue);
expect(types(channel), ['subscribe']);
final frame = jsonDecode(channel.outgoing.single as String) as Map;
expect(frame['workspace'], '');
await session.stop();
await flush();
expect(types(channel), ['subscribe', 'unsubscribe']);
expect(session.connected, isFalse);
session.dispose();
},
);
test('reconnect subscribes the new socket again', () async {
final first = FakeChannel()..readyCompleter.complete();
final second = FakeChannel()..readyCompleter.complete();
var sockets = 0;
final client = PipelinesClient(
channelFactory: (_) => sockets++ == 0 ? first : second,
backoffBase: const Duration(milliseconds: 5),
);
final session = AppSession(
endpoint: Uri.parse('ws://example.test/connect'),
client: client,
);
addTearDown(session.dispose);
await session.start();
await flush();
expect(types(first), ['subscribe']);
expect(session.connectionEpoch, 1);
first.incoming.addError(StateError('network drop'));
await waitFor(
() => sockets == 2 && session.connected && second.outgoing.isNotEmpty,
'reconnected socket did not subscribe',
);
expect(types(second), ['subscribe']);
expect(session.connectionEpoch, 2);
});
test('a subscribe ack marks the watch live, a refusal reports it', () async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final session = AppSession(
endpoint: Uri.parse('ws://example.test/connect'),
client: client,
);
addTearDown(session.dispose);
await session.start();
await flush();
final subscribe =
jsonDecode(channel.outgoing.single as String) as Map<String, dynamic>;
expect(session.subscription, RunSubscriptionState.pending);
channel.incoming.add(
jsonEncode({'id': subscribe['id'], 'type': 'subscribe'}),
);
await flush();
expect(session.subscription, RunSubscriptionState.live);
expect(session.subscriptionError, isEmpty);
session.resubscribe();
await flush();
final repeated =
jsonDecode(channel.outgoing.last as String) as Map<String, dynamic>;
expect(repeated['type'], 'subscribe');
channel.incoming.add(
jsonEncode({
'id': repeated['id'],
'type': 'error',
'error': 'too many run subscriptions on this connection',
}),
);
await flush();
expect(session.subscription, RunSubscriptionState.refused);
expect(session.subscriptionError, contains('too many run subscriptions'));
});
test('dispose drops the watch and releases the client', () async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final session = AppSession(
endpoint: Uri.parse('ws://example.test/connect'),
client: client,
);
await session.start();
await flush();
session.dispose();
await flush();
expect(types(channel), ['subscribe', 'unsubscribe']);
await expectLater(
client.connect(Uri.parse('ws://example.test/connect')),
throwsStateError,
);
});
test('a missing endpoint is not replaced by a guessed connection', () async {
final channel = FakeChannel();
final client = PipelinesClient(channelFactory: (_) => channel);
final session = AppSession(endpoint: null, client: client);
addTearDown(session.dispose);
await session.start();
await flush();
expect(channel.outgoing, isEmpty);
expect(session.connected, isFalse);
});
}
@@ -0,0 +1,177 @@
import 'dart:convert';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/application/pipelines_app.dart';
import 'package:pipelines_app/application/server_endpoint.dart';
import 'package:pipelines_app/core/transport/pipelines_client.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,
);
Map<String, dynamic>? requestOf(FakeChannel channel, String type) => channel
.outgoing
.map((frame) => jsonDecode(frame as String) as Map<String, dynamic>)
.where((frame) => frame['type'] == type)
.firstOrNull;
Future<void> pumpApp(
WidgetTester tester, {
required FakeChannel channel,
required PipelinesClient client,
required PlatformRouteInformationProvider provider,
}) async {
await tester.pumpWidget(
PipelinesApp(
server: webServer(),
client: client,
cupertino: false,
routeInformationProvider: provider,
),
);
await tester.pump();
}
Future<void> replyList(
WidgetTester tester,
FakeChannel channel, {
required List<Map<String, dynamic>> runs,
}) async {
for (
var attempt = 0;
attempt < 40 && requestOf(channel, 'list_runs') == null;
attempt++
) {
await tester.pump(const Duration(milliseconds: 5));
}
final request = requestOf(channel, 'list_runs')!;
channel.incoming.add(
jsonEncode({'id': request['id'], 'type': 'runs', 'runs': runs}),
);
await tester.pump();
await tester.pump();
}
Future<void> replyDetail(
WidgetTester tester,
FakeChannel channel,
num id,
) async {
for (
var attempt = 0;
attempt < 40 && requestOf(channel, 'get_run') == null;
attempt++
) {
await tester.pump(const Duration(milliseconds: 5));
}
final request = requestOf(channel, 'get_run')!;
expect(request['run_id'], id);
channel.incoming.add(
jsonEncode({
'id': request['id'],
'type': 'run',
'run_detail': {'id': id, 'title': 'Run $id', 'status': 'running'},
}),
);
await tester.pump();
await tester.pump();
}
void main() {
testWidgets('a /runs/1 boot link opens that run and Back returns to /runs', (
tester,
) async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/runs/1')),
);
addTearDown(provider.dispose);
await pumpApp(tester, channel: channel, client: client, provider: provider);
await replyList(
tester,
channel,
runs: [
{'id': 1, 'name': 'Deep-linked run', 'status': 'running'},
],
);
await replyDetail(tester, channel, 1);
expect(find.text('Run 1'), findsWidgets);
expect(find.text('Back to list'), findsOneWidget);
expect(provider.value.uri.path, '/runs/1');
expect(find.text('Pipelines — Not implemented'), findsNothing);
await tester.tap(find.text('Back to list'));
await tester.pump();
expect(provider.value.uri.path, '/runs');
expect(find.text('Deep-linked run'), findsWidgets);
});
testWidgets('tabs and run selection update the route; browser Back pops it', (
tester,
) async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/pipelines')),
);
addTearDown(provider.dispose);
await pumpApp(tester, channel: channel, client: client, provider: provider);
expect(provider.value.uri.path, '/pipelines');
await tester.tap(find.text('Runs').last);
await tester.pump();
expect(provider.value.uri.path, '/runs');
await replyList(
tester,
channel,
runs: [
{'id': 2, 'name': 'Beta run', 'status': 'failed'},
],
);
await tester.tap(find.text('Beta run'));
await tester.pump();
expect(provider.value.uri.path, '/runs/2');
await replyDetail(tester, channel, 2);
expect(find.text('Back to list'), findsOneWidget);
// The engine's browser Back arrives as new route information.
await provider.didPushRouteInformation(
RouteInformation(uri: Uri.parse('/runs')),
);
await tester.pump();
expect(find.text('Beta run'), findsWidgets);
expect(find.text('Back to list'), findsNothing);
// Forward re-opens the same run.
await provider.didPushRouteInformation(
RouteInformation(uri: Uri.parse('/runs/2')),
);
await tester.pump();
await replyDetail(tester, channel, 2);
expect(find.text('Back to list'), findsOneWidget);
});
testWidgets('a malformed boot path is corrected to /pipelines', (
tester,
) async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/runs/abc')),
);
addTearDown(provider.dispose);
await pumpApp(tester, channel: channel, client: client, provider: provider);
expect(provider.value.uri.path, '/pipelines');
expect(find.text('Pipelines — Not implemented'), findsOneWidget);
});
}
@@ -0,0 +1,302 @@
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/app_session.dart';
import 'package:pipelines_app/application/runs_destination.dart';
import 'package:pipelines_app/application/server_endpoint.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'package:pipelines_app/features/runs/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,
);
Widget materialApp(
AppSession session,
ServerEndpoint server, {
int? initialRunId,
ValueChanged<int>? onRunSelected,
VoidCallback? onBack,
}) => MaterialApp(
home: Scaffold(
body: RunsDestination(
session: session,
server: server,
initialRunId: initialRunId,
onRunSelected: onRunSelected,
onBack: onBack,
),
),
);
Map<String, dynamic>? listRequest(FakeChannel channel) => channel.outgoing
.map((frame) => jsonDecode(frame as String) as Map<String, dynamic>)
.where((frame) => frame['type'] == 'list_runs')
.firstOrNull;
Iterable<Map<String, dynamic>> detailRequests(FakeChannel channel, int id) =>
channel.outgoing
.map((frame) => jsonDecode(frame as String) as Map<String, dynamic>)
.where((frame) => frame['type'] == 'get_run' && frame['run_id'] == id);
void main() {
testWidgets('gate explains missing native configuration without connecting', (
tester,
) async {
final channel = FakeChannel();
final client = PipelinesClient(channelFactory: (_) => channel);
final server = ServerEndpoint.resolve(
base: Uri.parse('http://127.0.0.1:8096/'),
web: false,
);
final session = AppSession(endpoint: server.uri, client: client);
addTearDown(session.dispose);
await tester.pumpWidget(materialApp(session, server));
expect(find.text('Server endpoint not configured'), findsOneWidget);
expect(find.textContaining('PIPELINES_SERVER_URL'), findsOneWidget);
expect(find.byType(RunsScreen), findsNothing);
expect(channel.outgoing, isEmpty);
});
testWidgets('gate shows connecting, then mounts the normal runs screen', (
tester,
) async {
final channel = FakeChannel();
final client = PipelinesClient(channelFactory: (_) => channel);
final server = webServer();
final session = AppSession(endpoint: server.uri, client: client);
addTearDown(session.dispose);
final starting = session.start();
await tester.pumpWidget(materialApp(session, server));
await tester.pump();
expect(find.text('Connecting to Pipelines…'), findsOneWidget);
expect(find.byType(CircularProgressIndicator), findsOneWidget);
expect(find.byType(RunsScreen), findsNothing);
channel.readyCompleter.complete();
await starting;
await tester.pump();
await tester.pump();
expect(find.byType(RunsScreen), findsOneWidget);
final list = listRequest(channel)!;
channel.incoming.add(
jsonEncode({
'id': list['id'],
'type': 'runs',
'runs': [
{'id': 5, 'name': 'Live fixture run', 'status': 'succeeded'},
],
}),
);
await tester.pump();
expect(find.text('Live fixture run'), findsOneWidget);
});
testWidgets('a refused socket shows a friendly reason, never the exception', (
tester,
) async {
final channel = FakeChannel();
final client = PipelinesClient(
channelFactory: (_) => channel,
backoffBase: const Duration(hours: 1),
backoffMax: const Duration(hours: 1),
);
final server = webServer();
final session = AppSession(endpoint: server.uri, client: client);
addTearDown(session.dispose);
final starting = session.start();
channel.readyCompleter.completeError(
StateError('WebSocketChannelException: Failed to connect'),
);
await starting;
await tester.pumpWidget(materialApp(session, server));
await tester.pump();
expect(find.text('Pipelines server unavailable'), findsOneWidget);
expect(find.textContaining('Retrying automatically'), findsOneWidget);
expect(find.textContaining('WebSocketChannelException'), findsNothing);
expect(find.widgetWithText(FilledButton, 'Retry'), findsOneWidget);
expect(find.byType(RunsScreen), findsNothing);
// The client keeps retrying by design; end the retry loop explicitly so
// the test does not leave a wall-clock timer behind.
session.dispose();
});
testWidgets(
'a drop keeps the list and reports reconnecting, not unavailable',
(tester) async {
final first = FakeChannel()..readyCompleter.complete();
final second = FakeChannel()..readyCompleter.complete();
var sockets = 0;
final client = PipelinesClient(
channelFactory: (_) => sockets++ == 0 ? first : second,
backoffBase: const Duration(milliseconds: 200),
);
final server = webServer();
final session = AppSession(endpoint: server.uri, client: client);
addTearDown(session.dispose);
await session.start();
await tester.pumpWidget(materialApp(session, server));
await tester.pump();
await tester.pump();
first.incoming.add(
jsonEncode({
'id': listRequest(first)!['id'],
'type': 'runs',
'runs': [
{'id': 1, 'name': 'Before reconnect', 'status': 'running'},
],
}),
);
await tester.pump();
expect(find.text('Before reconnect'), findsOneWidget);
first.incoming.addError(StateError('network drop'));
await tester.pump();
expect(find.byType(RunsScreen), findsOneWidget);
expect(find.text('Reconnecting to Pipelines…'), findsOneWidget);
expect(find.text('Pipelines server unavailable'), findsNothing);
expect(find.text('Before reconnect'), findsOneWidget);
for (
var attempt = 0;
attempt < 40 && listRequest(second) == null;
attempt++
) {
await tester.pump(const Duration(milliseconds: 50));
}
expect(listRequest(second), isNotNull);
expect(find.text('Reconnecting to Pipelines…'), findsNothing);
second.incoming.add(
jsonEncode({
'id': listRequest(second)!['id'],
'type': 'runs',
'runs': [
{'id': 2, 'name': 'After reconnect', 'status': 'succeeded'},
],
}),
);
await tester.pump();
expect(find.text('After reconnect'), findsOneWidget);
expect(find.text('Before reconnect'), findsNothing);
},
);
testWidgets(
'reconnect keeps the open detail instead of resetting to the list',
(tester) async {
final first = FakeChannel()..readyCompleter.complete();
final second = FakeChannel()..readyCompleter.complete();
var sockets = 0;
final client = PipelinesClient(
channelFactory: (_) => sockets++ == 0 ? first : second,
backoffBase: const Duration(milliseconds: 10),
);
final server = webServer();
final session = AppSession(endpoint: server.uri, client: client);
addTearDown(session.dispose);
await session.start();
await tester.pumpWidget(materialApp(session, server, initialRunId: 1));
await tester.pump();
await tester.pump();
first.incoming.add(
jsonEncode({
'id': listRequest(first)!['id'],
'type': 'runs',
'runs': [
{'id': 1, 'name': 'Open run', 'status': 'running'},
],
}),
);
await tester.pump();
final firstDetail = detailRequests(first, 1).single;
first.incoming.add(
jsonEncode({
'id': firstDetail['id'],
'type': 'run',
'run_detail': {'id': 1, 'title': 'Open run', 'status': 'running'},
}),
);
await tester.pump();
expect(find.text('Open run'), findsOneWidget);
expect(find.text('Back to list'), findsOneWidget);
first.incoming.addError(StateError('network drop'));
await tester.pump();
expect(find.byType(RunsScreen), findsOneWidget);
expect(find.text('Open run'), findsOneWidget);
expect(find.text('Reconnecting to Pipelines…'), findsOneWidget);
for (
var attempt = 0;
attempt < 60 && listRequest(second) == null;
attempt++
) {
await tester.pump(const Duration(milliseconds: 10));
}
expect(listRequest(second), isNotNull);
second.incoming.add(
jsonEncode({
'id': listRequest(second)!['id'],
'type': 'runs',
'runs': [
{'id': 1, 'name': 'Open run', 'status': 'running'},
],
}),
);
for (
var attempt = 0;
attempt < 20 && detailRequests(second, 1).isEmpty;
attempt++
) {
await tester.pump(const Duration(milliseconds: 10));
}
final refreshed = detailRequests(second, 1).single;
expect(find.text('Open run'), findsOneWidget);
second.incoming.add(
jsonEncode({
'id': refreshed['id'],
'type': 'run',
'run_detail': {'id': 1, 'title': 'Open run', 'status': 'succeeded'},
}),
);
await tester.pump();
expect(find.text('Open run'), findsOneWidget);
expect(find.text('Back to list'), findsOneWidget);
},
);
testWidgets('gate uses Cupertino progress without a Material ancestor', (
tester,
) async {
final channel = FakeChannel();
final client = PipelinesClient(channelFactory: (_) => channel);
final server = webServer();
final session = AppSession(endpoint: server.uri, client: client);
addTearDown(session.dispose);
session.start();
await tester.pumpWidget(
CupertinoApp(
home: CupertinoPageScaffold(
child: RunsDestination(session: session, server: server),
),
),
);
await tester.pump();
expect(find.text('Connecting to Pipelines…'), findsOneWidget);
expect(find.byType(CupertinoActivityIndicator), findsOneWidget);
expect(find.byType(CircularProgressIndicator), findsNothing);
expect(find.byType(Material), findsNothing);
});
}
@@ -0,0 +1,61 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/application/server_endpoint.dart';
void main() {
test('web infers its own origin over ws at /connect', () {
final server = ServerEndpoint.resolve(
base: Uri.parse('http://127.0.0.1:8096/runs?filter=active'),
web: true,
);
expect(server.uri, Uri.parse('ws://127.0.0.1:8096/connect'));
expect(server.error, isNull);
expect(server.requiresConfiguration, isFalse);
});
test('secure web origin uses wss and the default port', () {
final server = ServerEndpoint.resolve(
base: Uri.parse('https://pipelines.example.test/runs'),
web: true,
);
expect(server.uri, Uri.parse('wss://pipelines.example.test/connect'));
});
test('web never guesses an endpoint when the page is not HTTP(S)', () {
final server = ServerEndpoint.resolve(
base: Uri.parse('file:///tmp/index.html'),
web: true,
);
expect(server.uri, isNull);
expect(server.error, contains('page URL'));
});
test('native requires an explicit endpoint instead of a baked host', () {
final server = ServerEndpoint.resolve(
base: Uri.parse('http://127.0.0.1:8096/'),
web: false,
);
expect(server.uri, isNull);
expect(server.requiresConfiguration, isTrue);
expect(server.error, isNull);
});
test('native validates and normalizes the configured endpoint', () {
final server = ServerEndpoint.resolve(
base: Uri.parse('http://127.0.0.1:8096/'),
web: false,
configured: ' ws://192.168.1.20:8081 ',
);
expect(server.uri, Uri.parse('ws://192.168.1.20:8081/connect'));
expect(server.requiresConfiguration, isFalse);
});
test('invalid native configuration reports the reason, never a fallback', () {
final server = ServerEndpoint.resolve(
base: Uri.parse('http://127.0.0.1:8096/'),
web: false,
configured: 'https://pipelines.example.test',
);
expect(server.uri, isNull);
expect(server.error, contains('Unsupported scheme'));
});
}