- A server sequence whose cached frames are no longer the server's (a load, a 409 merge or a frames change event finds other frame ids) turns read-only and says why; opening it, or a sequence without cached proxies, queues the proxy download and opens it when done. Signed out reads "Connect to the server". - Server jobs show queued, running (with a bar), paused and analysing glyphs in the header with Cancel; a failed or cancelled one raises a toast with Retry. Progress alone no longer refreshes the whole editor. - The server job list is read by state and kind to its last page; "Add to queue" sets the job's priority; the event listener survives any failure and always releases its frames and connection. - An account that may render but not edit picks the preset to render without changing the project; preset cards and their batch boxes say why they wait. - Export tab of a server sequence: no local encoder, notices or folder; no Jobs history link. Sync at 12 px beside a folder that keeps 96 px and trims. - Restore's Undo puts back the project from before the restore, only while that restore is the editor's last change. - Export "Sync" of a render that ends while the session is down is queued for the saved session's server; skips are logged; sync follows the engine before it starts. - Capture: sequence-remote-export, -running and -readonly, and sequence-export-copies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
329 lines
15 KiB
C#
329 lines
15 KiB
C#
using System.IO;
|
|
using Titano.App.Editor;
|
|
using Titano.Core.Codecs.Jpeg;
|
|
using Titano.Core.Diagnostics.Synthetic;
|
|
using Titano.Core.Imaging;
|
|
using Titano.Core.Imaging.Decoders;
|
|
using Titano.Core.Model;
|
|
using Titano.Core.Project;
|
|
using Titano.Data.Processing;
|
|
using Titano.Data.Sync;
|
|
using Titano.Protocol;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.App.Tests.Editor;
|
|
|
|
/// <summary>
|
|
/// A server sequence in the editor (BRIEF §14.5): proxies from the cache, the project from the server with its
|
|
/// version, saves that merge a change made meanwhile on the server, and analyses and renders posted as server jobs.
|
|
/// </summary>
|
|
public sealed class RemoteSequenceSourceTests(TestContext context)
|
|
{
|
|
private const string Server = "https://nas.test:8443";
|
|
|
|
[Test]
|
|
public async Task TheProjectComesFromTheServerAndFramesFromTheProxies()
|
|
{
|
|
var (source, api, _) = Open(frames: 6, cached: 5);
|
|
await using (source)
|
|
{
|
|
api.SettingsJson = new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 0.4 } }.ToJson();
|
|
var content = await source.LoadAsync(context.Cancellation);
|
|
Assert.True(source.IsRemote);
|
|
Assert.Equal(6, content.Count);
|
|
Assert.Equal(0.4, content.Project.Exposure.ExposureOffsetEv);
|
|
Assert.Equal(3, content.Sequence.SettingsVersion);
|
|
Assert.Null(content.ExportsFolder, "renders of a server sequence are not on this PC");
|
|
Assert.Equal(string.Empty, content.Renders.Single().OutputPath);
|
|
Assert.Empty(content.SavedCopies);
|
|
Assert.Null(content.Deep, "never analysed on the server");
|
|
|
|
using (var frame = await source.DecodeAsync(2, 32, DecodePurpose.Thumbnail, context.Cancellation))
|
|
{
|
|
Assert.NotNull(frame);
|
|
Assert.LessOrEqual(Math.Max(frame!.Width, frame.Height), 32);
|
|
}
|
|
Assert.Null(await source.DecodeAsync(5, 32, DecodePurpose.Render, context.Cancellation), "a frame without a cached proxy");
|
|
Assert.Null(await source.DecodeAsync(9, 32, DecodePurpose.Render, context.Cancellation));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task TheServersAnalysisIsUsedWhenItDescribesTheseFrames()
|
|
{
|
|
var sunset = EditorFixture.Sequence(8);
|
|
var (source, api, _) = Open(sunset);
|
|
await using (source)
|
|
{
|
|
api.Report = EditorFixture.Content(sunset).Deep!.ToJson();
|
|
Assert.NotNull((await source.LoadAsync(context.Cancellation)).Deep);
|
|
api.Report = EditorFixture.Content(EditorFixture.Sequence(9)).Deep!.ToJson();
|
|
Assert.Null((await source.LoadAsync(context.Cancellation)).Deep, "a report of other frames is not shown");
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task SavesSendTheVersionTheyWereMadeFrom()
|
|
{
|
|
var (source, api, _) = Open();
|
|
await using (source)
|
|
{
|
|
var content = await source.LoadAsync(context.Cancellation);
|
|
var first = content.Project with { Exposure = content.Project.Exposure with { ExposureOffsetEv = 0.5 } };
|
|
var stored = await source.SaveProjectAsync(first, content.Project, context.Cancellation);
|
|
Assert.True(ProjectMerge.SameValues(first, stored));
|
|
var second = stored with { Exposure = stored.Exposure with { ExposureOffsetEv = 0.7 } };
|
|
await source.SaveProjectAsync(second, stored, context.Cancellation);
|
|
|
|
Assert.SequenceEqual([3, 4], api.Edits.Select(e => e.Request.ExpectedSettingsVersion ?? -1));
|
|
Assert.True(api.Edits.All(e => e.Accepted));
|
|
Assert.Equal(5, api.SettingsVersion);
|
|
Assert.Equal(0.7, ProjectSettings.FromJson(api.SettingsJson!).Exposure.ExposureOffsetEv);
|
|
Assert.Null(api.Edits[0].Request.Title, "only the settings travel");
|
|
Assert.Null(api.Edits[0].Request.Tags);
|
|
|
|
// Nothing changed, nothing sent.
|
|
Assert.True(ProjectMerge.SameValues(second, await source.SaveProjectAsync(second, second, context.Cancellation)));
|
|
Assert.Count(2, api.Edits);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task AChangeMadeOnTheServerMeanwhileIsMergedNotOverwritten()
|
|
{
|
|
var (source, api, _) = Open();
|
|
await using (source)
|
|
{
|
|
var content = await source.LoadAsync(context.Cancellation);
|
|
api.EditsBefore.Enqueue(json =>
|
|
{
|
|
var theirs = ProjectSettings.FromJson(json ?? "{}");
|
|
return (theirs with { Camera = theirs.Camera with { OffsetY = -0.2 } }).ToJson();
|
|
});
|
|
var mine = content.Project with { Exposure = content.Project.Exposure with { ExposureOffsetEv = 0.5 } };
|
|
var merged = await source.SaveProjectAsync(mine, content.Project, context.Cancellation);
|
|
|
|
Assert.Equal(0.5, merged.Exposure.ExposureOffsetEv);
|
|
Assert.Equal(-0.2, merged.Camera.OffsetY, "the server's change is kept");
|
|
Assert.SequenceEqual([false, true], api.Edits.Select(e => e.Accepted), "the 409 is followed by a merged edit");
|
|
Assert.Equal(4, api.Edits[1].Request.ExpectedSettingsVersion);
|
|
var stored = ProjectSettings.FromJson(api.SettingsJson!);
|
|
Assert.Equal(0.5, stored.Exposure.ExposureOffsetEv);
|
|
Assert.Equal(-0.2, stored.Camera.OffsetY);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task ConflictsThatKeepComingAreReported()
|
|
{
|
|
var (source, api, _) = Open();
|
|
await using (source)
|
|
{
|
|
var content = await source.LoadAsync(context.Cancellation);
|
|
for (var i = 1; i <= 3; i++)
|
|
{
|
|
var offset = i / 10.0;
|
|
api.EditsBefore.Enqueue(json =>
|
|
{
|
|
var theirs = ProjectSettings.FromJson(json ?? "{}");
|
|
return (theirs with { Camera = theirs.Camera with { OffsetX = offset } }).ToJson();
|
|
});
|
|
}
|
|
await Assert.ThrowsAsync<IOException>(() => source.SaveProjectAsync(content.Project with { HeroFrame = 2 }, content.Project, context.Cancellation));
|
|
Assert.False(api.Edits.Any(e => e.Accepted));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task TheHeroTravelsAsAFrameOfTheSequence()
|
|
{
|
|
var (source, api, remote) = Open();
|
|
await using (source)
|
|
{
|
|
var content = await source.LoadAsync(context.Cancellation);
|
|
var hero = await source.SaveProjectAsync(content.Project with { HeroFrame = 2 }, content.Project, context.Cancellation);
|
|
var other = await source.SaveProjectAsync(hero with { Exposure = hero.Exposure with { ExposureOffsetEv = 0.1 } }, hero, context.Cancellation);
|
|
await source.SaveProjectAsync(other with { HeroFrame = null }, other, context.Cancellation);
|
|
Assert.SequenceEqual([remote.Frames[2].Id, (Guid?)null, Guid.Empty], api.Edits.Select(e => e.Request.HeroFrameId));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task AnAccountWithoutTheEditRightOnlyLooks()
|
|
{
|
|
var (source, api, _) = Open(readOnly: true);
|
|
await using (source)
|
|
{
|
|
var content = await source.LoadAsync(context.Cancellation);
|
|
Assert.True(source.IsReadOnly);
|
|
var session = new EditorSession(source, content, [], TimeSpan.FromMilliseconds(20));
|
|
await using (session)
|
|
{
|
|
Assert.True(session.IsReadOnly);
|
|
Assert.False(session.Edit(p => p with { Exposure = p.Exposure with { ExposureOffsetEv = 0.5 } }));
|
|
Assert.False(session.SetHero(3));
|
|
Assert.Empty(session.OptimiseAll());
|
|
Assert.False(session.Undo());
|
|
Assert.True(await session.FlushAsync(), "nothing waits for a save");
|
|
}
|
|
|
|
// The source refuses a change as well; the same project is no change.
|
|
await Assert.ThrowsAsync<InvalidOperationException>(() =>
|
|
source.SaveProjectAsync(content.Project with { HeroFrame = 1 }, content.Project, context.Cancellation));
|
|
Assert.True(ProjectMerge.SameValues(content.Project, await source.SaveProjectAsync(content.Project, content.Project, context.Cancellation)));
|
|
Assert.Empty(api.Edits);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task AnalysesAndRendersArePostedToTheServerOnePerPreset()
|
|
{
|
|
var (source, api, remote) = Open();
|
|
await using (source)
|
|
{
|
|
var content = await source.LoadAsync(context.Cancellation);
|
|
var session = new EditorSession(source, content, [], TimeSpan.FromMilliseconds(20));
|
|
await using (session)
|
|
{
|
|
Assert.True(session.IsRemote);
|
|
await session.SubmitAsync(session.AnalysisRequest());
|
|
var export = session.Project.Export with { Preset = new Auto<string>().WithManual("4k-hevc"), Batch = ["1440p-h264", "4k-h264"] };
|
|
// The preset, then each batch preset.
|
|
var renders = session.RenderRequests(export);
|
|
Assert.Count(3, renders);
|
|
foreach (var request in renders) await session.SubmitAsync(request);
|
|
|
|
Assert.SequenceEqual(["deep-analyse", "render", "render", "render"], api.Posted.Select(p => p.Kind));
|
|
Assert.True(api.Posted.All(p => p.Target.SequenceEqual([remote.Sequence.Id])));
|
|
var presets = api.Posted.Skip(1).Select(p => ProcessingJobs.ReadRender(p.ParametersJson).Export).ToList();
|
|
Assert.True(presets.All(e => e.Batch.Count == 0), "a server job renders one preset");
|
|
Assert.SequenceEqual(["4k-hevc", "1440p-h264", "4k-h264"], presets.Select(e => e.Preset.Manual));
|
|
}
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void ThisPcRendersABatchAsOneJob()
|
|
{
|
|
var content = EditorFixture.Content(EditorFixture.Sunset);
|
|
var session = new EditorSession(new FakeSequenceSource(EditorFixture.Sunset, content), content, []);
|
|
var requests = session.RenderRequests(session.Project.Export with { Batch = ["1440p-h264", "4k-h264"] });
|
|
Assert.Count(1, requests);
|
|
Assert.Count(2, ProcessingJobs.ReadRender(requests[0].ParametersJson).Export.Batch);
|
|
}
|
|
|
|
[Test]
|
|
public async Task TheServersJobEventsReachTheEditor()
|
|
{
|
|
var (source, api, remote) = Open();
|
|
await using (source)
|
|
{
|
|
await source.LoadAsync(context.Cancellation);
|
|
var seen = new List<Job>();
|
|
source.JobChanged += job =>
|
|
{
|
|
lock (seen) seen.Add(job);
|
|
};
|
|
var id = await source.SubmitAsync(ProcessingJobs.DeepAnalysis([remote.Sequence.Id]), context.Cancellation);
|
|
Assert.Equal(JobState.Queued, Last(seen).State);
|
|
|
|
api.Jobs[id] = api.Job(id, "deep-analyse", "running");
|
|
api.Publish(new JobStateEvent(id, "running", null));
|
|
await WaitAsync(() => Last(seen).State == JobState.Running);
|
|
api.Publish(new JobProgressEvent(id, 10, 20, 2.5, 4));
|
|
await WaitAsync(() => Last(seen).ProgressDone == 10);
|
|
Assert.Equal(TimeSpan.FromSeconds(4), Last(seen).Eta);
|
|
|
|
// Another client's job on another sequence is not this editor's business.
|
|
var foreign = Guid.CreateVersion7();
|
|
api.Jobs[foreign] = api.Job(foreign, "render", "running", Guid.CreateVersion7());
|
|
api.Publish(new JobStateEvent(foreign, "running", null));
|
|
api.Jobs[id] = api.Job(id, "deep-analyse", "done", done: 20, total: 20);
|
|
api.Publish(new JobStateEvent(id, "done", null));
|
|
await WaitAsync(() => Last(seen).State == JobState.Done);
|
|
lock (seen) Assert.False(seen.Any(j => j.Id == foreign));
|
|
|
|
var other = Guid.CreateVersion7();
|
|
api.Jobs[other] = api.Job(other, "render", "queued");
|
|
api.Jobs[foreign] = api.Job(foreign, "render", "queued", Guid.CreateVersion7());
|
|
Assert.SequenceEqual([other], (await source.ListJobsAsync(context.Cancellation)).Select(j => j.Id), "active jobs of this sequence only");
|
|
|
|
// A late progress event of the finished job does not bring it back to running.
|
|
api.Publish(new JobProgressEvent(id, 19, 20, 2.5, 1));
|
|
api.Jobs[other] = api.Job(other, "render", "running");
|
|
api.Publish(new JobStateEvent(other, "running", null));
|
|
await WaitAsync(() => Last(seen).Id == other);
|
|
lock (seen) Assert.Equal(JobState.Done, seen.Last(j => j.Id == id).State);
|
|
}
|
|
Assert.True(api.Disposed, "closing the editor closes its connection");
|
|
}
|
|
|
|
[Test]
|
|
public async Task ASequenceGoneFromTheServerReadsAsMissing()
|
|
{
|
|
var (source, api, _) = Open();
|
|
await using (source)
|
|
{
|
|
var content = await source.LoadAsync(context.Cancellation);
|
|
api.Gone = true;
|
|
await Assert.ThrowsAsync<InvalidOperationException>(() => source.LoadAsync(context.Cancellation));
|
|
await Assert.ThrowsAsync<InvalidOperationException>(() => source.SaveProjectAsync(content.Project with { HeroFrame = 1 }, content.Project, context.Cancellation));
|
|
}
|
|
}
|
|
|
|
private (RemoteSequenceSource Source, FakeRemoteSequenceApi Api, RemoteSequence Remote) Open(int frames = 6, int cached = int.MaxValue,
|
|
bool readOnly = false) =>
|
|
Open(EditorFixture.Sequence(frames), cached, readOnly);
|
|
|
|
/// <summary>A server sequence whose first <paramref name="cached"/> proxies are on this PC (synthetic JPEGs).</summary>
|
|
private (RemoteSequenceSource Source, FakeRemoteSequenceApi Api, RemoteSequence Remote) Open(SyntheticSequence sunset, int cached = int.MaxValue,
|
|
bool readOnly = false)
|
|
{
|
|
var folder = context.CreateTempDirectory("proxies");
|
|
var sequence = new Sequence
|
|
{
|
|
Id = Guid.CreateVersion7(),
|
|
ArchiveId = Guid.Empty,
|
|
Title = "Harbour sunset",
|
|
Folder = "2026/2026-09-28 Synthetic",
|
|
FrameCount = sunset.Count,
|
|
Origin = SequenceOrigin.Server,
|
|
SyncState = SyncState.RemoteOnly,
|
|
};
|
|
var frames = sunset.Frames.Select((f, i) => f with { Id = Guid.CreateVersion7(), SequenceId = sequence.Id, Flags = i < cached ? f.Flags : f.Flags | FrameFlags.Missing })
|
|
.ToList();
|
|
var files = new string?[frames.Count];
|
|
for (var i = 0; i < files.Length && i < cached; i++)
|
|
{
|
|
using var image = sunset.Render(i);
|
|
files[i] = Path.Combine(folder, frames[i].Id.ToString("N") + ".jpg");
|
|
File.WriteAllBytes(files[i]!, JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 90 }));
|
|
}
|
|
var remote = new RemoteSequence(Server, sequence, frames, files);
|
|
var api = new FakeRemoteSequenceApi(sequence, [.. frames.Select(f => f.Id)]) { SettingsJson = new ProjectSettings().ToJson() };
|
|
return (new RemoteSequenceSource(remote, api, DecoderFactory.CreateClient(null), readOnly), api, remote);
|
|
}
|
|
|
|
private static Job Last(List<Job> seen)
|
|
{
|
|
lock (seen) return seen[^1];
|
|
}
|
|
|
|
private static async Task WaitAsync(Func<bool> condition)
|
|
{
|
|
for (var i = 0; i < 200; i++)
|
|
{
|
|
try
|
|
{
|
|
if (condition()) return;
|
|
}
|
|
catch (ArgumentOutOfRangeException)
|
|
{
|
|
// Nothing seen yet.
|
|
}
|
|
await Task.Delay(25);
|
|
}
|
|
Assert.True(condition(), "timed out waiting");
|
|
}
|
|
}
|