using System.Net; using System.Net.Http.Json; using MeetingAssistant.Recording; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; namespace MeetingAssistant.Tests; public sealed class HealthEndpointTests : IClassFixture> { private readonly WebApplicationFactory factory; public HealthEndpointTests(WebApplicationFactory factory) { this.factory = factory.WithWebHostBuilder(builder => { builder.ConfigureAppConfiguration((_, configuration) => { configuration.AddInMemoryCollection(new Dictionary { ["MeetingAssistant:FunAsr:Backend:Enabled"] = "false" }); }); }); } [Fact] public async Task HealthEndpointReportsServiceStatus() { using var client = factory.CreateClient(); using var response = await client.GetAsync("/health"); var body = await response.Content.ReadFromJsonAsync(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("meeting-assistant", body?.Service); Assert.Equal("ok", body?.Status); } [Fact] public async Task ApplicationStartupDeletesStaleTemporaryRecordings() { await using var cleanupFactory = factory.WithWebHostBuilder(builder => { builder.ConfigureTestServices(services => { services.RemoveAll(); services.AddSingleton(); }); }); using var client = cleanupFactory.CreateClient(); using var response = await client.GetAsync("/health"); var store = cleanupFactory.Services.GetRequiredService() as StartupCleanupRecordedAudioStore; Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.True(store?.StaleRecordingsDeleted); } private sealed record HealthResponse(string Service, string Status); private sealed class StartupCleanupRecordedAudioStore : IRecordedAudioStore { public bool StaleRecordingsDeleted { get; private set; } public Task CreateSessionAsync(CancellationToken cancellationToken) { throw new NotSupportedException(); } public Task DeleteStaleRecordingsAsync(CancellationToken cancellationToken) { StaleRecordingsDeleted = true; return Task.CompletedTask; } } }