Support unfocused chat composer paste

This commit is contained in:
2026-11-07 22:32:07 +00:00
parent f6ca558302
commit 63758cd176
12 changed files with 619 additions and 35 deletions
@@ -20,20 +20,26 @@ class _FakePaste {
ChatPasteFiles? onFiles;
ChatPasteCanAccept? canAccept;
ChatPasteFailed? onError;
ChatPasteText? onText;
ChatPasteCanAcceptText? canAcceptText;
int listens = 0;
int stops = 0;
ChatClipboardPaste get source => ChatClipboardPaste(
subscribe: (onFiles, canAccept, onError) {
subscribe: (onFiles, canAccept, onError, onText, canAcceptText) {
listens += 1;
this.onFiles = onFiles;
this.canAccept = canAccept;
this.onError = onError;
this.onText = onText;
this.canAcceptText = canAcceptText;
return () {
stops += 1;
this.onFiles = null;
this.canAccept = null;
this.onError = null;
this.onText = null;
this.canAcceptText = null;
};
},
);
@@ -82,10 +88,16 @@ void main() {
Future<void> pumpWith(
WidgetTester tester, {
ChatClipboardPaste? paste,
bool pasteTarget = true,
}) async {
await tester.pumpWidget(
materialHost(
ChatComposerBar(composer: composer, running: false, paste: paste),
ChatComposerBar(
composer: composer,
running: false,
paste: paste,
pasteTarget: pasteTarget,
),
),
);
await tester.pump();
@@ -95,6 +107,7 @@ void main() {
WidgetTester tester, {
ChatClipboardPaste? paste,
ChatDraftStore? drafts,
bool pasteTarget = true,
}) async {
composer = ChatComposerController(
gateway,
@@ -103,7 +116,7 @@ void main() {
newOperationId: () => 'op/1',
);
controllers.add(composer);
await pumpWith(tester, paste: paste);
await pumpWith(tester, paste: paste, pasteTarget: pasteTarget);
}
Future<void> focusField(WidgetTester tester) async {
@@ -116,7 +129,6 @@ void main() {
) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
await focusField(tester);
fake.paste([
_file('shot.png', [1, 2, 3], mime: 'image/png'),
@@ -261,20 +273,43 @@ void main() {
expect(drafts.entries, {'s': 'hello there world'});
});
testWidgets('paste is only taken while this composer owns the field', (
testWidgets('unfocused text paste is inserted at the composer caret', (
tester,
) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
await tester.enterText(_field, 'hello');
await tester.pumpAndSettle();
tester.binding.focusManager.primaryFocus?.unfocus();
await tester.pump();
expect(fake.canAcceptText!(), isTrue);
fake.onText!(' world');
await tester.pumpAndSettle();
expect(composer.draft, 'hello world');
expect(find.text('hello world'), findsOneWidget);
});
testWidgets('text paste is not accepted by an inactive pane', (tester) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source, pasteTarget: false);
expect(fake.canAcceptText!(), isFalse);
});
testWidgets('paste is only taken by the active composer', (
tester,
) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
expect(fake.canAccept!(), isTrue);
await pumpWith(tester, paste: fake.source, pasteTarget: false);
expect(fake.canAccept!(), isFalse);
await pumpWith(tester, paste: fake.source, pasteTarget: true);
await focusField(tester);
expect(fake.canAccept!(), isTrue);
tester.binding.focusManager.primaryFocus?.unfocus();
await tester.pump();
expect(fake.canAccept!(), isFalse);
});
testWidgets('a paste during an in-flight prompt adds nothing', (
@@ -0,0 +1,167 @@
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 'package:pipelines_app/features/chat/conversation/chat_composer_controller.dart';
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste.dart';
import 'chat_composer_bar_fixtures.dart';
Finder _field(String id) => find.descendant(
of: find.byKey(ValueKey('bar-$id')),
matching: find.byKey(const Key('chat-composer-field')),
);
void main() {
late ComposerBarGateway gateway;
late ChatComposerController active, inactive;
var reads = 0;
setUp(() {
gateway = ComposerBarGateway();
active = ChatComposerController(
gateway,
'active',
drafts: MemoryChatDraftStore(),
);
inactive = ChatComposerController(
gateway,
'inactive',
drafts: MemoryChatDraftStore(),
);
reads = 0;
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.platform, (call) async {
if (call.method == 'Clipboard.getData') {
reads++;
return {'text': 'controlled paste'};
}
if (call.method == 'Clipboard.hasStrings') return {'value': true};
return null;
});
});
tearDown(() {
active.dispose();
inactive.dispose();
gateway.close();
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.platform, null);
});
Future<void> mount(WidgetTester tester) async {
await tester.pumpWidget(
materialHost(
Column(
mainAxisSize: MainAxisSize.min,
children: [
const TextField(key: Key('other-editor')),
ChatComposerBar(
key: const ValueKey('bar-inactive'),
composer: inactive,
running: false,
pasteTarget: false,
),
ChatComposerBar(
key: const ValueKey('bar-active'),
composer: active,
running: false,
),
],
),
),
);
await tester.pumpAndSettle();
}
Future<void> paste(WidgetTester tester, {bool meta = false}) async {
final modifier = meta
? LogicalKeyboardKey.metaLeft
: LogicalKeyboardKey.controlLeft;
await tester.sendKeyDownEvent(modifier);
await tester.sendKeyEvent(LogicalKeyboardKey.keyV);
await tester.sendKeyUpEvent(modifier);
await tester.pumpAndSettle();
}
Future<void> done(WidgetTester tester) => tester.pumpWidget(const SizedBox());
for (final meta in [false, true]) {
testWidgets(
'unfocused active composer takes one controlled native ${meta ? 'Cmd' : 'Ctrl'}+V',
(tester) async {
await mount(tester);
FocusManager.instance.primaryFocus?.unfocus();
await tester.pump();
await paste(tester, meta: meta);
expect(active.draft, 'controlled paste');
expect(inactive.draft, isEmpty);
expect(reads, 1);
await done(tester);
},
);
}
testWidgets('another focused field keeps native paste', (tester) async {
await mount(tester);
await tester.showKeyboard(find.byKey(const Key('other-editor')));
await paste(tester);
final editor = tester.widget<EditableText>(
find.descendant(
of: find.byKey(const Key('other-editor')),
matching: find.byType(EditableText),
),
);
expect(editor.controller.text, 'controlled paste');
expect(active.draft, isEmpty);
expect(inactive.draft, isEmpty);
expect(reads, 1);
await done(tester);
});
testWidgets(
'focused active composer retains its own native path exactly once',
(tester) async {
await mount(tester);
await tester.showKeyboard(_field('active'));
await paste(tester);
expect(active.draft, 'controlled paste');
expect(reads, 1);
await done(tester);
},
);
testWidgets('inactive focused composer cannot swallow active-pane paste', (
tester,
) async {
await mount(tester);
await tester.showKeyboard(_field('inactive'));
await paste(tester);
expect(active.draft, 'controlled paste');
expect(inactive.draft, isEmpty);
expect(reads, 1);
await done(tester);
});
testWidgets('file/text acceptance excludes another Flutter editor', (
tester,
) async {
ChatPasteCanAccept? files;
ChatPasteCanAcceptText? text;
final source = ChatClipboardPaste(
subscribe: (onFiles, canAccept, onError, onText, canAcceptText) {
files = canAccept;
text = canAcceptText;
return () {};
},
);
await tester.pumpWidget(
materialHost(
Column(
children: [
const TextField(key: Key('other-editor')),
ChatComposerBar(composer: active, running: false, paste: source),
],
),
),
);
await tester.pumpAndSettle();
await tester.showKeyboard(find.byKey(const Key('other-editor')));
expect(files!(), isFalse);
expect(text!(), isFalse);
await done(tester);
});
}
@@ -27,7 +27,7 @@ void main() {
var listens = 0;
var stops = 0;
final paste = ChatClipboardPaste(
subscribe: (onFiles, canAccept, onError) {
subscribe: (onFiles, canAccept, onError, onText, canAcceptText) {
listens += 1;
return () => stops += 1;
},
@@ -0,0 +1,170 @@
@TestOn('browser')
library;
import 'dart:js_interop';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart';
import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste.dart';
@JS('Function')
external JSFunction _function(JSString body);
JSAny? _js(String body) => _function(body.toJS).callAsFunction(null);
bool _paste(String target, {String? filename}) =>
(_js('''
const target = $target;
const data = new DataTransfer();
${filename != null ? "data.items.add(new File([new Uint8Array([104,105])], '$filename', {type:'${filename.endsWith('.png') ? 'image/png' : 'text/plain'}'}));" : "data.setData('text/plain', 'browser text');"}
const event = new ClipboardEvent('paste', {clipboardData:data, bubbles:true, composed:true, cancelable:true});
target.dispatchEvent(event);
return event.defaultPrevented;
''')
as JSBoolean)
.toDart;
void main() {
late List<ChatPickedFile> files;
late List<String> texts;
late List<Object> errors;
late ChatPasteUnsubscribe stop;
var accepts = true, acceptsText = true;
setUp(() {
files = [];
texts = [];
errors = [];
accepts = true;
acceptsText = true;
stop = hostChatClipboardPaste.listen(
onFiles: files.addAll,
canAccept: () => accepts,
onError: errors.add,
onText: texts.add,
canAcceptText: () => acceptsText,
);
});
tearDown(() {
stop();
_js("document.getElementById('paste-test-host')?.remove();");
});
void editingElement({
bool flutter = false,
bool shadow = false,
bool closed = false,
}) {
_js('''
const host = document.createElement('${shadow ? 'paste-test-editor' : 'div'}'); host.id='paste-test-host'; host.tabIndex=0;
const input = document.createElement('textarea');
${flutter ? "input.className='flt-text-editing';" : ''}
const root = ${shadow ? "host.attachShadow({mode:'${closed ? 'closed' : 'open'}'})" : 'host'};
root.append(input); document.body.append(host); input.focus();
globalThis.pasteTestTarget=input;
''');
}
Future<void> filesRead() async {
for (var i = 0; i < 50 && files.isEmpty && errors.isEmpty; i++) {
await Future<void>.delayed(const Duration(milliseconds: 10));
}
}
test(
'production bridge consumes unfocused text and clipboard files/images',
() async {
expect(_paste('document.body'), isTrue);
expect(texts, ['browser text']);
expect(_paste('document.body', filename: 'clipboard.txt'), isTrue);
await filesRead();
expect(files.single.name, 'clipboard.txt');
expect(files.single.bytes, [104, 105]);
files.clear();
expect(_paste('document.body', filename: 'clipboard.png'), isTrue);
await filesRead();
expect(files.single.mime, 'image/png');
expect(errors, isEmpty);
},
);
test(
'semantics-owned unclassed editor preserves native text and accepts files',
() async {
_js(
"const host=document.createElement('flt-semantics');host.id='paste-test-host';const input=document.createElement('textarea');host.append(input);document.body.append(host);input.focus();globalThis.pasteTestTarget=input;",
);
acceptsText = false;
expect(_paste('globalThis.pasteTestTarget'), isFalse);
expect(
_paste('globalThis.pasteTestTarget', filename: 'clipboard.txt'),
isTrue,
);
await filesRead();
expect(files.single.name, 'clipboard.txt');
accepts = false;
expect(
_paste('globalThis.pasteTestTarget', filename: 'clipboard.txt'),
isFalse,
);
},
);
test(
'external DOM and open-shadow editors keep both text and files',
() async {
for (final shadow in [false, true]) {
editingElement(shadow: shadow);
expect(_paste('globalThis.pasteTestTarget'), isFalse);
expect(
_paste('globalThis.pasteTestTarget', filename: 'clipboard.png'),
isFalse,
);
_js("document.getElementById('paste-test-host').remove();");
}
expect(texts, isEmpty);
expect(files, isEmpty);
},
);
test('closed external shadow editor is conservatively left native', () {
editingElement(shadow: true, closed: true);
expect(_paste('globalThis.pasteTestTarget'), isFalse);
expect(texts, isEmpty);
});
test(
'Flutter focus eligibility rejects unrelated editor sharing engine class',
() {
accepts = false;
acceptsText = false;
editingElement(flutter: true, shadow: true);
expect(_paste('globalThis.pasteTestTarget'), isFalse);
expect(
_paste('globalThis.pasteTestTarget', filename: 'clipboard.png'),
isFalse,
);
expect(texts, isEmpty);
expect(files, isEmpty);
},
);
test('inactive Flutter editing target is consumed before target listeners', () {
editingElement(flutter: true, shadow: true);
_js(
"globalThis.pasteAtTarget=null; pasteTestTarget.addEventListener('paste',e=>globalThis.pasteAtTarget=e.defaultPrevented);",
);
expect(_paste('globalThis.pasteTestTarget'), isTrue);
expect(texts, ['browser text']);
expect(
(_js('return globalThis.pasteAtTarget;') as JSBoolean).toDart,
isTrue,
);
});
test(
'focused composer native text path is untouched while file path remains available',
() async {
acceptsText = false;
editingElement(flutter: true, shadow: true);
expect(_paste('globalThis.pasteTestTarget'), isFalse);
expect(texts, isEmpty);
expect(
_paste('globalThis.pasteTestTarget', filename: 'clipboard.txt'),
isTrue,
);
await filesRead();
expect(files.single.name, 'clipboard.txt');
},
);
}