Files
pipelines/app/test/features/chat/conversation/chat_transcript_cache_test.dart
T
alan cf4579ab15 App: keep a left chat's cached transcript following its live frames
A left conversation stays subscribed on the server for the rest of the
connection, but the cached transcript was frozen when it was left. A reply
that arrived in the background only appeared after the reopened view's
subscribe and newest-page read (~1-2s over the network on large chats).
The cache now folds the session's chat events into the kept transcript,
so the reply is drawn the moment the chat opens; the page still replaces it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 14:43:52 +00:00

323 lines
11 KiB
Dart

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<Object?> _ids(List<JsonMap> 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);
});
});
group('a transcript left while its session stays subscribed', () {
late FakeChatGateway gateway;
setUp(() => gateway = FakeChatGateway());
tearDown(() => gateway.close());
ChatTranscriptSnapshot left() => ChatTranscriptSnapshot(
messages: [
_text('u1', 'user', 'question'),
_text('a1', 'assistant', 'first answer'),
],
cursor: 'older',
hasMore: true,
);
test('folds the replies that arrive in the background', () async {
final cache = ChatTranscriptCache();
cache.write('s', left(), frames: gateway.frames);
cache.write('other', _snapshot(1));
gateway.emitEvent(chatTextFrame('new ', messageId: 'a2'));
gateway.emitEvent(chatTextFrame('reply', messageId: 'a2'));
// Another session's frame and a non-event envelope change nothing.
gateway.emitEvent(chatTextFrame('elsewhere', session: 'x'));
gateway.emitRaw({'type': 'chat_queue', 'chat_queue': {}});
await pumpEventQueue();
final held = cache.read('s')!;
expect(_ids(held.messages), ['u1', 'a1', 'a2']);
expect((held.messages.last['parts'] as List).single['text'], 'new reply');
expect(held.cursor, 'older');
expect(held.hasMore, isTrue);
expect(cache.messageCount, 4);
});
test('stops following once replaced, removed or cleared', () async {
final cache = ChatTranscriptCache();
cache.write('s', left(), frames: gateway.frames);
cache.write('s', left());
gateway.emitEvent(chatTextFrame('ignored', messageId: 'a2'));
await pumpEventQueue();
expect(_ids(cache.read('s')!.messages), ['u1', 'a1']);
cache.write('s', left(), frames: gateway.frames);
cache.remove('s');
cache.write('t', left(), frames: gateway.frames);
cache.clear();
gateway.emitEvent(chatTextFrame('ignored', messageId: 'a2'));
await pumpEventQueue();
expect(cache.sessionIds, isEmpty);
expect(cache.messageCount, 0);
});
test('a growing transcript keeps the message bound', () async {
final cache = ChatTranscriptCache(maxMessages: 4);
cache.write('old', _snapshot(2, prefix: 'o'));
cache.write('s', left(), frames: gateway.frames);
gateway.emitEvent(chatTextFrame('reply', messageId: 'a2'));
await pumpEventQueue();
expect(cache.sessionIds, ['s']);
expect(cache.messageCount, 3);
});
test('a reopened controller shows the background reply before its read '
'returns, and the page still replaces it', () async {
final cache = ChatTranscriptCache();
cache.write('s', left(), frames: gateway.frames);
gateway.emitEvent(chatTextFrame('background reply', messageId: 'a2'));
await pumpEventQueue();
final controller = ChatThreadController(
gateway,
's',
cached: cache.read('s'),
);
addTearDown(controller.dispose);
expect(_ids(controller.messages), ['u1', 'a1', 'a2']);
final load = controller.load();
await pumpEventQueue();
expect(_ids(controller.messages), ['u1', 'a1', 'a2']);
gateway.historyRequests.single.complete(
chatPage([
_text('u1', 'user', 'question'),
_text('a1', 'assistant', 'first answer'),
_text('a2', 'assistant', 'recorded reply'),
]),
);
await load;
expect(_ids(controller.messages), ['u1', 'a1', 'a2']);
expect(
(controller.messages.last['parts'] as List).single['text'],
'recorded reply',
);
});
});
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<JsonMap>), [
'u1',
'a1',
'u2',
'live',
]);
expect(
((merged['messages'] as List<JsonMap>)[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<JsonMap>();
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);
});
});
}