using System.Net.Http.Json; using Heimdall.Application.DTOs; using Heimdall.IntegrationTests.Factories; using Shouldly; namespace Heimdall.IntegrationTests.Controllers; public sealed class AgentLogsControllerTests(PostgreSqlContainerFixture postgres) : PostgresTestBase(postgres) { [Fact] public async Task SubmitAgentLogs_CentralLevelNames_ArePersistedAndQueryable() { var admin = await Factory.CreateAdminClientAsync(); var siteResponse = await admin.PostAsJsonAsync("api/sites", new CreateSiteRequest("Agent log acceptance", null, "UTC")); siteResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var site = await siteResponse.Content.ReadFromJsonAsync(); site.ShouldNotBeNull(); site.RegistrationToken.ShouldNotBeNullOrWhiteSpace(); var agentClient = Factory.CreateClient(); var registerResponse = await agentClient.PostAsJsonAsync("api/agents/register", new RegisterAgentRequest(site.RegistrationToken!, "log-acceptance", "test", null)); registerResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var agent = await registerResponse.Content.ReadFromJsonAsync(); agent.ShouldNotBeNull(); agent.ApiKey.ShouldNotBeNullOrWhiteSpace(); agentClient.DefaultRequestHeaders.Add("X-API-Key", agent.ApiKey); var timestamp = DateTime.UtcNow; var levels = new[] { "Trace", "Debug", "Information", "Warning", "Error", "Critical", "Info", "Warn" }; var submitResponse = await agentClient.PostAsJsonAsync($"api/agents/{agent.AgentId}/logs", new SubmitAgentLogsRequest(levels.Select(level => new AgentLogEntryRequest( timestamp, level, "Synthetic PR138 log", null, "acceptance")).ToArray())); submitResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var readResponse = await admin.GetAsync($"api/agents/{agent.AgentId}/logs?page=1&pageSize=50"); readResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var logs = await readResponse.Content.ReadFromJsonAsync>(); logs.ShouldNotBeNull(); logs.TotalCount.ShouldBe(6); logs.Items.Select(log => log.Level).Order(StringComparer.Ordinal) .ShouldBe(new[] { "Critical", "Debug", "Error", "Information", "Trace", "Warning" }); } }