using Grpc.Core; using MxGateway.Contracts.Proto.Galaxy; namespace MxGateway.Client.Tests; internal sealed class FakeGalaxyRepositoryTransport(MxGatewayClientOptions options) : IGalaxyRepositoryClientTransport { public MxGatewayClientOptions Options { get; } = options; public GalaxyRepository.GalaxyRepositoryClient? RawClient => null; public List<(TestConnectionRequest Request, CallOptions CallOptions)> TestConnectionCalls { get; } = []; public List<(GetLastDeployTimeRequest Request, CallOptions CallOptions)> GetLastDeployTimeCalls { get; } = []; public List<(DiscoverHierarchyRequest Request, CallOptions CallOptions)> DiscoverHierarchyCalls { get; } = []; public TestConnectionReply TestConnectionReply { get; set; } = new() { Ok = true }; public GetLastDeployTimeReply GetLastDeployTimeReply { get; set; } = new() { Present = false }; public DiscoverHierarchyReply DiscoverHierarchyReply { get; set; } = new(); public Queue DiscoverHierarchyReplies { get; } = new(); public Queue TestConnectionExceptions { get; } = new(); public Queue GetLastDeployTimeExceptions { get; } = new(); public Queue DiscoverHierarchyExceptions { get; } = new(); public Task TestConnectionAsync( TestConnectionRequest request, CallOptions callOptions) { TestConnectionCalls.Add((request, callOptions)); if (TestConnectionExceptions.TryDequeue(out Exception? exception)) { throw exception; } return Task.FromResult(TestConnectionReply); } public Task GetLastDeployTimeAsync( GetLastDeployTimeRequest request, CallOptions callOptions) { GetLastDeployTimeCalls.Add((request, callOptions)); if (GetLastDeployTimeExceptions.TryDequeue(out Exception? exception)) { throw exception; } return Task.FromResult(GetLastDeployTimeReply); } public Task DiscoverHierarchyAsync( DiscoverHierarchyRequest request, CallOptions callOptions) { DiscoverHierarchyCalls.Add((request, callOptions)); if (DiscoverHierarchyExceptions.TryDequeue(out Exception? exception)) { throw exception; } return Task.FromResult( DiscoverHierarchyReplies.TryDequeue(out DiscoverHierarchyReply? reply) ? reply : DiscoverHierarchyReply); } public List<(WatchDeployEventsRequest Request, CallOptions CallOptions)> WatchDeployEventsCalls { get; } = []; public List WatchDeployEvents { get; } = []; public Exception? WatchDeployEventsException { get; set; } /// /// When set, awaited before each event yield so tests can observe cancellation /// mid-stream. Receives the call's cancellation token. /// public Func? WatchDeployEventsBeforeYield { get; set; } public async IAsyncEnumerable WatchDeployEventsAsync( WatchDeployEventsRequest request, CallOptions callOptions) { WatchDeployEventsCalls.Add((request, callOptions)); if (WatchDeployEventsException is not null) { throw WatchDeployEventsException; } foreach (DeployEvent deployEvent in WatchDeployEvents) { if (WatchDeployEventsBeforeYield is not null) { await WatchDeployEventsBeforeYield(callOptions.CancellationToken).ConfigureAwait(false); } callOptions.CancellationToken.ThrowIfCancellationRequested(); yield return deployEvent; } } }