107 lines
4.0 KiB
C#
107 lines
4.0 KiB
C#
using System.Net.Sockets;
|
|
using Heimdall.TestCommon;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Playwright;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.E2E.Tests;
|
|
|
|
/// <summary>
|
|
/// Base class for Playwright E2E tests against the Heimdall Blazor Server app.
|
|
/// Starts a real Kestrel HTTP server on a free port so Playwright's Chromium
|
|
/// can connect via HTTP and WebSocket (SignalR).
|
|
/// </summary>
|
|
[Collection("E2E")]
|
|
public abstract class E2ETestBase : IAsyncLifetime
|
|
{
|
|
private readonly string _connectionString;
|
|
private WebApplication? _app;
|
|
private int _port;
|
|
private IPage? _page;
|
|
private IBrowser? _browser;
|
|
private IBrowserContext? _context;
|
|
|
|
protected E2ETestBase(PostgreSqlContainerFixture postgres)
|
|
{
|
|
_connectionString = postgres.ConnectionString;
|
|
}
|
|
|
|
protected IPage Page => _page ?? throw new InvalidOperationException("Playwright not initialized");
|
|
protected string BaseUrl => $"http://127.0.0.1:{_port}";
|
|
|
|
public virtual async Task InitializeAsync()
|
|
{
|
|
// Find a free TCP port
|
|
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
socket.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 0));
|
|
_port = ((System.Net.IPEndPoint)socket.LocalEndPoint!).Port;
|
|
|
|
// Build and start a real Kestrel WebApplication for E2E tests
|
|
_app = Program.CreateTestApplication(configure: builder =>
|
|
{
|
|
builder.Environment.EnvironmentName = "Testing";
|
|
// Must add in-memory config BEFORE ConfigureServices reads it
|
|
builder.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
|
|
{
|
|
["ConnectionStrings:Default"] = _connectionString,
|
|
["Database:MigrateOnStartup"] = "true",
|
|
["Jwt:Key"] = "ThisIsATestKeyThatIsAtLeast32BytesLong!",
|
|
["Jwt:RefreshKey"] = "ThisIsATestRefreshKeyThatIsAlso32Bytes!",
|
|
["Jwt:Issuer"] = "heimdall-test",
|
|
["Jwt:Audience"] = "heimdall-test",
|
|
["Cors:AllowedOrigins"] = "*",
|
|
});
|
|
builder.WebHost.UseUrls($"http://127.0.0.1:{_port}");
|
|
builder.WebHost.UseKestrel();
|
|
});
|
|
|
|
await _app.StartAsync();
|
|
|
|
// Give the server a moment to be ready
|
|
await Task.Delay(1500);
|
|
|
|
await StartPlaywrightAsync();
|
|
}
|
|
|
|
private async Task StartPlaywrightAsync()
|
|
{
|
|
// Use system Node.js if bundled one is incompatible
|
|
if (File.Exists("/usr/bin/node"))
|
|
Environment.SetEnvironmentVariable("PLAYWRIGHT_NODEJS_PATH", "/usr/bin/node");
|
|
|
|
var playwright = await Microsoft.Playwright.Playwright.CreateAsync();
|
|
|
|
// Use system Chromium (Alpine — bundled one needs glibc)
|
|
var chromePaths = new[]
|
|
{
|
|
"/usr/bin/chromium",
|
|
"/usr/bin/chromium-browser",
|
|
"/root/.cache/ms-playwright/chromium-1228/chrome-linux64/chrome",
|
|
};
|
|
var chromePath = chromePaths.FirstOrDefault(File.Exists)
|
|
?? throw new FileNotFoundException("Chromium not found. Install with: apk add chromium");
|
|
|
|
_browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
|
{
|
|
Headless = true,
|
|
ExecutablePath = chromePath,
|
|
Args = ["--no-sandbox", "--disable-setuid-sandbox", "--disable-gpu", "--disable-dev-shm-usage"]
|
|
});
|
|
_context = await _browser.NewContextAsync(new BrowserNewContextOptions { IgnoreHTTPSErrors = true });
|
|
_page = await _context.NewPageAsync();
|
|
}
|
|
|
|
public async Task DisposeAsync()
|
|
{
|
|
if (_page is not null) await _page.CloseAsync();
|
|
if (_context is not null) await _context.CloseAsync();
|
|
if (_browser is not null) await _browser.CloseAsync();
|
|
if (_app is not null) await _app.StopAsync();
|
|
}
|
|
}
|
|
|
|
[CollectionDefinition("E2E")]
|
|
public class E2ETestCollectionFixture : ICollectionFixture<PostgreSqlContainerFixture>;
|