using System.Net; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Security.Cryptography.X509Certificates; using Microsoft.Extensions.DependencyInjection; using Chronos.Core.Diagnostics; using Chronos.Core.Imaging.Decoders; using Chronos.Core.Model; using Chronos.Core.Video.Managed; using Chronos.Data; using Chronos.Data.Archiving; using Chronos.Data.Jobs; using Chronos.Data.Processing; using Chronos.Data.Scheduling; using Chronos.Protocol; using Chronos.Server.Api; using Chronos.Server.Configuration; using Chronos.Server.Hosting; using Chronos.Server.Jobs; using Chronos.Server.Security; using Chronos.Testing; namespace Chronos.Server.Tests.Hosting; /// /// The server Chronos Desktop hosts to share this PC's library (owner review 5, #67): built over an index, engines, a job /// engine and a scheduler the test owns, as the desktop does, which it never creates, starts, stops or disposes. /// [Category("server")] [Serial] // Log.Sink is process-wide: the standalone server tests running beside these set their own public sealed class HostedServerTests(TestContext context) { [Test] public async Task ItAnswersWithTheCertificateOfItsDataFolderAndKeepsItsIdAcrossRestarts() { await using var desktop = await DesktopFixture.StartAsync(context); Guid? firstId; string? fingerprint; await using (var shared = await desktop.ShareAsync()) { using var http = desktop.Client(shared); using var health = await http.GetAsync("/health"); Assert.Equal(HttpStatusCode.OK, health.StatusCode); var info = Assert.NotNull(await http.GetFromJsonAsync("/api/v1/server/info", ProtocolJson.Default.ServerInfo)); var pfx = X509CertificateLoader.LoadPkcs12FromFile(Path.Combine(desktop.ShareData, "tls", "server.pfx"), null); Assert.Equal(CertificateStore.Fingerprint(pfx), shared.Fingerprint); Assert.Equal(shared.ServerId, info.ServerId); Assert.True(shared.HttpsPort > 0, "the HTTPS listener's port"); firstId = shared.ServerId; fingerprint = shared.Fingerprint; } // A second start on the same folder and index: the same certificate, id and administrator. await using var again = await desktop.ShareAsync(); Assert.Equal(fingerprint, again.Fingerprint); Assert.Equal(firstId, again.ServerId); Assert.Count(1, (await desktop.Database.Users.ListAsync()).Where(u => u.Role == UserRole.Admin)); } [Test] public async Task JobsQueuedByClientsRunOnTheDesktopsEngineWhichOutlivesTheServer() { await using var desktop = await DesktopFixture.StartAsync(context); var sink = Log.Sink; await using (var shared = await desktop.ShareAsync()) { Assert.Same(desktop.Engine, shared.Services.GetRequiredService().Engine); Assert.True(shared.Services.GetRequiredService().Hosted, "hosted"); using var admin = await desktop.AdminAsync(shared); using var created = await admin.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("scan", [], null), ProtocolJson.Default.CreateJobRequest); Assert.Equal(HttpStatusCode.Accepted, created.StatusCode); var job = Assert.NotNull(await created.Content.ReadFromJsonAsync(ProtocolJson.Default.JobDto)); var done = await desktop.WaitAsync(job.Id); Assert.Equal(JobState.Done, done.State); Assert.Equal(DesktopFixture.WorkerId, done.NodeId); Assert.Same(sink, Log.Sink); } // Stopped: the index answers, the engine runs jobs, the scheduler still works, the log sink is the desktop's. Assert.Same(sink, Log.Sink); var after = await desktop.Engine.EnqueueAsync(JobKind.Scan, [desktop.Archive.Id], "after"); Assert.Equal(JobState.Done, (await desktop.WaitAsync(after.Id)).State); Assert.NotNull(await desktop.Scheduler.ListAsync(context.Cancellation)); } [Test] public async Task TheServerAddsOnlyItsOwnSystemSchedulesToTheDesktops() { await using var desktop = await DesktopFixture.StartAsync(context); var before = (await desktop.Scheduler.ListAsync(context.Cancellation)).Items.Select(s => s.SystemKey).OfType().ToHashSet(); await using var shared = await desktop.ShareAsync(); var after = (await desktop.Scheduler.ListAsync(context.Cancellation)).Items.Select(s => s.SystemKey).OfType().ToHashSet(); Assert.SequenceEqual([ServerScheduleHost.AuditCleanup, ServerScheduleHost.UploadCleanup], after.Except(before).Order(StringComparer.Ordinal)); Assert.SequenceEqual([DesktopFixture.ScanKey], before); } [Test] public async Task ItServesTheDesktopsArchiveAloneAndHidesItsPrivateJobs() { await using var desktop = await DesktopFixture.StartAsync(context); var mine = await desktop.SequenceAsync(desktop.Archive); var other = await desktop.Archives.CreateAsync(context.CreateTempDirectory("other"), "Another archive", cancellationToken: context.Cancellation); var theirs = await desktop.SequenceAsync(other); var workspace = await desktop.Engine.EnqueueAsync(JobKind.WorkspaceCopy, [mine.Id], "workspace"); await using var shared = await desktop.ShareAsync(); using var admin = await desktop.AdminAsync(shared); using (var ok = await admin.GetAsync($"/api/v1/sequences/{mine.Id}")) Assert.Equal(HttpStatusCode.OK, ok.StatusCode); foreach (var path in new[] { $"/api/v1/sequences/{theirs.Id}", $"/api/v1/sequences/{theirs.Id}/projects", $"/api/v1/sequences/{theirs.Id}/frames" }) { using var hidden = await admin.GetAsync(path); Assert.Equal(HttpStatusCode.NotFound, hidden.StatusCode, path); } using (var job = await admin.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("thumbs", [theirs.Id], null), ProtocolJson.Default.CreateJobRequest)) Assert.Equal(HttpStatusCode.NotFound, job.StatusCode); var listed = Assert.NotNull(await admin.GetFromJsonAsync("/api/v1/jobs", ProtocolJson.Default.PageJobDto)); Assert.False(listed.Items.Any(j => j.Id == workspace.Id), "a workspace job is the desktop's own"); using (var get = await admin.GetAsync($"/api/v1/jobs/{workspace.Id}")) Assert.Equal(HttpStatusCode.NotFound, get.StatusCode); using (var cancel = await admin.PostAsync($"/api/v1/jobs/{workspace.Id}/cancel", null)) Assert.Equal(HttpStatusCode.NotFound, cancel.StatusCode); Assert.Equal(JobState.Queued, (await desktop.Database.Jobs.GetAsync(workspace.Id))!.State); } [Test] public async Task TheDesktopOwnsItsSettingsAndTheFirstAdministratorsPasswordIsInTheShareFolder() { await using var desktop = await DesktopFixture.StartAsync(context); Environment.SetEnvironmentVariable("CHRONOS_SERVER_NAME", "from a variable"); try { var options = ServerOptions.ForHosted(desktop.ShareData, desktop.Settings with { ServerName = "Studio PC" }); Assert.Equal("Studio PC", options.ServerName); Assert.True(options.Hosted, "hosted"); Assert.Contains(nameof(ServerOptions.HttpsPort), options.Locked); Assert.Equal(ServerOptions.DesktopLock, options.LockedBy[nameof(ServerOptions.Parallelism)]); Assert.False(options.Locked.Contains(nameof(ServerOptions.TokenLifetimeDays)), "the admin panel owns the token lifetime"); Assert.Equal(Microsoft.Extensions.Logging.LogLevel.None, options.ConsoleLogLevel); } finally { Environment.SetEnvironmentVariable("CHRONOS_SERVER_NAME", null); } await using var shared = await desktop.ShareAsync(); Assert.True(File.Exists(shared.AdminPasswordFile), "the temporary password's file"); Assert.Equal(Path.Combine(desktop.ShareData, AdminBootstrap.PasswordFileName), shared.AdminPasswordFile); Assert.NotNull(shared.AdminPassword()); using var admin = await desktop.AdminAsync(shared); Assert.Null(shared.AdminPassword()); var settings = Assert.NotNull(await admin.GetFromJsonAsync("/api/v1/server/settings", ProtocolJson.Default.ServerSettingsDto)); Assert.Equal(ServerOptions.DesktopLock, settings.LockedBy["HttpsPort"]); } [Test] public async Task ClientsAreKeptForFiveMinutes() { var time = new ManualTime(DateTimeOffset.Parse("2026-11-15T10:00:00Z", System.Globalization.CultureInfo.InvariantCulture)); var activity = new ClientActivity(time); var user = new User { Id = Guid.CreateVersion7(), Username = "alberto", PasswordHash = "x", Role = UserRole.Editor, Created = time.GetUtcNow() }; activity.Record(new Caller(user, "hash", CredentialSource.Bearer, "Chronos Uploader (Studio)"), "192.168.1.20"); activity.Record(new Caller(user, "hash", CredentialSource.Cookie, null), "192.168.1.21"); var seen = activity.Recent(); Assert.Count(2, seen); Assert.Contains("Chronos Uploader (Studio)", seen.Select(s => s.Client)); Assert.Contains(ClientActivity.AdminPanel, seen.Select(s => s.Client)); time.Advance(TimeSpan.FromMinutes(6)); Assert.Count(0, activity.Recent()); } [Test] public void ARenderNeverKeepsTheExportFolderAClientChose() { var parameters = new RenderJobParameters("1080p", """{"preset":"1080p","Folder":"C:\\Users\\someone","nameTemplate":"{title}"}""", "h264", 1920, 1080, false, null); var stripped = JobEndpoints.WithoutExportFolder(parameters); Assert.False(stripped.PresetJson.Contains("Users", StringComparison.Ordinal), stripped.PresetJson); Assert.True(stripped.PresetJson.Contains("nameTemplate", StringComparison.Ordinal), stripped.PresetJson); var plain = parameters with { PresetJson = """{"preset":"1080p"}""" }; Assert.Same(plain, JobEndpoints.WithoutExportFolder(plain)); } [Test] public void TheServiceSwitchesAreGone() { var parsed = ServerCommandLine.Parse(["--service", "--install-service", "--uninstall-service"]); Assert.Equal(ServerCommand.Run, parsed.Command); Assert.SequenceEqual(["--service", "--install-service", "--uninstall-service"], parsed.HostArguments); Assert.False(ServerCommandLine.Usage.Contains("service", StringComparison.OrdinalIgnoreCase), "usage names a service"); } /// A clock the test moves. private sealed class ManualTime(DateTimeOffset start) : TimeProvider { private DateTimeOffset _now = start; public override DateTimeOffset GetUtcNow() => _now; public void Advance(TimeSpan by) => _now += by; } } /// /// What Chronos Desktop has when it shares its library: a SQLite index, the archive and processing services (managed /// codecs), a started job engine and a started scheduler with one system schedule of its own. /// internal sealed class DesktopFixture : IAsyncDisposable { public const string WorkerId = "STUDIO-PC"; public const string ScanKey = "archive-scan"; private readonly TestContext _context; private DesktopFixture(TestContext context, string root, ChronosDatabase db, ArchiveService archives, ProcessingService processing, JobEngine engine, Scheduler scheduler, Archive archive) { _context = context; Root = root; Database = db; Archives = archives; Processing = processing; Engine = engine; Scheduler = scheduler; Archive = archive; } public string Root { get; } public ChronosDatabase Database { get; } public ArchiveService Archives { get; } public ProcessingService Processing { get; } public JobEngine Engine { get; } public Scheduler Scheduler { get; } public Archive Archive { get; } public string ShareData => Path.Combine(Root, "data", "server"); public HostedSettings Settings => new(null, 0, 0, null, null, Discovery: false, Archive.RootPath, null, 2, false, new Dictionary(), "en", LogLevel.Warning, 14, 50, 1024, LogRotation.Daily, 4); public static async Task StartAsync(TestContext context) { var root = context.CreateTempDirectory("desktop"); Directory.CreateDirectory(Path.Combine(root, "data")); var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(root, "data", "chronos.db")), context.Cancellation); var archiveOptions = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Parallelism = () => 2 }; var archives = new ArchiveService(db, archiveOptions); var processing = new ProcessingService(db, new ProcessingOptions { Archive = archiveOptions, Encoders = [new ManagedEncoderProvider()] }); var archive = await archives.CreateAsync(Path.Combine(root, "archive"), "Studio", cancellationToken: context.Cancellation); var handlers = archives.CreateJobHandlers().Concat(processing.CreateJobHandlers()).ToList(); var engine = new JobEngine(db, handlers, new JobEngineOptions { WorkerId = WorkerId, Kinds = [.. handlers.Select(h => h.Kind).Distinct()], PollInterval = TimeSpan.FromMilliseconds(100), }); await engine.StartAsync(context.Cancellation); var scheduler = new Scheduler(db, engine); await scheduler.StartAsync(new Host(archives, archive), context.Cancellation); return new DesktopFixture(context, root, db, archives, processing, engine, scheduler, archive); } public async Task ShareAsync() { var options = ServerOptions.ForHosted(ShareData, Settings); options.LoginAttemptsPerMinute = 1000; var server = ServerHost.BuildHosted(options, new HostedEngine(Database, Archives, Processing, Engine, Scheduler, Archive)); await server.StartAsync(_context.Cancellation); return server; } public HttpClient Client(HostedServer server, string? token = null) { var client = new HttpClient { BaseAddress = new Uri($"http://127.0.0.1:{server.HttpPort}"), Timeout = TimeSpan.FromSeconds(60) }; if (token is not null) client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); return client; } /// The first administrator, signed in with its temporary password, which it then replaces. public async Task AdminAsync(HostedServer server) { var temporary = server.AdminPassword() ?? throw new InvalidOperationException("No temporary password"); using var anonymous = Client(server); using var login = await anonymous.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", temporary, "tests"), ProtocolJson.Default.LoginRequest); login.EnsureSuccessStatusCode(); var token = (await login.Content.ReadFromJsonAsync(ProtocolJson.Default.LoginResponse))!.Token!; var client = Client(server, token); using var changed = await client.PostAsJsonAsync("/api/v1/auth/password", new ChangePasswordRequest(temporary, "a-good-password-1"), ProtocolJson.Default.ChangePasswordRequest); changed.EnsureSuccessStatusCode(); return client; } public async Task SequenceAsync(Archive archive) { var sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = archive.Id, Title = "Roof " + archive.Name, Folder = string.Empty, Origin = SequenceOrigin.Server, UpdatedAt = DateTimeOffset.UtcNow, }; await Database.Sequences.UpsertAsync(sequence, _context.Cancellation); return sequence; } public async Task WaitAsync(Guid id) { var until = DateTime.UtcNow + TimeSpan.FromSeconds(30); while (true) { if (await Database.Jobs.GetAsync(id, _context.Cancellation) is { State: JobState.Done or JobState.Failed or JobState.Cancelled } job) return job; Assert.True(DateTime.UtcNow < until, "the job did not finish"); await Task.Delay(50, _context.Cancellation); } } public async ValueTask DisposeAsync() { await Scheduler.DisposeAsync(); await Engine.DisposeAsync(); await Database.DisposeAsync(); } /// A desktop's schedule host: one archive scan schedule, the archive service's clean-up steps. private sealed class Host(ArchiveService archives, Archive archive) : IScheduleHost { public TimeZoneInfo TimeZone => TimeZoneInfo.Utc; public IReadOnlyList SystemSchedules => [new(ScanKey, "Archive scan", ScheduleActions.Scan, ScheduleRecurrence.Interval(10, RecurrenceUnit.Minutes), CatchUp: false)]; public IReadOnlyList CleanupSteps => [.. archives.CleanupSteps.Select(s => s.Name)]; public bool DeepAnalysisAfterScan => false; public ValueTask GetArchiveAsync(CancellationToken cancellationToken) => ValueTask.FromResult(archive); public string? Unavailable(string action) => null; } }