272 lines
8.6 KiB
Dart
272 lines
8.6 KiB
Dart
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/chat/conversation/chat_draft_store.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
|
|
|
import 'chat_thread_fixtures.dart';
|
|
|
|
/// A thread gateway whose prompt replies the test settles one by one.
|
|
class _Gateway extends FakeChatGateway {
|
|
final prompts = <Completer<JsonMap>>[];
|
|
final promptBodies = <JsonMap>[];
|
|
List<JsonMap> messages = [];
|
|
|
|
_Gateway() {
|
|
history = (_) async => chatPage(messages);
|
|
}
|
|
|
|
@override
|
|
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
|
if (type == 'chat_prompt') {
|
|
promptBodies.add(fields['chat_prompt'] as JsonMap);
|
|
final reply = Completer<JsonMap>();
|
|
prompts.add(reply);
|
|
return reply.future;
|
|
}
|
|
return super.call(type, fields);
|
|
}
|
|
}
|
|
|
|
Future<void> _pump(WidgetTester tester) async {
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 100));
|
|
await tester.pump();
|
|
}
|
|
|
|
/// Hosts one thread; [generation] changes the key so a second call simulates
|
|
/// a page reload: a fresh thread and composer over the same device store.
|
|
Future<void> _host(
|
|
WidgetTester tester,
|
|
_Gateway gateway,
|
|
MemoryChatDraftStore drafts, {
|
|
bool cupertino = false,
|
|
int generation = 0,
|
|
}) async {
|
|
var sequence = 0;
|
|
final thread = ChatThreadView(
|
|
key: ValueKey(generation),
|
|
gateway: gateway,
|
|
sessionId: 's',
|
|
onBack: () {},
|
|
drafts: drafts,
|
|
newOperationId: () => 'op-$generation-${++sequence}',
|
|
);
|
|
await tester.pumpWidget(
|
|
cupertino
|
|
? CupertinoApp(home: CupertinoPageScaffold(child: thread))
|
|
: MaterialApp(home: Scaffold(body: thread)),
|
|
);
|
|
await _pump(tester);
|
|
}
|
|
|
|
Future<void> _send(WidgetTester tester, String text) async {
|
|
await tester.enterText(find.byKey(const Key('chat-composer-field')), text);
|
|
await tester.pump();
|
|
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
|
await _pump(tester);
|
|
}
|
|
|
|
Finder _state(String name) => find.byKey(Key('chat-delivery-$name'));
|
|
|
|
void main() {
|
|
for (final cupertino in [false, true]) {
|
|
testWidgets('send shows Sending… then Sent ($cupertino)', (tester) async {
|
|
final gateway = _Gateway();
|
|
addTearDown(gateway.close);
|
|
await _host(
|
|
tester,
|
|
gateway,
|
|
MemoryChatDraftStore(),
|
|
cupertino: cupertino,
|
|
);
|
|
|
|
await _send(tester, 'Hello');
|
|
expect(_state('sending'), findsOneWidget);
|
|
expect(find.text('Sending…'), findsOneWidget);
|
|
|
|
gateway.prompts.single.complete({});
|
|
await _pump(tester);
|
|
expect(_state('sending'), findsNothing);
|
|
expect(_state('sent'), findsOneWidget);
|
|
|
|
// Only the newest accepted message keeps its quiet check.
|
|
await _send(tester, 'Again');
|
|
gateway.prompts.last.complete({});
|
|
await _pump(tester);
|
|
expect(_state('sent'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('a steered reply is distinct from Sent ($cupertino)', (
|
|
tester,
|
|
) async {
|
|
final gateway = _Gateway();
|
|
addTearDown(gateway.close);
|
|
await _host(
|
|
tester,
|
|
gateway,
|
|
MemoryChatDraftStore(),
|
|
cupertino: cupertino,
|
|
);
|
|
await _send(tester, 'Handle this in the running reply');
|
|
gateway.prompts.single.complete({'chat_steered': true});
|
|
await _pump(tester);
|
|
expect(_state('steered'), findsOneWidget);
|
|
expect(find.text('Delivered to the running reply'), findsOneWidget);
|
|
expect(_state('sent'), findsNothing);
|
|
await _send(tester, 'And this one too');
|
|
gateway.prompts.last.complete({'chat_steered': true});
|
|
await _pump(tester);
|
|
expect(_state('steered'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('a queued reply shows Queued until the turn moves on '
|
|
'($cupertino)', (tester) async {
|
|
final gateway = _Gateway();
|
|
addTearDown(gateway.close);
|
|
await _host(
|
|
tester,
|
|
gateway,
|
|
MemoryChatDraftStore(),
|
|
cupertino: cupertino,
|
|
);
|
|
|
|
await _send(tester, 'Later please');
|
|
gateway.prompts.single.complete({'chat_queued': true});
|
|
await _pump(tester);
|
|
expect(_state('queued'), findsOneWidget);
|
|
expect(find.text('Queued'), findsOneWidget);
|
|
|
|
gateway.emitEvent(chatStepFrame('running'));
|
|
await _pump(tester);
|
|
expect(_state('queued'), findsNothing);
|
|
expect(_state('sent'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('a refusal shows Not sent and Retry reuses the operation id '
|
|
'($cupertino)', (tester) async {
|
|
final gateway = _Gateway();
|
|
addTearDown(gateway.close);
|
|
final drafts = MemoryChatDraftStore();
|
|
await _host(tester, gateway, drafts, cupertino: cupertino);
|
|
|
|
await _send(tester, 'Please refuse');
|
|
gateway.prompts.single.completeError(StateError('refused'));
|
|
await _pump(tester);
|
|
expect(_state('failed'), findsOneWidget);
|
|
expect(find.text('Not sent'), findsOneWidget);
|
|
// The text is back in the composer, so the bubble offers Retry only.
|
|
expect(find.byKey(const Key('chat-delivery-edit')), findsNothing);
|
|
expect(drafts.entries['s'], 'Please refuse');
|
|
|
|
await tester.tap(find.byKey(const Key('chat-delivery-retry')));
|
|
await _pump(tester);
|
|
expect(_state('sending'), findsOneWidget);
|
|
expect(_state('failed'), findsNothing);
|
|
expect(gateway.promptBodies.map((body) => body['operation_id']), [
|
|
'op-0-1',
|
|
'op-0-1',
|
|
]);
|
|
|
|
gateway.prompts.last.complete({});
|
|
await _pump(tester);
|
|
expect(_state('sent'), findsOneWidget);
|
|
expect(drafts.entries, isEmpty);
|
|
expect(drafts.unsentEntries, isEmpty);
|
|
});
|
|
}
|
|
|
|
testWidgets('a message in flight at reload comes back as Not sent with '
|
|
'Retry and Edit', (tester) async {
|
|
final gateway = _Gateway();
|
|
addTearDown(gateway.close);
|
|
final drafts = MemoryChatDraftStore();
|
|
await _host(tester, gateway, drafts);
|
|
|
|
await _send(tester, 'Did this arrive?');
|
|
expect(_state('sending'), findsOneWidget);
|
|
// The draft is gone from the composer but the message is on the device.
|
|
expect(drafts.entries, isEmpty);
|
|
expect(drafts.unsentEntries['s'], hasLength(1));
|
|
|
|
// Reload: a new thread and composer; the old request never answers.
|
|
await _host(tester, gateway, drafts, generation: 1);
|
|
expect(_state('failed'), findsOneWidget);
|
|
expect(find.text('Did this arrive?'), findsOneWidget);
|
|
expect(find.byKey(const Key('chat-delivery-edit')), findsOneWidget);
|
|
|
|
await tester.tap(find.byKey(const Key('chat-delivery-retry')));
|
|
await _pump(tester);
|
|
expect(gateway.promptBodies.last['operation_id'], 'op-0-1');
|
|
expect(gateway.promptBodies.last['native_message_id'], 'msg_op01');
|
|
gateway.prompts.last.complete({});
|
|
await _pump(tester);
|
|
expect(_state('sent'), findsOneWidget);
|
|
expect(drafts.unsentEntries, isEmpty);
|
|
});
|
|
|
|
testWidgets('Edit puts a reloaded unsent message back in the composer', (
|
|
tester,
|
|
) async {
|
|
final gateway = _Gateway();
|
|
addTearDown(gateway.close);
|
|
final drafts = MemoryChatDraftStore(
|
|
unsent: {
|
|
's': [
|
|
{
|
|
'session_id': 's',
|
|
'text': 'Fix the typo',
|
|
'operation_id': 'op-old',
|
|
'native_message_id': 'msg_opold',
|
|
},
|
|
],
|
|
},
|
|
);
|
|
await _host(tester, gateway, drafts);
|
|
expect(_state('failed'), findsOneWidget);
|
|
|
|
await tester.tap(find.byKey(const Key('chat-delivery-edit')));
|
|
await _pump(tester);
|
|
expect(_state('failed'), findsNothing);
|
|
final field = tester.widget<EditableText>(find.byType(EditableText));
|
|
expect(field.controller.text, 'Fix the typo');
|
|
expect(drafts.unsentEntries, isEmpty);
|
|
expect(gateway.promptBodies, isEmpty);
|
|
});
|
|
|
|
testWidgets('a stored message the history already records is cleared as '
|
|
'sent', (tester) async {
|
|
final gateway = _Gateway()
|
|
..messages = [
|
|
{
|
|
'id': 'msg_opold',
|
|
'role': 'user',
|
|
'parts': [
|
|
{'type': 'text', 'text': 'Already there'},
|
|
],
|
|
},
|
|
];
|
|
addTearDown(gateway.close);
|
|
final drafts = MemoryChatDraftStore(
|
|
unsent: {
|
|
's': [
|
|
{
|
|
'session_id': 's',
|
|
'text': 'Already there',
|
|
'operation_id': 'op-old',
|
|
'native_message_id': 'msg_opold',
|
|
},
|
|
],
|
|
},
|
|
);
|
|
await _host(tester, gateway, drafts);
|
|
|
|
expect(_state('failed'), findsNothing);
|
|
expect(find.text('Already there'), findsOneWidget);
|
|
expect(drafts.unsentEntries, isEmpty);
|
|
});
|
|
}
|