using Microsoft.Extensions.Logging;
namespace HSchool.AppHost.Tests;
///
/// Boots the AppHost once for the whole suite — starting it per test costs about ten
/// seconds each. The client resource is skipped (HSchool:Headless), so the tests
/// need no Node install.
///
public sealed class AppHostFixture : IAsyncLifetime
{
private static readonly TimeSpan StartupTimeout = TimeSpan.FromSeconds(120);
private DistributedApplication? _app;
public DistributedApplication App =>
_app ?? throw new InvalidOperationException("The AppHost has not been started.");
public async ValueTask InitializeAsync()
{
var appHost = await DistributedApplicationTestingBuilder
.CreateAsync(["--HSchool:Headless=true"], CancellationToken.None);
appHost.Services.AddLogging(logging =>
{
logging.SetMinimumLevel(LogLevel.Warning);
logging.AddFilter("Aspire.", LogLevel.Warning);
});
_app = await appHost.BuildAsync().WaitAsync(StartupTimeout);
await _app.StartAsync().WaitAsync(StartupTimeout);
await _app.ResourceNotifications
.WaitForResourceHealthyAsync("server", CancellationToken.None)
.WaitAsync(StartupTimeout);
}
public async ValueTask DisposeAsync()
{
if (_app is not null)
{
await _app.DisposeAsync();
}
}
}
/// Groups every integration test around the single AppHost instance.
[CollectionDefinition(Name)]
public sealed class AppHostCollection : ICollectionFixture
{
public const string Name = "apphost";
}