Files
Chronos/tests/Chronos.Data.Tests/Workspaces/WorkspaceRenderTests.cs
T
Alby96andClaude Opus 5.5 2654683a12 fix(workspace): close the phase 5c review findings
Originals are now judged by their bytes, never by the media index: a removal
or clean-up hashes every file under media/originals and deletes it only when
the home holds that SHA-256; every other file, named or not, goes to the
workspace trash. Trash entries are filled under a staging name that Empty
never lists. A ledger can only name files inside exports/.

Jobs follow the workspace: a copy, render or send resolves the folder that
holds its project now (a moved workspace, a retry after a move) and waits
while a move has not switched yet. A clean-up failure after the switch is a
warning that keeps the journal for a later resume; entries that changed
since they were copied are kept, and a same-volume rename refused across
volumes falls back to copy and verify.

Sends map a replayed version by content hash, or go with the project only,
and note an old server that drops provenance. The media index (format 2) and
project file (format 3) refuse older readers while they hold originals.
Metered connections also hold sync downloads and server copies. The editor
gains Send again, the Settings page can resume a stopped move, the disabled
tooltips say why, and unused strings are pruned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 17:32:48 +01:00

256 lines
15 KiB
C#

using Chronos.Core.Diagnostics.Synthetic;
using Chronos.Core.Export;
using Chronos.Core.Imaging;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Archiving;
using Chronos.Data.Archiving.Jobs;
using Chronos.Data.Jobs;
using Chronos.Data.Processing;
using Chronos.Data.Processing.Jobs;
using Chronos.Data.Projects;
using Chronos.Data.Tests.Processing;
using Chronos.Data.Workspaces;
using Chronos.Testing;
namespace Chronos.Data.Tests.Workspaces;
/// <summary>
/// A workspace project rendered on this PC from the originals its media hold (phase 5c, <see cref="JobKind.WorkspaceRender"/>):
/// into its exports, recorded in its ledger and sent home after; refused without the originals; a cancel leaves nothing half written.
/// </summary>
[Category("workspace")]
public sealed class WorkspaceRenderTests(TestContext context)
{
private const int Frames = 24;
private static readonly ExportSettings Mjpeg = new()
{
Preset = new Auto<string>().WithManual("mjpeg-avi"),
Overrides = new ExportOverrides { Width = 96, Height = 64 },
NameTemplate = "clip-{codec}",
};
private static SyntheticSequence Scene() => SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = Frames, StartEv = 0.5, EndEv = -0.5 });
private sealed record Bed(ProcessingFixture Fx, Sequence Sequence, WorkspaceStore Store, WorkspaceProject Project, JobEngine Engine) : IAsyncDisposable
{
public async ValueTask DisposeAsync()
{
await Engine.DisposeAsync();
await Fx.DisposeAsync();
}
}
/// <summary>A sequence of the archive, Main checked out into a workspace with <paramref name="media"/> copied, and an engine running every handler.</summary>
private async Task<Bed> BedAsync(string media = BundleMedia.Originals, Action<ProcessingFixture, Sequence>? beforeCopy = null)
{
var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
var engine = new JobEngine(fx.Db,
[
.. fx.Archive.Service.CreateJobHandlers(), .. fx.Processing.CreateJobHandlers(),
.. WorkspaceJobs.CreateHandlers(fx.Db, fx.Archive.Service, null, new WorkspaceJobOptions(fx.Processing.Options, () => false)),
],
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
await engine.StartAsync(fx.Ct);
var store = new WorkspaceStore(Path.Combine(context.CreateTempDirectory("render-ws"), "workspace"));
var sync = new WorkspaceSync(store, "test") { LockWait = TimeSpan.FromSeconds(TestTiming.Budget(10)) };
var home = new ArchiveProjectHome(fx.Db, fx.Archive.Service.Projects, sequence.Id);
var main = await fx.Archive.Service.Projects.MainAsync(sequence.Id, fx.Ct);
var project = await sync.CheckoutAsync(home, main.Id, media, fx.Ct);
beforeCopy?.Invoke(fx, sequence);
var copy = await engine.EnqueueAsync(WorkspaceJobs.Copy(sequence, store.Root, main.Id, media), cancellationToken: fx.Ct);
await fx.WaitForAsync(copy.Id, JobState.Done);
return new Bed(fx, sequence, store, project, engine);
}
private static ArchiveJobRequest RenderRequest(Bed bed, Guid? versionId = null)
{
var json = ProcessingJobs.Render(bed.Sequence, Mjpeg, 96, 64, new ProjectSettings()).ParametersJson!;
return WorkspaceJobs.Render(bed.Sequence.Id, bed.Sequence.DisplayTitle, bed.Store.Root, bed.Project.ProjectId, versionId, json);
}
[Test]
public async Task ARenderFromTheOriginalsIsWrittenToExportsRecordedAndSentAfter()
{
await using var bed = await BedAsync();
var fx = bed.Fx;
var job = await bed.Engine.EnqueueAsync(RenderRequest(bed), cancellationToken: fx.Ct);
await fx.WaitForAsync(job.Id, JobState.Done);
var output = Path.Combine(bed.Project.ExportsDirectory, "clip-MJPEG.avi");
Assert.FileExists(output);
Assert.Empty(Directory.EnumerateFileSystemEntries(bed.Project.ExportsDirectory, ".*"));
var entry = bed.Project.ReadRenders().Entries.Single();
Assert.Equal(RenderJobHandler.RenderId(job.Id, 0), entry.RenderId);
Assert.Equal((job.Id, "clip-MJPEG.avi", new FileInfo(output).Length, WorkspaceRenderEntry.Pending, bed.Project.ProjectId),
(entry.JobId, entry.File, entry.Size, entry.State, entry.ProjectId));
Assert.Equal(await Chronos.Core.Archiving.ContentHash.Sha256Async(output), entry.Sha256);
Assert.Equal(("Mjpeg", 96, 64), (entry.Codec, entry.Width, entry.Height));
Assert.Null(entry.ProjectVersionId, "the working state the job embeds is no version this workspace holds");
// The send home follows with the new render, and files it in the archive with its project.
var send = (await fx.Db.Jobs.ListAsync(null, 50, null, fx.Ct)).Items.Single(j => j.Kind == JobKind.WorkspaceSend);
var sent = Assert.NotNull(WorkspaceJobs.SendParameters(send));
// Every render not sent yet, this one among them: one a send failed earlier goes with it.
Assert.Empty(sent.RenderIds);
Assert.Equal((bed.Project.ProjectId, bed.Store.Root), (sent.ProjectId, sent.WorkspaceRoot));
await fx.WaitForAsync(send.Id, JobState.Done);
var filed = Assert.NotNull(await fx.Db.Renders.GetAsync(entry.RenderId, fx.Ct));
Assert.Equal((bed.Sequence.Id, (Guid?)bed.Project.ProjectId, (Guid?)null), (filed.SequenceId, filed.ProjectId, filed.ProjectVersionId));
entry = Assert.NotNull(bed.Project.ReadRenders().Find(entry.RenderId));
Assert.True(entry.IsSent, "the ledger says it went home");
// The same render of the archive's own sequence: the same pictures, at the same size and length.
var archive = await bed.Engine.EnqueueAsync(ProcessingJobs.Render(bed.Sequence, Mjpeg, 96, 64), cancellationToken: fx.Ct);
await fx.WaitForAsync(archive.Id, JobState.Done);
var row = Assert.NotNull(await fx.Db.Renders.GetAsync(RenderJobHandler.RenderId(archive.Id, 0), fx.Ct));
Assert.Equal((row.Width, row.Height, row.Fps, row.DurationSeconds), (entry.Width, entry.Height, entry.Fps, entry.DurationSeconds));
// Again: another name, the first output and its entry kept.
var again = await bed.Engine.EnqueueAsync(RenderRequest(bed), cancellationToken: fx.Ct);
await fx.WaitForAsync(again.Id, JobState.Done);
var entries = bed.Project.ReadRenders().Entries;
Assert.Equal(2, entries.Count);
Assert.True(entries[1].File != entries[0].File, entries[1].File);
Assert.Equal(entry, entries[0]);
}
[Test]
public async Task AVersionHeldInTheWorkspaceIsRecordedWithTheRender()
{
await using var bed = await BedAsync();
var fx = bed.Fx;
var (_, sync) = (bed.Store, new WorkspaceSync(bed.Store, "test"));
var session = await sync.OpenAsync(bed.Project.ProjectId, fx.Ct);
await sync.SaveAsync(bed.Project.ProjectId, session.Working with { ExcludedFrames = [2] }, fx.Ct);
var version = Assert.NotNull(await sync.CommitAsync(bed.Project.ProjectId, "Render", cancellationToken: fx.Ct));
var json = ProcessingJobs.Render(bed.Sequence, Mjpeg, 96, 64).ParametersJson!;
// The job names the version alone: the settings come from versions/.
var stripped = System.Text.Json.Nodes.JsonNode.Parse(json)!.AsObject();
stripped.Remove("settingsJson");
var job = await bed.Engine.EnqueueAsync(
WorkspaceJobs.Render(bed.Sequence.Id, bed.Sequence.DisplayTitle, bed.Store.Root, bed.Project.ProjectId, version.Id, stripped.ToJsonString()), cancellationToken: fx.Ct);
await fx.WaitForAsync(job.Id, JobState.Done);
var entry = bed.Project.ReadRenders().Entries.Single();
Assert.Equal(version.Id, entry.ProjectVersionId);
Assert.Equal(version.ContentHash, entry.SettingsHash);
// A version the workspace does not hold: refused, saying so.
var unknown = await bed.Engine.EnqueueAsync(
WorkspaceJobs.Render(bed.Sequence.Id, bed.Sequence.DisplayTitle, bed.Store.Root, bed.Project.ProjectId, Guid.CreateVersion7(), stripped.ToJsonString()),
cancellationToken: fx.Ct);
Assert.Equal(Loc.L("Errors.Workspace.RenderVersion"), (await fx.WaitForAsync(unknown.Id, JobState.Failed)).Error);
}
/// <summary>
/// The whole way of a local render to an archive home: originals copied, a version saved for the render, the render on this
/// PC, the send that pushes the version first and files the render with its project and version, then the clean-up that
/// removes the media and the sent file of the project, now in step with its home.
/// </summary>
[Test]
public async Task ALocalRenderGoesHomeWithItsVersionAndTheCleanUpThenFreesTheProject()
{
await using var bed = await BedAsync();
var fx = bed.Fx;
var id = bed.Project.ProjectId;
var sync = new WorkspaceSync(bed.Store, "test") { LockWait = TimeSpan.FromSeconds(TestTiming.Budget(10)) };
var home = new ArchiveProjectHome(fx.Db, fx.Archive.Service.Projects, bed.Sequence.Id);
Assert.True(bed.Project.ReadMedia() is { HoldsOriginals: true }, "the copy brought the originals");
// The archive's photos hashed (a Verify), so the home can say it holds each original as it is here.
var verify = await bed.Engine.EnqueueAsync(ArchiveJobs.Verify(fx.Archive.Archive, [bed.Sequence.Id]), cancellationToken: fx.Ct);
await fx.WaitForAsync(verify.Id, JobState.Done);
// Version on render: the working state saved as a version here, not sent yet.
var session = await sync.OpenAsync(id, fx.Ct);
await sync.SaveAsync(id, session.Working with { ExcludedFrames = [4] }, fx.Ct);
var version = Assert.NotNull(await sync.CommitAsync(id, "Render", cancellationToken: fx.Ct));
Assert.Null(await home.GetVersionAsync(id, version.Id, fx.Ct), "the home does not have the version yet");
var json = System.Text.Json.Nodes.JsonNode.Parse(ProcessingJobs.Render(bed.Sequence, Mjpeg, 96, 64).ParametersJson!)!.AsObject();
json.Remove("settingsJson");
var render = await bed.Engine.EnqueueAsync(
WorkspaceJobs.Render(bed.Sequence.Id, bed.Sequence.DisplayTitle, bed.Store.Root, id, version.Id, json.ToJsonString()), cancellationToken: fx.Ct);
await fx.WaitForAsync(render.Id, JobState.Done);
var entry = bed.Project.ReadRenders().Entries.Single();
Assert.Equal((version.Id, WorkspaceRenderEntry.Pending), (entry.ProjectVersionId, entry.State));
// The send pushes the version, then files the render with its project and version.
var send = (await fx.Db.Jobs.ListAsync(null, 50, null, fx.Ct)).Items.Single(j => j.Kind == JobKind.WorkspaceSend);
send = await fx.WaitForAsync(send.Id, JobState.Done);
Assert.Contains(Loc.L("Archive.Log.WorkspaceVersionPushed", bed.Project.ReadBundle().Workspace!.Name, version.Number), send.LogTail);
Assert.NotNull(await home.GetVersionAsync(id, version.Id, fx.Ct));
var row = Assert.NotNull(await fx.Db.Renders.GetAsync(entry.RenderId, fx.Ct));
Assert.Equal((bed.Sequence.Id, (Guid?)id, (Guid?)version.Id, entry.Size), (row.SequenceId, row.ProjectId, row.ProjectVersionId, row.Bytes));
Assert.Equal(entry.Sha256, await Chronos.Core.Archiving.ContentHash.Sha256Async(Path.Combine(fx.Archive.Paths.Root, row.OutputPath)));
Assert.True(bed.Project.ReadRenders().Find(entry.RenderId)!.IsSent, "the ledger says it went home");
Assert.Equal(WorkspaceState.InSync, (await sync.StatusAsync(id, home, fx.Ct)).State);
// The clean-up (here the size limit) frees the project: its proxies, originals and the sent render go; the originals
// are the home's own, so none is set aside in the trash.
var exported = bed.Project.ExportFile(entry.File);
var report = await new WorkspaceCleaner(sync, fx.Db, _ => home) { LockWait = TimeSpan.FromSeconds(TestTiming.Budget(10)) }
.CleanAsync(new WorkspaceCleanOptions { SizeLimitBytes = 1 }, fx.Ct);
var cleaned = Assert.NotNull(report.Cleaned.SingleOrDefault());
Assert.Equal((id, 0), (cleaned.ProjectId, cleaned.Trashed));
Assert.Empty(report.Kept);
Assert.FileMissing(exported);
Assert.Empty(bed.Project.ReadRenders().Entries);
Assert.False(Directory.Exists(bed.Project.OriginalsDirectory), "the originals went");
Assert.False(Directory.Exists(bed.Project.ProxiesDirectory), "the proxies went");
Assert.True(bed.Project.ReadMedia() is { IsCleaned: true }, "the media index says it was cleaned");
Assert.Equal(WorkspaceState.MediaRemoved, (await sync.StatusAsync(id, home, fx.Ct)).State);
// The home keeps the render and the version.
Assert.FileExists(Path.Combine(fx.Archive.Paths.Root, row.OutputPath));
}
[Test]
public async Task WithoutTheOriginalsTheRenderIsRefusedSayingHowManyLack()
{
await using var bed = await BedAsync(beforeCopy: (fx, sequence) =>
{
File.Delete(Path.Combine(fx.Archive.Paths.Root, "Synthetic", ProcessingFixture.FileName(3)));
File.Delete(Path.Combine(fx.Archive.Paths.Root, "Synthetic", ProcessingFixture.FileName(7)));
});
var fx = bed.Fx;
Assert.Equal(Frames - 2, bed.Project.ReadMedia()!.Frames.Count(f => f.HasOriginal));
var job = await bed.Engine.EnqueueAsync(RenderRequest(bed), cancellationToken: fx.Ct);
Assert.Equal(Loc.L("Errors.Workspace.RenderNoOriginals", 2), (await fx.WaitForAsync(job.Id, JobState.Failed)).Error);
Assert.Empty(bed.Project.ReadRenders().Entries);
Assert.False(Directory.Exists(bed.Project.ExportsDirectory) && Directory.EnumerateFileSystemEntries(bed.Project.ExportsDirectory).Any());
}
[Test]
public async Task ProxiesAloneAreRefused()
{
await using var bed = await BedAsync(BundleMedia.Proxies);
var job = await bed.Engine.EnqueueAsync(RenderRequest(bed), cancellationToken: bed.Fx.Ct);
Assert.Equal(Loc.L("Errors.Workspace.OriginalsMissing"), (await bed.Fx.WaitForAsync(job.Id, JobState.Failed)).Error);
}
[Test]
public async Task ACancelledRenderLeavesNoPartialOutput()
{
await using var bed = await BedAsync();
var fx = bed.Fx;
fx.Decoder.HoldPurpose = DecodePurpose.Render;
fx.Decoder.Hold = Path.GetFileName(bed.Project.ReadMedia()!.Frames[12].Original);
var job = await bed.Engine.EnqueueAsync(RenderRequest(bed), cancellationToken: fx.Ct);
await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct);
await bed.Engine.CancelAsync(job.Id, fx.Ct);
await fx.WaitForAsync(job.Id, JobState.Cancelled);
var until = DateTime.UtcNow.AddSeconds(30);
while (bed.Engine.RunningJobIds.Contains(job.Id))
{
Assert.True(DateTime.UtcNow < until, "the render did not stop");
await Task.Delay(20, fx.Ct);
}
var left = Directory.Exists(bed.Project.ExportsDirectory) ? Directory.GetFileSystemEntries(bed.Project.ExportsDirectory) : [];
Assert.Empty(left);
Assert.Empty(bed.Project.ReadRenders().Entries);
Assert.False((await fx.Db.Jobs.ListAsync(null, 50, null, fx.Ct)).Items.Any(j => j.Kind == JobKind.WorkspaceSend), "nothing to send");
}
}