diff --git a/app/lib/features/chat/conversation/chat_deliveries.dart b/app/lib/features/chat/conversation/chat_deliveries.dart index b04eb826..5eeaf22f 100644 --- a/app/lib/features/chat/conversation/chat_deliveries.dart +++ b/app/lib/features/chat/conversation/chat_deliveries.dart @@ -243,7 +243,10 @@ class ChatDeliveries extends ChangeNotifier { for (final intent in intents) { if (_states.containsKey(intent.operationId)) continue; _remember(intent); - _states[intent.operationId] = ChatDelivery.failed; + // Unchecked, not failed: it shows as sending until [reconcile] has read + // the transcript, so a message the server did record never flashes + // "Not sent" while the chat opens. + _states[intent.operationId] = ChatDelivery.sending; _unverified.add(intent); changed = true; } @@ -264,6 +267,11 @@ class ChatDeliveries extends ChangeNotifier { for (final intent in confirmed) { _states[intent.operationId] = ChatDelivery.sent; } + for (final intent in _unverified) { + if (!confirmed.contains(intent)) { + _states[intent.operationId] = ChatDelivery.failed; + } + } _unsent.removeWhere(confirmed.contains); _unverified.removeWhere(confirmed.contains); _unsent.addAll(_unverified); diff --git a/app/test/features/chat/conversation/chat_deliveries_restore_test.dart b/app/test/features/chat/conversation/chat_deliveries_restore_test.dart new file mode 100644 index 00000000..17660a07 --- /dev/null +++ b/app/test/features/chat/conversation/chat_deliveries_restore_test.dart @@ -0,0 +1,33 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_composer_send.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_deliveries.dart'; + +const _send = ChatComposerSend( + sessionId: 's', + text: 'Did it land?', + operationId: 'op-1', + nativeMessageId: 'msg_op-1', +); + +final _message = {'role': 'user', 'id': 'msg_op-1', 'operation_id': 'op-1'}; + +void main() { + test('a restored unchecked send never shows "Not sent" before the history ' + 'read; the server having it makes it sent', () { + final deliveries = ChatDeliveries()..restore([_send]); + expect(deliveries.stateFor(_message), ChatDelivery.sending); + deliveries.reconcile([_message]); + // Settled as sent: an older accepted send is drawn quietly. + expect(deliveries.stateFor(_message), isNull); + expect(deliveries.unsent, isEmpty); + deliveries.dispose(); + }); + + test('a restored send the history does not have becomes "Not sent"', () { + final deliveries = ChatDeliveries()..restore([_send]); + deliveries.reconcile(const []); + expect(deliveries.stateFor(_message), ChatDelivery.failed); + expect(deliveries.unsent, [_send]); + deliveries.dispose(); + }); +}