import 'dart:async'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/core/transport/connection_state.dart'; import 'package:pipelines_app/features/attention/domain/attention_acks.dart'; /// An in-memory stand-in for the server's `attention_acks` / `ack_attention` /// protocol with any number of connected clients. Like the Go handler, an ack /// that changes the set is pushed to every *other* connected client as an /// `attention_acks_changed` delta. class FakeAckServer { AttentionAcks state = const AttentionAcks(); final clients = []; /// When set, every ack_attention is refused with this reason. String? refuse; /// When true the server predates shared acks and answers `pong`. bool legacy = false; /// The chat list this server reports, for chat-list controllers. List chatSessions = const []; FakeAckClient connect() { final client = FakeAckClient(this); clients.add(client); return client; } JsonMap _reply() => { 'type': 'attention_acks', 'attention_acks': state.toJson(), }; JsonMap _ack(FakeAckClient from, JsonMap fields) { final incoming = AttentionAcks.fromJson(fields['attention_acks']); final delta = AttentionAcks( dismissed: incoming.dismissed.difference(state.dismissed), chatSeen: { for (final entry in incoming.chatSeen.entries) if (entry.value > (state.chatSeen[entry.key] ?? 0)) entry.key: entry.value, }, ); state = state.merge(incoming); if (!delta.isEmpty) { for (final client in clients) { if (identical(client, from) || !client.online) continue; client.pushed.add({ 'type': 'attention_acks_changed', 'attention_acks': delta.toJson(), }); } } return _reply(); } } class FakeAckClient implements ApiGateway { FakeAckClient(this.server); final FakeAckServer server; final pushed = StreamController.broadcast(); final calls = []; bool online = true; @override Stream get frames => pushed.stream; @override Future call(String type, [JsonMap fields = const {}]) async { calls.add(type); if (!online) throw const NotConnectedError(); switch (type) { case 'attention_acks': case 'ack_attention': if (server.legacy) return {'type': 'pong'}; if (type == 'attention_acks') return server._reply(); final reason = server.refuse; if (reason != null) throw ProtocolError(reason); return server._ack(this, fields); case 'chat_sessions': return {'type': 'chat_sessions', 'chat_sessions': server.chatSessions}; } return {'type': 'pong'}; } int count(String type) => calls.where((call) => call == type).length; var _sequence = 0; /// The socket's send-only path: the reply (or error) arrives later as a /// frame carrying the request id, like the real transport. String sendOnly(String type, JsonMap fields) { if (!online) throw const NotConnectedError(); final id = 'send-${++_sequence}'; scheduleMicrotask(() async { try { pushed.add({...await call(type, fields), 'id': id}); } on ProtocolError catch (error) { pushed.add({'type': 'error', 'id': id, 'error': error.message}); } }); return id; } } /// Lets queued microtasks and stream deliveries settle. Future settle() async { for (var i = 0; i < 10; i++) { await Future.delayed(Duration.zero); } }