Merge branch 'main' into flutter/schedule-draft
This commit is contained in:
@@ -5,8 +5,11 @@ 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 'package:pipelines_app/features/pipelines/screen.dart';
|
||||
|
||||
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
|
||||
import '../features/pipelines/fixtures.dart';
|
||||
import 'pipelines_replies.dart';
|
||||
|
||||
ServerEndpoint webServer() => ServerEndpoint.resolve(
|
||||
base: Uri.parse('http://127.0.0.1:8096/'),
|
||||
@@ -125,18 +128,32 @@ void main() {
|
||||
|
||||
await pumpApp(tester, channel: channel, client: client, provider: provider);
|
||||
expect(provider.value.uri.path, '/pipelines');
|
||||
final fixture = PipelineFixture();
|
||||
await replyPipelines(tester, channel, fixture);
|
||||
final pipelinesRunsId =
|
||||
pipelineRequest(channel, 'list_runs')!['id'] as String;
|
||||
|
||||
await tester.tap(find.text('Runs').last);
|
||||
await tester.pump();
|
||||
expect(provider.value.uri.path, '/runs');
|
||||
|
||||
await replyList(
|
||||
final runsRequest = await waitForLaterRequest(
|
||||
tester,
|
||||
channel,
|
||||
runs: [
|
||||
{'id': 2, 'name': 'Beta run', 'status': 'failed'},
|
||||
],
|
||||
'list_runs',
|
||||
pipelinesRunsId,
|
||||
);
|
||||
channel.incoming.add(
|
||||
jsonEncode({
|
||||
'id': runsRequest['id'],
|
||||
'type': 'runs',
|
||||
'runs': [
|
||||
{'id': 2, 'name': 'Beta run', 'status': 'failed'},
|
||||
],
|
||||
}),
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
await tester.tap(find.text('Beta run'));
|
||||
await tester.pump();
|
||||
expect(provider.value.uri.path, '/runs/2');
|
||||
@@ -172,6 +189,10 @@ void main() {
|
||||
|
||||
await pumpApp(tester, channel: channel, client: client, provider: provider);
|
||||
expect(provider.value.uri.path, '/pipelines');
|
||||
expect(find.text('Pipelines — Not implemented'), findsOneWidget);
|
||||
await replyPipelines(tester, channel, PipelineFixture());
|
||||
expect(find.byType(PipelinesScreen), findsOneWidget);
|
||||
expect(find.text('Pipelines — Not implemented'), findsNothing);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pump();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,237 @@
|
||||
import 'dart:convert';
|
||||
|
||||
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/pipelines_destination.dart';
|
||||
import 'package:pipelines_app/application/server_endpoint.dart';
|
||||
import 'package:pipelines_app/core/transport/pipelines_client.dart';
|
||||
import 'package:pipelines_app/features/pipelines/screen.dart';
|
||||
|
||||
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
|
||||
import '../features/pipelines/fixtures.dart';
|
||||
|
||||
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;
|
||||
|
||||
Widget materialApp(AppSession session, ServerEndpoint server) => MaterialApp(
|
||||
home: Scaffold(
|
||||
body: PipelinesDestination(session: session, server: server),
|
||||
),
|
||||
);
|
||||
|
||||
Future<void> waitFor(
|
||||
WidgetTester tester,
|
||||
bool Function() check,
|
||||
String label,
|
||||
) async {
|
||||
for (var attempt = 0; attempt < 60 && !check(); attempt++) {
|
||||
await tester.pump(const Duration(milliseconds: 10));
|
||||
}
|
||||
expect(check(), isTrue, reason: 'Timed out waiting for $label');
|
||||
}
|
||||
|
||||
Future<void> replyList(
|
||||
WidgetTester tester,
|
||||
FakeChannel channel,
|
||||
PipelineFixture fixture,
|
||||
) async {
|
||||
await waitFor(
|
||||
tester,
|
||||
() => requestOf(channel, 'list_pipelines') != null,
|
||||
'list_pipelines',
|
||||
);
|
||||
final pipelines = requestOf(channel, 'list_pipelines')!;
|
||||
channel.incoming.add(
|
||||
jsonEncode({
|
||||
'id': pipelines['id'],
|
||||
'type': 'pipelines',
|
||||
'pipelines': fixture.summaries,
|
||||
}),
|
||||
);
|
||||
await tester.pump();
|
||||
await waitFor(
|
||||
tester,
|
||||
() => requestOf(channel, 'list_runs') != null,
|
||||
'list_runs',
|
||||
);
|
||||
final runs = requestOf(channel, 'list_runs')!;
|
||||
channel.incoming.add(
|
||||
jsonEncode({'id': runs['id'], 'type': 'runs', 'runs': fixture.runs}),
|
||||
);
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
Future<void> replyDetail(
|
||||
WidgetTester tester,
|
||||
FakeChannel channel,
|
||||
PipelineFixture fixture,
|
||||
int id,
|
||||
) async {
|
||||
await waitFor(
|
||||
tester,
|
||||
() => requestOf(channel, 'get_pipeline') != null,
|
||||
'get_pipeline',
|
||||
);
|
||||
final request = requestOf(channel, 'get_pipeline')!;
|
||||
final detail = fixture.detailFor(
|
||||
request['pipeline_id'] as int,
|
||||
request['version'] as int,
|
||||
)!;
|
||||
channel.incoming.add(
|
||||
jsonEncode({'id': request['id'], 'type': 'pipeline', 'detail': detail}),
|
||||
);
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('missing native configuration never opens a socket', (
|
||||
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(PipelinesScreen), findsNothing);
|
||||
expect(channel.outgoing, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('the list is only requested after the first connection', (
|
||||
tester,
|
||||
) async {
|
||||
final fixture = PipelineFixture();
|
||||
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(PipelinesScreen), findsNothing);
|
||||
expect(requestOf(channel, 'list_pipelines'), isNull);
|
||||
expect(channel.outgoing, isEmpty);
|
||||
|
||||
channel.readyCompleter.complete();
|
||||
await starting;
|
||||
await tester.pump();
|
||||
await replyList(tester, channel, fixture);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.byType(PipelinesScreen), findsOneWidget);
|
||||
expect(find.text('Alpha review'), findsOneWidget);
|
||||
expect(requestOf(channel, 'list_pipelines'), isNotNull);
|
||||
});
|
||||
|
||||
testWidgets('an unavailable first connection is honest and retryable', (
|
||||
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('Failed to connect'));
|
||||
await starting;
|
||||
await tester.pumpWidget(materialApp(session, server));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Pipelines server unavailable'), findsOneWidget);
|
||||
expect(find.textContaining('Failed to connect'), findsNothing);
|
||||
expect(find.widgetWithText(FilledButton, 'Retry'), findsOneWidget);
|
||||
expect(find.byType(PipelinesScreen), findsNothing);
|
||||
expect(channel.outgoing, isEmpty);
|
||||
// The client keeps retrying by design; end its backoff timer explicitly.
|
||||
session.dispose();
|
||||
});
|
||||
|
||||
testWidgets('reconnect refreshes the list and the open detail in place', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(1100, 900);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
|
||||
final fixture = PipelineFixture();
|
||||
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));
|
||||
await tester.pump();
|
||||
await replyList(tester, first, fixture);
|
||||
await tester.pump();
|
||||
expect(find.text('Alpha review'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byKey(const Key('pipeline-row-1')));
|
||||
await tester.pump();
|
||||
await replyDetail(tester, first, fixture, 1);
|
||||
await tester.pump();
|
||||
expect(find.text('hash-alpha'), findsOneWidget);
|
||||
expect(
|
||||
tester.widget<ListTile>(find.byKey(const Key('pipeline-row-1'))).selected,
|
||||
isTrue,
|
||||
);
|
||||
|
||||
first.incoming.addError(StateError('network drop'));
|
||||
await tester.pump();
|
||||
expect(find.byType(PipelinesScreen), findsOneWidget);
|
||||
expect(find.text('Reconnecting to Pipelines…'), findsOneWidget);
|
||||
expect(find.text('hash-alpha'), findsOneWidget);
|
||||
|
||||
await waitFor(
|
||||
tester,
|
||||
() => requestOf(second, 'list_pipelines') != null,
|
||||
'reconnected list request',
|
||||
);
|
||||
await replyList(tester, second, fixture);
|
||||
await waitFor(
|
||||
tester,
|
||||
() => requestOf(second, 'get_pipeline') != null,
|
||||
'reconnected detail request',
|
||||
);
|
||||
await replyDetail(tester, second, fixture, 1);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Reconnecting to Pipelines…'), findsNothing);
|
||||
expect(find.text('hash-alpha'), findsOneWidget);
|
||||
expect(
|
||||
tester.widget<ListTile>(find.byKey(const Key('pipeline-row-1'))).selected,
|
||||
isTrue,
|
||||
reason: 'A reconnect must not remount the screen or drop the selection.',
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
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/core/transport/pipelines_client.dart';
|
||||
|
||||
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
|
||||
import '../features/pipelines/fixtures.dart';
|
||||
import '../features/pipelines/test_helpers.dart' show selectableText;
|
||||
import 'pipelines_app_test.dart' show webServer;
|
||||
import 'pipelines_replies.dart';
|
||||
|
||||
void main() {
|
||||
for (final cupertino in [false, true]) {
|
||||
testWidgets(
|
||||
'the /pipelines route renders real rows, detail and source ($cupertino)',
|
||||
(tester) 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,
|
||||
);
|
||||
}
|
||||
|
||||
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 tester.pumpWidget(
|
||||
PipelinesApp(
|
||||
server: webServer(),
|
||||
client: client,
|
||||
cupertino: cupertino,
|
||||
routeInformationProvider: provider,
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
await replyPipelines(tester, channel, fixture);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Alpha review'), findsOneWidget);
|
||||
expect(find.text('id 1 · v2 · 2 stages'), findsOneWidget);
|
||||
expect(find.text('Pipelines — Not implemented'), findsNothing);
|
||||
expect(sockets, 1);
|
||||
|
||||
// The shell owns the only navigation bar; the embedded screen adds no
|
||||
// second AppBar or CupertinoNavigationBar.
|
||||
if (cupertino) {
|
||||
expect(find.byType(CupertinoNavigationBar), findsOneWidget);
|
||||
expect(find.byType(CupertinoPageScaffold), findsOneWidget);
|
||||
expect(find.byType(Material), findsNothing);
|
||||
} else {
|
||||
expect(find.byType(AppBar), findsOneWidget);
|
||||
expect(find.byType(Scaffold), findsOneWidget);
|
||||
}
|
||||
|
||||
await tester.tap(find.byKey(const Key('pipeline-row-1')));
|
||||
await tester.pump();
|
||||
await replyPipelineDetail(tester, channel, fixture);
|
||||
await tester.pump();
|
||||
expect(find.text('hash-alpha'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('Source'));
|
||||
await tester.pump();
|
||||
expect(
|
||||
selectableText(
|
||||
'name: Alpha review\nstages:\n review:\n type: model',
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pump();
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
|
||||
import '../features/pipelines/fixtures.dart';
|
||||
|
||||
Map<String, dynamic>? pipelineRequest(FakeChannel channel, String type) =>
|
||||
channel.outgoing
|
||||
.map((frame) => jsonDecode(frame as String) as Map<String, dynamic>)
|
||||
.where((frame) => frame['type'] == type)
|
||||
.firstOrNull;
|
||||
|
||||
Future<void> waitForRequest(
|
||||
WidgetTester tester,
|
||||
FakeChannel channel,
|
||||
String type,
|
||||
) async {
|
||||
for (
|
||||
var attempt = 0;
|
||||
attempt < 60 && pipelineRequest(channel, type) == null;
|
||||
attempt++
|
||||
) {
|
||||
await tester.pump(const Duration(milliseconds: 10));
|
||||
}
|
||||
expect(
|
||||
pipelineRequest(channel, type),
|
||||
isNotNull,
|
||||
reason: 'Timed out waiting for $type',
|
||||
);
|
||||
}
|
||||
|
||||
/// Waits for a later request of [type] than [afterId]. The transport serializes
|
||||
/// requests, so a destination that mounts later issues its read only after the
|
||||
/// previous reply arrives.
|
||||
Future<Map<String, dynamic>> waitForLaterRequest(
|
||||
WidgetTester tester,
|
||||
FakeChannel channel,
|
||||
String type,
|
||||
String afterId,
|
||||
) async {
|
||||
Map<String, dynamic>? later() => channel.outgoing
|
||||
.map((frame) => jsonDecode(frame as String) as Map<String, dynamic>)
|
||||
.where((frame) => frame['type'] == type && frame['id'] != afterId)
|
||||
.firstOrNull;
|
||||
for (var attempt = 0; attempt < 60 && later() == null; attempt++) {
|
||||
await tester.pump(const Duration(milliseconds: 10));
|
||||
}
|
||||
expect(later(), isNotNull, reason: 'Timed out waiting for later $type');
|
||||
return later()!;
|
||||
}
|
||||
|
||||
Future<void> replyPipelines(
|
||||
WidgetTester tester,
|
||||
FakeChannel channel,
|
||||
PipelineFixture fixture,
|
||||
) async {
|
||||
await waitForRequest(tester, channel, 'list_pipelines');
|
||||
final pipelines = pipelineRequest(channel, 'list_pipelines')!;
|
||||
channel.incoming.add(
|
||||
jsonEncode({
|
||||
'id': pipelines['id'],
|
||||
'type': 'pipelines',
|
||||
'pipelines': fixture.summaries,
|
||||
}),
|
||||
);
|
||||
await tester.pump();
|
||||
await waitForRequest(tester, channel, 'list_runs');
|
||||
final runs = pipelineRequest(channel, 'list_runs')!;
|
||||
channel.incoming.add(
|
||||
jsonEncode({'id': runs['id'], 'type': 'runs', 'runs': fixture.runs}),
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
Future<void> replyPipelineDetail(
|
||||
WidgetTester tester,
|
||||
FakeChannel channel,
|
||||
PipelineFixture fixture,
|
||||
) async {
|
||||
await waitForRequest(tester, channel, 'get_pipeline');
|
||||
final request = pipelineRequest(channel, 'get_pipeline')!;
|
||||
final detail = fixture.detailFor(
|
||||
request['pipeline_id'] as int,
|
||||
request['version'] as int,
|
||||
)!;
|
||||
channel.incoming.add(
|
||||
jsonEncode({'id': request['id'], 'type': 'pipeline', 'detail': detail}),
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
}
|
||||
@@ -8,7 +8,9 @@ import 'package:pipelines_app/core/transport/pipelines_client.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
|
||||
import '../features/pipelines/fixtures.dart';
|
||||
import 'pipelines_app_test.dart' show webServer, requestOf;
|
||||
import 'pipelines_replies.dart';
|
||||
|
||||
void main() {
|
||||
for (final cupertino in [false, true]) {
|
||||
@@ -46,6 +48,7 @@ void main() {
|
||||
isNull,
|
||||
reason: 'Unvisited settings must not issue hidden requests',
|
||||
);
|
||||
await replyPipelines(tester, channel, PipelineFixture());
|
||||
|
||||
await tester.tap(find.text('Settings').last);
|
||||
await tester.pump();
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_controller.dart';
|
||||
|
||||
import 'chat_thread_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
late FakeChatGateway gateway;
|
||||
late ChatThreadController controller;
|
||||
|
||||
setUp(() {
|
||||
gateway = FakeChatGateway();
|
||||
controller = ChatThreadController(gateway, 's');
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
controller.dispose();
|
||||
gateway.close();
|
||||
});
|
||||
|
||||
test(
|
||||
'subscribes the exact session, then reads newest history with all data',
|
||||
() async {
|
||||
final unknown = chatMessage(
|
||||
'a',
|
||||
'assistant',
|
||||
parts: [
|
||||
{
|
||||
'type': 'future_part',
|
||||
'id': 'p1',
|
||||
'payload': {'nested': true},
|
||||
'unknown_field': 7,
|
||||
},
|
||||
],
|
||||
)..['future_message_field'] = 'keep';
|
||||
gateway.history = (_) async =>
|
||||
chatPage([unknown], cursor: 'opaque/next?x=1', hasMore: true);
|
||||
|
||||
await controller.load();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), [
|
||||
'chat_subscribe',
|
||||
'chat_messages',
|
||||
]);
|
||||
expect(gateway.calls.first.fields, {
|
||||
'chat_subscribe': {'session_id': 's'},
|
||||
});
|
||||
expect(gateway.calls.last.fields['chat_query'], {
|
||||
'session_id': 's',
|
||||
'limit': 200,
|
||||
'cursor': '',
|
||||
});
|
||||
expect(controller.loaded, isTrue);
|
||||
expect(controller.loading, isFalse);
|
||||
expect(controller.error, isNull);
|
||||
expect(controller.running, isFalse);
|
||||
expect(controller.hasMore, isTrue);
|
||||
expect(controller.cursor, 'opaque/next?x=1');
|
||||
expect(controller.messages.single, unknown);
|
||||
expect(controller.messages.single['future_message_field'], 'keep');
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'loadOlder passes the opaque cursor back and prepends the page',
|
||||
() async {
|
||||
gateway.history = (_) async => chatPage(
|
||||
[chatMessage('b', 'user')],
|
||||
cursor: 'older-1',
|
||||
hasMore: true,
|
||||
);
|
||||
await controller.load();
|
||||
|
||||
final olderReply = Completer<JsonMap>();
|
||||
gateway.history = (_) => olderReply.future;
|
||||
final loadingStates = <bool>[];
|
||||
controller.addListener(() => loadingStates.add(controller.loading));
|
||||
final older = controller.loadOlder();
|
||||
|
||||
expect(gateway.calls.last.fields['chat_query'], {
|
||||
'session_id': 's',
|
||||
'limit': 200,
|
||||
'cursor': 'older-1',
|
||||
});
|
||||
expect(controller.loading, isTrue);
|
||||
expect(loadingStates, [true]);
|
||||
olderReply.complete(
|
||||
chatPage(
|
||||
[chatMessage('a', 'assistant')],
|
||||
cursor: 'last',
|
||||
hasMore: false,
|
||||
),
|
||||
);
|
||||
await older;
|
||||
|
||||
expect(controller.messages.map((item) => item['id']), ['a', 'b']);
|
||||
expect(controller.cursor, 'last');
|
||||
expect(controller.hasMore, isFalse);
|
||||
expect(controller.loading, isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'a superseded history reply never replaces the newer transcript',
|
||||
() async {
|
||||
final first = Completer<JsonMap>();
|
||||
final second = Completer<JsonMap>();
|
||||
var request = 0;
|
||||
gateway.history = (_) => request++ == 0 ? first.future : second.future;
|
||||
|
||||
final stale = controller.load();
|
||||
await pumpEventQueue();
|
||||
final current = controller.load();
|
||||
await pumpEventQueue();
|
||||
second.complete(chatPage([chatMessage('new', 'user')]));
|
||||
await current;
|
||||
expect(controller.messages.single['id'], 'new');
|
||||
|
||||
first.complete(chatPage([chatMessage('old', 'user')]));
|
||||
await stale;
|
||||
expect(controller.messages.single['id'], 'new');
|
||||
expect(controller.loaded, isTrue);
|
||||
expect(controller.loading, isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'a failed history read reports the error, never an empty success',
|
||||
() async {
|
||||
gateway.history = (_) => Future.error(StateError('history unavailable'));
|
||||
|
||||
await controller.load();
|
||||
|
||||
expect(controller.error, contains('history unavailable'));
|
||||
expect(controller.error, isNot(''));
|
||||
expect(controller.messages, isEmpty);
|
||||
expect(controller.loading, isFalse);
|
||||
expect(controller.loaded, isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
test('a refused subscribe leaves history usable', () async {
|
||||
gateway.subscribe = () => Future.error(StateError('not live'));
|
||||
gateway.history = (_) async => chatPage([chatMessage('a', 'user')]);
|
||||
|
||||
await controller.load();
|
||||
|
||||
expect(controller.messages.single['id'], 'a');
|
||||
expect(controller.error, isNull);
|
||||
expect(controller.loaded, isTrue);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
|
||||
/// A fake gateway whose subscribe/history replies the test controls.
|
||||
class FakeChatGateway implements ApiGateway {
|
||||
final calls = <({String type, JsonMap fields})>[];
|
||||
final historyRequests = <Completer<JsonMap>>[];
|
||||
final _frames = StreamController<JsonMap>.broadcast();
|
||||
Future<JsonMap> Function(JsonMap fields)? history;
|
||||
Future<JsonMap> Function()? subscribe;
|
||||
void Function()? onSubscribe;
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add((type: type, fields: fields));
|
||||
switch (type) {
|
||||
case 'chat_subscribe':
|
||||
onSubscribe?.call();
|
||||
return subscribe == null ? <String, dynamic>{} : subscribe!();
|
||||
case 'chat_messages':
|
||||
final completer = Completer<JsonMap>();
|
||||
historyRequests.add(completer);
|
||||
if (history != null) return history!(fields);
|
||||
return completer.future;
|
||||
}
|
||||
return <String, dynamic>{};
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => _frames.stream;
|
||||
|
||||
void emitEvent(JsonMap body) {
|
||||
_frames.add({'type': 'chat_event', 'chat_event': body});
|
||||
}
|
||||
|
||||
void emitRaw(JsonMap envelope) => _frames.add(envelope);
|
||||
|
||||
void close() => _frames.close();
|
||||
}
|
||||
|
||||
JsonMap chatPage(
|
||||
List<JsonMap> messages, {
|
||||
String cursor = '',
|
||||
bool hasMore = false,
|
||||
}) => {
|
||||
'chat_history': {
|
||||
'messages': messages,
|
||||
'next_cursor': cursor,
|
||||
'has_more': hasMore,
|
||||
},
|
||||
};
|
||||
|
||||
JsonMap chatMessage(String id, String role, {List<JsonMap> parts = const []}) =>
|
||||
{'id': id, 'role': role, 'parts': parts};
|
||||
|
||||
JsonMap chatTextFrame(
|
||||
String text, {
|
||||
String session = 's',
|
||||
String messageId = 'a',
|
||||
}) => {
|
||||
'session_id': session,
|
||||
'message_id': messageId,
|
||||
'kind': 'text',
|
||||
'part': {'id': 'p', 'type': 'text'},
|
||||
'delta': text,
|
||||
};
|
||||
|
||||
JsonMap chatStepFrame(String status, {String session = 's'}) => {
|
||||
'session_id': session,
|
||||
'kind': 'step',
|
||||
'step': {'status': status, 'turn_id': 'turn', 'intermediate': false},
|
||||
};
|
||||
@@ -0,0 +1,188 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_controller.dart';
|
||||
|
||||
import 'chat_thread_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
late FakeChatGateway gateway;
|
||||
late ChatThreadController controller;
|
||||
|
||||
setUp(() {
|
||||
gateway = FakeChatGateway();
|
||||
controller = ChatThreadController(gateway, 's');
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
controller.dispose();
|
||||
gateway.close();
|
||||
});
|
||||
|
||||
test('folds own session frames only and keeps live turn activity', () async {
|
||||
gateway.history = (_) async => chatPage([
|
||||
chatMessage(
|
||||
'a',
|
||||
'assistant',
|
||||
parts: [
|
||||
{'type': 'reasoning', 'id': 'r', 'text': 'held'},
|
||||
],
|
||||
),
|
||||
]);
|
||||
await controller.load();
|
||||
var notifications = 0;
|
||||
controller.addListener(() => notifications++);
|
||||
|
||||
gateway.emitEvent(chatTextFrame('foreign', session: 'other'));
|
||||
gateway.emitEvent(chatTextFrame('nodata')..remove('session_id'));
|
||||
gateway.emitRaw({'type': 'run_event', 'session_id': 's'});
|
||||
await pumpEventQueue();
|
||||
expect(notifications, 0);
|
||||
expect(controller.messages.single['parts'], [
|
||||
{'type': 'reasoning', 'id': 'r', 'text': 'held'},
|
||||
]);
|
||||
expect(controller.running, isFalse);
|
||||
|
||||
gateway.emitEvent(chatTextFrame('one'));
|
||||
gateway.emitEvent(chatTextFrame(' two'));
|
||||
await pumpEventQueue();
|
||||
expect(controller.running, isTrue);
|
||||
expect(controller.messages.single['parts'], [
|
||||
{'type': 'reasoning', 'id': 'r', 'text': 'held'},
|
||||
{'type': 'text', 'id': 'p', 'text': 'one two'},
|
||||
]);
|
||||
|
||||
gateway.emitEvent(chatStepFrame('ended'));
|
||||
await pumpEventQueue();
|
||||
expect(controller.running, isFalse);
|
||||
expect(notifications, greaterThan(0));
|
||||
});
|
||||
|
||||
test(
|
||||
'resync re-subscribes, settles the held turn, and replaces it with the page',
|
||||
() async {
|
||||
gateway.history = (_) async => chatPage(
|
||||
[
|
||||
chatMessage('stale-user', 'user'),
|
||||
chatMessage('stale-assistant', 'assistant'),
|
||||
],
|
||||
cursor: 'stale-cursor',
|
||||
hasMore: true,
|
||||
);
|
||||
await controller.load();
|
||||
gateway.emitEvent(chatTextFrame('still running'));
|
||||
await pumpEventQueue();
|
||||
expect(controller.running, isTrue);
|
||||
|
||||
gateway.onSubscribe = () {
|
||||
// Settled before the re-attach, then replay proves the fresh turn.
|
||||
expect(controller.running, isFalse);
|
||||
gateway.emitEvent(chatStepFrame('started'));
|
||||
};
|
||||
gateway.history = (_) async => chatPage(
|
||||
[
|
||||
chatMessage('fresh-user', 'user'),
|
||||
chatMessage('fresh-assistant', 'assistant'),
|
||||
],
|
||||
cursor: 'fresh-cursor',
|
||||
hasMore: false,
|
||||
);
|
||||
|
||||
await controller.resync();
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(
|
||||
gateway.calls.where((call) => call.type == 'chat_subscribe').length,
|
||||
2,
|
||||
);
|
||||
expect(
|
||||
gateway.calls.where((call) => call.type == 'chat_messages').length,
|
||||
2,
|
||||
);
|
||||
expect(gateway.calls.last.fields['chat_query'], {
|
||||
'session_id': 's',
|
||||
'limit': 200,
|
||||
'cursor': '',
|
||||
});
|
||||
expect(controller.messages.map((item) => item['id']), [
|
||||
'fresh-user',
|
||||
'fresh-assistant',
|
||||
]);
|
||||
expect(controller.cursor, 'fresh-cursor');
|
||||
expect(controller.hasMore, isFalse);
|
||||
expect(controller.running, isTrue);
|
||||
expect(controller.error, isNull);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'a failed resync keeps replayed activity and reports the error',
|
||||
() async {
|
||||
var attempt = 0;
|
||||
gateway.history = (_) async {
|
||||
attempt++;
|
||||
if (attempt == 1) return chatPage([chatMessage('held', 'user')]);
|
||||
throw StateError('resync unavailable');
|
||||
};
|
||||
await controller.load();
|
||||
gateway.onSubscribe = () {
|
||||
gateway.emitEvent(chatStepFrame('started'));
|
||||
};
|
||||
|
||||
await controller.resync();
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(controller.messages.single['id'], 'held');
|
||||
expect(controller.running, isTrue);
|
||||
expect(controller.error, contains('resync unavailable'));
|
||||
expect(controller.loading, isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'a resync frame reattaches and replaces the new authoritative page',
|
||||
() async {
|
||||
var attempt = 0;
|
||||
gateway.history = (_) async => attempt++ == 0
|
||||
? chatPage([chatMessage('held', 'user')], hasMore: true)
|
||||
: chatPage([chatMessage('fresh', 'assistant')]);
|
||||
await controller.load();
|
||||
|
||||
gateway.emitEvent({'session_id': 's', 'kind': 'resync'});
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(
|
||||
gateway.calls.where((call) => call.type == 'chat_subscribe').length,
|
||||
2,
|
||||
);
|
||||
expect(controller.messages.single['id'], 'fresh');
|
||||
expect(controller.hasMore, isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'disposal ignores a late reply and never invents an unsubscribe',
|
||||
() async {
|
||||
final pending = Completer<JsonMap>();
|
||||
gateway.history = (_) => pending.future;
|
||||
var notifications = 0;
|
||||
controller.addListener(() => notifications++);
|
||||
final load = controller.load();
|
||||
await pumpEventQueue();
|
||||
final before = notifications;
|
||||
|
||||
controller.dispose();
|
||||
pending.complete(chatPage([chatMessage('late', 'user')]));
|
||||
await load;
|
||||
|
||||
expect(notifications, before);
|
||||
expect(
|
||||
gateway.calls.where((call) => call.type == 'chat_unsubscribe'),
|
||||
isEmpty,
|
||||
);
|
||||
// Keep tearDown's dispose idempotent.
|
||||
controller = ChatThreadController(gateway, 's');
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -128,6 +128,32 @@ void main() {
|
||||
expect(find.text('Alpha review'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('embedded framing adds no bar and keeps a content refresh', (
|
||||
tester,
|
||||
) async {
|
||||
final fixture = PipelineFixture();
|
||||
final gateway = gatewayFor(fixture);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text('Host bar')),
|
||||
body: PipelinesScreen(gateway: gateway, embedded: true),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(AppBar), findsOneWidget);
|
||||
expect(find.text('Host bar'), findsOneWidget);
|
||||
expect(find.text('Pipelines'), findsNothing);
|
||||
expect(find.text('Alpha review'), findsOneWidget);
|
||||
|
||||
final lists = gateway.callsOf('list_pipelines').length;
|
||||
await tester.tap(find.byTooltip('Refresh'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.callsOf('list_pipelines').length, lists + 1);
|
||||
});
|
||||
|
||||
testWidgets('graph tab states it is pending without a hook', (tester) async {
|
||||
final fixture = PipelineFixture();
|
||||
await pumpWide(tester, gatewayFor(fixture));
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/cancel_wording.dart';
|
||||
import 'package:pipelines_app/features/runs/screen.dart';
|
||||
|
||||
class _Gateway implements ApiGateway {
|
||||
final events = StreamController<JsonMap>.broadcast();
|
||||
final requests = <({String type, JsonMap fields})>[];
|
||||
final canceled = Completer<JsonMap>();
|
||||
final JsonMap run = {
|
||||
'id': 7,
|
||||
'title': 'Controlled run',
|
||||
'name': 'fixture',
|
||||
'status': 'running',
|
||||
'executing': true,
|
||||
'steps': <JsonMap>[],
|
||||
};
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => events.stream;
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
requests.add((type: type, fields: fields));
|
||||
return switch (type) {
|
||||
'list_runs' => Future.value({
|
||||
'runs': [Map.of(run)],
|
||||
}),
|
||||
'get_run' => Future.value({'run_detail': Map.of(run)}),
|
||||
'cancel_run' => canceled.future,
|
||||
_ => Future.error(UnsupportedError(type)),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
for (final cupertino in [false, true]) {
|
||||
testWidgets(
|
||||
'actual detail confirms cancel and folds accepted state ($cupertino)',
|
||||
(tester) async {
|
||||
final gateway = _Gateway();
|
||||
addTearDown(gateway.events.close);
|
||||
final screen = RunsScreen(gateway: gateway, initialRunId: 7);
|
||||
await tester.pumpWidget(
|
||||
cupertino
|
||||
? CupertinoApp(home: CupertinoPageScaffold(child: screen))
|
||||
: MaterialApp(home: Scaffold(body: screen)),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Cancel run 7?'), findsOneWidget);
|
||||
expect(gateway.requests.where((r) => r.type == 'cancel_run'), isEmpty);
|
||||
await tester.tap(find.byKey(cancelKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.requests.where((r) => r.type == 'cancel_run'), isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(cancelConfirmKey));
|
||||
await tester.pump();
|
||||
expect(
|
||||
gateway.requests.where((r) => r.type == 'cancel_run').single.fields,
|
||||
{'run_id': 7},
|
||||
);
|
||||
expect(find.text('canceled'), findsNothing);
|
||||
gateway.canceled.complete({
|
||||
'run_detail': {
|
||||
...gateway.run,
|
||||
'status': 'canceled',
|
||||
'executing': false,
|
||||
},
|
||||
});
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(cancelActionKey), findsNothing);
|
||||
expect(find.text('canceled'), findsOneWidget);
|
||||
await tester.tap(find.text(cupertino ? 'Back' : 'Back to list'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
find.textContaining('canceled'),
|
||||
findsWidgets,
|
||||
reason:
|
||||
'The accepted server state must also update the held list row',
|
||||
);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user