using Grpc.Core; using Grpc.Core.Interceptors; using MariesWonderland.Data; using MariesWonderland.Tests.Infrastructure; namespace MariesWonderland.Tests.Interceptors; /// /// Base class for interceptor tests. Provides shared helpers for invoking /// an interceptor's unary handler directly and constructing empty user stores. /// public abstract class InterceptorTestBase { /// /// Invokes an interceptor's UnaryServerHandler directly, bypassing the gRPC pipeline. /// The continuation simply calls and returns its result. /// protected static Task CallInterceptor( Interceptor interceptor, TRequest request, ServerCallContext context, Func continuationBody) where TRequest : class where TResponse : class { return interceptor.UnaryServerHandler( request, context, (req, ctx) => Task.FromResult(continuationBody())); } /// Creates a with no users registered. protected static UserDataStore CreateEmptyStore() => new(new DarkMasterMemoryDatabase()); /// /// Creates a pre-loaded with the given user database. /// protected static UserDataStore CreateStoreWithUser(long userId, DarkUserMemoryDatabase userDb) { var store = new UserDataStore(new DarkMasterMemoryDatabase()); store.Set(userId, userDb); return store; } /// Shorthand for . protected static FakeServerCallContext ContextFor(long userId) => FakeServerCallContext.For(userId); }