Files
meeting-assistant/MeetingAssistant.Tests/HealthEndpointTests.cs
2026-05-20 02:06:16 +02:00

82 lines
2.7 KiB
C#

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<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> factory;
public HealthEndpointTests(WebApplicationFactory<Program> factory)
{
this.factory = factory.WithWebHostBuilder(builder =>
{
builder.ConfigureAppConfiguration((_, configuration) =>
{
configuration.AddInMemoryCollection(new Dictionary<string, string?>
{
["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<HealthResponse>();
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<IRecordedAudioStore>();
services.AddSingleton<IRecordedAudioStore, StartupCleanupRecordedAudioStore>();
});
});
using var client = cleanupFactory.CreateClient();
using var response = await client.GetAsync("/health");
var store = cleanupFactory.Services.GetRequiredService<IRecordedAudioStore>() 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<IRecordedAudioSink> CreateSessionAsync(CancellationToken cancellationToken)
{
throw new NotSupportedException();
}
public Task DeleteStaleRecordingsAsync(CancellationToken cancellationToken)
{
StaleRecordingsDeleted = true;
return Task.CompletedTask;
}
}
}