235 lines
7.1 KiB
Dart
235 lines
7.1 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';
|
|
|
|
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);
|
|
},
|
|
);
|
|
|
|
testWidgets('Enter inserts a newline; Ctrl+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();
|
|
|
|
// A hardware Enter is left to the IME and never submits; the engine then
|
|
// inserts the newline as an editing value.
|
|
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
|
await tester.pump();
|
|
expect(gateway.promptBodies, isEmpty);
|
|
tester.testTextInput.updateEditingValue(
|
|
const TextEditingValue(
|
|
text: 'line one\n',
|
|
selection: TextSelection.collapsed(offset: 9),
|
|
),
|
|
);
|
|
await tester.pump();
|
|
expect(fieldText(tester), 'line one\n');
|
|
|
|
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
|
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
|
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(gateway.promptBodies.single['text'], 'line one');
|
|
});
|
|
|
|
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');
|
|
});
|
|
|
|
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));
|
|
});
|
|
|
|
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 optimistic turn is withdrawn, not left as a fake message.
|
|
expect(find.text('keep me'), 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);
|
|
|
|
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');
|
|
});
|
|
}
|