import 'dart:async'; 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_draft_store.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_injected_rows.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_message_bubble.dart'; import 'chat_thread_fixtures.dart'; /// Content the harness delivered (agent reports, task notices) carries the /// server's message kind; it is drawn on the agent's side and never as the /// operator's bubble, and it does not disturb the operator's delivery line. const _report = '## Segmented bar report\n\n- first finding\n - nested detail\n\n' 'The **whole** report.'; JsonMap _reportMessage() => { 'id': 'agent-report-1', 'role': 'assistant', 'kind': 'agent_report', 'parts': [ { 'type': 'text', 'id': 'agent-report-1#text#0', 'text': _report, 'name': 'general-purpose', }, ], }; JsonMap _noticeMessage() => { 'id': 'task-notice-toolu_1-completed', 'role': 'assistant', 'kind': 'notice', 'parts': [ { 'type': 'text', 'text': 'Background task finished: Background command "Build" completed', }, ], }; class _Gateway extends FakeChatGateway { final prompts = >[]; List messages = []; _Gateway() { history = (_) async => chatPage(messages); } @override Future call(String type, [JsonMap fields = const {}]) { if (type == 'chat_prompt') { final reply = Completer(); prompts.add(reply); return reply.future; } return super.call(type, fields); } } Future _pump(WidgetTester tester) async { await tester.pump(); await tester.pump(const Duration(milliseconds: 100)); await tester.pump(); } Future _host(WidgetTester tester, _Gateway gateway) async { var sequence = 0; final thread = ChatThreadView( gateway: gateway, sessionId: 's', onBack: () {}, drafts: MemoryChatDraftStore(), newOperationId: () => 'op-${++sequence}', ); await tester.pumpWidget(MaterialApp(home: Scaffold(body: thread))); await _pump(tester); } void main() { test('transcriptRows gives injected kinds their own rows', () { final rows = transcriptRows([ chatMessage( 'u1', 'user', parts: [ {'type': 'text', 'text': 'hi'}, ], ), chatMessage( 'a1', 'assistant', parts: [ {'type': 'text', 'id': 'a1#text#0', 'text': 'answer'}, ], ), _reportMessage(), _noticeMessage(), chatMessage( 'a2', 'assistant', parts: [ {'type': 'text', 'id': 'a2#text#0', 'text': 'next'}, ], ), ]); expect(rows.map((row) => row['kind']), [ 'user', 'assistant', 'agent_report', 'system_notice', 'assistant', ]); expect(rows[1]['messageIds'], ['a1']); expect(rows[4]['messageIds'], ['a2']); }); test('a live report frame keeps its kind and does not start a turn', () { final thread = applyChatEvent( {'sessionId': 's', 'messages': []}, { 'session_id': 's', 'message_id': 'agent-report-1', 'message_kind': 'agent_report', 'kind': 'text', 'part': {'id': 'agent-report-1#text#0', 'name': 'general-purpose'}, 'delta': _report, }, ); final message = (thread['messages'] as List).single as Map; expect(message['kind'], 'agent_report'); expect(message['role'], 'assistant'); expect((message['parts'] as List).single['name'], 'general-purpose'); expect(thread['running'], isNot(true)); }); test('the summary is the first line without markdown markers', () { expect(chatAgentReportSummary(_report), 'Segmented bar report'); expect(chatAgentReportSummary('\n- **Done**: all\n'), 'Done: all'); }); testWidgets('a report is a collapsed card on the agent side, a notice is ' 'one line, a prompt stays a bubble', (tester) async { tester.view.physicalSize = const Size(1000, 1600); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final gateway = _Gateway() ..messages = [ chatMessage( 'u1', 'user', parts: [ {'type': 'text', 'text': 'Please delegate this'}, ], ), _reportMessage(), _noticeMessage(), ]; addTearDown(gateway.close); await _host(tester, gateway); final prompt = find.text('Please delegate this'); expect(prompt, findsOneWidget); expect( find.ancestor(of: prompt, matching: find.byType(ChatUserBubble)), findsOneWidget, ); final card = find.byType(ChatAgentReportCard); expect(card, findsOneWidget); expect(find.text('Agent report · general-purpose'), findsOneWidget); expect(find.text('Segmented bar report'), findsOneWidget); // Collapsed: the rest of the report is not laid out yet. expect(find.textContaining('nested detail'), findsNothing); expect( find.ancestor(of: card, matching: find.byType(ChatUserBubble)), findsNothing, ); // The card starts on the agent's (leading) side; the prompt ends on // the operator's (trailing) side. final width = tester.getSize(find.byType(ChatThreadView)).width; expect(tester.getTopLeft(card).dx, lessThan(width * 0.2)); expect( tester.getTopRight(find.byType(ChatUserBubble)).dx, greaterThan(width * 0.8), ); final notice = find.text( 'Background task finished: Background command "Build" completed', ); expect(notice, findsOneWidget); expect( find.ancestor(of: notice, matching: find.byType(ChatUserBubble)), findsNothing, ); expect(find.bySemanticsLabel('System notice'), findsOneWidget); await tester.tap(find.text('Agent report · general-purpose')); await _pump(tester); expect(find.textContaining('nested detail'), findsOneWidget); expect(find.textContaining('first finding'), findsOneWidget); // Markdown is rendered, not shown raw. expect(find.textContaining('## Segmented'), findsNothing); expect(find.textContaining('**whole**'), findsNothing); }); testWidgets('a live report leaves the prompt\'s delivery line alone ', ( tester, ) async { final gateway = _Gateway(); addTearDown(gateway.close); await _host(tester, gateway); await tester.enterText( find.byKey(const Key('chat-composer-field')), 'Hello', ); await tester.pump(); await tester.tap(find.byKey(const Key('chat-composer-primary'))); await _pump(tester); gateway.prompts.single.complete({}); await _pump(tester); expect(find.byKey(const Key('chat-delivery-sent')), findsOneWidget); gateway.emitEvent({ 'session_id': 's', 'message_id': 'agent-report-1', 'message_kind': 'agent_report', 'kind': 'text', 'part': {'id': 'agent-report-1#text#0', 'name': 'general-purpose'}, 'delta': _report, }); await _pump(tester); expect(find.byType(ChatAgentReportCard), findsOneWidget); expect(find.byKey(const Key('chat-delivery-sent')), findsOneWidget); expect( find.ancestor( of: find.text('Hello'), matching: find.byType(ChatUserBubble), ), findsOneWidget, ); }); }