using System.Net; using System.Reflection; using Heimdall.Agent.Services; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NSubstitute; using Shouldly; namespace Heimdall.Agent.Tests; public sealed class AgentUpdateServiceTests { [Fact] public async Task FetchLatestRelease_UsesAgentApiKeyManifestEndpoint() { HttpRequestMessage? sent = null; var handler = new MockHttpMessageHandler(request => { sent = request; return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"version\":\"1.2.3\",\"assets\":{}}") }; }); var client = new HttpClient(handler) { BaseAddress = new Uri("https://central.example/") }; var factory = Substitute.For(); factory.CreateClient("Central").Returns(client); var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "agent-key" }; var service = new AgentUpdateService(state, Options.Create(new AgentOptions()), factory, Substitute.For>(), new ContainerInfoProvider()); var fetch = typeof(AgentUpdateService).GetMethod("FetchLatestReleaseAsync", BindingFlags.Instance | BindingFlags.NonPublic)!; var task = (Task)fetch.Invoke(service, [CancellationToken.None])!; await task; sent.ShouldNotBeNull(); sent.RequestUri!.AbsolutePath.ShouldBe("/api/updates/agent/latest"); sent.Headers.GetValues("X-API-Key").Single().ShouldBe("agent-key"); task.GetType().GetProperty("Result")!.GetValue(task).ShouldNotBeNull(); } }