import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/domain/chat_shown_items.dart'; import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; JsonMap _item(String id, int at, {String kind = 'notify'}) => { 'id': id, 'kind': kind, 'created_at': at, 'title': id, }; JsonMap _message(String id, String role, int at, {List? parts}) => { 'id': id, 'role': role, 'created_at': at, 'parts': parts ?? [ {'type': 'text', 'text': id}, ], }; List _shape(List rows) => [ for (final row in rows) row['kind'] == 'shown' ? 'shown:${(row['item'] as Map)['id']}' : '${row['kind']}:${row['key']}', ]; void main() { group('chatShownItemsOf', () { test('reads only this session and known kinds with ids', () { final body = { 'session_id': 's', 'items': [ _item('a', 1), {'id': '', 'kind': 'file'}, {'id': 'x', 'kind': 'future'}, 'junk', ], }; expect(chatShownItemsOf(body, 's')!.map((i) => i['id']), ['a']); expect(chatShownItemsOf(body, 'other'), isNull); expect(chatShownItemsOf(null, 's'), isNull); }); }); test('merge replaces by id in place and appends new items', () { final held = [_item('a', 1), _item('b', 2)]; final updated = {..._item('a', 1), 'title': 'A2'}; final merged = mergeChatShownItems(held, [updated, _item('c', 3)]); expect(merged.map((i) => i['id']), ['a', 'b', 'c']); expect(merged.first['title'], 'A2'); expect( mergeChatShownItems(held, [_item('z', 9)], replace: true).single['id'], 'z', ); }); group('withChatShownItems', () { final messages = [ _message('u1', 'user', 100), _message('a1', 'assistant', 110), _message('u2', 'user', 200), _message('a2', 'assistant', 210), ]; test('interleaves items by time before trailing cards', () { final rows = transcriptRows( messages, trailing: [ {'kind': 'permission', 'key': 'p'}, ], ); final out = withChatShownItems(rows, [ _item('late', 500), _item('mid', 150), ], hasMore: false); expect(_shape(out), [ 'user:u1', 'assistant:a1', 'shown:mid', 'user:u2', 'assistant:a2', 'shown:late', 'permission:p', ]); final shown = out.firstWhere((row) => row['kind'] == 'shown'); expect(shown['messageIds'], ['shown:mid']); }); test('older items wait for their history page while more remains', () { final rows = transcriptRows(messages); final early = [_item('early', 50), _item('mid', 150)]; expect( _shape(withChatShownItems(rows, early, hasMore: true)), isNot(contains('shown:early')), ); expect( _shape(withChatShownItems(rows, early, hasMore: false)).first, 'shown:early', ); }); test('items lead an empty transcript only when no history remains', () { expect(_shape(withChatShownItems([], [_item('a', 1)], hasMore: false)), [ 'shown:a', ]); expect(withChatShownItems([], [_item('a', 1)], hasMore: true), isEmpty); }); test('hides represented Pipelines tool calls, keeps others and errors', () { JsonMap tool(String name, {String status = 'completed'}) => { 'type': 'tool', 'id': name, 'name': name, 'state': {'status': status}, }; final turn = _message( 'a', 'assistant', 10, parts: [ {'type': 'text', 'text': 'Here it is'}, tool('mcp__pipelines__show_file'), tool('pipelines_show_html'), tool('pipelines/notify'), tool('pipelines_show_image', status: 'error'), tool('bash'), tool('pipelines_other'), ], ); final out = withChatShownItems( transcriptRows([turn]), [], hasMore: false, ); final names = [ for (final part in out.single['parts'] as List) if (part['type'] == 'tool') part['name'], ]; expect(names, ['pipelines_show_image', 'bash', 'pipelines_other']); }); test('a turn holding only a represented call is not drawn', () { final turn = _message( 'a', 'assistant', 10, parts: [ { 'type': 'tool', 'name': 'pipelines_notify', 'state': {'status': 'completed'}, }, ], ); final out = withChatShownItems(transcriptRows([turn]), [ _item('n', 20), ], hasMore: false); expect(_shape(out), ['shown:n']); }); }); }