using Heimdall.Agent.Services; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NSubstitute; namespace Heimdall.Agent.Tests; public class ConfigReceiverServiceTests { private static ConfigReceiverService CreateService(AgentState? state = null) { state ??= new AgentState { AgentId = Guid.Empty }; var options = Options.Create(new AgentOptions { CentralUrl = "http://localhost:5000", SiteToken = "test" }); var logger = Substitute.For>(); var httpFactory = Substitute.For(); return new ConfigReceiverService(state, options, logger, httpFactory); } [Fact] public void Constructor_WithDependencies_DoesNotThrow() { var service = CreateService(); service.ShouldNotBeNull(); } [Fact] public async Task StartStop_WhenNotRegistered_StopsGracefully() { var state = new AgentState { AgentId = Guid.Empty }; var service = CreateService(state); await service.StartAsync(CancellationToken.None); var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3)); await service.StopAsync(cts.Token); } [Fact] public async Task StartStop_WhenRegistered_StopsGracefully() { var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "test-key" }; var service = CreateService(state); await service.StartAsync(CancellationToken.None); var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); await service.StopAsync(cts.Token); } [Fact] public async Task StopAsync_WithoutStart_DoesNotThrow() { var service = CreateService(); await service.StopAsync(CancellationToken.None); } }