diet-guard/app/lib/services/claude_relay_fake.dart
Krzysztof kuhy Rudnicki b0c556b745 Send meals to Claude over the E2E relay
RtdbClaudeRelay against the contract fixture, ULID ids, uuid5 entry ids, withdraw on local log.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZHWP4KZ6x5TcXDBFC
2026-11-17 21:41:07 +01:00

50 lines
1.5 KiB
Dart

/// A controllable [ClaudeRelayClient] for tests and the unwired app.
library;
import 'dart:async';
import 'package:diet_guard_app/services/claude_relay_client.dart';
/// Records every request and lets the test emit the events by hand.
///
/// [emit] pushes to the most recent send's stream; a terminal event closes it,
/// as a real client's would.
class FakeClaudeRelayClient implements ClaudeRelayClient {
/// Every request sent, oldest first.
final List<ClaudeRelayRequest> sent = [];
/// Events replayed on each send before the stream waits for [emit].
final List<ClaudeRelayEvent> script = [];
/// Request ids passed to [cancel], oldest first.
final List<String> cancelled = [];
/// What [cancel] answers: true = withdrawn before any claim.
bool cancelResult = true;
StreamController<ClaudeRelayEvent>? _open;
@override
Stream<ClaudeRelayEvent> send(ClaudeRelayRequest request) {
sent.add(request);
final controller = StreamController<ClaudeRelayEvent>();
_open = controller;
script.forEach(emit);
return controller.stream;
}
/// Delivers [event] to the open stream; a terminal one closes it.
void emit(ClaudeRelayEvent event) {
final controller = _open;
if (controller == null || controller.isClosed) return;
controller.add(event);
if (event is! ClaudeRelayQueued) unawaited(controller.close());
}
@override
Future<bool> cancel(String requestId) async {
cancelled.add(requestId);
return cancelResult;
}
}