2026-07-23 15:27:34 +00:00
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using System.Text.Json;
|
2026-10-25 20:33:40 +01:00
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using Heimdall.Infrastructure.Data;
|
2026-10-25 20:44:31 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-10-25 20:33:40 +01:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2026-07-23 15:27:34 +00:00
|
|
|
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<JsonElement>();
|
|
|
|
|
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<JsonElement>();
|
|
|
|
|
content.TryGetProperty("timestamp", out _).ShouldBeTrue();
|
|
|
|
|
}
|
2026-10-25 20:33:40 +01:00
|
|
|
|
|
|
|
|
[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<JsonElement>();
|
|
|
|
|
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<JsonElement>();
|
|
|
|
|
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<AppDbContext>();
|
2026-10-25 20:44:31 +01:00
|
|
|
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);
|
|
|
|
|
}
|
2026-10-25 20:33:40 +01:00
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
2026-07-23 15:27:34 +00:00
|
|
|
}
|