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

82 lines
2.3 KiB
Dart

import 'dart:async';
import 'package:pipelines_app/core/api_gateway.dart';
/// A fake gateway whose subscribe/history replies the test controls.
class FakeChatGateway implements ApiGateway {
final calls = <({String type, JsonMap fields})>[];
final historyRequests = <Completer<JsonMap>>[];
final _frames = StreamController<JsonMap>.broadcast();
Future<JsonMap> Function(JsonMap fields)? history;
Future<JsonMap> Function()? subscribe;
Future<JsonMap> Function(JsonMap fields)? reply;
Future<JsonMap> Function(JsonMap fields)? answer;
void Function()? onSubscribe;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
calls.add((type: type, fields: fields));
switch (type) {
case 'chat_subscribe':
onSubscribe?.call();
return subscribe == null ? <String, dynamic>{} : subscribe!();
case 'chat_messages':
final completer = Completer<JsonMap>();
historyRequests.add(completer);
if (history != null) return history!(fields);
return completer.future;
case 'chat_reply':
if (reply != null) return reply!(fields);
return <String, dynamic>{};
case 'chat_answer':
if (answer != null) return answer!(fields);
return <String, dynamic>{};
}
return <String, dynamic>{};
}
@override
Stream<JsonMap> get frames => _frames.stream;
void emitEvent(JsonMap body) {
_frames.add({'type': 'chat_event', 'chat_event': body});
}
void emitRaw(JsonMap envelope) => _frames.add(envelope);
void close() => _frames.close();
}
JsonMap chatPage(
List<JsonMap> messages, {
String cursor = '',
bool hasMore = false,
}) => {
'chat_history': {
'messages': messages,
'next_cursor': cursor,
'has_more': hasMore,
},
};
JsonMap chatMessage(String id, String role, {List<JsonMap> parts = const []}) =>
{'id': id, 'role': role, 'parts': parts};
JsonMap chatTextFrame(
String text, {
String session = 's',
String messageId = 'a',
}) => {
'session_id': session,
'message_id': messageId,
'kind': 'text',
'part': {'id': 'p', 'type': 'text'},
'delta': text,
};
JsonMap chatStepFrame(String status, {String session = 's'}) => {
'session_id': session,
'kind': 'step',
'step': {'status': status, 'turn_id': 'turn', 'intermediate': false},
};