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 files) { final handler = onFiles; if (handler == null) return; if (!(canAccept?.call() ?? false)) return; handler(files); } } ChatPickedFile _file( String name, List bytes, { String mime = 'text/plain', }) => ChatPickedFile( name: name, mime: mime, bytes: bytes is Uint8List ? 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 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 pumpWith( WidgetTester tester, { ChatClipboardPaste? paste, }) async { await tester.pumpWidget( materialHost( ChatComposerBar(composer: composer, running: false, paste: paste), ), ); await tester.pump(); } Future 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 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); // A pasted image shows its picture, not only its name. expect( find.byKey(const ValueKey('chat-composer-thumbnail-0')), 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', Uint8List(ChatAttachmentDraft.maxTotalBytes + 1), 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', Uint8List(ChatAttachmentDraft.maxTotalBytes), 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( 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(); 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); }, ); }