fix(chat): Return breaks lines on phones and align the activity line

On Android and iOS the keyboard's Return now inserts a newline and the
Send button sends; desktop keeps Enter to send. The Material activity
line drops TextButton's inset so it starts at the answer's edge now that
replies have no bubble.
This commit is contained in:
2026-10-30 20:07:27 +00:00
parent 027c2f800e
commit 44b2cff347
9 changed files with 349 additions and 174 deletions
@@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:math' as math; import 'dart:math' as math;
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'chat_stop_icon.dart'; import 'chat_stop_icon.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@@ -30,6 +31,16 @@ String chatComposerPlaceholderFor(String directory) {
return name.isEmpty ? chatComposerPlaceholder : 'Work on $name'; return name.isEmpty ? chatComposerPlaceholder : 'Work on $name';
} }
/// Whether Enter sends on [platform] (the running host by default).
///
/// On a phone the on-screen keyboard's Return breaks the line and the Send
/// button sends, as in other chat apps; a desktop keyboard's Enter sends and
/// Shift+Enter breaks the line. Ctrl/Cmd+Enter sends everywhere.
bool chatEnterSends([TargetPlatform? platform]) {
final target = platform ?? defaultTargetPlatform;
return target != TargetPlatform.android && target != TargetPlatform.iOS;
}
/// The single primary control's words while a turn is and is not running. /// The single primary control's words while a turn is and is not running.
const String chatSendLabel = 'Send'; const String chatSendLabel = 'Send';
const String chatStopLabel = 'Stop'; const String chatStopLabel = 'Stop';
@@ -82,8 +93,12 @@ class ChatComposerBar extends StatefulWidget {
this.paste, this.paste,
this.backgroundTasks, this.backgroundTasks,
this.placeholder = chatComposerPlaceholder, this.placeholder = chatComposerPlaceholder,
this.enterSends,
}); });
/// Overrides [chatEnterSends] for this composer; null follows the host.
final bool? enterSends;
final ChatComposerController composer; final ChatComposerController composer;
/// The empty field's hint; see [chatComposerPlaceholderFor]. /// The empty field's hint; see [chatComposerPlaceholderFor].
@@ -270,6 +285,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
if (_text.value.composing.isValid && !_text.value.composing.isCollapsed) { if (_text.value.composing.isValid && !_text.value.composing.isCollapsed) {
return KeyEventResult.ignored; return KeyEventResult.ignored;
} }
// A phone keyboard's Return is the field's own newline; only the
// modifier shortcut sends from the keyboard there.
if (!sendShortcut && !_enterSends) return KeyEventResult.ignored;
if (!sendShortcut && keyboard.isShiftPressed) { if (!sendShortcut && keyboard.isShiftPressed) {
if (event is KeyDownEvent) { if (event is KeyDownEvent) {
final selection = _text.selection; final selection = _text.selection;
@@ -292,6 +310,8 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
return KeyEventResult.handled; return KeyEventResult.handled;
} }
bool get _enterSends => widget.enterSends ?? chatEnterSends();
/// Hardware Enter, the keyboard's Send action and modifier shortcuts share /// Hardware Enter, the keyboard's Send action and modifier shortcuts share
/// the same command/prompt path. An active IME candidate is never submitted. /// the same command/prompt path. An active IME candidate is never submitted.
void _submitDraft() { void _submitDraft() {
@@ -939,7 +959,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
// strips Shift+Enter's newline on the next text edit. // strips Shift+Enter's newline on the next text edit.
maxLines: empty && lines > 1 ? lines : 6, maxLines: empty && lines > 1 ? lines : 6,
keyboardType: TextInputType.multiline, keyboardType: TextInputType.multiline,
textInputAction: TextInputAction.send, textInputAction: _enterSends
? TextInputAction.send
: TextInputAction.newline,
onEditingComplete: () {}, onEditingComplete: () {},
onSubmitted: (_) => _submitDraft(), onSubmitted: (_) => _submitDraft(),
onChanged: _onDraftEdited, onChanged: _onDraftEdited,
@@ -954,7 +976,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
minLines: 1, minLines: 1,
maxLines: 6, maxLines: 6,
keyboardType: TextInputType.multiline, keyboardType: TextInputType.multiline,
textInputAction: TextInputAction.send, textInputAction: _enterSends
? TextInputAction.send
: TextInputAction.newline,
onEditingComplete: () {}, onEditingComplete: () {},
onSubmitted: (_) => _submitDraft(), onSubmitted: (_) => _submitDraft(),
onChanged: _onDraftEdited, onChanged: _onDraftEdited,
@@ -186,6 +186,12 @@ class _ChatTurnActivityViewState extends State<ChatTurnActivityView> {
) )
: TextButton( : TextButton(
key: const Key('chat-turn-activity'), key: const Key('chat-turn-activity'),
// No inset of its own: the line starts at the answer's edge,
// which no longer sits inside a padded bubble.
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
alignment: AlignmentDirectional.centerStart,
),
onPressed: _open, onPressed: _open,
child: content, child: content,
), ),
+9
View File
@@ -30,3 +30,12 @@ Branch `chat/chatgpt-look`.
expand for details" activity label) fail on checks that predate this change. expand for details" activity label) fail on checks that predate this change.
The message probe's semantics dump does show the new `Copy` control. The message probe's semantics dump does show the new `Copy` control.
- Not verified on a device. - Not verified on a device.
## Follow-up: phone newline and activity alignment
- On Android and iOS (`chatEnterSends`), the keyboard's Return inserts a line
break (`TextInputAction.newline`) and Send sends; Ctrl/Cmd+Enter still sends.
Desktop keeps Enter to send and Shift+Enter for a newline. Desktop keyboard
tests run under `TargetPlatformVariant.only(TargetPlatform.linux)`.
- The Material activity line ("Ran 1 command ›") had TextButton's 12px inset,
which used to match the bubble padding; it now starts at the answer's edge.
@@ -4,6 +4,10 @@ import 'package:flutter_test/flutter_test.dart';
import 'chat_composer_bar_fixtures.dart'; import 'chat_composer_bar_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() { void main() {
late ComposerBarGateway gateway; late ComposerBarGateway gateway;
late int sequence; late int sequence;
@@ -34,52 +38,60 @@ void main() {
Finder palette() => find.byKey(const Key('chat-command-palette')); Finder palette() => find.byKey(const Key('chat-command-palette'));
Finder row(String name) => find.byKey(ValueKey<String>('chat-command-$name')); Finder row(String name) => find.byKey(ValueKey<String>('chat-command-$name'));
testWidgets('a leading slash opens the filtered recorded command palette', ( testWidgets(
tester, 'a leading slash opens the filtered recorded command palette',
) async { (tester) async {
await pumpComposer(tester); await pumpComposer(tester);
await tester.enterText(find.byKey(const Key('chat-composer-field')), '/'); await tester.enterText(find.byKey(const Key('chat-composer-field')), '/');
await tester.pump(); await tester.pump();
expect(palette(), findsOneWidget); expect(palette(), findsOneWidget);
for (final name in ['new', 'close', 'rename', 'stop', 'help']) { for (final name in ['new', 'close', 'rename', 'stop', 'help']) {
expect(row(name), findsOneWidget); expect(row(name), findsOneWidget);
} }
expect(find.text('/new'), findsOneWidget); expect(find.text('/new'), findsOneWidget);
expect( expect(
find.text('Start a fresh session in this directory'), find.text('Start a fresh session in this directory'),
findsOneWidget, findsOneWidget,
); );
await tester.enterText(find.byKey(const Key('chat-composer-field')), '/cl'); await tester.enterText(
await tester.pump(); find.byKey(const Key('chat-composer-field')),
'/cl',
);
await tester.pump();
expect(row('close'), findsOneWidget); expect(row('close'), findsOneWidget);
expect(row('new'), findsNothing); expect(row('new'), findsNothing);
expect(row('rename'), findsNothing); expect(row('rename'), findsNothing);
expect(row('stop'), findsNothing); expect(row('stop'), findsNothing);
expect(row('help'), findsNothing); expect(row('help'), findsNothing);
// Recognition is case-insensitive, and an argument or plain text hides it. // Recognition is case-insensitive, and an argument or plain text hides it.
await tester.enterText(find.byKey(const Key('chat-composer-field')), '/CL'); await tester.enterText(
await tester.pump(); find.byKey(const Key('chat-composer-field')),
expect(row('close'), findsOneWidget); '/CL',
);
await tester.pump();
expect(row('close'), findsOneWidget);
await tester.enterText( await tester.enterText(
find.byKey(const Key('chat-composer-field')), find.byKey(const Key('chat-composer-field')),
'/close now', '/close now',
); );
await tester.pump(); await tester.pump();
expect(palette(), findsNothing); expect(palette(), findsNothing);
await tester.enterText( await tester.enterText(
find.byKey(const Key('chat-composer-field')), find.byKey(const Key('chat-composer-field')),
'plain text', 'plain text',
); );
await tester.pump(); await tester.pump();
expect(palette(), findsNothing); expect(palette(), findsNothing);
}); },
variant: _desktop,
);
testWidgets('Escape closes the palette until the draft is edited again', ( testWidgets('Escape closes the palette until the draft is edited again', (
tester, tester,
@@ -112,7 +124,7 @@ void main() {
await tester.enterText(field, '/he'); await tester.enterText(field, '/he');
await tester.pump(); await tester.pump();
expect(palette(), findsOneWidget); expect(palette(), findsOneWidget);
}); }, variant: _desktop);
testWidgets('an unknown command reports locally and never prompts', ( testWidgets('an unknown command reports locally and never prompts', (
tester, tester,
@@ -160,27 +172,32 @@ void main() {
expect(find.byKey(const Key('chat-composer-command-notice')), findsNothing); expect(find.byKey(const Key('chat-composer-command-notice')), findsNothing);
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
}); }, variant: _desktop);
testWidgets('Send as message is the only route an unknown command takes', ( testWidgets(
tester, 'Send as message is the only route an unknown command takes',
) async { (tester) async {
await pumpComposer(tester); await pumpComposer(tester);
final field = find.byKey(const Key('chat-composer-field')); final field = find.byKey(const Key('chat-composer-field'));
await tester.enterText(field, '/nope'); await tester.enterText(field, '/nope');
await tester.pump(); await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
await tester.tap(find.byKey(const Key('chat-command-send-as-message'))); await tester.tap(find.byKey(const Key('chat-command-send-as-message')));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(gateway.promptBodies.single['text'], '/nope'); expect(gateway.promptBodies.single['text'], '/nope');
expect(fieldText(tester), ''); expect(fieldText(tester), '');
expect(find.byKey(const Key('chat-composer-command-notice')), findsNothing); expect(
}); find.byKey(const Key('chat-composer-command-notice')),
findsNothing,
);
},
variant: _desktop,
);
testWidgets('a completed command never becomes a prompt', (tester) async { testWidgets('a completed command never becomes a prompt', (tester) async {
await pumpComposer(tester); await pumpComposer(tester);
@@ -215,65 +232,72 @@ void main() {
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
expect(fieldText(tester), ''); expect(fieldText(tester), '');
expect(find.text('/close does not take an argument.'), findsOneWidget); expect(find.text('/close does not take an argument.'), findsOneWidget);
}); }, variant: _desktop);
testWidgets('/help consumes the draft and lists commands in recorded order', ( testWidgets(
tester, '/help consumes the draft and lists commands in recorded order',
) async { (tester) async {
await pumpComposer(tester); await pumpComposer(tester);
await tester.enterText( await tester.enterText(
find.byKey(const Key('chat-composer-field')), find.byKey(const Key('chat-composer-field')),
'/help', '/help',
); );
await tester.pump(); await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump(); await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(fieldText(tester), ''); expect(fieldText(tester), '');
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
expect( expect(
find.text( find.text(
'/new — Start a fresh session in this directory\n' '/new — Start a fresh session in this directory\n'
'/close — Finish this session for every client, keeping its history\n' '/close — Finish this session for every client, keeping its history\n'
'/rename <title> — Rename this session\n' '/rename <title> — Rename this session\n'
'/stop — Stop the turn that is running\n' '/stop — Stop the turn that is running\n'
'/help — List every command', '/help — List every command',
), ),
findsOneWidget, findsOneWidget,
); );
expect(find.text('Command'), findsOneWidget); expect(find.text('Command'), findsOneWidget);
await tester.tap(find.byKey(const Key('chat-command-dismiss'))); await tester.tap(find.byKey(const Key('chat-command-dismiss')));
await tester.pump(); await tester.pump();
expect(find.byKey(const Key('chat-composer-command-notice')), findsNothing); expect(
}); find.byKey(const Key('chat-composer-command-notice')),
findsNothing,
);
},
variant: _desktop,
);
testWidgets('/stop without a running turn and extra arguments fail locally', ( testWidgets(
tester, '/stop without a running turn and extra arguments fail locally',
) async { (tester) async {
await pumpComposer(tester); await pumpComposer(tester);
final field = find.byKey(const Key('chat-composer-field')); final field = find.byKey(const Key('chat-composer-field'));
await tester.enterText(field, '/stop'); await tester.enterText(field, '/stop');
await tester.pump(); await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump(); await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(fieldText(tester), ''); expect(fieldText(tester), '');
expect(gateway.interruptBodies, isEmpty); expect(gateway.interruptBodies, isEmpty);
expect(find.text('Command failed'), findsOneWidget); expect(find.text('Command failed'), findsOneWidget);
expect(find.text('No turn is running in this chat.'), findsOneWidget); expect(find.text('No turn is running in this chat.'), findsOneWidget);
await tester.enterText(field, '/help x'); await tester.enterText(field, '/help x');
await tester.pump(); await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(fieldText(tester), ''); expect(fieldText(tester), '');
expect(find.text('/help does not take an argument.'), findsOneWidget); expect(find.text('/help does not take an argument.'), findsOneWidget);
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
}); },
variant: _desktop,
);
testWidgets('/stop interrupts the current session through the composer', ( testWidgets('/stop interrupts the current session through the composer', (
tester, tester,
@@ -301,7 +325,7 @@ void main() {
expect(fieldText(tester), ''); expect(fieldText(tester), '');
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
expect(find.text('Stopping the running turn…'), findsOneWidget); expect(find.text('Stopping the running turn…'), findsOneWidget);
}); }, variant: _desktop);
testWidgets('a command picked from the palette is never sent either', ( testWidgets('a command picked from the palette is never sent either', (
tester, tester,
@@ -324,7 +348,7 @@ void main() {
expect(fieldText(tester), ''); expect(fieldText(tester), '');
expect(find.text('Command'), findsOneWidget); expect(find.text('Command'), findsOneWidget);
expect(find.textContaining('/help — List every command'), findsOneWidget); expect(find.textContaining('/help — List every command'), findsOneWidget);
}); }, variant: _desktop);
testWidgets('a row completes the command and never prompts', (tester) async { testWidgets('a row completes the command and never prompts', (tester) async {
await pumpComposer(tester); await pumpComposer(tester);
@@ -338,7 +362,7 @@ void main() {
expect(fieldText(tester), '/close'); expect(fieldText(tester), '/close');
expect(palette(), findsNothing); expect(palette(), findsNothing);
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
}); }, variant: _desktop);
testWidgets('Up/Down move the highlight and Enter completes it', ( testWidgets('Up/Down move the highlight and Enter completes it', (
tester, tester,
@@ -357,7 +381,7 @@ void main() {
expect(fieldText(tester), '/close'); expect(fieldText(tester), '/close');
expect(palette(), findsNothing); expect(palette(), findsNothing);
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
}); }, variant: _desktop);
testWidgets('ArrowUp wraps the highlight to the last command', ( testWidgets('ArrowUp wraps the highlight to the last command', (
tester, tester,
@@ -376,7 +400,7 @@ void main() {
expect(fieldText(tester), '/help'); expect(fieldText(tester), '/help');
expect(palette(), findsNothing); expect(palette(), findsNothing);
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
}); }, variant: _desktop);
testWidgets('the highlight resets when the filtered list shrinks', ( testWidgets('the highlight resets when the filtered list shrinks', (
tester, tester,
@@ -402,7 +426,7 @@ void main() {
expect(fieldText(tester), '/new'); expect(fieldText(tester), '/new');
expect(palette(), findsNothing); expect(palette(), findsNothing);
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
}); }, variant: _desktop);
testWidgets( testWidgets(
'the Cupertino host reports an unknown command with its controls', 'the Cupertino host reports an unknown command with its controls',
@@ -424,6 +448,7 @@ void main() {
expect(gateway.promptBodies.single['text'], '/nope'); expect(gateway.promptBodies.single['text'], '/nope');
expect(fieldText(tester), ''); expect(fieldText(tester), '');
}, },
variant: _desktop,
); );
testWidgets('Escape dismisses the local command report', (tester) async { testWidgets('Escape dismisses the local command report', (tester) async {
@@ -444,7 +469,7 @@ void main() {
expect(find.byKey(const Key('chat-composer-command-notice')), findsNothing); expect(find.byKey(const Key('chat-composer-command-notice')), findsNothing);
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
}); }, variant: _desktop);
testWidgets('the Cupertino host shows the same palette', (tester) async { testWidgets('the Cupertino host shows the same palette', (tester) async {
await pumpComposer(tester, cupertino: true); await pumpComposer(tester, cupertino: true);
@@ -461,7 +486,7 @@ void main() {
await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pump(); await tester.pump();
expect(palette(), findsNothing); expect(palette(), findsNothing);
}); }, variant: _desktop);
testWidgets('the palette fits a 320px phone at 2x text', (tester) async { testWidgets('the palette fits a 320px phone at 2x text', (tester) async {
tester.view.physicalSize = const Size(640, 1200); tester.view.physicalSize = const Size(640, 1200);
@@ -479,5 +504,5 @@ void main() {
expect(palette(), findsOneWidget); expect(palette(), findsOneWidget);
expect(row('new'), findsOneWidget); expect(row('new'), findsOneWidget);
expect(row('help'), findsOneWidget); expect(row('help'), findsOneWidget);
}); }, variant: _desktop);
} }
@@ -9,6 +9,10 @@ import 'package:pipelines_app/features/chat/conversation/chat_transcript_cache.d
import 'chat_composer_bar_fixtures.dart'; import 'chat_composer_bar_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);
/// Answers the session commands' create, close and rename requests. /// Answers the session commands' create, close and rename requests.
class _SessionGateway extends ComposerBarGateway { class _SessionGateway extends ComposerBarGateway {
String? refuse; String? refuse;
@@ -104,7 +108,7 @@ void main() {
expect(created?['id'], 'n1'); expect(created?['id'], 'n1');
expect(createdNotice, 'Started a new session “New chat” in /w.'); expect(createdNotice, 'Started a new session “New chat” in /w.');
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
}); }, variant: _desktop);
testWidgets('/new without a directory sends nothing and reports it', ( testWidgets('/new without a directory sends nothing and reports it', (
tester, tester,
@@ -120,7 +124,7 @@ void main() {
), ),
findsOneWidget, findsOneWidget,
); );
}); }, variant: _desktop);
testWidgets('/rename sends the trimmed title and reports it', (tester) async { testWidgets('/rename sends the trimmed title and reports it', (tester) async {
await pump(tester); await pump(tester);
@@ -130,7 +134,7 @@ void main() {
expect(body, {'session_id': 's', 'title': 'Fresh title'}); expect(body, {'session_id': 's', 'title': 'Fresh title'});
expect(renamed, 'Fresh title'); expect(renamed, 'Fresh title');
expect(find.text('Renamed this session to “Fresh title”.'), findsOneWidget); expect(find.text('Renamed this session to “Fresh title”.'), findsOneWidget);
}); }, variant: _desktop);
testWidgets('/rename without a title reports usage and sends nothing', ( testWidgets('/rename without a title reports usage and sends nothing', (
tester, tester,
@@ -143,7 +147,7 @@ void main() {
find.text('Usage: /rename <title> — renames this session.'), find.text('Usage: /rename <title> — renames this session.'),
findsOneWidget, findsOneWidget,
); );
}); }, variant: _desktop);
testWidgets('/close confirms, closes and a refusal keeps the reason', ( testWidgets('/close confirms, closes and a refusal keeps the reason', (
tester, tester,
@@ -164,7 +168,7 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(closed, 1); expect(closed, 1);
expect(find.text('chat is locked'), findsOneWidget); expect(find.text('chat is locked'), findsOneWidget);
}); }, variant: _desktop);
testWidgets('/close forgets the cached transcript, also after leaving', ( testWidgets('/close forgets the cached transcript, also after leaving', (
tester, tester,
@@ -182,5 +186,5 @@ void main() {
// Leaving the closed conversation does not write it back. // Leaving the closed conversation does not write it back.
await tester.pumpWidget(const SizedBox.shrink()); await tester.pumpWidget(const SizedBox.shrink());
expect(transcripts.sessionIds, ['other']); expect(transcripts.sessionIds, ['other']);
}); }, variant: _desktop);
} }
@@ -8,6 +8,10 @@ import 'package:pipelines_app/core/api_gateway.dart';
import 'chat_composer_bar_fixtures.dart'; import 'chat_composer_bar_fixtures.dart';
import 'chat_thread_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() { void main() {
late ComposerBarGateway gateway; late ComposerBarGateway gateway;
late int sequence; late int sequence;
@@ -89,6 +93,7 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(find.text('hello'), findsOneWidget); expect(find.text('hello'), findsOneWidget);
}, },
variant: _desktop,
); );
testWidgets('Shift+Enter inserts a newline; Enter sends', (tester) async { testWidgets('Shift+Enter inserts a newline; Enter sends', (tester) async {
@@ -113,7 +118,7 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(gateway.promptBodies.single['text'], 'line one'); expect(gateway.promptBodies.single['text'], 'line one');
}); }, variant: _desktop);
testWidgets('Cmd+Enter sends on the Cupertino host', (tester) async { testWidgets('Cmd+Enter sends on the Cupertino host', (tester) async {
await tester.pumpWidget( await tester.pumpWidget(
@@ -130,7 +135,7 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(gateway.promptBodies.single['text'], 'mac line'); expect(gateway.promptBodies.single['text'], 'mac line');
}); }, variant: _desktop);
testWidgets('Send is disabled for a blank draft and while sending', ( testWidgets('Send is disabled for a blank draft and while sending', (
tester, tester,
@@ -174,53 +179,55 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(primary().onPressed, isNotNull); expect(primary().onPressed, isNotNull);
expect(gateway.promptBodies, hasLength(1)); expect(gateway.promptBodies, hasLength(1));
}); }, variant: _desktop);
testWidgets('a refusal restores the draft visibly and reuses its intent', ( testWidgets(
tester, 'a refusal restores the draft visibly and reuses its intent',
) async { (tester) async {
var attempts = 0; var attempts = 0;
gateway.prompt = (_, _) async { gateway.prompt = (_, _) async {
if (++attempts == 1) throw StateError('response lost'); if (++attempts == 1) throw StateError('response lost');
return const {}; return const {};
}; };
await tester.pumpWidget( await tester.pumpWidget(
composerThread(gateway, newOperationId: nextOperation), composerThread(gateway, newOperationId: nextOperation),
); );
await tester.pumpAndSettle(); await tester.pumpAndSettle();
await tester.enterText( await tester.enterText(
find.byKey(const Key('chat-composer-field')), find.byKey(const Key('chat-composer-field')),
' keep me ', ' keep me ',
); );
await tester.pump(); await tester.pump();
await tester.tap(find.byKey(const Key('chat-composer-primary'))); await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(fieldText(tester), ' keep me '); expect(fieldText(tester), ' keep me ');
expect(find.byKey(const Key('chat-composer-error')), findsOneWidget); expect(find.byKey(const Key('chat-composer-error')), findsOneWidget);
expect(find.textContaining('response lost'), findsOneWidget); expect(find.textContaining('response lost'), findsOneWidget);
expect(gateway.promptBodies.single['operation_id'], 'op/1'); expect(gateway.promptBodies.single['operation_id'], 'op/1');
// The refused optimistic turn is withdrawn, not left as a fake message. // The refused optimistic turn is withdrawn, not left as a fake message.
expect(find.text('keep me'), findsNothing); expect(find.text('keep me'), findsNothing);
await tester.tap(find.byKey(const Key('chat-composer-primary'))); await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(gateway.promptBodies.map((body) => body['operation_id']), [ expect(gateway.promptBodies.map((body) => body['operation_id']), [
'op/1', 'op/1',
'op/1', 'op/1',
]); ]);
expect(fieldText(tester), ''); expect(fieldText(tester), '');
expect(find.byKey(const Key('chat-composer-error')), findsNothing); expect(find.byKey(const Key('chat-composer-error')), findsNothing);
await tester.enterText( await tester.enterText(
find.byKey(const Key('chat-composer-field')), find.byKey(const Key('chat-composer-field')),
'different', 'different',
); );
await tester.pump(); await tester.pump();
await tester.tap(find.byKey(const Key('chat-composer-primary'))); await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(gateway.promptBodies.last['operation_id'], 'op/2'); expect(gateway.promptBodies.last['operation_id'], 'op/2');
}); },
variant: _desktop,
);
} }
@@ -6,6 +6,10 @@ import 'package:flutter_test/flutter_test.dart';
import 'chat_composer_bar_fixtures.dart'; import 'chat_composer_bar_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() { void main() {
for (final cupertino in [false, true]) { for (final cupertino in [false, true]) {
for (final shortcut in ['enter', 'numpad', 'control', 'meta', 'software']) { for (final shortcut in ['enter', 'numpad', 'control', 'meta', 'software']) {
@@ -46,6 +50,7 @@ void main() {
expect(editor.focusNode.hasFocus, isTrue); expect(editor.focusNode.hasFocus, isTrue);
expect(editor.textInputAction, TextInputAction.send); expect(editor.textInputAction, TextInputAction.send);
}, },
variant: _desktop,
); );
} }
@@ -76,7 +81,7 @@ void main() {
const TextSelection.collapsed(offset: 2), const TextSelection.collapsed(offset: 2),
); );
expect(gateway.promptBodies, isEmpty); expect(gateway.promptBodies, isEmpty);
}); }, variant: _desktop);
testWidgets( testWidgets(
'Enter confirms IME composition without submitting ($cupertino)', 'Enter confirms IME composition without submitting ($cupertino)',
@@ -108,6 +113,7 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(gateway.promptBodies.single['text'], '仮'); expect(gateway.promptBodies.single['text'], '仮');
}, },
variant: _desktop,
); );
} }
@@ -137,5 +143,6 @@ void main() {
reply.complete({}); reply.complete({});
await tester.pumpAndSettle(); await tester.pumpAndSettle();
}, },
variant: _desktop,
); );
} }
@@ -0,0 +1,78 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart';
import 'chat_composer_bar_fixtures.dart';
final _phones = TargetPlatformVariant(const {
TargetPlatform.android,
TargetPlatform.iOS,
});
void main() {
test('Enter sends only on a desktop host', () {
expect(chatEnterSends(TargetPlatform.android), isFalse);
expect(chatEnterSends(TargetPlatform.iOS), isFalse);
for (final desktop in [
TargetPlatform.linux,
TargetPlatform.macOS,
TargetPlatform.windows,
TargetPlatform.fuchsia,
]) {
expect(chatEnterSends(desktop), isTrue);
}
});
for (final cupertino in [false, true]) {
final style = cupertino ? 'Cupertino' : 'Material';
testWidgets('a phone keyboard breaks lines and Send sends ($style)', (
tester,
) async {
final gateway = ComposerBarGateway()..page = fixtureTurn();
addTearDown(gateway.close);
await tester.pumpWidget(composerThread(gateway, cupertino: cupertino));
await tester.pumpAndSettle();
final field = find.byKey(const Key('chat-composer-field'));
final editor = tester.widget<EditableText>(
find.descendant(of: field, matching: find.byType(EditableText)),
);
expect(editor.textInputAction, TextInputAction.newline);
await tester.enterText(field, 'line one');
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(gateway.promptBodies, isEmpty);
expect(fieldText(tester), startsWith('line one'));
await tester.enterText(field, 'line one\nline two');
await tester.pump();
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle();
expect(gateway.promptBodies.single['text'], 'line one\nline two');
}, variant: _phones);
testWidgets(
'Ctrl+Enter still sends from a phone keyboard ($style)',
(tester) async {
final gateway = ComposerBarGateway()..page = fixtureTurn();
addTearDown(gateway.close);
await tester.pumpWidget(composerThread(gateway, cupertino: cupertino));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('chat-composer-field')),
'message',
);
await tester.pump();
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pumpAndSettle();
expect(gateway.promptBodies.single['text'], 'message');
},
variant: TargetPlatformVariant.only(TargetPlatform.android),
);
}
}
@@ -64,6 +64,21 @@ void main() {
); );
}); });
testWidgets('the activity line starts at the answer\'s edge ($style)', (
tester,
) async {
await tester.pumpWidget(_host([_answer], cupertino: cupertino));
final activity = find.descendant(
of: find.byKey(const Key('chat-turn-activity')),
matching: find.byType(Text),
);
expect(
tester.getTopLeft(activity.first).dx,
tester.getTopLeft(find.text('First paragraph.')).dx,
);
});
testWidgets('a finished reply copies its answer text ($style)', ( testWidgets('a finished reply copies its answer text ($style)', (
tester, tester,
) async { ) async {