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.
238 lines
7.6 KiB
Dart
238 lines
7.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/core/api_gateway.dart';
|
|
|
|
import 'chat_composer_bar_fixtures.dart';
|
|
import 'chat_thread_fixtures.dart';
|
|
|
|
/// These tests drive a desktop keyboard, where Enter sends; on a phone the
|
|
/// on-screen keyboard's Return breaks the line instead.
|
|
final _desktop = TargetPlatformVariant.only(TargetPlatform.linux);
|
|
|
|
void main() {
|
|
late ComposerBarGateway gateway;
|
|
late int sequence;
|
|
|
|
String nextOperation() => 'op/${++sequence}';
|
|
|
|
setUp(() {
|
|
gateway = ComposerBarGateway()..page = fixtureTurn();
|
|
sequence = 0;
|
|
});
|
|
|
|
tearDown(() => gateway.close());
|
|
|
|
testWidgets(
|
|
'Send posts the exact trimmed prompt identity and shows it once',
|
|
(tester) async {
|
|
await tester.pumpWidget(
|
|
composerThread(gateway, newOperationId: nextOperation),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.enterText(
|
|
find.byKey(const Key('chat-composer-field')),
|
|
' hello ',
|
|
);
|
|
await tester.pump();
|
|
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(gateway.promptBodies.single, {
|
|
'session_id': 's',
|
|
'text': 'hello',
|
|
'operation_id': 'op/1',
|
|
'native_message_id': 'msg_op1',
|
|
});
|
|
expect(fieldText(tester), '');
|
|
expect(find.text('hello'), findsOneWidget);
|
|
|
|
// The live user frame carries the same intent; the local row disappears
|
|
// instead of drawing the operator's turn twice.
|
|
gateway.emitEvent({
|
|
'session_id': 's',
|
|
'message_id': 'msg_op1',
|
|
'kind': 'user',
|
|
'operation_id': 'op/1',
|
|
'part': {'type': 'text', 'text': 'hello'},
|
|
});
|
|
await tester.pump();
|
|
expect(find.text('hello'), findsOneWidget);
|
|
|
|
// A history resync that includes the stored prompt keeps exactly one row.
|
|
gateway.page = chatPage([
|
|
{
|
|
'id': 'u1',
|
|
'role': 'user',
|
|
'parts': [
|
|
{'type': 'text', 'text': 'Stored question'},
|
|
],
|
|
},
|
|
{
|
|
'id': 'a1',
|
|
'role': 'assistant',
|
|
'parts': [
|
|
{'type': 'text', 'text': 'Stored answer'},
|
|
],
|
|
},
|
|
{
|
|
'id': 'msg_op1',
|
|
'role': 'user',
|
|
'operation_id': 'op/1',
|
|
'parts': [
|
|
{'type': 'text', 'text': 'hello'},
|
|
],
|
|
},
|
|
]);
|
|
await tester.pumpWidget(
|
|
composerThread(gateway, syncToken: 1, newOperationId: nextOperation),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('hello'), findsOneWidget);
|
|
},
|
|
variant: _desktop,
|
|
);
|
|
|
|
testWidgets('Shift+Enter inserts a newline; Enter sends', (tester) async {
|
|
await tester.pumpWidget(
|
|
composerThread(gateway, newOperationId: nextOperation),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
final field = find.byKey(const Key('chat-composer-field'));
|
|
await tester.enterText(field, 'line one');
|
|
await tester.pump();
|
|
|
|
// Shift+Enter inserts a newline without triggering the keyboard Send action.
|
|
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
|
|
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
|
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
|
|
await tester.pump();
|
|
expect(gateway.promptBodies, isEmpty);
|
|
expect(fieldText(tester), 'line one\n');
|
|
|
|
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(gateway.promptBodies.single['text'], 'line one');
|
|
}, variant: _desktop);
|
|
|
|
testWidgets('Cmd+Enter sends on the Cupertino host', (tester) async {
|
|
await tester.pumpWidget(
|
|
composerThread(gateway, cupertino: true, newOperationId: nextOperation),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
final field = find.byKey(const Key('chat-composer-field'));
|
|
await tester.enterText(field, 'mac line');
|
|
await tester.pump();
|
|
await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft);
|
|
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
|
await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft);
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(gateway.promptBodies.single['text'], 'mac line');
|
|
}, variant: _desktop);
|
|
|
|
testWidgets('Send is disabled for a blank draft and while sending', (
|
|
tester,
|
|
) async {
|
|
final reply = Completer<JsonMap>();
|
|
gateway.prompt = (_, _) => reply.future;
|
|
await tester.pumpWidget(
|
|
composerThread(gateway, newOperationId: nextOperation),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
IconButton primary() => tester.widget<IconButton>(
|
|
find.byKey(const Key('chat-composer-primary')),
|
|
);
|
|
|
|
expect(primary().onPressed, isNull);
|
|
await tester.enterText(find.byKey(const Key('chat-composer-field')), ' ');
|
|
await tester.pump();
|
|
expect(primary().onPressed, isNull);
|
|
|
|
await tester.enterText(
|
|
find.byKey(const Key('chat-composer-field')),
|
|
'ready',
|
|
);
|
|
await tester.pump();
|
|
expect(primary().onPressed, isNotNull);
|
|
|
|
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
|
await tester.pump();
|
|
expect(gateway.promptBodies, hasLength(1));
|
|
|
|
// Newer text cannot start a second prompt while the first awaits its reply.
|
|
await tester.enterText(
|
|
find.byKey(const Key('chat-composer-field')),
|
|
'second',
|
|
);
|
|
await tester.pump();
|
|
expect(primary().onPressed, isNull);
|
|
|
|
reply.complete(const {});
|
|
await tester.pumpAndSettle();
|
|
expect(primary().onPressed, isNotNull);
|
|
expect(gateway.promptBodies, hasLength(1));
|
|
}, variant: _desktop);
|
|
|
|
testWidgets(
|
|
'a refusal restores the draft visibly and reuses its intent',
|
|
(tester) async {
|
|
var attempts = 0;
|
|
gateway.prompt = (_, _) async {
|
|
if (++attempts == 1) throw StateError('response lost');
|
|
return const {};
|
|
};
|
|
await tester.pumpWidget(
|
|
composerThread(gateway, newOperationId: nextOperation),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.enterText(
|
|
find.byKey(const Key('chat-composer-field')),
|
|
' keep me ',
|
|
);
|
|
await tester.pump();
|
|
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(fieldText(tester), ' keep me ');
|
|
expect(find.byKey(const Key('chat-composer-error')), findsOneWidget);
|
|
expect(find.textContaining('response lost'), findsOneWidget);
|
|
expect(gateway.promptBodies.single['operation_id'], 'op/1');
|
|
// The refused turn says it was not sent; its text is back in the
|
|
// composer, so it offers Retry but not Edit.
|
|
expect(find.byKey(const Key('chat-delivery-failed')), findsOneWidget);
|
|
expect(find.byKey(const Key('chat-delivery-retry')), findsOneWidget);
|
|
expect(find.byKey(const Key('chat-delivery-edit')), findsNothing);
|
|
|
|
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(gateway.promptBodies.map((body) => body['operation_id']), [
|
|
'op/1',
|
|
'op/1',
|
|
]);
|
|
expect(fieldText(tester), '');
|
|
expect(find.byKey(const Key('chat-composer-error')), findsNothing);
|
|
expect(find.byKey(const Key('chat-delivery-failed')), findsNothing);
|
|
|
|
await tester.enterText(
|
|
find.byKey(const Key('chat-composer-field')),
|
|
'different',
|
|
);
|
|
await tester.pump();
|
|
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
|
await tester.pumpAndSettle();
|
|
expect(gateway.promptBodies.last['operation_id'], 'op/2');
|
|
},
|
|
variant: _desktop,
|
|
);
|
|
}
|