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 optimistic; late List 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>(); 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 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 '); 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')); 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 = 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 { final reply = Completer>(); 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')); // The optimistic turn still carries the intent that was refused. expect(optimistic.single.text, 'original'); }); 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>(); 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, ''); }); }