using Bunit; using Microsoft.Extensions.DependencyInjection; using MudBlazor; using MudBlazor.Services; namespace Heimdall.Web.Tests.Components; /// /// Base TestContext for MudBlazor component tests. /// Registers MudBlazor services with NSubstitute-mocked IKeyInterceptorService /// (avoids IAsyncDisposable-only conflict with bUnit's sync Dispose). /// public abstract class MudTestBase : TestContext { protected MudTestBase() { var services = (IServiceCollection)Services; // Register all MudBlazor services except KeyInterceptorService services.AddMudServices(config => { config.SnackbarConfiguration.PositionClass = "mud-alert-center-top"; config.SnackbarConfiguration.PreventDuplicates = true; config.SnackbarConfiguration.ShowCloseIcon = true; config.SnackbarConfiguration.VisibleStateDuration = 4000; }); // Replace KeyInterceptorService with a manual mock that implements // both IDisposable and IAsyncDisposable (NSubstitute Castle proxy // only implements IAsyncDisposable, causing disposal failures) services.RemoveService(); services.AddScoped(_ => { var mock = new ManualMockKeyInterceptorService(); return mock; }); // JSInterop setup for MudBlazor internals JSInterop.SetupVoid("mudKeyInterceptor.connect", _ => true); JSInterop.SetupVoid("mudKeyInterceptor.disconnect", _ => true); JSInterop.SetupVoid("mudPopover.connect", _ => true); JSInterop.SetupVoid("mudPopover.disconnect", _ => true); JSInterop.SetupVoid("mudElementReference.captureElement", _ => true); JSInterop.SetupVoid("mudElementRef.addOnBlurEvent", _ => true); JSInterop.SetupVoid("mudElementRef.removeOnBlurEvent", _ => true); JSInterop.SetupVoid("mudTextField.focus", _ => true); } } /// /// Manual mock for IKeyInterceptorService that implements both IDisposable /// and IAsyncDisposable. Required because NSubstitute Castle proxies only /// implement IAsyncDisposable, which breaks bUnit's sync service provider Dispose. /// internal sealed class ManualMockKeyInterceptorService : IKeyInterceptorService, IDisposable, IAsyncDisposable { public void Dispose() { } public ValueTask DisposeAsync() { GC.SuppressFinalize(this); return ValueTask.CompletedTask; } public Task SubscribeAsync(IKeyInterceptorObserver observer, KeyInterceptorOptions options) => Task.CompletedTask; public Task SubscribeAsync(string elementId, KeyInterceptorOptions options, Action builder) => Task.CompletedTask; public Task SubscribeAsync(string elementId, KeyInterceptorOptions options, IKeyDownObserver? keyDown, IKeyUpObserver? keyUp) => Task.CompletedTask; public Task SubscribeAsync(string elementId, KeyInterceptorOptions options, Action? keyDown, Action? keyUp) => Task.CompletedTask; public Task SubscribeAsync(string elementId, KeyInterceptorOptions options, Func? keyDown, Func? keyUp) => Task.CompletedTask; public Task UnsubscribeAsync(string elementId) => Task.CompletedTask; public Task UnsubscribeAsync(IKeyInterceptorObserver observer) => Task.CompletedTask; public Task DispatchAsync(string elementId, KeyEventKind kind, Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args) => Task.CompletedTask; public Task UpdateKeyAsync(IKeyInterceptorObserver observer, KeyOptions options) => Task.CompletedTask; public Task UpdateKeyAsync(string elementId, KeyOptions options) => Task.CompletedTask; } internal static class ServiceCollectionExtensions { public static void RemoveService(this IServiceCollection services) where T : class { var descriptor = services.FirstOrDefault(d => d.ServiceType == typeof(T)); if (descriptor is not null) services.Remove(descriptor); } }