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 = >[]; final _frames = StreamController.broadcast(); Future Function(JsonMap fields)? history; Future Function()? subscribe; Future Function(JsonMap fields)? reply; Future Function(JsonMap fields)? answer; void Function()? onSubscribe; @override Future call(String type, [JsonMap fields = const {}]) async { calls.add((type: type, fields: fields)); switch (type) { case 'chat_subscribe': onSubscribe?.call(); return subscribe == null ? {} : subscribe!(); case 'chat_messages': final completer = Completer(); historyRequests.add(completer); if (history != null) return history!(fields); return completer.future; case 'chat_reply': if (reply != null) return reply!(fields); return {}; case 'chat_answer': if (answer != null) return answer!(fields); return {}; } return {}; } @override Stream 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 messages, { String cursor = '', bool hasMore = false, }) => { 'chat_history': { 'messages': messages, 'next_cursor': cursor, 'has_more': hasMore, }, }; JsonMap chatMessage(String id, String role, {List 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}, };