import 'package:flutter/gestures.dart'; 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_thread_view.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_attachment_gallery.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_injected_rows.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart'; import 'chat_thread_fixtures.dart'; import 'chat_thread_scroll_test.dart' show host; /// The transcript content kinds a wheel can land on. enum ChatWheelKind { text, code, wide, table, agent, notice, image } const _png = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlE' 'QVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='; const _wideLine = 'go test ./internal/server/... -run TestWheel && '; String _table() => '| ${[for (var c = 0; c < 16; c++) 'Column $c'].join(' | ')} |\n' '| ${List.filled(16, '---').join(' | ')} |\n' '| ${[for (var c = 0; c < 16; c++) 'value $c'].join(' | ')} |'; /// One message drawing [kind], with id [id]. JsonMap chatWheelMessage(ChatWheelKind kind, String id) { JsonMap text(String role, String body, {String? messageKind}) => { ...chatMessage( id, role, parts: [ {'type': 'text', 'name': 'reviewer', 'text': body}, ], ), 'kind': ?messageKind, }; return switch (kind) { ChatWheelKind.text => text('assistant', 'A plain answer paragraph. ' * 4), ChatWheelKind.code => text( 'assistant', '```dart\n${List.generate(4, (l) => 'final v$l = $l;').join('\n')}\n```', ), ChatWheelKind.wide => text( 'assistant', '```sh\n${List.filled(12, _wideLine).join()}true\n```', ), ChatWheelKind.table => text('assistant', _table()), ChatWheelKind.agent => text( 'user', '## Review\n\nAll checks passed.', messageKind: 'agent_report', ), ChatWheelKind.notice => text( 'user', 'Background task finished.', messageKind: 'notice', ), ChatWheelKind.image => chatMessage( id, 'user', parts: [ {'type': 'file', 'mime': 'image/png', 'name': 'a.png', 'uri': _png}, {'type': 'text', 'text': 'With an image'}, ], ), }; } /// The widget that draws [kind] in the transcript. Finder chatWheelTarget(ChatWheelKind kind) { Finder code(String text) => find.ancestor( of: find.textContaining(text), matching: find.byType(SingleChildScrollView), ); return switch (kind) { ChatWheelKind.text => find.byWidgetPredicate( (widget) => widget is ChatMessageView && (widget.row['messageIds'] as List?)?.contains('target') == true, ), ChatWheelKind.code => code('final v0'), ChatWheelKind.wide => code('go test'), ChatWheelKind.table => find.byType(Table), ChatWheelKind.agent => find.byType(ChatAgentReportCard), ChatWheelKind.notice => find.byType(ChatSystemNoticeLine), ChatWheelKind.image => find.byType(ChatAttachmentGallery), }; } List chatWheelFillers(int count) => [ for (var index = 0; index < count; index++) chatMessage( 'f$index', index.isEven ? 'user' : 'assistant', parts: [ { 'type': 'text', 'text': 'Filler $index. ${List.filled(5, 'A sentence that wraps across the transcript width.').join(' ')}', }, ], ), ]; Finder get chatWheelThread => find.byKey(const Key('chat-thread-s')); Future openChatWheelThread( WidgetTester tester, FakeChatGateway gateway, ) async { await tester.pumpWidget( host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})), ); await tester.pumpAndSettle(); } /// The centre of [finder]'s part between the floating bar and the composer. Offset chatWheelPoint(WidgetTester tester, Finder finder) { final top = tester.getRect(find.byKey(const Key('chat-info'))).bottom + 4; final bottom = tester.getRect(find.byKey(const Key('chat-composer-pill'))).top - 4; final shown = tester .getRect(finder.first) .intersect(Rect.fromLTRB(0, top, 800, bottom)); expect(shown.height, greaterThan(8), reason: '$finder is not in view'); return shown.center; } /// One wheel or trackpad tick of [delta] at [at]. Future wheel( WidgetTester tester, Offset at, Offset delta, { PointerDeviceKind kind = PointerDeviceKind.mouse, }) async { final pointer = TestPointer(1, kind); await tester.sendEventToBinding(pointer.hover(at)); await tester.sendEventToBinding(pointer.scroll(delta)); await tester.pump(); }