Files
Chronos/tests/Chronos.Data.Tests/Workspaces/WorkspaceRenderTests.cs
T
Alby96andClaude Opus 5.5 59e7aca006 feat(workspace): render a project on this PC from its originals
A workspace project that holds its originals now renders locally
(JobKind.WorkspaceRender), so the full-resolution render no longer
waits on the home. This holds also when the home is a server.

- The handler reads its inputs under the project's lease. It refuses
  proxies-only media, and it refuses frames without an original,
  saying how many lack one. It renders into exports/ with unique
  names and a resumable checkpoint.
- Each output gets a ledger entry: SHA-256, the render id from
  RenderJobHandler.RenderId, and the version, recorded only when
  versions/ holds it and its hash verifies.
- After rendering, the handler queues a WorkspaceSend follow-up for
  the outputs it recorded. A cancel leaves no partial file.
- The editor routes Originals media to the local engine even for a
  server home, and lists those jobs beside the server's. It treats
  WorkspaceRender like Render for the toast and the reload, says
  "Sent home" when a send ends, and shows unsent ledger renders as
  "Not sent".
- Version on render saves a changed project as a version noted
  "Render" before it renders, and sends it with Sync automatically.
  With the setting off, the render behaves as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 16:03:05 +01:00

190 lines
10 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");
Assert.Empty(await fx.Db.Renders.ListBySequenceAsync(bed.Sequence.Id, fx.Ct));
// The send home follows with the new render.
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));
Assert.SequenceEqual([entry.RenderId], sent.RenderIds);
Assert.Equal((bed.Project.ProjectId, bed.Store.Root), (sent.ProjectId, sent.WorkspaceRoot));
// 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 = (await fx.Db.Renders.ListBySequenceAsync(bed.Sequence.Id, fx.Ct)).Single();
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);
}
[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");
}
}