using System.Net.Http.Json; using System.Text.Json; using Heimdall.Domain.Entities; using Heimdall.Infrastructure.Data; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Shouldly; namespace Heimdall.IntegrationTests.Controllers; public class HealthControllerTests : PostgresTestBase { public HealthControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { } [Fact] public async Task Health_ReturnsOk() { var client = Factory.CreateClient(); var response = await client.GetAsync("/healthz"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var content = await response.Content.ReadFromJsonAsync(); content.GetProperty("status").GetString().ShouldBe("healthy"); } [Fact] public async Task Health_ReturnsTimestamp() { var client = Factory.CreateClient(); var response = await client.GetAsync("/healthz"); var content = await response.Content.ReadFromJsonAsync(); content.TryGetProperty("timestamp", out _).ShouldBeTrue(); } [Fact] public async Task Readiness_ReturnsHealthy_WhenPostgresAndActiveStorageAreAvailable() { var storageRoot = Path.Combine(Path.GetTempPath(), "heimdall-readiness", Guid.NewGuid().ToString("N")); await SetSettingsAsync( new AppSetting("storage.provider", "Local"), new AppSetting("storage.local.path", storageRoot)); try { var response = await Factory.CreateClient().GetAsync("/readyz"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var body = await response.Content.ReadFromJsonAsync(); body.GetProperty("status").GetString().ShouldBe("healthy"); } finally { if (Directory.Exists(storageRoot)) Directory.Delete(storageRoot, recursive: true); } } [Fact] public async Task Readiness_ReturnsUnavailableWithoutLeakingStorageConfiguration() { const string privateProviderValue = "unknown-provider-should-not-appear"; await SetSettingsAsync(new AppSetting("storage.provider", privateProviderValue)); var response = await Factory.CreateClient().GetAsync("/readyz"); var body = await response.Content.ReadAsStringAsync(); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.ServiceUnavailable); body.ShouldContain("unhealthy"); body.ShouldContain("unavailable or misconfigured"); body.ShouldNotContain(privateProviderValue); } [Fact] public async Task Live_ReturnsHealthyWhenOptionalAiBackendsAreNotConfigured() { // Empty DB values deliberately override any runner environment fallbacks. await SetSettingsAsync( new AppSetting("ai.cpaiUrl", string.Empty), new AppSetting("ai.ollamaUrl", string.Empty)); var response = await Factory.CreateClient().GetAsync("/health/live"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var body = await response.Content.ReadFromJsonAsync(); body.GetProperty("status").GetString().ShouldBe("healthy"); body.GetProperty("degradationLevel").GetInt32().ShouldBe(0); } private async Task SetSettingsAsync(params AppSetting[] settings) { await using var scope = Factory.Services.CreateAsyncScope(); var db = scope.ServiceProvider.GetRequiredService(); foreach (var setting in settings) { var current = await db.AppSettings.FirstOrDefaultAsync(candidate => candidate.Key == setting.Key); if (current is null) db.AppSettings.Add(setting); else current.UpdateValue(setting.Value); } await db.SaveChangesAsync(); } }