import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/servers/server_catalog.dart'; import 'package:pipelines_app/core/servers/server_catalog_store.dart'; import 'package:pipelines_app/core/servers/server_context.dart'; import 'package:pipelines_app/features/chat/conversation/chat_seen_interaction.dart'; import 'package:pipelines_app/features/settings/server_connections.dart'; /// One open chat's pane: a field and a long transcript, both inside the region. Widget _pane({ required VoidCallback onInteract, ScrollController? scroll, FocusNode? focus, }) => MaterialApp( home: Scaffold( body: ChatSeenInteraction( sessionId: 'ses_a', onInteract: onInteract, child: Column( children: [ TextField(focusNode: focus), Expanded( child: ListView.builder( controller: scroll, itemCount: 200, itemBuilder: (context, index) => SizedBox(height: 40, child: Text('row $index')), ), ), ], ), ), ), ); void main() { testWidgets('showing the pane reports nothing', (tester) async { var count = 0; await tester.pumpWidget(_pane(onInteract: () => count++)); await tester.pump(); await tester.pump(const Duration(seconds: 2)); expect(count, 0); }); testWidgets('a programmatic scroll reports nothing', (tester) async { var count = 0; final scroll = ScrollController(); addTearDown(scroll.dispose); await tester.pumpWidget(_pane(onInteract: () => count++, scroll: scroll)); scroll.jumpTo(600); await tester.pump(); expect(count, 0); }); testWidgets('a click or tap in the pane reports once per press', ( tester, ) async { var count = 0; await tester.pumpWidget(_pane(onInteract: () => count++)); await tester.tap(find.text('row 3')); await tester.pump(); expect(count, greaterThanOrEqualTo(1)); }); testWidgets('a user drag of the transcript reports', (tester) async { var count = 0; await tester.pumpWidget(_pane(onInteract: () => count++)); await tester.drag(find.text('row 0'), const Offset(0, -300)); await tester.pumpAndSettle(); expect(count, greaterThanOrEqualTo(1)); }); testWidgets('a mouse-wheel scroll of the transcript reports', (tester) async { var count = 0; final scroll = ScrollController(); addTearDown(scroll.dispose); await tester.pumpWidget(_pane(onInteract: () => count++, scroll: scroll)); final pointer = TestPointer(1, PointerDeviceKind.mouse); await tester.sendEventToBinding( pointer.hover(tester.getCenter(find.text('row 0'))), ); await tester.sendEventToBinding(pointer.scroll(const Offset(0, 200))); await tester.pumpAndSettle(); expect(scroll.offset, greaterThan(0)); expect(count, greaterThanOrEqualTo(1)); }); testWidgets('a key press while focus is in the pane reports, with no click', ( tester, ) async { var count = 0; final focus = FocusNode(); addTearDown(focus.dispose); await tester.pumpWidget(_pane(onInteract: () => count++, focus: focus)); await tester.sendKeyEvent(LogicalKeyboardKey.keyA); await tester.pump(); expect( count, 0, reason: 'a key with no focus in the pane is not its input', ); focus.requestFocus(); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.keyA); await tester.pump(); expect(count, 1); }); testWidgets('code-driven focus into the pane reports nothing', ( tester, ) async { var count = 0; final focus = FocusNode(); addTearDown(focus.dispose); await tester.pumpWidget(_pane(onInteract: () => count++, focus: focus)); focus.requestFocus(); await tester.pump(); await tester.pump(const Duration(seconds: 1)); expect(focus.hasFocus, isTrue); expect( count, 0, reason: 'focus moved in code, with no key press, is passive', ); }); testWidgets('Tab from outside the pane into it reports once', (tester) async { var count = 0; final outside = FocusNode(); final focus = FocusNode(); addTearDown(outside.dispose); addTearDown(focus.dispose); await tester.pumpWidget( MaterialApp( home: Scaffold( body: Column( children: [ TextField(focusNode: outside), Expanded( child: ChatSeenInteraction( sessionId: 'ses_a', onInteract: () => count++, child: Column( children: [ TextField(focusNode: focus), const Expanded(child: SizedBox()), ], ), ), ), ], ), ), ), ); outside.requestFocus(); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.tab); await tester.pump(); expect(focus.hasFocus, isTrue, reason: 'Tab moved focus into the pane'); expect(count, 1); }); testWidgets('a notification tap that opened this chat reports once', ( tester, ) async { var count = 0; final connections = ServerConnections( store: MemoryServerCatalogStore( catalog: ServerCatalog( servers: [ SavedServer.validated( id: 'mac', name: 'Mac', endpoints: const ['ws://mac.test/connect'], ), ], selectedId: 'mac', ), ), onSelected: (_) async {}, ); addTearDown(connections.dispose); await connections.load(); await tester.pumpWidget( ServerContext( connections: connections, serverId: 'mac', child: _pane(onInteract: () => count++), ), ); await tester.pump(); expect(count, 0); // Another chat's notification is not this pane's open. connections.requestChatOpen('ses_other'); await tester.pump(); await tester.pump(); expect(count, 0); connections.requestChatOpen('ses_a'); await tester.pump(); await tester.pump(); expect(count, 1); // The tap is consumed: a later rebuild does not report it again. connections.requestChatOpen('ses_other'); await tester.pump(); await tester.pump(); expect(count, 1); }); }