import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/conversation/chat_row_keys.dart'; import 'package:pipelines_app/features/chat/conversation/chat_transcript_anchor.dart'; import 'package:pipelines_app/features/chat/domain/chat_activity_groups.dart'; import 'package:pipelines_app/features/chat/domain/chat_transcript_rows.dart'; JsonMap _user(String id) => { 'id': id, 'role': 'user', 'parts': [ {'type': 'text', 'text': 'question $id'}, ], }; JsonMap _answer(String id, {List tools = const []}) => { 'id': id, 'role': 'assistant', 'parts': [ ...tools, {'type': 'text', 'text': 'answer $id'}, ], }; JsonMap _tool(String id, String name, {bool failed = false}) => { 'type': 'tool', 'id': id, 'name': name, 'state': {'status': failed ? 'error' : 'completed'}, }; JsonMap _work(String id, List parts) => { 'id': id, 'role': 'assistant', 'parts': parts, }; JsonMap _notice(String id, {String outcome = 'finished'}) => { 'id': id, 'role': 'assistant', 'kind': 'notice', 'parts': [ {'type': 'text', 'text': 'Background task $outcome: task $id'}, ], }; List _rows(List messages) => groupChatActivity(transcriptRows(messages)); List _kinds(List rows) => [ for (final row in rows) '${row['kind']}', ]; /// The transcript the operator reported: two answers with tool-only turns and /// background-task notices between them. List _backgroundSession() => [ _user('u1'), _answer('a1'), _notice('n1'), _work('w1', [for (var i = 0; i < 9; i++) _tool('w1-$i', 'read')]), _notice('n2'), _notice('n3'), _work('w2', [_tool('w2-0', 'bash')]), _notice('n4'), _work('w3', [ for (var i = 0; i < 4; i++) _tool('w3-$i', 'bash', failed: i == 1), ]), _notice('n5'), _work('w4', [ {'type': 'reasoning', 'text': 'thinking'}, ]), _notice('n6'), _answer('a2'), ]; void main() { test('a run of activity rows between two answers is one summary row', () { final rows = _rows(_backgroundSession()); expect(_kinds(rows), [ 'user', 'assistant', chatActivityGroupKind, 'assistant', ]); final group = rows[2]; expect( chatActivityGroupLabel(group), 'Ran 5 commands · used 9 tools · 6 background tasks finished · 1 failed', ); final segments = chatActivityGroupSegments(group); expect(segments.last, (text: '1 failed', failed: true)); expect(segments.where((s) => s.failed), hasLength(1)); // The members render as before, with every notice folded into one chip // at the newest notice's place, so the chip shows once. final members = chatActivityGroupRows(group); expect(_kinds(members), [ 'assistant', 'assistant', 'assistant', 'assistant', 'task_notices', ]); expect(members.last['messageIds'], ['n1', 'n2', 'n3', 'n4', 'n5', 'n6']); expect(group['messageIds'], [ 'w1', 'w2', 'w3', 'w4', 'n1', 'n2', 'n3', 'n4', 'n5', 'n6', ]); }); test('content rows bound runs and a single tool turn stays itself', () { final rows = _rows([ _user('u1'), _answer('a1'), _notice('n1'), _answer('a2'), _user('u2'), _work('w1', [_tool('t', 'bash')]), _user('u3'), _work('w2', [_tool('t2', 'bash')]), _work('w3', [ {'type': 'text', 'text': ' '}, ]), ]); // The lone notice is a summary, not a full-width line; the lone tool turn // (folded with its blank neighbour into one turn) keeps its own row. expect(_kinds(rows), [ 'user', 'assistant', chatActivityGroupKind, 'assistant', 'user', 'assistant', 'user', 'assistant', ]); expect(chatActivityGroupLabel(rows[2]), '1 background task finished'); expect(chatActivityGroupRows(rows[2]).single['kind'], 'system_notice'); }); test('a turn with answer text, a chip or a live stream is content', () { final work = transcriptRows([ _work('w1', [_tool('t', 'bash')]), ]).single; expect(chatActivityOnlyRow(work), isTrue); expect(chatActivityOnlyRow({...work, 'streaming': true}), isFalse); expect(chatActivityOnlyRow({...work, 'turnDiff': {}}), isFalse); expect( chatActivityOnlyRow({ ...work, 'shellHistory': ['r1'], }), isFalse, ); expect( chatActivityOnlyRow({ ...work, 'questions': [{}], }), isFalse, ); final answer = transcriptRows([_answer('a1')]).single; expect(chatActivityOnlyRow(answer), isFalse); expect( chatActivityOnlyRow({ ...work, 'parts': [ {'type': 'file', 'name': 'a.png', 'uri': 'x'}, ], }), isFalse, ); // The live turn stays its own row after the run before it. final rows = groupChatActivity( transcriptRows([ _user('u1'), _answer('a1'), _notice('n1'), _notice('n2'), _work('w1', [_tool('t', 'bash')]), ], running: true), ); expect(_kinds(rows), [ 'user', 'assistant', chatActivityGroupKind, 'assistant', ]); expect(rows.last['streaming'], isTrue); }); test('failed and stopped tasks are carried into the summary', () { final rows = _rows([ _answer('a1'), _notice('n1', outcome: 'failed'), _notice('n2', outcome: 'stopped'), _user('u1'), ]); expect( chatActivityGroupLabel(rows[1]), '2 background tasks ended · 1 failed', ); }); test('a repeated task notice counts once, at its latest position', () { final rows = _rows([ _answer('a1'), _notice('n1'), _work('w1', [_tool('t', 'bash')]), _user('u1'), _answer('a2'), _notice('n1'), _user('u2'), ]); // The earlier run keeps only its tool turn, which stays its own row; the // task is counted where it was reported last. expect(_kinds(rows), [ 'assistant', 'assistant', 'user', 'assistant', chatActivityGroupKind, 'user', ]); expect(rows[1]['key'], 'w1'); expect(chatActivityGroupLabel(rows[4]), '1 background task finished'); }); test('a repeated notice leaves only the later chip', () { final rows = _rows([ _answer('a1'), _notice('n1'), _notice('n2'), _user('u1'), _answer('a2'), _notice('n1'), _notice('n2'), _user('u2'), ]); expect(_kinds(rows), [ 'assistant', 'user', 'assistant', chatActivityGroupKind, 'user', ]); expect(chatActivityGroupLabel(rows[3]), '2 background tasks finished'); }); test( 'identity holds across a streamed delta and the key carries forward', () { final cache = ChatTranscriptRows(); final history = _backgroundSession(); final live = { 'id': 'live', 'role': 'assistant', 'parts': [ {'type': 'text', 'text': 'word'}, ], }; final before = groupChatActivity( cache.rows([...history, _user('u9'), live], running: true), ); final delta = { ...live, 'parts': [ {'type': 'text', 'text': 'word word'}, ], }; final after = groupChatActivity( cache.rows([...history, _user('u9'), delta], running: true), ); expect(identical(before[2], after[2]), isTrue); expect( identical( chatActivityGroupRows(before[2]).last, chatActivityGroupRows(after[2]).last, ), isTrue, ); // A tool turn that already had a row keeps its key when it joins a group. final keys = ChatRowKeys(); final single = _rows([ _user('u1'), _work('w1', [_tool('t', 'bash')]), ]); final firstKeys = keys.forRows(single); final grown = _rows([ _user('u1'), _work('w1', [_tool('t', 'bash')]), _notice('n1'), ]); expect(keys.forRows(grown)[1], firstKeys[1]); }, ); test('a group can anchor the transcript split by its agent turns', () { final rows = _rows([..._backgroundSession(), _user('u2'), _answer('a3')]); final anchor = chatTranscriptAnchor(rows); expect(anchor, 'u2'); expect(chatTranscriptAnchorIndex(rows, 'w3'), 2); expect(chatTranscriptAnchorIndex(rows, 'n3'), -1); }); }