using System.Reflection; // Fixture-only empty reads. Every non-Get call is rejected, including writes, // endpoint verification, certificate trust and notification sending. public class ReadOnlyFixtureProxy : DispatchProxy { private string? _failOnceMethod; private IReadOnlyDictionary>? _reads; public static T WithReads(IReadOnlyDictionary> reads) where T : class { var proxy = Create(); ((ReadOnlyFixtureProxy)(object)proxy)._reads = reads; return proxy; } public static T EmptyReads(string? failOnceMethod = null) where T : class { var proxy = Create(); ((ReadOnlyFixtureProxy)(object)proxy)._failOnceMethod = failOnceMethod; return proxy; } protected override object? Invoke(MethodInfo? method, object?[]? args) { if (method is not null && _reads?.TryGetValue(method.Name, out var read) == true) return read(args ?? []); if (method is null || (!method.Name.StartsWith("Get", StringComparison.Ordinal) && method.Name != "ListAsync")) throw new InvalidOperationException("Local fixture rejects non-read operations."); if (method.Name == _failOnceMethod) { _failOnceMethod = null; throw new InvalidOperationException("Synthetic local read failure; retry is safe."); } var result = method.ReturnType; if (!result.IsGenericType || result.GetGenericTypeDefinition() != typeof(Task<>)) throw new InvalidOperationException("Unsupported fixture read shape."); var valueType = result.GenericTypeArguments[0]; object? value = valueType.IsValueType ? Activator.CreateInstance(valueType) : null; if (valueType.IsGenericType) { var shape = valueType.GetGenericTypeDefinition(); if (shape == typeof(IReadOnlyList<>) || shape == typeof(IList<>) || shape == typeof(IEnumerable<>)) value = Activator.CreateInstance(typeof(List<>).MakeGenericType(valueType.GenericTypeArguments)); else if (shape == typeof(IReadOnlyDictionary<,>) || shape == typeof(IDictionary<,>)) value = Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(valueType.GenericTypeArguments)); } return typeof(Task).GetMethod(nameof(Task.FromResult))!.MakeGenericMethod(valueType).Invoke(null, new[] { value }); } }