Files
Chronos/tests/Titano.Server.Tests/TestServer.cs
T
Claude c40b6a7cd3 feat: Phase 0 shells, design system, server skeleton, Docker and CI
- Titano.Ui: DESIGN.md tokens (dark/light), generated 16-px icon set,
  styles for all standard controls, custom chrome window with snap
  layouts, rail, job bar, Auto/manual slider, chips, toasts.
- Titano desktop shell with sections, Settings (language, theme),
  About and a --capture screenshot mode.
- Titano Server skeleton: env/server.json configuration, self-signed
  TLS with fingerprint, /health, /server/info, i18n endpoint and the
  embedded admin panel; in-process server tests.
- Docker image (multi-arch, PUID/PGID, health check), compose file,
  Unraid template; CI, release and nightly workflows; changelog tool.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 09:35:03 +00:00

62 lines
2.3 KiB
C#

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.DependencyInjection;
using Titano.Server.Configuration;
using Titano.Server.Hosting;
using Titano.Testing;
namespace Titano.Server.Tests;
/// <summary>In-process Kestrel on ephemeral ports with temporary archive/data folders (BRIEF §20.1).</summary>
public sealed class TestServer : IAsyncDisposable
{
private readonly WebApplication _app;
private TestServer(WebApplication app, ServerOptions options, Uri http, Uri? https)
{
_app = app;
Options = options;
HttpBase = http;
HttpsBase = https;
Client = new HttpClient { BaseAddress = http, Timeout = TimeSpan.FromSeconds(30) };
}
public ServerOptions Options { get; }
public Uri HttpBase { get; }
public Uri? HttpsBase { get; }
public HttpClient Client { get; }
public IServiceProvider Services => _app.Services;
public static async Task<TestServer> StartAsync(TestContext context, Action<ServerOptions>? configure = null)
{
var root = context.CreateTempDirectory("server");
var options = new ServerOptions
{
DataPath = Path.Combine(root, "data"),
ArchivePath = Path.Combine(root, "archive"),
HttpPort = 0,
HttpsPort = 0,
TlsMode = TlsMode.Auto,
Database = DatabaseKind.Sqlite,
};
Directory.CreateDirectory(options.ArchivePath);
configure?.Invoke(options);
var app = ServerHost.Build(options, []);
await app.StartAsync(context.Cancellation);
var addresses = app.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>()!.Addresses
.Select(a => new Uri(a.Replace("[::]", "127.0.0.1", StringComparison.Ordinal).Replace("0.0.0.0", "127.0.0.1", StringComparison.Ordinal)))
.ToList();
var http = addresses.First(a => a.Scheme == "http");
var https = addresses.FirstOrDefault(a => a.Scheme == "https");
return new TestServer(app, options, http, https);
}
public async ValueTask DisposeAsync()
{
Client.Dispose();
await _app.StopAsync();
await _app.DisposeAsync();
}
}