Files
pipelines/app/test/features/chat/conversation/chat_automation_lifecycle_test.dart
T

135 lines
4.3 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_background_tasks_controller.dart';
JsonMap _report(String id, int count) => {
'chat_automations': {
'session_id': id,
'status': 'supported',
'active_count': count,
},
};
class _Gateway implements ApiGateway {
final events = StreamController<JsonMap>.broadcast();
final reads = <String>[];
late Future<JsonMap> Function(String id) automation;
@override
Stream<JsonMap> get frames => events.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'chat_shells') {
return Future.value({'chat_shells': <JsonMap>[]});
}
if (type == 'chat_automations') {
final id = (fields['chat_query'] as Map)['session_id'] as String;
reads.add(id);
return automation(id);
}
throw UnsupportedError(type);
}
void ended(String id) => events.add({
'type': 'chat_event',
'chat_event': {
'session_id': id,
'kind': 'step',
'step': {'status': 'ended', 'intermediate': false},
},
});
}
void main() {
testWidgets('replacement loads while old server reply is still pending', (
tester,
) async {
final oldReply = Completer<JsonMap>();
final nextReply = Completer<JsonMap>();
final old = _Gateway()..automation = (_) => oldReply.future;
final next = _Gateway()..automation = (_) => nextReply.future;
final controller = ChatBackgroundTasksController(old, 'same-id');
final notifications = <int?>[];
controller.addListener(
() => notifications.add(controller.automationStatus.count),
);
addTearDown(() async {
controller.dispose();
await old.events.close();
await next.events.close();
});
await tester.pump();
controller.replace(next, 'same-id', notify: false);
await tester.pump();
expect(next.reads, ['same-id']);
expect(controller.loading, isFalse);
notifications.clear();
// The shell list already settled. This slower response must still notify.
nextReply.complete(_report('same-id', 2));
await tester.pump();
expect(controller.automationStatus.count, 2);
expect(notifications, contains(2));
oldReply.complete(_report('same-id', 99));
await tester.pump();
expect(controller.automationStatus.count, 2);
expect(notifications, isNot(contains(99)));
});
testWidgets('a real completion during a read receives one trailing refresh', (
tester,
) async {
final first = Completer<JsonMap>();
final gateway = _Gateway();
gateway.automation = (id) =>
gateway.reads.length == 1 ? first.future : Future.value(_report(id, 1));
final controller = ChatBackgroundTasksController(gateway, 'chat');
addTearDown(() async {
controller.dispose();
await gateway.events.close();
});
await tester.pump();
gateway.ended('chat');
await tester.pump();
await tester.pump(const Duration(milliseconds: 251));
expect(gateway.reads.length, 1);
first.complete(_report('chat', 0));
await tester.pump();
await tester.pump(const Duration(milliseconds: 251));
expect(gateway.reads.length, 2);
expect(controller.automationStatus.count, 1);
gateway.ended('another-chat');
gateway.events.add({
'type': 'chat_event',
'chat_event': {'session_id': 'chat', 'kind': 'text', 'text': 'token'},
});
await tester.pump();
await tester.pump(const Duration(milliseconds: 251));
expect(gateway.reads.length, 2);
});
testWidgets('losing metadata notifies and clears the old positive count', (
tester,
) async {
final gateway = _Gateway()..automation = (id) async => _report(id, 3);
final controller = ChatBackgroundTasksController(gateway, 'chat');
addTearDown(() async {
controller.dispose();
await gateway.events.close();
});
await tester.pump();
expect(controller.automationStatus.count, 3);
var notifications = 0;
controller.addListener(() => notifications++);
gateway.automation = (_) => throw UnsupportedError('older server');
unawaited(controller.refreshAutomations());
await tester.pump();
expect(controller.automationStatus.count, isNull);
expect(notifications, greaterThan(0));
expect(controller.error, isNull);
});
}