import 'package:flutter/cupertino.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/domain/chat_transcript.dart'; import 'chat_thread_fixtures.dart'; JsonMap _record({ String type = 'activity', String? id, String? kind, String? state, String? label, }) => { 'type': type, 'id': ?id, 'kind': ?kind, 'state': ?state, 'label': ?label, }; JsonMap _turn(String id, List parts) => { 'id': id, 'role': 'assistant', 'parts': parts, }; List _activityRows(List rows) => rows.where((row) => row['kind'] == 'activity').toList(); void main() { group('recorded activity wording', () { test('running kinds use the web phase words', () { expect( chatActivityLabel(_record(kind: 'compaction', state: 'running')), 'Compacting context…', ); expect( chatActivityLabel(_record(kind: 'thinking', state: 'running')), 'Thinking…', ); expect( chatActivityLabel(_record(kind: 'tool', state: 'running')), 'Running a tool…', ); expect( chatActivityLabel(_record(kind: 'waiting', state: 'running')), 'Waiting…', ); }); test('the part type is the kind when the record does not name one', () { expect( chatActivityLabel(_record(type: 'compaction', state: 'running')), 'Compacting context…', ); }); test('terminal records keep their recorded label', () { expect( chatActivityLabel( _record( kind: 'compaction', state: 'completed', label: 'Context compacted', ), ), 'Context compacted', ); expect( chatActivityLabel( _record( kind: 'compaction', state: 'failed', label: 'Compaction failed', ), ), 'Compaction failed', ); }); test('an unknown kind keeps its recorded label and state', () { expect( chatActivityLabel( _record(kind: 'weaving', state: 'running', label: 'Weaving context'), ), 'Weaving context', ); expect( chatActivityLabel( _record(kind: 'weaving', state: 'failed', label: 'Weave failed'), ), 'Weave failed', ); }); test('no recorded label keeps the recorded kind and state verbatim', () { expect( chatActivityLabel(_record(kind: 'weaving', state: 'completed')), 'weaving · completed', ); expect(chatActivityLabel(_record()), 'activity'); }); }); group('recorded activity rows', () { test('one line per sequence, worded from its latest recorded update', () { final rows = transcriptRows([ _turn('a1', [ _record(kind: 'thinking', state: 'running', id: 'e1'), {'type': 'text', 'text': 'answer'}, _record(type: 'compaction', state: 'running', id: 'e2'), _record( type: 'compaction', state: 'completed', id: 'e3', label: 'Context compacted', ), ]), ]); expect(rows.map((row) => row['kind']), ['activity', 'assistant']); final activity = rows.first; expect(activity['records'], hasLength(3)); expect(activity['label'], 'Context compacted'); expect(activity['state'], 'completed'); expect((rows.last['parts'] as List).single['text'], 'answer'); }); test('a sequence can span consecutive assistant messages', () { final rows = transcriptRows([ _turn('a1', [_record(kind: 'thinking', state: 'running', id: 'e1')]), _turn('a2', [ _record( kind: 'thinking', state: 'completed', id: 'e2', label: 'Thought', ), {'type': 'text', 'text': 'answer'}, ]), ]); expect(_activityRows(rows), hasLength(1)); expect(_activityRows(rows).single['label'], 'Thought'); }); test('separate turns keep separate lines', () { final rows = transcriptRows([ _turn('a1', [_record(kind: 'compaction', state: 'running', id: 'e1')]), {'id': 'u1', 'role': 'user', 'parts': []}, _turn('a2', [_record(kind: 'compaction', state: 'running', id: 'e2')]), ]); expect(_activityRows(rows), hasLength(2)); expect(_activityRows(rows).map((row) => row['key']), [ 'chat-activity-a1', 'chat-activity-a2', ]); }); test('an activity-only turn renders no empty assistant row', () { final rows = transcriptRows([ _turn('a1', [_record(kind: 'compaction', state: 'running')]), ]); expect(rows.map((row) => row['kind']), ['activity']); }); }); group('recorded activity rendering', () { Future pumpThread( WidgetTester tester, { required bool cupertino, }) async { final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage([ chatMessage( 'u1', 'user', parts: [ {'type': 'text', 'text': 'Prompt'}, ], ), chatMessage( 'a1', 'assistant', parts: [ { 'type': 'activity', 'id': 'e1', 'kind': 'compaction', 'state': 'running', }, {'type': 'text', 'id': 't1', 'text': 'Answer'}, ], ), ]); final view = ChatThreadView( gateway: gateway, sessionId: 's', onBack: () {}, ); await tester.pumpWidget( cupertino ? CupertinoApp( theme: const CupertinoThemeData(brightness: Brightness.dark), home: CupertinoPageScaffold(child: view), ) : MaterialApp( theme: ThemeData(), home: Scaffold(body: view), ), ); await tester.pumpAndSettle(); return gateway; } testWidgets('Material shows the recorded line once beside the answer', ( tester, ) async { await pumpThread(tester, cupertino: false); expect(find.text('Compacting context…'), findsOneWidget); expect(find.text('Answer'), findsOneWidget); expect(find.textContaining('Unrecognised part'), findsNothing); }); testWidgets('Cupertino shows the same recorded line', (tester) async { await pumpThread(tester, cupertino: true); expect(find.text('Compacting context…'), findsOneWidget); expect(find.text('Answer'), findsOneWidget); expect(find.textContaining('Unrecognised part'), findsNothing); }); }); }