import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_controller.dart'; import 'package:pipelines_app/features/chat/conversation/chat_transcript_cache.dart'; import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; import 'chat_thread_fixtures.dart'; JsonMap _text(String id, String role, String text) => chatMessage( id, role, parts: [ {'type': 'text', 'id': 'p-$id', 'text': text}, ], ); ChatTranscriptSnapshot _snapshot(int count, {String prefix = 'm'}) => ChatTranscriptSnapshot( messages: [ for (var i = 0; i < count; i++) chatMessage('$prefix$i', 'user'), ], ); List _ids(List messages) => [ for (final message in messages) message['id'], ]; void main() { group('ChatTranscriptCache', () { test('keeps the most recently used sessions within the session bound', () { final cache = ChatTranscriptCache(maxSessions: 3, maxMessages: 100); for (final id in ['a', 'b', 'c']) { cache.write(id, _snapshot(1)); } // Reading marks a conversation recently used, so it outlives b. expect(cache.read('a'), isNotNull); cache.write('d', _snapshot(1)); expect(cache.sessionIds, ['c', 'a', 'd']); expect(cache.read('b'), isNull); }); test('evicts the least recently used until the message bound holds', () { final cache = ChatTranscriptCache(maxSessions: 10, maxMessages: 10); cache.write('a', _snapshot(4)); cache.write('b', _snapshot(4)); cache.write('c', _snapshot(4)); expect(cache.sessionIds, ['b', 'c']); expect(cache.messageCount, 8); // Rewriting one conversation replaces its count instead of adding to it. cache.write('b', _snapshot(2)); expect(cache.messageCount, 6); expect(cache.sessionIds, ['c', 'b']); }); test('never keeps a transcript larger than the whole bound', () { final cache = ChatTranscriptCache(maxSessions: 10, maxMessages: 10); cache.write('a', _snapshot(3)); cache.write('a', _snapshot(11)); expect(cache.read('a'), isNull); expect(cache.messageCount, 0); }); test('remove and clear forget transcripts and their counts', () { final cache = ChatTranscriptCache(); cache.write('a', _snapshot(2)); cache.write('b', _snapshot(3)); cache.remove('a'); expect(cache.sessionIds, ['b']); expect(cache.messageCount, 3); cache.clear(); expect(cache.sessionIds, isEmpty); expect(cache.messageCount, 0); }); }); test('refreshCachedChatHistory lets the page decide and keeps live ' 'arrivals', () { final held = [ _text('gone', 'user', 'deleted on the server'), _text('u1', 'user', 'cached question'), _text('a1', 'assistant', 'cached stale answer'), _text('live', 'assistant', 'arrived after reopening'), ]; final page = { 'messages': [ _text('u1', 'user', 'cached question'), _text('a1', 'assistant', 'authoritative answer'), _text('u2', 'user', 'sent from another client'), ], 'nextCursor': 'next', 'hasMore': true, }; final merged = refreshCachedChatHistory(held, page, {'gone', 'u1', 'a1'}); expect(_ids(merged['messages'] as List), [ 'u1', 'a1', 'u2', 'live', ]); expect( ((merged['messages'] as List)[1]['parts'] as List) .single['text'], 'authoritative answer', ); expect(merged['nextCursor'], 'next'); expect(merged['hasMore'], isTrue); }); group('a controller reopened on a cached transcript', () { late FakeChatGateway gateway; late ChatThreadController controller; final cached = ChatTranscriptSnapshot( messages: [ _text('u1', 'user', 'question'), _text('a', 'assistant', 'cached answer'), ], cursor: 'cached-cursor', hasMore: true, ); setUp(() { gateway = FakeChatGateway(); controller = ChatThreadController(gateway, 's', cached: cached); }); tearDown(() { controller.dispose(); gateway.close(); }); test('shows the cached transcript before the read, then re-subscribes ' 'and lets the newest page replace it', () async { expect(controller.loaded, isTrue); expect(controller.showingCached, isTrue); expect(_ids(controller.messages), ['u1', 'a']); // A cached transcript is not backed by a read on this connection. expect(controller.snapshot, isNull); final load = controller.load(); await pumpEventQueue(); expect(gateway.calls.map((call) => call.type), [ 'chat_subscribe', 'chat_messages', ]); expect(controller.loading, isTrue); expect(_ids(controller.messages), ['u1', 'a']); gateway.historyRequests.single.complete( chatPage([ _text('a', 'assistant', 'fresh answer'), _text('u2', 'user', 'newer question'), ]), ); await load; expect(controller.showingCached, isFalse); expect(controller.loading, isFalse); expect(_ids(controller.messages), ['a', 'u2']); expect( (controller.messages.first['parts'] as List).single['text'], 'fresh answer', ); expect(controller.cursor, ''); expect(controller.hasMore, isFalse); expect(_ids(controller.snapshot!.messages), ['a', 'u2']); }); test('keeps a live frame that arrives while the read is pending', () async { final load = controller.load(); await pumpEventQueue(); gateway.emitEvent(chatTextFrame('streamed reply', messageId: 'live')); await pumpEventQueue(); expect(_ids(controller.messages), ['u1', 'a', 'live']); gateway.historyRequests.single.complete( chatPage([ _text('u1', 'user', 'question'), _text('a', 'assistant', 'cached answer'), ]), ); await load; expect(_ids(controller.messages), ['u1', 'a', 'live']); expect( (controller.messages.last['parts'] as List).single['text'], 'streamed reply', ); expect(controller.running, isTrue); }); test('a refused read keeps the cached transcript with its error and ' 'offers nothing to cache', () async { gateway.history = (_) async => throw StateError('offline'); await controller.load(); expect(controller.error, contains('offline')); expect(_ids(controller.messages), ['u1', 'a']); expect(controller.snapshot, isNull); }); test('a socket gap withdraws the snapshot until the repair read', () async { gateway.history = (_) async => chatPage([_text('a', 'assistant', 'x')]); await controller.load(); expect(controller.snapshot, isNotNull); final repaired = Completer(); gateway.history = (_) => repaired.future; final resync = controller.resync(); await pumpEventQueue(); expect(controller.snapshot, isNull); repaired.complete(chatPage([_text('a', 'assistant', 'y')])); await resync; expect(controller.snapshot, isNotNull); }); }); }