import 'dart:async'; import 'dart:collection'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/core/transport/connection_state.dart'; /// Scripted gateway for session-browser widget tests. class FakeChatGateway implements ApiGateway { final calls = <({String type, JsonMap fields})>[]; final pendingSessions = Queue>(); final failures = {}; JsonMap sessionsReply = const {'chat_sessions': []}; Future Function()? sessionsHandler; JsonMap createReply = const { 'chat_session': {'id': 'created', 'title': 'New chat'}, }; Future Function(JsonMap fields)? createHandler; Future Function(String type, JsonMap fields)? actionHandler; Future Function()? directoriesHandler; Future Function()? backendsHandler; /// Server-pushed frames; tests add `chat_event` or `chat_permission` here. final pushed = StreamController.broadcast(); @override Stream get frames => pushed.stream; @override Future call(String type, [JsonMap fields = const {}]) { calls.add((type: type, fields: fields)); final failure = failures[type]; if (failure != null) return Future.error(ProtocolError(failure)); switch (type) { case 'chat_sessions': if (pendingSessions.isNotEmpty) { return pendingSessions.removeFirst().future; } if (sessionsHandler != null) return sessionsHandler!(); return Future.value(sessionsReply); case 'chat_directories': return directoriesHandler?.call() ?? Future.value(const { 'chat_directories': ['/work', '/archive'], }); case 'chat_backends': return backendsHandler?.call() ?? Future.value(const { 'chat_backends': ['opencode', 'codex'], }); case 'chat_create': if (createHandler != null) return createHandler!(fields); return Future.value(createReply); default: if (actionHandler != null) return actionHandler!(type, fields); return Future.value(const {}); } } int count(String type) => calls.where((call) => call.type == type).length; JsonMap? lastFields(String type) { for (final call in calls.reversed) { if (call.type == type) return call.fields; } return null; } }