70cdf985c5
The thread reads the session's queued prompts from the chat_subscribe reply and chat_queue frames: a reload draws them as Queued after the transcript, a delivered one enters the transcript as Sent, a refused one becomes Not sent, and Remove sends chat_unqueue. A prompt the server reports queued is dropped from the device unsent store; the any-frame queued heuristic is used only with a server that does not report its queue.
214 lines
6.7 KiB
Dart
214 lines
6.7 KiB
Dart
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_composer_send.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 server holds [queue] for session `s` and answers
|
|
/// `chat_unqueue` by removing the named prompt.
|
|
class _Gateway extends FakeChatGateway {
|
|
List<JsonMap> queue = [];
|
|
final unqueued = <String>[];
|
|
String? refuseUnqueue;
|
|
|
|
_Gateway() {
|
|
history = (_) async => chatPage([
|
|
chatMessage(
|
|
'm1',
|
|
'assistant',
|
|
parts: [
|
|
{'type': 'text', 'id': 'm1#t', 'text': 'Working on it'},
|
|
],
|
|
),
|
|
]);
|
|
subscribe = () async => {'chat_queue': queueBody()};
|
|
}
|
|
|
|
JsonMap queueBody({String? delivered, String? removed}) => {
|
|
'session_id': 's',
|
|
'prompts': queue,
|
|
'delivered': ?delivered,
|
|
'removed': ?removed,
|
|
};
|
|
|
|
@override
|
|
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
|
if (type == 'chat_unqueue') {
|
|
calls.add((type: type, fields: fields));
|
|
final body = fields['chat_unqueue'] as JsonMap;
|
|
final operation = '${body['operation_id']}';
|
|
if (refuseUnqueue != null) throw StateError(refuseUnqueue!);
|
|
unqueued.add(operation);
|
|
queue = [
|
|
for (final prompt in queue)
|
|
if (prompt['operation_id'] != operation) prompt,
|
|
];
|
|
return {'chat_queue': queueBody(removed: operation)};
|
|
}
|
|
return super.call(type, fields);
|
|
}
|
|
}
|
|
|
|
JsonMap _prompt(String operation, String text, {String client = 'Phone'}) => {
|
|
'operation_id': operation,
|
|
'message_id': 'msg_$operation',
|
|
'native_message_id': 'msg_$operation',
|
|
'text': text,
|
|
'client': client,
|
|
};
|
|
|
|
Future<void> _pump(WidgetTester tester) async {
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 100));
|
|
await tester.pump();
|
|
}
|
|
|
|
Future<void> _host(
|
|
WidgetTester tester,
|
|
_Gateway gateway,
|
|
MemoryChatDraftStore drafts, {
|
|
bool cupertino = false,
|
|
}) async {
|
|
final thread = ChatThreadView(
|
|
gateway: gateway,
|
|
sessionId: 's',
|
|
onBack: () {},
|
|
drafts: drafts,
|
|
newOperationId: () => 'op-new',
|
|
);
|
|
await tester.pumpWidget(
|
|
cupertino
|
|
? CupertinoApp(home: CupertinoPageScaffold(child: thread))
|
|
: MaterialApp(home: Scaffold(body: thread)),
|
|
);
|
|
await _pump(tester);
|
|
}
|
|
|
|
Finder _state(String name) => find.byKey(Key('chat-delivery-$name'));
|
|
|
|
void main() {
|
|
for (final cupertino in [false, true]) {
|
|
testWidgets('a reload draws the server-queued prompts as Queued '
|
|
'($cupertino)', (tester) async {
|
|
final gateway = _Gateway()
|
|
..queue = [_prompt('op-1', 'First queued'), _prompt('op-2', 'Second')];
|
|
addTearDown(gateway.close);
|
|
await _host(
|
|
tester,
|
|
gateway,
|
|
MemoryChatDraftStore(),
|
|
cupertino: cupertino,
|
|
);
|
|
|
|
expect(find.text('First queued'), findsOneWidget);
|
|
expect(find.text('Second'), findsOneWidget);
|
|
expect(_state('queued'), findsNWidgets(2));
|
|
expect(find.byKey(const Key('chat-delivery-remove')), findsNWidgets(2));
|
|
// They follow the transcript: nothing queued has reached the harness.
|
|
expect(
|
|
tester.getTopLeft(find.text('First queued')).dy,
|
|
greaterThan(tester.getTopLeft(find.text('Working on it')).dy),
|
|
);
|
|
});
|
|
}
|
|
|
|
testWidgets('a delivered queued prompt enters the transcript as Sent', (
|
|
tester,
|
|
) async {
|
|
final gateway = _Gateway()..queue = [_prompt('op-1', 'Do this next')];
|
|
addTearDown(gateway.close);
|
|
await _host(tester, gateway, MemoryChatDraftStore());
|
|
expect(_state('queued'), findsOneWidget);
|
|
|
|
// The server re-publishes the user turn as it hands it to the harness,
|
|
// then reports the queue without it.
|
|
gateway.emitEvent({
|
|
'session_id': 's',
|
|
'kind': 'user',
|
|
'message_id': 'msg_op-1',
|
|
'operation_id': 'op-1',
|
|
'client': 'Phone',
|
|
'part': {'type': 'text', 'id': 'usr', 'text': 'Do this next'},
|
|
});
|
|
gateway.queue = [];
|
|
gateway.emitRaw({
|
|
'type': 'chat_queue',
|
|
'chat_queue': gateway.queueBody(delivered: 'op-1'),
|
|
});
|
|
await _pump(tester);
|
|
|
|
expect(find.text('Do this next'), findsOneWidget);
|
|
expect(_state('queued'), findsNothing);
|
|
expect(_state('sent'), findsOneWidget);
|
|
expect(find.byKey(const Key('chat-delivery-remove')), findsNothing);
|
|
});
|
|
|
|
testWidgets('Remove takes a queued prompt off the server queue', (
|
|
tester,
|
|
) async {
|
|
final gateway = _Gateway()
|
|
..queue = [_prompt('op-1', 'Keep me'), _prompt('op-2', 'Drop me')];
|
|
addTearDown(gateway.close);
|
|
await _host(tester, gateway, MemoryChatDraftStore());
|
|
|
|
final dropRow = find.byKey(const ValueKey<String>('chat-queued-op-2'));
|
|
await tester.tap(
|
|
find.descendant(
|
|
of: dropRow,
|
|
matching: find.byKey(const Key('chat-delivery-remove')),
|
|
),
|
|
);
|
|
await _pump(tester);
|
|
|
|
expect(gateway.unqueued, ['op-2']);
|
|
expect(find.text('Drop me'), findsNothing);
|
|
expect(find.text('Keep me'), findsOneWidget);
|
|
expect(_state('queued'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('a Remove the server refuses stays visible', (tester) async {
|
|
final gateway = _Gateway()
|
|
..queue = [_prompt('op-1', 'Too late')]
|
|
..refuseUnqueue = 'that message is no longer queued; it was already sent';
|
|
addTearDown(gateway.close);
|
|
await _host(tester, gateway, MemoryChatDraftStore());
|
|
|
|
await tester.tap(find.byKey(const Key('chat-delivery-remove')));
|
|
await _pump(tester);
|
|
expect(find.textContaining('already sent'), findsOneWidget);
|
|
expect(find.text('Too late'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('an own send the server queued is not kept on the device', (
|
|
tester,
|
|
) async {
|
|
final gateway = _Gateway();
|
|
addTearDown(gateway.close);
|
|
// A previous visit stored the intent but never saw its acknowledgement;
|
|
// the server's queue says it holds it.
|
|
final stored = const ChatComposerSend(
|
|
sessionId: 's',
|
|
text: 'Sent before reload',
|
|
operationId: 'op-old',
|
|
nativeMessageId: 'msg_op-old',
|
|
);
|
|
final drafts = MemoryChatDraftStore(
|
|
unsent: {
|
|
's': [stored.toJson()],
|
|
},
|
|
);
|
|
gateway.queue = [_prompt('op-old', 'Sent before reload')];
|
|
await _host(tester, gateway, drafts);
|
|
await _pump(tester);
|
|
|
|
expect(find.text('Sent before reload'), findsOneWidget);
|
|
expect(_state('queued'), findsOneWidget);
|
|
expect(_state('failed'), findsNothing);
|
|
expect(drafts.unsentEntries['s'], isNull);
|
|
});
|
|
}
|