WIP: message delivery status and refresh safety; fix reconcile listener recursion
The transcript listener read the lazily built _controller while _attach was still building it (load() notifies synchronously), so it built another controller, recursively, until the test process ran out of memory.
This commit is contained in:
@@ -26,6 +26,13 @@ class _DelayedDraftStore implements ChatDraftStore {
|
||||
writes.add(text);
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Object?>> readUnsent(String sessionId) async => const [];
|
||||
|
||||
@override
|
||||
Future<bool> writeUnsent(String sessionId, List<Object?> intents) async =>
|
||||
true;
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
||||
@@ -90,41 +90,47 @@ void main() {
|
||||
expect(controller.sending, isFalse);
|
||||
});
|
||||
|
||||
test('a refused unchanged intent retries with the same identity', () async {
|
||||
var fail = true;
|
||||
gateway.prompt = (_, _) async {
|
||||
if (fail) throw StateError('response lost');
|
||||
return const {};
|
||||
};
|
||||
await controller.setDraft(' keep me ');
|
||||
test(
|
||||
'a refused intent is kept as Not sent and Retry reuses its identity',
|
||||
() async {
|
||||
var fail = true;
|
||||
gateway.prompt = (_, _) async {
|
||||
if (fail) throw StateError('response lost');
|
||||
return const {};
|
||||
};
|
||||
await controller.setDraft(' keep me ');
|
||||
|
||||
await controller.send();
|
||||
expect(controller.error, contains('response lost'));
|
||||
expect(controller.draft, ' keep me ');
|
||||
expect(await drafts.read('s'), ' keep me ');
|
||||
expect(rejected.single, contains('response lost'));
|
||||
await controller.send();
|
||||
expect(controller.error, contains('response lost'));
|
||||
// The composer is free for the next message; the refused one is kept.
|
||||
expect(controller.draft, '');
|
||||
expect(rejected.single, contains('response lost'));
|
||||
expect(controller.deliveries.unsent.single.operationId, 'op/1');
|
||||
expect(drafts.unsentEntries['s'], hasLength(1));
|
||||
|
||||
fail = false;
|
||||
await controller.send();
|
||||
expect(gateway.promptBodies.map((body) => body['operation_id']), [
|
||||
'op/1',
|
||||
'op/1',
|
||||
]);
|
||||
expect(gateway.promptBodies.map((body) => body['native_message_id']), [
|
||||
'msg_op1',
|
||||
'msg_op1',
|
||||
]);
|
||||
expect(controller.draft, '');
|
||||
expect(controller.error, isNull);
|
||||
fail = false;
|
||||
expect(await controller.retry('op/1'), isTrue);
|
||||
expect(gateway.promptBodies.map((body) => body['operation_id']), [
|
||||
'op/1',
|
||||
'op/1',
|
||||
]);
|
||||
expect(gateway.promptBodies.map((body) => body['native_message_id']), [
|
||||
'msg_op1',
|
||||
'msg_op1',
|
||||
]);
|
||||
expect(controller.error, isNull);
|
||||
expect(controller.deliveries.unsent, isEmpty);
|
||||
expect(drafts.unsentEntries, isEmpty);
|
||||
|
||||
fail = true;
|
||||
await controller.setDraft('other');
|
||||
await controller.send();
|
||||
expect(gateway.promptBodies.last['operation_id'], 'op/2');
|
||||
expect(gateway.promptBodies.last['native_message_id'], 'msg_op2');
|
||||
});
|
||||
fail = true;
|
||||
await controller.setDraft('other');
|
||||
await controller.send();
|
||||
expect(gateway.promptBodies.last['operation_id'], 'op/2');
|
||||
expect(gateway.promptBodies.last['native_message_id'], 'msg_op2');
|
||||
},
|
||||
);
|
||||
|
||||
test('a refusal keeps newer typing instead of restoring the draft', () async {
|
||||
test('a refusal keeps newer typing and the refused message apart', () async {
|
||||
final reply = Completer<Map<String, dynamic>>();
|
||||
gateway.prompt = (_, _) => reply.future;
|
||||
await controller.setDraft('original');
|
||||
@@ -140,8 +146,13 @@ void main() {
|
||||
expect(await drafts.read('s'), 'newer');
|
||||
expect(controller.error, contains('refused'));
|
||||
expect(rejected.single, contains('refused'));
|
||||
// The optimistic turn still carries the intent that was refused.
|
||||
expect(optimistic.single.text, 'original');
|
||||
expect(controller.deliveries.unsent.single.text, 'original');
|
||||
|
||||
// Edit puts the refused text after the newer typing and forgets it.
|
||||
await controller.edit('op/1');
|
||||
expect(controller.draft, 'newer\noriginal');
|
||||
expect(controller.deliveries.unsent, isEmpty);
|
||||
expect(drafts.unsentEntries, isEmpty);
|
||||
});
|
||||
|
||||
test(
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
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 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);
|
||||
expect(find.text('Please refuse'), findsOneWidget);
|
||||
expect(drafts.unsentEntries['s'], hasLength(1));
|
||||
|
||||
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(find.text('Please refuse'), findsOneWidget);
|
||||
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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user