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_sent_entrance.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'package:pipelines_app/ui/motion/motion.dart'; import 'chat_thread_fixtures.dart'; // A message the operator sends rises in from the composer once; history, its // server copy, other devices' messages and replies simply appear. void main() { Future open( WidgetTester tester, FakeChatGateway gateway, { bool reduced = false, }) async { var serial = 0; await tester.pumpWidget( MaterialApp( theme: ThemeData(platform: TargetPlatform.android), builder: (context, child) => MediaQuery( data: MediaQuery.of(context).copyWith(disableAnimations: reduced), child: child!, ), home: Scaffold( body: ChatThreadView( gateway: gateway, sessionId: 's', onBack: () {}, newOperationId: () => 'op${++serial}', ), ), ), ); await tester.pumpAndSettle(); } JsonMap text(String id, String role, String body, {String? operation}) => { ...chatMessage( id, role, parts: [ {'type': 'text', 'text': body}, ], ), 'operation_id': ?operation, }; Future send(WidgetTester tester, String message) async { await tester.enterText( find.byKey(const Key('chat-composer-field')), message, ); await tester.pump(); await tester.tap(find.byKey(const Key('chat-composer-primary'))); await tester.pump(); } Finder entranceOf(String body) => find.ancestor( of: find.text(body), matching: find.byType(ChatSentEntrance), ); double opacityOf(WidgetTester tester, String body) => tester .widget( find .descendant( of: entranceOf(body), matching: find.byType(FadeTransition), ) .first, ) .opacity .value; // How far the drawn text sits below where its row is laid out: the // entrance only moves the painting, never the layout. double dropOf(WidgetTester tester, String body) => tester.getRect(find.text(body)).bottom - tester.getRect(entranceOf(body)).bottom; bool animating(WidgetTester tester, String body) => opacityOf(tester, body) < 1; testWidgets('a sent message rises in and settles fully visible at rest', ( tester, ) async { final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage([text('h1', 'user', 'From history')]); await open(tester, gateway); expect(animating(tester, 'From history'), isFalse); await send(tester, 'Hello there'); expect(entranceOf('Hello there'), findsOneWidget); expect(opacityOf(tester, 'Hello there'), lessThan(0.05)); await tester.pump(Motion.sentMessageDuration ~/ 4); final midOpacity = opacityOf(tester, 'Hello there'); final midDrop = dropOf(tester, 'Hello there'); expect(midOpacity, inExclusiveRange(0, 1)); await tester.pump(Motion.sentMessageDuration); await tester.pump(); expect(opacityOf(tester, 'Hello there'), 1); final restDrop = dropOf(tester, 'Hello there'); expect(midDrop, greaterThan(restDrop + 0.5)); // At rest the transforms are identities. final transforms = tester.widgetList( find.descendant( of: entranceOf('Hello there'), matching: find.byType(Transform), ), ); for (final transform in transforms.take(2)) { expect(transform.transform.isIdentity(), isTrue); } }); testWidgets('the server copy replacing a sent message keeps its row and ' 'does not play again', (tester) async { final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage(const []); await open(tester, gateway); await send(tester, 'Echo me'); await tester.pump(Motion.sentMessageDuration); await tester.pump(); final before = tester.state(entranceOf('Echo me')); // The authoritative copy carries another id but the same operation. gateway.history = (_) async => chatPage([ { ...text('srv-1', 'user', 'Echo me', operation: 'op1'), 'native_message_id': 'msg_op1', }, ]); gateway.emitEvent({'session_id': 's', 'kind': 'resync'}); await tester.pump(); await tester.pump(); expect(find.text('Echo me'), findsOneWidget); expect(tester.state(entranceOf('Echo me')), same(before)); expect(animating(tester, 'Echo me'), isFalse); }); testWidgets('history, other devices and replies appear without the ' 'entrance', (tester) async { final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage([ text('h1', 'user', 'Earlier question', operation: 'old'), text('h2', 'assistant', 'Earlier answer'), ]); await open(tester, gateway); expect(animating(tester, 'Earlier question'), isFalse); gateway.emitEvent({ 'session_id': 's', 'kind': 'user', 'message_id': 'other-device', 'operation_id': 'elsewhere', 'part': {'type': 'text', 'text': 'From the phone'}, }); await tester.pump(); await tester.pump(); expect(find.text('From the phone'), findsOneWidget); expect(animating(tester, 'From the phone'), isFalse); // A reply is not a sent message, so it has no entrance at all. gateway.emitEvent(chatTextFrame('Agent reply')); await tester.pump(); await tester.pump(const Duration(milliseconds: 50)); final reply = find.textContaining('Agent reply', findRichText: true); expect(reply, findsWidgets); expect( find.ancestor(of: reply, matching: find.byType(ChatSentEntrance)), findsNothing, ); }); testWidgets('a message queued while a turn runs rises in too', ( tester, ) async { final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage(const []); await open(tester, gateway); gateway.emitEvent(chatStepFrame('started')); await tester.pump(); await send(tester, 'Queued one'); expect(animating(tester, 'Queued one'), isTrue); await tester.pump(Motion.sentMessageDuration); await tester.pump(); expect(opacityOf(tester, 'Queued one'), 1); }); testWidgets('under reduced motion a sent message appears at once', ( tester, ) async { final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage(const []); await open(tester, gateway, reduced: true); await send(tester, 'Right away'); expect(opacityOf(tester, 'Right away'), 1); expect(dropOf(tester, 'Right away'), lessThanOrEqualTo(0)); await tester.pump(const Duration(milliseconds: 16)); expect(opacityOf(tester, 'Right away'), 1); }); }