Fix web transcript cross-message selection

This commit is contained in:
2026-11-05 16:34:58 +00:00
parent 3a9ba55277
commit 9e7140449f
10 changed files with 929 additions and 117 deletions
@@ -0,0 +1,162 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_link_launcher.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_link_spans.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_link_text.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_transcript_selection.dart';
import 'chat_transcript_selection_support.dart';
const _paragraph = 'go here now';
const _url = 'https://x.test/link';
void main() {
late List<String> copied;
late List<Uri> opened;
setUp(() {
ChatTranscriptSelectionArea.debugEnabled = true;
copied = recordTranscriptClipboard();
opened = [];
});
tearDown(() {
ChatTranscriptSelectionArea.debugEnabled = null;
ChatLinkSpans.debugOwnsSecondaryClick = null;
});
Future<void> pump(WidgetTester tester) async {
await tester.pumpWidget(
ChatLinkScope(
launcher: (uri) async {
opened.add(uri);
return true;
},
child: MaterialApp(
home: Scaffold(
body: ChatTranscriptSelectionArea(
child: const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ChatLinkText('Plain question'),
SizedBox(height: 24),
ChatMarkdownView(text: 'go [here](https://x.test/link) now'),
],
),
),
),
),
),
);
await tester.pumpAndSettle();
}
testWidgets(
'shared document links still activate with a drifting mouse click',
(tester) async {
await pump(tester);
final link = transcriptWord(tester, _paragraph, 'here');
await dragTranscriptSelection(tester, link, link + const Offset(3, 0));
expect(opened.map((uri) => '$uri'), [_url]);
expect(copied, isEmpty);
},
);
testWidgets(
'desktop link context copy and ordinary selection copy both work',
(tester) async {
ChatLinkSpans.debugOwnsSecondaryClick = true;
await pump(tester);
final link = transcriptWord(tester, _paragraph, 'here');
final mouse = await tester.startGesture(
link,
kind: PointerDeviceKind.mouse,
buttons: kSecondaryMouseButton,
);
await mouse.up();
await tester.pumpAndSettle();
await mouse.removePointer();
expect(find.text('Copy link'), findsOneWidget);
await tester.tap(find.text('Copy link'));
await tester.pumpAndSettle();
expect(copied.single, _url);
expect(opened, isEmpty);
await dragTranscriptSelection(
tester,
transcriptBoundary(tester, _paragraph),
transcriptBoundary(tester, _paragraph, end: true),
);
await copyTranscriptSelection(tester);
expect(copied.last, _paragraph);
expect(opened, isEmpty);
},
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
testWidgets(
'touch Copy link uses touched URL and plain selection clears link context',
(tester) async {
ChatLinkSpans.debugOwnsSecondaryClick = false;
await pump(tester);
await tester.longPressAt(transcriptWord(tester, _paragraph, 'here'));
await tester.pumpAndSettle();
expect(find.text('Copy link'), findsOneWidget);
await tester.tap(find.text('Copy link'));
await tester.pumpAndSettle();
expect(copied.single, _url);
expect(opened, isEmpty);
await tester.longPressAt(
transcriptWord(tester, 'Plain question', 'Plain'),
);
await tester.pumpAndSettle();
expect(find.text('Copy link'), findsNothing);
expect(find.text('Copy'), findsOneWidget);
await tester.tap(find.text('Copy'));
await tester.pumpAndSettle();
expect(copied.last, contains('Plain'));
expect(copied.last, isNot(contains(_url)));
expect(opened, isEmpty);
},
variant: TargetPlatformVariant.only(TargetPlatform.iOS),
);
testWidgets(
'selecting later paragraph never includes unrelated scrollable code',
(tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: ChatTranscriptSelectionArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ChatMarkdownView(text: '```dart\nearlierCode();\n```'),
ChatLinkText('Second question'),
ChatMarkdownView(
text:
'```dart\nlatestCode();\n```\n\nRead [here](https://x.test/link) now',
),
],
),
),
),
),
);
await tester.pumpAndSettle();
await dragTranscriptSelection(
tester,
transcriptBoundary(tester, 'Read here now'),
transcriptBoundary(tester, 'Read here now', end: true),
);
await copyTranscriptSelection(tester);
expect(copied.single, 'Read here now');
await dragTranscriptSelection(
tester,
transcriptBoundary(tester, 'Read here now', end: true),
transcriptBoundary(tester, 'Read here now'),
);
await copyTranscriptSelection(tester);
expect(copied.last, 'Read here now');
},
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
}
@@ -0,0 +1,83 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
Future<void> dragTranscriptSelection(
WidgetTester tester,
Offset start,
Offset end,
) async {
final mouse = await tester.startGesture(start, kind: PointerDeviceKind.mouse);
await tester.pump();
for (var step = 1; step <= 20; step++) {
await mouse.moveTo(Offset.lerp(start, end, step / 20)!);
await tester.pump();
}
await mouse.up();
await tester.pumpAndSettle();
await mouse.removePointer();
}
Future<void> copyTranscriptSelection(WidgetTester tester) async {
await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyC);
await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft);
await tester.pump();
}
RenderParagraph transcriptParagraph(WidgetTester tester, String text) =>
tester.renderObject<RenderParagraph>(
find
.byWidgetPredicate(
(widget) => widget is RichText && widget.text.toPlainText() == text,
)
.first,
);
Offset transcriptBoundary(
WidgetTester tester,
String text, {
bool end = false,
}) {
final paragraph = transcriptParagraph(tester, text);
final offset = end ? text.length - 1 : 0;
final box = paragraph
.getBoxesForSelection(
TextSelection(baseOffset: offset, extentOffset: offset + 1),
)
.first
.toRect();
return paragraph.localToGlobal(
Offset(end ? box.right - 0.1 : box.left + 0.1, box.center.dy),
);
}
Offset transcriptWord(WidgetTester tester, String text, String word) {
final paragraph = transcriptParagraph(tester, text);
final offset = text.indexOf(word);
final box = paragraph
.getBoxesForSelection(
TextSelection(baseOffset: offset, extentOffset: offset + word.length),
)
.first
.toRect();
return paragraph.localToGlobal(box.center);
}
List<String> recordTranscriptClipboard() {
final copied = <String>[];
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.platform, (call) async {
if (call.method == 'Clipboard.setData') {
copied.add((call.arguments as Map)['text'] as String);
}
return null;
});
addTearDown(
() => TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.platform, null),
);
return copied;
}
@@ -0,0 +1,219 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_link_text.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_transcript_selection.dart';
import 'chat_transcript_selection_support.dart';
Widget _host(Widget child) => MaterialApp(home: Scaffold(body: child));
void main() {
late List<String> copied;
setUp(() {
ChatTranscriptSelectionArea.debugEnabled = true;
copied = recordTranscriptClipboard();
});
tearDown(() {
ChatTranscriptSelectionArea.debugEnabled = null;
});
for (final reverse in [false, true]) {
testWidgets(
'plain user and assistant cross-copy ${reverse ? 'backward' : 'forward'}',
(tester) async {
await tester.pumpWidget(
_host(
const ChatTranscriptSelectionArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ChatLinkText('User question'),
SizedBox(height: 24),
ChatMarkdownView(text: 'Assistant answer.'),
],
),
),
),
);
await tester.pumpAndSettle();
final user = tester.getRect(find.text('User question'));
final answer = tester.getRect(find.text('Assistant answer.'));
final start = user.topLeft + Offset(1, user.height / 2);
final end = answer.bottomRight - Offset(1, answer.height / 2);
await dragTranscriptSelection(
tester,
reverse ? end : start,
reverse ? start : end,
);
await copyTranscriptSelection(tester);
expect(copied, hasLength(1));
expect(copied.single, contains('User question'));
expect(copied.single, contains('Assistant answer.'));
expect(
copied.single.indexOf('User question'),
lessThan(copied.single.indexOf('Assistant answer.')),
);
},
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
}
testWidgets(
'selection includes exact multiline code across message blocks',
(tester) async {
await tester.pumpWidget(
_host(
const ChatTranscriptSelectionArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ChatLinkText('Question with code'),
ChatMarkdownView(
text:
'Reply before code.\n\n```dart\nfinal a = 1;\nfinal b = 2;\n```\n\nReply after code.',
),
],
),
),
),
);
await tester.pumpAndSettle();
await dragTranscriptSelection(
tester,
transcriptBoundary(tester, 'Question with code'),
transcriptBoundary(tester, 'Reply after code.', end: true),
);
await copyTranscriptSelection(tester);
expect(copied.single, contains('Question with code'));
expect(copied.single, contains('final a = 1;\nfinal b = 2;'));
expect(copied.single, contains('Reply after code.'));
await dragTranscriptSelection(
tester,
transcriptBoundary(tester, 'Reply after code.', end: true),
transcriptBoundary(tester, 'Question with code'),
);
await copyTranscriptSelection(tester);
expect(copied.last, copied.first);
await dragTranscriptSelection(
tester,
transcriptBoundary(tester, 'final a = 1;\nfinal b = 2;'),
transcriptBoundary(tester, 'final a = 1;\nfinal b = 2;', end: true),
);
await copyTranscriptSelection(tester);
expect(copied.last, 'final a = 1;\nfinal b = 2;');
},
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
testWidgets(
'dragging into composer does not copy or alter its draft',
(tester) async {
final draft = TextEditingController(text: 'COMPOSER DRAFT');
addTearDown(draft.dispose);
await tester.pumpWidget(
_host(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const ChatTranscriptSelectionArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ChatLinkText('User question'),
ChatMarkdownView(text: 'Assistant answer.'),
],
),
),
TextField(controller: draft),
],
),
),
);
await tester.pumpAndSettle();
await dragTranscriptSelection(
tester,
transcriptBoundary(tester, 'User question'),
tester.getCenter(find.byType(TextField)),
);
await copyTranscriptSelection(tester);
expect(copied.single, contains('User question'));
expect(copied.single, contains('Assistant answer.'));
expect(copied.single, isNot(contains('COMPOSER DRAFT')));
expect(draft.text, 'COMPOSER DRAFT');
await tester.tap(find.byType(TextField));
await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyA);
await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft);
await copyTranscriptSelection(tester);
expect(copied.last, 'COMPOSER DRAFT');
},
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
testWidgets(
'unrelated transcript update preserves actual copied selection',
(tester) async {
late StateSetter refresh;
var revision = 0;
await tester.pumpWidget(
_host(
StatefulBuilder(
builder: (context, setState) {
refresh = setState;
return ChatTranscriptSelectionArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const ChatLinkText('User question'),
const ChatMarkdownView(text: 'Assistant answer.'),
Text('Activity revision $revision'),
],
),
);
},
),
),
);
await tester.pumpAndSettle();
await dragTranscriptSelection(
tester,
transcriptBoundary(tester, 'User question'),
transcriptBoundary(tester, 'Assistant answer.', end: true),
);
await copyTranscriptSelection(tester);
final before = copied.single;
refresh(() => revision++);
await tester.pumpAndSettle();
await copyTranscriptSelection(tester);
expect(copied.last, before);
expect(copied.last, contains('User question'));
expect(copied.last, contains('Assistant answer.'));
},
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
testWidgets(
'disabled shared scope preserves standalone native selection',
(tester) async {
ChatTranscriptSelectionArea.debugEnabled = false;
await tester.pumpWidget(
_host(
const ChatTranscriptSelectionArea(
child: ChatLinkText('Standalone user message'),
),
),
);
await tester.pumpAndSettle();
final text = tester.getRect(find.text('Standalone user message'));
await dragTranscriptSelection(
tester,
text.centerLeft + const Offset(1, 0),
text.centerRight - const Offset(1, 0),
);
await copyTranscriptSelection(tester);
expect(copied.single, 'Standalone user message');
},
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
}