8d147ac83b
A sent message is stored on the device until the server accepts it, so a reload mid-request no longer loses it: on reopening, a stored intent the history records is cleared as sent, otherwise it shows as Not sent with Retry (same operation id) and Edit. The operator's bubble shows Sending, Sent (newest only), Queued or Not sent. A live refusal keeps the old contract (exact draft and chips back in the composer, unchanged re-send reuses the id) and marks the bubble Not sent with Retry. On web, leaving or reloading the page while a prompt is in flight asks first.
257 lines
8.1 KiB
Dart
257 lines
8.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_composer_controller.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
|
|
|
import 'chat_composer_fixtures.dart';
|
|
|
|
void main() {
|
|
late FakeComposerGateway gateway;
|
|
late MemoryChatDraftStore drafts;
|
|
late ChatComposerController controller;
|
|
late List<ChatComposerSend> optimistic;
|
|
late List<String> rejected;
|
|
late int sequence;
|
|
|
|
setUp(() {
|
|
gateway = FakeComposerGateway();
|
|
drafts = MemoryChatDraftStore();
|
|
optimistic = [];
|
|
rejected = [];
|
|
sequence = 0;
|
|
controller = ChatComposerController(
|
|
gateway,
|
|
's',
|
|
drafts: drafts,
|
|
newOperationId: () => 'op/${++sequence}',
|
|
onOptimistic: optimistic.add,
|
|
onRejected: (send, error) => rejected.add(error),
|
|
);
|
|
});
|
|
|
|
tearDown(() {
|
|
controller.dispose();
|
|
gateway.close();
|
|
});
|
|
|
|
test('sends the exact trimmed wire identity and clears the draft', () async {
|
|
await controller.setDraft(' hello ');
|
|
|
|
await controller.send();
|
|
|
|
expect(gateway.calls, hasLength(1));
|
|
expect(gateway.calls.single.type, 'chat_prompt');
|
|
expect(gateway.calls.single.fields, {
|
|
'chat_prompt': {
|
|
'session_id': 's',
|
|
'text': 'hello',
|
|
'operation_id': 'op/1',
|
|
'native_message_id': 'msg_op1',
|
|
},
|
|
});
|
|
expect(controller.draft, '');
|
|
expect(drafts.entries, isEmpty);
|
|
expect(controller.sending, isFalse);
|
|
expect(controller.error, isNull);
|
|
expect(optimistic.single.text, 'hello');
|
|
expect(optimistic.single.operationId, 'op/1');
|
|
expect(optimistic.single.nativeMessageId, 'msg_op1');
|
|
expect(optimistic.single.optimisticId, 'msg_op1');
|
|
});
|
|
|
|
test('a whitespace-only draft sends nothing', () async {
|
|
await controller.setDraft(' \n ');
|
|
|
|
await controller.send();
|
|
|
|
expect(gateway.calls, isEmpty);
|
|
expect(optimistic, isEmpty);
|
|
expect(controller.draft, ' \n ');
|
|
expect(controller.sending, isFalse);
|
|
});
|
|
|
|
test('a second send is refused while one request is in flight', () async {
|
|
final reply = Completer<Map<String, dynamic>>();
|
|
gateway.prompt = (_, _) => reply.future;
|
|
await controller.setDraft('one');
|
|
|
|
final first = controller.send();
|
|
await pumpEventQueue();
|
|
expect(controller.sending, isTrue);
|
|
expect(gateway.promptBodies, hasLength(1));
|
|
|
|
await controller.send();
|
|
expect(gateway.promptBodies, hasLength(1));
|
|
expect(optimistic, hasLength(1));
|
|
|
|
reply.complete(const {});
|
|
await first;
|
|
expect(controller.sending, isFalse);
|
|
});
|
|
|
|
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'));
|
|
// The exact draft is back in the composer, and the message is Not sent.
|
|
expect(controller.draft, ' keep me ');
|
|
expect(await drafts.read('s'), ' keep me ');
|
|
expect(rejected.single, contains('response lost'));
|
|
expect(controller.deliveries.unsent.single.operationId, 'op/1');
|
|
expect(controller.isInComposer('op/1'), isTrue);
|
|
// The draft store keeps it across a reload, so no second stored copy.
|
|
expect(drafts.unsentEntries, isEmpty);
|
|
|
|
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.draft, '');
|
|
expect(controller.error, isNull);
|
|
expect(controller.deliveries.unsent, isEmpty);
|
|
expect(drafts.unsentEntries, isEmpty);
|
|
expect(controller.composerSendsAccepted, 1);
|
|
|
|
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 and the refused message apart', () async {
|
|
final reply = Completer<Map<String, dynamic>>();
|
|
gateway.prompt = (_, _) => reply.future;
|
|
await controller.setDraft('original');
|
|
|
|
final sending = controller.send();
|
|
await pumpEventQueue();
|
|
await controller.setDraft('newer');
|
|
|
|
reply.completeError(StateError('refused'));
|
|
await sending;
|
|
|
|
expect(controller.draft, 'newer');
|
|
expect(await drafts.read('s'), 'newer');
|
|
expect(controller.error, contains('refused'));
|
|
expect(rejected.single, contains('refused'));
|
|
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(
|
|
'takes the queued flag from the reply and clears it on a live frame',
|
|
() async {
|
|
gateway.prompt = (_, _) async => {'chat_queued': true};
|
|
|
|
await controller.setDraft('first');
|
|
await controller.send();
|
|
expect(controller.queued, isTrue);
|
|
|
|
gateway.emitEvent({'session_id': 'other', 'kind': 'text'});
|
|
await pumpEventQueue();
|
|
expect(controller.queued, isTrue, reason: 'not this conversation');
|
|
|
|
gateway.emitEvent({'session_id': 's', 'kind': 'usage'});
|
|
gateway.emitEvent({'session_id': 's', 'kind': 'compaction'});
|
|
await pumpEventQueue();
|
|
expect(controller.queued, isTrue, reason: 'no turn moved on');
|
|
|
|
gateway.emitEvent({'session_id': 's', 'kind': 'step'});
|
|
await pumpEventQueue();
|
|
expect(controller.queued, isFalse);
|
|
},
|
|
);
|
|
|
|
test('a reply without chat_queued clears a previous queued flag', () async {
|
|
gateway.prompt = (_, _) async => {'chat_queued': true};
|
|
await controller.setDraft('first');
|
|
await controller.send();
|
|
expect(controller.queued, isTrue);
|
|
|
|
gateway.prompt = (_, _) async => const {};
|
|
await controller.setDraft('second');
|
|
await controller.send();
|
|
expect(controller.queued, isFalse);
|
|
});
|
|
|
|
test(
|
|
'Stop sends the exact interrupt query and leaves state to the frame',
|
|
() async {
|
|
gateway.prompt = (_, _) async => {'chat_queued': true};
|
|
await controller.setDraft('queued');
|
|
await controller.send();
|
|
|
|
await controller.stop();
|
|
|
|
final stop = gateway.calls.singleWhere(
|
|
(call) => call.type == 'chat_interrupt',
|
|
);
|
|
expect(stop.fields, {
|
|
'chat_query': {'session_id': 's'},
|
|
});
|
|
expect(controller.error, isNull);
|
|
expect(controller.stopping, isFalse);
|
|
// Nothing settles the queued prompt behind the composer's back.
|
|
expect(controller.queued, isTrue);
|
|
},
|
|
);
|
|
|
|
test('a refused Stop stays visible and settles nothing', () async {
|
|
gateway.prompt = (_, _) async => {'chat_queued': true};
|
|
await controller.setDraft('queued');
|
|
await controller.send();
|
|
gateway.interrupt = () async => throw StateError('cannot stop');
|
|
|
|
await controller.stop();
|
|
|
|
expect(controller.error, contains('cannot stop'));
|
|
expect(controller.stopping, isFalse);
|
|
expect(controller.queued, isTrue);
|
|
expect(controller.sending, isFalse);
|
|
});
|
|
|
|
test('a late reply after disposal changes and reports nothing', () async {
|
|
final reply = Completer<Map<String, dynamic>>();
|
|
gateway.prompt = (_, _) => reply.future;
|
|
await controller.setDraft('bye');
|
|
|
|
final sending = controller.send();
|
|
await pumpEventQueue();
|
|
expect(optimistic, hasLength(1));
|
|
|
|
controller.dispose();
|
|
gateway.emitEvent({'session_id': 's', 'kind': 'step'});
|
|
await controller.send();
|
|
expect(gateway.promptBodies, hasLength(1));
|
|
|
|
reply.completeError(StateError('late response'));
|
|
await sending;
|
|
|
|
expect(rejected, isEmpty);
|
|
expect(controller.error, isNull);
|
|
expect(controller.draft, '');
|
|
});
|
|
}
|