using AvParser.Core.Proxies; namespace AvParser.UI.Tests.Fakes; /// A feed whose contents the test supplies. internal sealed class FakeProxySource(IEnumerable? endpoints = null) : IProxySource { public string Id => "fake-feed"; public string DisplayName => "Fake feed"; public ProxySourceKind Kind => ProxySourceKind.Feed; public List Endpoints { get; } = [.. endpoints ?? []]; public Task> GetProxiesAsync(CancellationToken cancellationToken = default) => Task.FromResult>(Endpoints.ToArray()); } /// An in-memory stand-in for the user's editable list. internal sealed class FakeMutableProxySource : IMutableProxySource { public string Id => "fake-custom"; public string DisplayName => "Fake custom list"; public ProxySourceKind Kind => ProxySourceKind.Custom; public List Endpoints { get; } = []; public Task> GetProxiesAsync(CancellationToken cancellationToken = default) => Task.FromResult>(Endpoints.ToArray()); public Task AddAsync(IEnumerable endpoints, CancellationToken cancellationToken = default) { var added = 0; foreach (var endpoint in endpoints) { if (Endpoints.Any(existing => string.Equals(existing.Key, endpoint.Key, StringComparison.Ordinal))) { continue; } Endpoints.Add(endpoint); added++; } return Task.FromResult(added); } public Task RemoveAsync(ProxyEndpoint endpoint, CancellationToken cancellationToken = default) => Task.FromResult( Endpoints.RemoveAll(existing => string.Equals(existing.Key, endpoint.Key, StringComparison.Ordinal)) > 0 ); public Task ClearAsync(CancellationToken cancellationToken = default) { Endpoints.Clear(); return Task.CompletedTask; } } /// A probe whose verdict the test decides. internal sealed class FakeProxyProbe : IProxyProbe { private readonly Dictionary _verdicts = new(StringComparer.Ordinal); public FakeProxyProbe Set(ProxyEndpoint endpoint, bool alive) { _verdicts[endpoint.Key] = alive; return this; } public Task ProbeAsync( ProxyEndpoint endpoint, ProxyOptions options, CancellationToken cancellationToken = default ) => Task.FromResult( _verdicts.TryGetValue(endpoint.Key, out var alive) && alive ? ProxyProbeResult.Success(TimeSpan.FromMilliseconds(20)) : ProxyProbeResult.Failure("dead") ); }