using FluentAssertions; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Abstractions; using NSubstitute; using SlpModularCms.Core.Hosting; using SlpModularCms.Core.Modules; namespace SlpModularCms.Core.Tests.Hosting; public class ModuleOrchestratorTests { private readonly ModuleOrchestrator _orchestrator = new(NullLogger.Instance); [Fact] public void RegisterModuleServices_CallsRegisterServicesOnDiscoveredModules() { var module = Substitute.For(); var services = new ServiceCollection(); InjectModules(_orchestrator, [module]); _orchestrator.RegisterModuleServices(services); module.Received(1).RegisterServices(services); } [Fact] public void UseModules_CallsUseModuleOnDiscoveredModules() { var module = Substitute.For(); var app = Substitute.For(); app.ApplicationServices.Returns(new ServiceCollection().BuildServiceProvider()); InjectModules(_orchestrator, [module]); _orchestrator.UseModules(app); module.Received(1).UseModule(app); } [Fact] public void UseModules_Rethrows_AModuleStartupException_SoTheApplicationDoesNotStart() { var module = Substitute.For(); module.When(m => m.UseModule(Arg.Any())).Do(_ => throw new ModuleStartupException("guard refuses")); var app = Substitute.For(); InjectModules(_orchestrator, [module]); var act = () => _orchestrator.UseModules(app); act.Should().Throw().WithMessage("guard refuses"); } [Fact] public void UseModules_KeepsGoing_WhenAModuleFailsWithAnOrdinaryException() { var failing = Substitute.For(); failing.When(m => m.UseModule(Arg.Any())).Do(_ => throw new InvalidOperationException("boom")); var other = Substitute.For(); var app = Substitute.For(); InjectModules(_orchestrator, [failing, other]); _orchestrator.UseModules(app); other.Received(1).UseModule(app); } [Fact] public void RegisterModuleServices_DoesNotThrow_WhenModuleListIsEmpty() { var services = new ServiceCollection(); var act = () => _orchestrator.RegisterModuleServices(services); act.Should().NotThrow(); } [Fact] public void UseModules_DoesNotThrow_WhenModuleListIsEmpty() { var app = Substitute.For(); app.ApplicationServices.Returns(new ServiceCollection().BuildServiceProvider()); var act = () => _orchestrator.UseModules(app); act.Should().NotThrow(); } [Fact] public void DiscoverModules_RunsWithoutThrowing_InTestEnvironment() { var act = () => _orchestrator.DiscoverModules(); act.Should().NotThrow(); } [Fact] public void RegisterModuleServices_ContinuesOnModuleThatThrows() { var badModule = Substitute.For(); badModule.When(m => m.RegisterServices(Arg.Any())) .Do(_ => throw new InvalidOperationException("boom")); var goodModule = Substitute.For(); var services = new ServiceCollection(); InjectModules(_orchestrator, [badModule, goodModule]); var act = () => _orchestrator.RegisterModuleServices(services); act.Should().NotThrow(); goodModule.Received(1).RegisterServices(services); } // Inject modules via reflection — ModuleOrchestrator stores them in a private List private static void InjectModules(ModuleOrchestrator orchestrator, IModule[] modules) { var field = typeof(ModuleOrchestrator) .GetField("_modules", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!; var list = (List)field.GetValue(orchestrator)!; list.AddRange(modules); } }