68 lines
2.3 KiB
Dart
68 lines
2.3 KiB
Dart
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<Completer<JsonMap>>();
|
|
final failures = <String, String>{};
|
|
JsonMap sessionsReply = const {'chat_sessions': []};
|
|
Future<JsonMap> Function()? sessionsHandler;
|
|
JsonMap createReply = const {
|
|
'chat_session': {'id': 'created', 'title': 'New chat'},
|
|
};
|
|
Future<JsonMap> Function(JsonMap fields)? createHandler;
|
|
Future<JsonMap> Function(String type, JsonMap fields)? actionHandler;
|
|
Future<JsonMap> Function()? directoriesHandler;
|
|
Future<JsonMap> Function()? backendsHandler;
|
|
|
|
/// Server-pushed frames; tests add `chat_event` or `chat_permission` here.
|
|
final pushed = StreamController<JsonMap>.broadcast();
|
|
|
|
@override
|
|
Stream<JsonMap> get frames => pushed.stream;
|
|
|
|
@override
|
|
Future<JsonMap> 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;
|
|
}
|
|
}
|