diff --git a/tests/Titano.Server.Tests/Api/RemoteEditingTests.cs b/tests/Titano.Server.Tests/Api/RemoteEditingTests.cs new file mode 100644 index 00000000..a87edf7c --- /dev/null +++ b/tests/Titano.Server.Tests/Api/RemoteEditingTests.cs @@ -0,0 +1,114 @@ +using System.Net; +using Microsoft.Extensions.DependencyInjection; +using Titano.Core.Archiving; +using Titano.Core.Model; +using Titano.Core.Project; +using Titano.Data.Jobs; +using Titano.Data.Processing; +using Titano.Protocol; +using Titano.Protocol.Client; +using Titano.Testing; + +namespace Titano.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)!); + const string report = """{"version":1,"frameCount":1}"""; + await File.WriteAllTextAsync(path, report, context.Cancellation); + Assert.Equal(report, await client.GetAnalysisReportAsync(frame.SequenceId, context.Cancellation)); + + 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 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()); + } + + private static TitanoClient Client(TestServer server, string token) => new(server.HttpBase, new CertificatePinning([]), "tests") { Token = token }; +}