feat(app): add chat thread controller for live conversations
Owns one open chat session: subscribes with chat_subscribe, reads the newest history page, folds only this session's chat_event frames, pages older history with the opaque cursor, and replaces the held turn on resync. Failures keep the held transcript and surface an error; disposal only cancels the local stream. Co-authored-by: C2 pipeline <noreply@pipeline.local>
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
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 'chat_thread_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
late FakeChatGateway gateway;
|
||||
late ChatThreadController controller;
|
||||
|
||||
setUp(() {
|
||||
gateway = FakeChatGateway();
|
||||
controller = ChatThreadController(gateway, 's');
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
controller.dispose();
|
||||
gateway.close();
|
||||
});
|
||||
|
||||
test(
|
||||
'subscribes the exact session, then reads newest history with all data',
|
||||
() async {
|
||||
final unknown = chatMessage(
|
||||
'a',
|
||||
'assistant',
|
||||
parts: [
|
||||
{
|
||||
'type': 'future_part',
|
||||
'id': 'p1',
|
||||
'payload': {'nested': true},
|
||||
'unknown_field': 7,
|
||||
},
|
||||
],
|
||||
)..['future_message_field'] = 'keep';
|
||||
gateway.history = (_) async =>
|
||||
chatPage([unknown], cursor: 'opaque/next?x=1', hasMore: true);
|
||||
|
||||
await controller.load();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), [
|
||||
'chat_subscribe',
|
||||
'chat_messages',
|
||||
]);
|
||||
expect(gateway.calls.first.fields, {
|
||||
'chat_subscribe': {'session_id': 's'},
|
||||
});
|
||||
expect(gateway.calls.last.fields['chat_query'], {
|
||||
'session_id': 's',
|
||||
'limit': 200,
|
||||
'cursor': '',
|
||||
});
|
||||
expect(controller.loaded, isTrue);
|
||||
expect(controller.loading, isFalse);
|
||||
expect(controller.error, isNull);
|
||||
expect(controller.running, isFalse);
|
||||
expect(controller.hasMore, isTrue);
|
||||
expect(controller.cursor, 'opaque/next?x=1');
|
||||
expect(controller.messages.single, unknown);
|
||||
expect(controller.messages.single['future_message_field'], 'keep');
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'loadOlder passes the opaque cursor back and prepends the page',
|
||||
() async {
|
||||
gateway.history = (_) async => chatPage(
|
||||
[chatMessage('b', 'user')],
|
||||
cursor: 'older-1',
|
||||
hasMore: true,
|
||||
);
|
||||
await controller.load();
|
||||
|
||||
final olderReply = Completer<JsonMap>();
|
||||
gateway.history = (_) => olderReply.future;
|
||||
final loadingStates = <bool>[];
|
||||
controller.addListener(() => loadingStates.add(controller.loading));
|
||||
final older = controller.loadOlder();
|
||||
|
||||
expect(gateway.calls.last.fields['chat_query'], {
|
||||
'session_id': 's',
|
||||
'limit': 200,
|
||||
'cursor': 'older-1',
|
||||
});
|
||||
expect(controller.loading, isTrue);
|
||||
expect(loadingStates, [true]);
|
||||
olderReply.complete(
|
||||
chatPage(
|
||||
[chatMessage('a', 'assistant')],
|
||||
cursor: 'last',
|
||||
hasMore: false,
|
||||
),
|
||||
);
|
||||
await older;
|
||||
|
||||
expect(controller.messages.map((item) => item['id']), ['a', 'b']);
|
||||
expect(controller.cursor, 'last');
|
||||
expect(controller.hasMore, isFalse);
|
||||
expect(controller.loading, isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'a superseded history reply never replaces the newer transcript',
|
||||
() async {
|
||||
final first = Completer<JsonMap>();
|
||||
final second = Completer<JsonMap>();
|
||||
var request = 0;
|
||||
gateway.history = (_) => request++ == 0 ? first.future : second.future;
|
||||
|
||||
final stale = controller.load();
|
||||
await pumpEventQueue();
|
||||
final current = controller.load();
|
||||
await pumpEventQueue();
|
||||
second.complete(chatPage([chatMessage('new', 'user')]));
|
||||
await current;
|
||||
expect(controller.messages.single['id'], 'new');
|
||||
|
||||
first.complete(chatPage([chatMessage('old', 'user')]));
|
||||
await stale;
|
||||
expect(controller.messages.single['id'], 'new');
|
||||
expect(controller.loaded, isTrue);
|
||||
expect(controller.loading, isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'a failed history read reports the error, never an empty success',
|
||||
() async {
|
||||
gateway.history = (_) => Future.error(StateError('history unavailable'));
|
||||
|
||||
await controller.load();
|
||||
|
||||
expect(controller.error, contains('history unavailable'));
|
||||
expect(controller.error, isNot(''));
|
||||
expect(controller.messages, isEmpty);
|
||||
expect(controller.loading, isFalse);
|
||||
expect(controller.loaded, isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
test('a refused subscribe leaves history usable', () async {
|
||||
gateway.subscribe = () => Future.error(StateError('not live'));
|
||||
gateway.history = (_) async => chatPage([chatMessage('a', 'user')]);
|
||||
|
||||
await controller.load();
|
||||
|
||||
expect(controller.messages.single['id'], 'a');
|
||||
expect(controller.error, isNull);
|
||||
expect(controller.loaded, isTrue);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user