wip(app): chat-paste-2 (retained from the cancelled overrun lane)
This commit is contained in:
@@ -0,0 +1,340 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.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_attachment_draft.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste.dart';
|
||||
|
||||
import 'chat_composer_bar_fixtures.dart';
|
||||
|
||||
/// A host paste source driven by the test. [paste] mirrors the web listener:
|
||||
/// it asks [canAccept] before it hands files to the composer.
|
||||
class _FakePaste {
|
||||
ChatPasteFiles? onFiles;
|
||||
ChatPasteCanAccept? canAccept;
|
||||
ChatPasteFailed? onError;
|
||||
int listens = 0;
|
||||
int stops = 0;
|
||||
|
||||
ChatClipboardPaste get source => ChatClipboardPaste(
|
||||
subscribe: (onFiles, canAccept, onError) {
|
||||
listens += 1;
|
||||
this.onFiles = onFiles;
|
||||
this.canAccept = canAccept;
|
||||
this.onError = onError;
|
||||
return () {
|
||||
stops += 1;
|
||||
this.onFiles = null;
|
||||
this.canAccept = null;
|
||||
this.onError = null;
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
void paste(List<ChatPickedFile> files) {
|
||||
final handler = onFiles;
|
||||
if (handler == null) return;
|
||||
if (!(canAccept?.call() ?? false)) return;
|
||||
handler(files);
|
||||
}
|
||||
}
|
||||
|
||||
ChatPickedFile _file(
|
||||
String name,
|
||||
List<int> bytes, {
|
||||
String mime = 'text/plain',
|
||||
}) => ChatPickedFile(name: name, mime: mime, bytes: Uint8List.fromList(bytes));
|
||||
|
||||
Finder get _field => find.byKey(const Key('chat-composer-field'));
|
||||
|
||||
Finder get _chips => find.byKey(const Key('chat-composer-attachments'));
|
||||
|
||||
void main() {
|
||||
late ComposerBarGateway gateway;
|
||||
late ChatComposerController composer;
|
||||
late List<ChatComposerController> controllers;
|
||||
|
||||
setUp(() {
|
||||
gateway = ComposerBarGateway();
|
||||
controllers = [];
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
for (final controller in controllers) {
|
||||
controller.dispose();
|
||||
}
|
||||
gateway.close();
|
||||
});
|
||||
|
||||
/// Rebuilds the same composer with a different paste source, so a test can
|
||||
/// tell a replaced listener from a disposed one.
|
||||
Future<void> pumpWith(
|
||||
WidgetTester tester, {
|
||||
ChatClipboardPaste? paste,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
materialHost(
|
||||
ChatComposerBar(composer: composer, running: false, paste: paste),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
Future<void> pumpBar(
|
||||
WidgetTester tester, {
|
||||
ChatClipboardPaste? paste,
|
||||
ChatDraftStore? drafts,
|
||||
}) async {
|
||||
composer = ChatComposerController(
|
||||
gateway,
|
||||
's',
|
||||
drafts: drafts ?? MemoryChatDraftStore(),
|
||||
newOperationId: () => 'op/1',
|
||||
);
|
||||
controllers.add(composer);
|
||||
await pumpWith(tester, paste: paste);
|
||||
}
|
||||
|
||||
Future<void> focusField(WidgetTester tester) async {
|
||||
await tester.showKeyboard(_field);
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
testWidgets('a pasted file makes the same chip as a picked file', (
|
||||
tester,
|
||||
) async {
|
||||
final fake = _FakePaste();
|
||||
await pumpBar(tester, paste: fake.source);
|
||||
await focusField(tester);
|
||||
|
||||
fake.paste([
|
||||
_file('shot.png', [1, 2, 3], mime: 'image/png'),
|
||||
]);
|
||||
await tester.pump();
|
||||
|
||||
expect(_chips, findsOneWidget);
|
||||
expect(find.text('shot.png'), findsOneWidget);
|
||||
expect(fake.listens, 1);
|
||||
expect(fake.stops, 0);
|
||||
});
|
||||
|
||||
testWidgets('pasting several files keeps clipboard order', (tester) async {
|
||||
final fake = _FakePaste();
|
||||
await pumpBar(tester, paste: fake.source);
|
||||
await focusField(tester);
|
||||
|
||||
fake.paste([
|
||||
_file('first.png', [1], mime: 'image/png'),
|
||||
_file('second.txt', [2]),
|
||||
]);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('first.png'), findsOneWidget);
|
||||
expect(find.text('second.txt'), findsOneWidget);
|
||||
expect(
|
||||
tester.getTopLeft(find.text('first.png')).dx,
|
||||
lessThan(tester.getTopLeft(find.text('second.txt')).dx),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('an over-limit paste shows the draft refusal and adds nothing', (
|
||||
tester,
|
||||
) async {
|
||||
final fake = _FakePaste();
|
||||
await pumpBar(tester, paste: fake.source);
|
||||
await focusField(tester);
|
||||
|
||||
fake.paste([
|
||||
_file(
|
||||
'huge.bin',
|
||||
List<int>.filled(ChatAttachmentDraft.maxTotalBytes + 1, 0),
|
||||
mime: 'application/octet-stream',
|
||||
),
|
||||
]);
|
||||
await tester.pump();
|
||||
|
||||
expect(
|
||||
find.text(ChatAttachmentRefusal.totalTooLarge.message),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(_chips, findsNothing);
|
||||
expect(find.text('huge.bin'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('a refused paste keeps the files already in the draft', (
|
||||
tester,
|
||||
) async {
|
||||
final fake = _FakePaste();
|
||||
await pumpBar(tester, paste: fake.source);
|
||||
await focusField(tester);
|
||||
|
||||
fake.paste([
|
||||
_file('kept.txt', [1]),
|
||||
_file(
|
||||
'huge.bin',
|
||||
List<int>.filled(ChatAttachmentDraft.maxTotalBytes, 0),
|
||||
mime: 'application/octet-stream',
|
||||
),
|
||||
]);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('kept.txt'), findsOneWidget);
|
||||
expect(find.text('huge.bin'), findsNothing);
|
||||
expect(
|
||||
find.text(ChatAttachmentRefusal.totalTooLarge.message),
|
||||
findsOneWidget,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('a pasted attachment sends its exact name, type and bytes', (
|
||||
tester,
|
||||
) async {
|
||||
final fake = _FakePaste();
|
||||
await pumpBar(tester, paste: fake.source);
|
||||
await focusField(tester);
|
||||
|
||||
fake.paste([
|
||||
_file('shot.png', [9, 8, 7], mime: 'image/png'),
|
||||
]);
|
||||
await tester.pump();
|
||||
await tester.enterText(_field, 'look');
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.promptBodies.single['attachments'], [
|
||||
{
|
||||
'name': 'shot.png',
|
||||
'mime': 'image/png',
|
||||
'data': base64Encode([9, 8, 7]),
|
||||
},
|
||||
]);
|
||||
expect(_chips, findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('pasted text keeps its caret, selection and draft', (
|
||||
tester,
|
||||
) async {
|
||||
final drafts = MemoryChatDraftStore();
|
||||
final fake = _FakePaste();
|
||||
await pumpBar(tester, paste: fake.source, drafts: drafts);
|
||||
await focusField(tester);
|
||||
|
||||
await tester.enterText(_field, 'hello world');
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final editable = tester.widget<EditableText>(
|
||||
find.descendant(of: _field, matching: find.byType(EditableText)),
|
||||
);
|
||||
editable.controller.selection = const TextSelection.collapsed(offset: 5);
|
||||
// The platform's text update for a paste between "hello" and " world".
|
||||
tester.testTextInput.updateEditingValue(
|
||||
const TextEditingValue(
|
||||
text: 'hello there world',
|
||||
selection: TextSelection.collapsed(offset: 11),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(editable.controller.text, 'hello there world');
|
||||
expect(
|
||||
editable.controller.selection,
|
||||
const TextSelection.collapsed(offset: 11),
|
||||
);
|
||||
expect(composer.draft, 'hello there world');
|
||||
expect(drafts.entries, {'s': 'hello there world'});
|
||||
});
|
||||
|
||||
testWidgets('paste is only taken while this composer owns the field', (
|
||||
tester,
|
||||
) async {
|
||||
final fake = _FakePaste();
|
||||
await pumpBar(tester, paste: fake.source);
|
||||
|
||||
expect(fake.canAccept!(), isFalse);
|
||||
|
||||
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', (
|
||||
tester,
|
||||
) async {
|
||||
final reply = Completer<JsonMap>();
|
||||
gateway.prompt = (_, _) => reply.future;
|
||||
final fake = _FakePaste();
|
||||
await pumpBar(tester, paste: fake.source);
|
||||
await focusField(tester);
|
||||
await tester.enterText(_field, 'hello');
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
||||
await tester.pump();
|
||||
|
||||
expect(fake.canAccept!(), isFalse);
|
||||
fake.paste([
|
||||
_file('late.png', [1], mime: 'image/png'),
|
||||
]);
|
||||
await tester.pump();
|
||||
expect(_chips, findsNothing);
|
||||
|
||||
reply.complete(const {});
|
||||
await tester.pumpAndSettle();
|
||||
});
|
||||
|
||||
testWidgets('an unreadable clipboard file is reported', (tester) async {
|
||||
final fake = _FakePaste();
|
||||
await pumpBar(tester, paste: fake.source);
|
||||
await focusField(tester);
|
||||
|
||||
fake.onError!(StateError('clipboard read failed'));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.textContaining('clipboard read failed'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('a partial clipboard read shows its files and the failure', (
|
||||
tester,
|
||||
) async {
|
||||
final fake = _FakePaste();
|
||||
await pumpBar(tester, paste: fake.source);
|
||||
await focusField(tester);
|
||||
|
||||
// The web source delivers readable files before a partial read's failure.
|
||||
fake.paste([
|
||||
_file('readable.png', [1], mime: 'image/png'),
|
||||
]);
|
||||
fake.onError!(StateError('second file failed'));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('readable.png'), findsOneWidget);
|
||||
expect(find.textContaining('second file failed'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'a replaced source stops listening and dispose stops the new one',
|
||||
(tester) async {
|
||||
final first = _FakePaste();
|
||||
await pumpBar(tester, paste: first.source);
|
||||
expect(first.listens, 1);
|
||||
|
||||
final second = _FakePaste();
|
||||
await pumpWith(tester, paste: second.source);
|
||||
expect(first.stops, 1);
|
||||
expect(second.listens, 1);
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
expect(second.stops, 1);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste.dart';
|
||||
|
||||
void main() {
|
||||
test('a nameless clipboard file takes the web client name for its type', () {
|
||||
expect(chatPastedAttachmentName('image/png', 1), 'pasted-1.png');
|
||||
expect(chatPastedAttachmentName('image/jpeg', 2), 'pasted-2.jpg');
|
||||
expect(
|
||||
chatPastedAttachmentName('application/octet-stream', 3),
|
||||
'pasted-3.octetstream',
|
||||
);
|
||||
expect(chatPastedAttachmentName('', 4), 'pasted-4.bin');
|
||||
});
|
||||
|
||||
test('the unsupported host keeps text paste and reports the gap', () {
|
||||
const paste = ChatClipboardPaste.unsupported('no files here');
|
||||
|
||||
expect(paste.isSupported, isFalse);
|
||||
expect(
|
||||
() =>
|
||||
paste.listen(onFiles: (_) {}, canAccept: () => true, onError: (_) {}),
|
||||
throwsUnsupportedError,
|
||||
);
|
||||
});
|
||||
|
||||
test('a supported host subscribes once and returns its unsubscribe', () {
|
||||
var listens = 0;
|
||||
var stops = 0;
|
||||
final paste = ChatClipboardPaste(
|
||||
subscribe: (onFiles, canAccept, onError) {
|
||||
listens += 1;
|
||||
return () => stops += 1;
|
||||
},
|
||||
);
|
||||
|
||||
final stop = paste.listen(
|
||||
onFiles: (_) {},
|
||||
canAccept: () => true,
|
||||
onError: (_) {},
|
||||
);
|
||||
|
||||
expect(paste.isSupported, isTrue);
|
||||
expect(listens, 1);
|
||||
stop();
|
||||
expect(stops, 1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user