76437db018
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>
189 lines
5.5 KiB
Dart
189 lines
5.5 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 '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('folds own session frames only and keeps live turn activity', () async {
|
|
gateway.history = (_) async => chatPage([
|
|
chatMessage(
|
|
'a',
|
|
'assistant',
|
|
parts: [
|
|
{'type': 'reasoning', 'id': 'r', 'text': 'held'},
|
|
],
|
|
),
|
|
]);
|
|
await controller.load();
|
|
var notifications = 0;
|
|
controller.addListener(() => notifications++);
|
|
|
|
gateway.emitEvent(chatTextFrame('foreign', session: 'other'));
|
|
gateway.emitEvent(chatTextFrame('nodata')..remove('session_id'));
|
|
gateway.emitRaw({'type': 'run_event', 'session_id': 's'});
|
|
await pumpEventQueue();
|
|
expect(notifications, 0);
|
|
expect(controller.messages.single['parts'], [
|
|
{'type': 'reasoning', 'id': 'r', 'text': 'held'},
|
|
]);
|
|
expect(controller.running, isFalse);
|
|
|
|
gateway.emitEvent(chatTextFrame('one'));
|
|
gateway.emitEvent(chatTextFrame(' two'));
|
|
await pumpEventQueue();
|
|
expect(controller.running, isTrue);
|
|
expect(controller.messages.single['parts'], [
|
|
{'type': 'reasoning', 'id': 'r', 'text': 'held'},
|
|
{'type': 'text', 'id': 'p', 'text': 'one two'},
|
|
]);
|
|
|
|
gateway.emitEvent(chatStepFrame('ended'));
|
|
await pumpEventQueue();
|
|
expect(controller.running, isFalse);
|
|
expect(notifications, greaterThan(0));
|
|
});
|
|
|
|
test(
|
|
'resync re-subscribes, settles the held turn, and replaces it with the page',
|
|
() async {
|
|
gateway.history = (_) async => chatPage(
|
|
[
|
|
chatMessage('stale-user', 'user'),
|
|
chatMessage('stale-assistant', 'assistant'),
|
|
],
|
|
cursor: 'stale-cursor',
|
|
hasMore: true,
|
|
);
|
|
await controller.load();
|
|
gateway.emitEvent(chatTextFrame('still running'));
|
|
await pumpEventQueue();
|
|
expect(controller.running, isTrue);
|
|
|
|
gateway.onSubscribe = () {
|
|
// Settled before the re-attach, then replay proves the fresh turn.
|
|
expect(controller.running, isFalse);
|
|
gateway.emitEvent(chatStepFrame('started'));
|
|
};
|
|
gateway.history = (_) async => chatPage(
|
|
[
|
|
chatMessage('fresh-user', 'user'),
|
|
chatMessage('fresh-assistant', 'assistant'),
|
|
],
|
|
cursor: 'fresh-cursor',
|
|
hasMore: false,
|
|
);
|
|
|
|
await controller.resync();
|
|
await pumpEventQueue();
|
|
|
|
expect(
|
|
gateway.calls.where((call) => call.type == 'chat_subscribe').length,
|
|
2,
|
|
);
|
|
expect(
|
|
gateway.calls.where((call) => call.type == 'chat_messages').length,
|
|
2,
|
|
);
|
|
expect(gateway.calls.last.fields['chat_query'], {
|
|
'session_id': 's',
|
|
'limit': 200,
|
|
'cursor': '',
|
|
});
|
|
expect(controller.messages.map((item) => item['id']), [
|
|
'fresh-user',
|
|
'fresh-assistant',
|
|
]);
|
|
expect(controller.cursor, 'fresh-cursor');
|
|
expect(controller.hasMore, isFalse);
|
|
expect(controller.running, isTrue);
|
|
expect(controller.error, isNull);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'a failed resync keeps replayed activity and reports the error',
|
|
() async {
|
|
var attempt = 0;
|
|
gateway.history = (_) async {
|
|
attempt++;
|
|
if (attempt == 1) return chatPage([chatMessage('held', 'user')]);
|
|
throw StateError('resync unavailable');
|
|
};
|
|
await controller.load();
|
|
gateway.onSubscribe = () {
|
|
gateway.emitEvent(chatStepFrame('started'));
|
|
};
|
|
|
|
await controller.resync();
|
|
await pumpEventQueue();
|
|
|
|
expect(controller.messages.single['id'], 'held');
|
|
expect(controller.running, isTrue);
|
|
expect(controller.error, contains('resync unavailable'));
|
|
expect(controller.loading, isFalse);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'a resync frame reattaches and replaces the new authoritative page',
|
|
() async {
|
|
var attempt = 0;
|
|
gateway.history = (_) async => attempt++ == 0
|
|
? chatPage([chatMessage('held', 'user')], hasMore: true)
|
|
: chatPage([chatMessage('fresh', 'assistant')]);
|
|
await controller.load();
|
|
|
|
gateway.emitEvent({'session_id': 's', 'kind': 'resync'});
|
|
await pumpEventQueue();
|
|
|
|
expect(
|
|
gateway.calls.where((call) => call.type == 'chat_subscribe').length,
|
|
2,
|
|
);
|
|
expect(controller.messages.single['id'], 'fresh');
|
|
expect(controller.hasMore, isFalse);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'disposal ignores a late reply and never invents an unsubscribe',
|
|
() async {
|
|
final pending = Completer<JsonMap>();
|
|
gateway.history = (_) => pending.future;
|
|
var notifications = 0;
|
|
controller.addListener(() => notifications++);
|
|
final load = controller.load();
|
|
await pumpEventQueue();
|
|
final before = notifications;
|
|
|
|
controller.dispose();
|
|
pending.complete(chatPage([chatMessage('late', 'user')]));
|
|
await load;
|
|
|
|
expect(notifications, before);
|
|
expect(
|
|
gateway.calls.where((call) => call.type == 'chat_unsubscribe'),
|
|
isEmpty,
|
|
);
|
|
// Keep tearDown's dispose idempotent.
|
|
controller = ChatThreadController(gateway, 's');
|
|
},
|
|
);
|
|
}
|