using System.Net; using Microsoft.Extensions.DependencyInjection; using Chronos.Core.Archiving; using Chronos.Core.Model; using Chronos.Core.Project; using Chronos.Data.Jobs; using Chronos.Data.Processing; using Chronos.Protocol; using Chronos.Protocol.Client; using Chronos.Testing; namespace Chronos.Server.Tests.Api; /// /// What a desktop editor of a server sequence asks of the API (BRIEF ยง14.5 "edit with proxies, render where the /// originals are"): the full analysis report, settings edits carrying their version and the hero frame, and the jobs /// it posts and reads back. /// [Category("server")] public sealed class RemoteEditingTests(TestContext context) { [Test] public async Task TheAnalysisReportIsServedOnceTheSequenceWasAnalysed() { await using var server = await TestServer.StartAsync(context); var (editor, _, token) = await server.UserAsync("alberto", "editor"); using var client = Client(server, token); var frame = await Uploads.UploadAsync(editor, Uploads.Photo(2000, 41)); Assert.Null(await client.GetAnalysisReportAsync(frame.SequenceId, context.Cancellation), "never deep-analysed"); var sequence = (await server.Database.Sequences.GetAsync(frame.SequenceId))!; var path = new ArchivePaths(server.Options.ArchivePath).Sequence(sequence.Folder).AnalysisPath; Directory.CreateDirectory(Path.GetDirectoryName(path)!); var report = Reports.Minimal(1).ToJson(); await File.WriteAllTextAsync(path, report, context.Cancellation); Assert.Equal(report, await client.GetAnalysisReportAsync(frame.SequenceId, context.Cancellation)); // One made for other frames is not this sequence's report. await File.WriteAllTextAsync(path, Reports.Minimal(2).ToJson(), context.Cancellation); Assert.Null(await client.GetAnalysisReportAsync(frame.SequenceId, context.Cancellation), "another frame count"); var gone = await Assert.ThrowsAsync(() => client.GetAnalysisReportAsync(Guid.CreateVersion7(), context.Cancellation)); Assert.Equal(HttpStatusCode.NotFound, gone.Status); } [Test] public async Task AnEditCarriesItsVersionAndTheHeroFrame() { await using var server = await TestServer.StartAsync(context); var (editor, _, token) = await server.UserAsync("alberto", "editor"); using var client = Client(server, token); var first = await Uploads.UploadAsync(editor, Uploads.Photo(2000, 42), "a.CR3", sessionKey: "harbour"); var second = await Uploads.UploadAsync(editor, Uploads.Photo(2000, 43), "b.CR3", sessionKey: "harbour"); var foreign = await Uploads.UploadAsync(editor, Uploads.Photo(2000, 44), "c.CR3", sessionKey: "elsewhere"); var id = first.SequenceId; Assert.Equal(id, second.SequenceId); var read = await client.GetSequenceAsync(id, context.Cancellation); var settings = new ProjectSettings { HeroFrame = second.Index }.ToJson(); var hero = await client.UpdateSequenceAsync(id, new UpdateSequenceRequest(null, null, null, null, settings, read.SettingsVersion, second.FrameId), context.Cancellation); Assert.Equal(second.FrameId, hero.HeroFrameId); Assert.Equal(read.SettingsVersion + 1, hero.SettingsVersion); // An edit made from an older version is refused and changes nothing (the editor merges and sends again). var stale = await Assert.ThrowsAsync(() => client.UpdateSequenceAsync(id, new UpdateSequenceRequest(null, null, null, null, settings, read.SettingsVersion, first.FrameId), context.Cancellation)); Assert.Equal(HttpStatusCode.Conflict, stale.Status); Assert.Equal(second.FrameId, (await client.GetSequenceAsync(id, context.Cancellation)).HeroFrameId); var other = await client.UpdateSequenceAsync(id, new UpdateSequenceRequest(null, null, null, null, null, hero.SettingsVersion, foreign.FrameId), context.Cancellation); Assert.Equal(second.FrameId, other.HeroFrameId, "a frame of another sequence is not its hero"); var cleared = await client.UpdateSequenceAsync(id, new UpdateSequenceRequest(null, null, null, null, null, other.SettingsVersion, Guid.Empty), context.Cancellation); Assert.Null(cleared.HeroFrameId); } [Test] public async Task SettingsAloneMoveTheHeroFrameWithTheProjectsHero() { await using var server = await TestServer.StartAsync(context); var (editor, _, token) = await server.UserAsync("alberto", "editor"); using var client = Client(server, token); await Uploads.UploadAsync(editor, Uploads.Photo(2000, 46), "a.CR3", sessionKey: "pier"); var second = await Uploads.UploadAsync(editor, Uploads.Photo(2000, 47), "b.CR3", sessionKey: "pier"); var id = second.SequenceId; var read = await client.GetSequenceAsync(id, context.Cancellation); // The sync of a local copy sends the project only. var project = new ProjectSettings { HeroFrame = second.Index }; var hero = await client.UpdateSequenceAsync(id, new UpdateSequenceRequest(null, null, null, null, project.ToJson(), read.SettingsVersion), context.Cancellation); Assert.Equal(second.FrameId, hero.HeroFrameId); var exposure = project with { Exposure = project.Exposure with { ExposureOffsetEv = 0.3 } }; var kept = await client.UpdateSequenceAsync(id, new UpdateSequenceRequest(null, null, null, null, exposure.ToJson(), hero.SettingsVersion), context.Cancellation); Assert.Equal(second.FrameId, kept.HeroFrameId, "other changes keep the hero"); var cleared = await client.UpdateSequenceAsync(id, new UpdateSequenceRequest(null, null, null, null, (exposure with { HeroFrame = null }).ToJson(), kept.SettingsVersion), context.Cancellation); Assert.Null(cleared.HeroFrameId, "no hero shows the first frame"); } [Test] public async Task JobsPostedOnePerPresetReadBackAsTheClientsJobs() { var handler = new BlockingJobHandler(JobKind.DeepAnalyse); await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton(handler)); var (editor, _, token) = await server.UserAsync("alberto", "editor"); using var client = Client(server, token); var frame = await Uploads.UploadAsync(editor, Uploads.Photo(2000, 45)); var dto = await client.GetSequenceAsync(frame.SequenceId, context.Cancellation); var sequence = new Sequence { Id = dto.Id, ArchiveId = Guid.Empty, Folder = dto.Folder, Title = dto.Title }; var export = new ExportSettings { Preset = new Auto().WithManual("4k-hevc"), Batch = ["1440p-h264"] }; var posted = new List(); foreach (var request in ProcessingJobs.RenderEach(sequence, export, 3840, 2160, new ProjectSettings { Export = export })) { var job = await client.CreateJobAsync(new CreateJobRequest(JobWire.Kind(request.Kind), request.Targets, request.ParametersJson), context.Cancellation); posted.Add(Assert.NotNull(JobWire.FromDto(job))); } Assert.Count(2, posted); Assert.True(posted.All(j => j is { Kind: JobKind.Render, State: JobState.Queued } && j.TargetIds.SequenceEqual([sequence.Id]))); var analysis = Assert.NotNull(JobWire.FromDto(await client.CreateJobAsync( new CreateJobRequest(JobWire.Kind(JobKind.DeepAnalyse), [sequence.Id], null), context.Cancellation))); Assert.Equal(JobKind.DeepAnalyse, analysis.Kind); await handler.Started.Task.WaitAsync(TimeSpan.FromSeconds(10)); handler.Release(); for (var i = 0; i < 200 && JobWire.FromDto(await client.GetJobAsync(analysis.Id, context.Cancellation))!.State != JobState.Done; i++) await Task.Delay(50); var done = Assert.NotNull(JobWire.FromDto(await client.GetJobAsync(analysis.Id, context.Cancellation))); Assert.Equal(JobState.Done, done.State); Assert.Equal((4L, 4L), (done.ProgressDone, done.ProgressTotal)); Assert.NotNull(done.Finished); var queued = (await client.ListJobsAsync("queued", null, context.Cancellation)).Items.Select(JobWire.FromDto).OfType().ToList(); Assert.SequenceEqual(posted.Select(j => j.Id).Order(), queued.Where(j => j.Kind == JobKind.Render).Select(j => j.Id).Order()); } [Test] public async Task TheJobListFiltersByKindAndGoesOnPastThePage() { await using var server = await TestServer.StartAsync(context); var (_, _, token) = await server.UserAsync("alberto", "editor"); using var client = Client(server, token); var target = Guid.CreateVersion7(); var created = DateTimeOffset.UtcNow; // Paused, so no worker claims them: a render of the editor's sequence behind a busy server's thumbnails. Job Paused(JobKind kind, int age) => new() { Id = Guid.CreateVersion7(), Kind = kind, TargetIds = [target], State = JobState.Paused, Created = created.AddSeconds(-age), }; var render = Paused(JobKind.Render, 300); await server.Database.Jobs.EnqueueAsync(render); for (var i = 0; i < 205; i++) await server.Database.Jobs.EnqueueAsync(Paused(JobKind.Thumbs, i)); var renders = await client.ListJobsAsync("paused", "render", null, context.Cancellation); Assert.SequenceEqual([render.Id], renders.Items.Select(j => j.Id)); Assert.Null(renders.NextCursor); var first = await client.ListJobsAsync("paused", null, null, context.Cancellation); Assert.NotNull(first.NextCursor, "more than a page"); Assert.False(first.Items.Any(j => j.Id == render.Id), "the oldest job is past the first page"); var rest = await client.ListJobsAsync("paused", null, first.NextCursor, context.Cancellation); Assert.Null(rest.NextCursor); Assert.Equal(206, first.Items.Concat(rest.Items).Select(j => j.Id).Distinct().Count()); } [Test] public async Task AnEditorHearsFramesAddedAndSettingsChangedOverItsEventSocket() { await using var server = await TestServer.StartAsync(context); var (editor, _, token) = await server.UserAsync("alberto", "editor"); using var client = Client(server, token); var first = await Uploads.UploadAsync(editor, Uploads.Photo(2000, 48), "a.CR3", sessionKey: "quay"); var id = first.SequenceId; var opened = await client.GetManifestAsync(id, context.Cancellation); using var listening = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation); listening.CancelAfter(TimeSpan.FromSeconds(20)); var received = new List(); var reader = Task.Run(async () => { await foreach (var e in client.EventsAsync(listening.Token)) { if (e is not SequenceChangedEvent changed || changed.SequenceId != id) continue; received.Add(changed); if (changed.Change == "settings") return; } }); var hub = server.Services.GetRequiredService(); for (var i = 0; i < 200 && hub.SubscriberCount == 0; i++) await Task.Delay(25); // Someone uploads another frame, then someone edits the project: the editor checks its frames, then its settings. await Uploads.UploadAsync(editor, Uploads.Photo(2000, 49), "b.CR3", sessionKey: "quay"); var read = await client.GetSequenceAsync(id, context.Cancellation); await client.UpdateSequenceAsync(id, new UpdateSequenceRequest(null, null, null, null, new ProjectSettings { HeroFrame = 1 }.ToJson(), read.SettingsVersion), context.Cancellation); await reader.WaitAsync(TimeSpan.FromSeconds(20)); var now = await client.GetManifestAsync(id, context.Cancellation); var frames = Assert.Contains(received, e => e.Change == "frames"); Assert.Equal(now.FramesVersion, frames.FramesVersion); Assert.True(frames.FramesVersion > opened.FramesVersion, "a frames version the editor has not seen"); Assert.Count(2, now.Frames); Assert.Equal(("settings", now.SettingsVersion), (received[^1].Change, received[^1].SettingsVersion)); } private static ChronosClient Client(TestServer server, string token) => new(server.HttpBase, new CertificatePinning([]), "tests") { Token = token }; }