Files
Chronos/tests/Chronos.Data.Tests/Processing/RenderJobTests.cs
T
Alby96andClaude Opus 5.5 7e4a3b481d
CI / Build and test (push) Successful in 9m15s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 15m35s
CI / PostgreSQL 16 integration (push) Successful in 7m26s
CI / PostgreSQL 17 integration (push) Successful in 6m47s
CI / Docker image (push) Successful in 59s
Release / Server image (push) Successful in 1m24s
feat: owner review 2 phase 4a: Reorganize sequences after import (#12)
- Reorganize… on the web panel and the desktop: folder, file and title templates applied to existing sequences, with a dry run and an explicit Apply that runs as a job.
- One naming rule (FrameNaming) for import, upload and Reorganize.
- Crash-safe plan and file journals, held plans, stale-plan refusal; exports move with their folder.
- Writers refuse a folder another program moved or gave to another sequence.
- Server: POST /api/v1/archive/reorganize with a bounded plan cache; template rules.
- Phase 3 docs and translations merged; strings in all 38 languages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-27 21:58:39 +01:00

571 lines
34 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Text;
using Chronos.Core.Diagnostics.Synthetic;
using Chronos.Core.Export;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Core.Video;
using Chronos.Data.Jobs;
using Chronos.Data.Processing;
using Chronos.Data.Tests.Archiving;
using Chronos.Testing;
namespace Chronos.Data.Tests.Processing;
[Category("processing")]
public sealed class RenderJobTests(TestContext context)
{
private const int Frames = 24;
/// <summary>MJPEG at the source size, named by codec so the tests know the file names.</summary>
private static readonly ExportSettings Mjpeg = new()
{
Preset = new Auto<string>().WithManual("mjpeg-avi"),
Overrides = new ExportOverrides { Width = 96, Height = 64 },
NameTemplate = "clip-{codec}",
};
/// <summary>HEVC (a render node's codec) at the size of <see cref="Mjpeg"/>.</summary>
private static readonly ExportSettings Hevc = Mjpeg with { Preset = new Auto<string>().WithManual("4k-hevc") };
private static SyntheticSequence Scene() => SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = Frames, StartEv = 0.5, EndEv = -0.5 });
[Test]
public async Task ABatchWritesPlayableManagedOutputsIntoExportsAndRecordsThem()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
await fx.StartEngineAsync();
var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg with { Batch = ["png-sequence", "gif-preview"] }, 96, 64));
await fx.WaitForAsync(job.Id, JobState.Done);
var exports = fx.Folder(sequence).Exports;
var avi = await File.ReadAllBytesAsync(Path.Combine(exports, "clip-MJPEG.avi"), fx.Ct);
Assert.Equal(("RIFF", "AVI "), (Encoding.ASCII.GetString(avi, 0, 4), Encoding.ASCII.GetString(avi, 8, 4)));
Assert.Contains("idx1", Encoding.ASCII.GetString(avi));
var pngs = Directory.GetFiles(Path.Combine(exports, "clip-PNG")).Order().ToList();
Assert.Equal(Frames, pngs.Count);
Assert.Equal("clip-PNG_000001.png", Path.GetFileName(pngs[0]));
using (var decoded = DecoderFactory.CreateServer().Decode(new DecodeRequest(pngs[^1])))
Assert.Equal((96, 64), (Assert.NotNull(decoded).Pixels.Width, decoded.Pixels.Height));
var gif = await File.ReadAllBytesAsync(Path.Combine(exports, "clip-GIF.gif"), fx.Ct);
Assert.Equal("GIF89a", Encoding.ASCII.GetString(gif, 0, 6));
Assert.Empty(Directory.EnumerateFileSystemEntries(exports, ".*"));
var renders = (await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct)).OrderBy(r => r.OutputPath, StringComparer.Ordinal).ToList();
Assert.SequenceEqual(["Synthetic/exports/clip-GIF.gif", "Synthetic/exports/clip-MJPEG.avi", "Synthetic/exports/clip-PNG"], renders.Select(r => r.OutputPath));
var mjpeg = renders[1];
Assert.Equal((Loc.L("Export.Preset.Custom"), "Mjpeg", 96, 64, "test-pc", (long)avi.Length),
(mjpeg.PresetName, mjpeg.Codec, mjpeg.Width, mjpeg.Height, mjpeg.ProducedBy, mjpeg.Bytes));
Assert.Equal(Loc.L("Export.Preset.Gif"), renders[0].PresetName);
Assert.Equal(3, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.RenderedCount);
}
[Test]
public async Task FramesThisHostCannotDecodeRenderFromTheirProxies()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
var folder = fx.Folder(sequence);
Directory.CreateDirectory(folder.Proxies);
foreach (var frame in await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))
{
// The originals are HEIF this host cannot read (BRIEF §2.3): only the proxies a client sent have pixels here.
var original = fx.Archive.Paths.FromRelative(frame.RelativePath);
File.Copy(original, folder.Proxy(frame.Index));
await File.WriteAllBytesAsync(original, [0, 0, 0, 24, .. "ftypheic"u8, 0, 0, 0, 0, .. "mif1heic"u8], fx.Ct);
}
await using (var session = await fx.Db.Database.OpenAsync(fx.Ct))
await session.ExecuteAsync("UPDATE frames SET format = $1 WHERE sequence_id = $2", [(int)ImageFileFormat.Heif, sequence.Id], fx.Ct);
await fx.StartEngineAsync();
// The job does not ask for proxies: the host takes them because it cannot decode the originals.
var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
await fx.WaitForAsync(job.Id, JobState.Done);
Assert.FileExists(Path.Combine(folder.Exports, "clip-MJPEG.avi"));
var decoded = fx.Decoder.Requests.Where(r => r.Purpose == DecodePurpose.Render).Select(r => Path.GetDirectoryName(r.Path)).Distinct().ToList();
Assert.SequenceEqual([folder.Proxies], decoded);
}
[Test]
public async Task ACancelledRenderLeavesNothingBehind()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
fx.Decoder.HoldFrame(12, DecodePurpose.Render);
await fx.StartEngineAsync();
var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct);
await fx.Engine.CancelAsync(job.Id, fx.Ct);
await fx.WaitForAsync(job.Id, JobState.Cancelled);
await WaitUntilIdleAsync(fx, job.Id);
Assert.Empty(Entries(fx.Folder(sequence).Exports));
Assert.Empty(await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct));
}
[Test]
public async Task AFailedRenderLeavesNothingBehindAndSaysWhy()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
// Every file damaged after indexing: nothing flags them, and no frame can stand in for another.
for (var i = 0; i < Frames; i++)
await File.WriteAllBytesAsync(Path.Combine(fx.Archive.Paths.Root, "Synthetic", ProcessingFixture.FileName(i)), new byte[5000], fx.Ct);
await fx.StartEngineAsync();
var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
var failed = await fx.WaitForAsync(job.Id, JobState.Failed);
Assert.Equal(Loc.L(SequenceFrameSource.FrameUnreadableKey, "1", ProcessingFixture.FileName(0)), failed.Error);
Assert.Empty(Entries(fx.Folder(sequence).Exports));
Assert.Empty(await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct));
}
[Test]
public async Task ARenderOfAFolderAnotherIndexMovedWaitsForThisIndexsScan()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
var left = fx.Folder(sequence).Folder;
var (server, archive) = await fx.Archive.OtherWriterAsync();
var plan = await server.PlanRenameAsync(archive.Id, [sequence.Id], "Moved/{date}", null, fx.Ct);
await server.ApplyRenameAsync(plan, null, fx.Ct);
await fx.StartEngineAsync();
// A server sharing the archive moved the folder; this index has not scanned since. Nothing renders, nothing is flagged.
var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
var failed = await fx.WaitForAsync(job.Id, JobState.Failed);
Assert.Equal(Loc.L("Errors.Archive.FolderMoved"), failed.Error);
Assert.False(Directory.Exists(left), "the folder it left is not created again");
Assert.Empty(await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct));
Assert.True((await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct)).All(f => (f.Flags & (FrameFlags.Missing | FrameFlags.Undecodable)) == 0), "no frame flagged");
// Once this index's scan found it there, the render goes into its exports.
await fx.Archive.ScanAsync();
var again = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
await fx.WaitForAsync(again.Id, JobState.Done);
var moved = fx.Archive.Paths.Sequence(plan.Steps.Single().ToFolder);
Assert.FileExists(Path.Combine(moved.Exports, "clip-MJPEG.avi"));
Assert.False(Directory.Exists(left));
}
[Test]
public async Task ARenderNeverWritesIntoAnotherSequenceMovedIntoItsFolder()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
var stranger = await fx.AddAsync(Scene(), "Other");
var (server, archive) = await fx.Archive.OtherWriterAsync();
await server.ApplyRenameAsync(await server.PlanRenameAsync(archive.Id, [sequence.Id], "Moved/{date}", null, fx.Ct), null, fx.Ct);
await server.ApplyRenameAsync(await server.PlanRenameAsync(archive.Id, [stranger.Id], sequence.Folder, null, fx.Ct), null, fx.Ct);
var folder = fx.Folder(sequence).Folder;
var before = ArchiveFixture.FilesOf(folder);
await fx.StartEngineAsync();
// A server sharing the archive moved the folder away and the other sequence into it; this index has not scanned since.
var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
var failed = await fx.WaitForAsync(job.Id, JobState.Failed);
Assert.Equal(Loc.L("Errors.Archive.FolderMoved"), failed.Error);
Assert.SequenceEqual(before, ArchiveFixture.FilesOf(folder), "the other sequence's files are untouched");
Assert.Empty(await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct));
}
[Test]
public async Task AFrameThatNoLongerDecodesIsReplacedByItsNeighbourAndLeftOutNextTime()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
// Damaged after indexing: nothing flags the frame, so only decoding finds out (BRIEF §8.2 tier 3).
await File.WriteAllBytesAsync(Path.Combine(fx.Archive.Paths.Root, "Synthetic", ProcessingFixture.FileName(10)), new byte[5000], fx.Ct);
await fx.StartEngineAsync();
var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
var done = await fx.WaitForAsync(job.Id, JobState.Done);
Assert.Contains(Loc.L("Export.Log.StandIns", "11"), done.LogTail);
Assert.SequenceEqual(["clip-MJPEG.avi"], Entries(fx.Folder(sequence).Exports));
var row = (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[10];
Assert.Equal(FrameFlags.Undecodable, row.Flags);
var sidecar = Chronos.Core.Archiving.ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath);
Assert.Equal((DecodeTier.Unavailable, FrameFlags.Undecodable), (sidecar.Frames[10].Tier, sidecar.Frames[10].Flags));
Assert.Equal(DecodeTier.Native, sidecar.Frames[9].Tier);
Assert.True(sidecar.Status.HasFlag(SequenceStatus.HasWarnings), "the sequence warns at once");
Assert.True((await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.Status.HasFlag(SequenceStatus.HasWarnings), "index status");
// The next render plans without the frame, so nothing has to stand in for it.
var again = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
var second = await fx.WaitForAsync(again.Id, JobState.Done);
Assert.False(second.LogTail?.Contains(Loc.L("Export.Log.StandIns", "11"), StringComparison.Ordinal) == true, "no stand-in");
Assert.Count(2, await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct));
}
[Test]
public async Task AFrameThatCannotBeReadIsShownAsItsNeighbourButNeverFlagged()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
// Another program holds the file during the render (antivirus, backup): a read error says nothing about the file.
fx.Decoder.Unreadable[ProcessingFixture.FileName(10)] = 0;
await fx.StartEngineAsync();
var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
var done = await fx.WaitForAsync(job.Id, JobState.Done);
Assert.Contains(Loc.L("Export.Log.StandIns", "11"), done.LogTail);
var sidecar = Chronos.Core.Archiving.ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath);
Assert.Equal(FrameFlags.None, sidecar.Frames[10].Flags & FrameFlags.Undecodable);
Assert.NotEqual(DecodeTier.Unavailable, sidecar.Frames[10].Tier);
Assert.False(sidecar.Status.HasFlag(SequenceStatus.HasWarnings), "no warning");
Assert.Equal(FrameFlags.None, (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[10].Flags);
}
[Test]
public async Task ASmallRenderKeepsTheStoredSizeOfTheLargestPreview()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
// A CR3 on the server: the probe finds its full-size 6000×4000 preview, which caps the output.
for (var i = 0; i < Frames; i++)
fx.Decoder.Tiers[ProcessingFixture.FileName(i)] = new FileTier(ImageFileFormat.Jpeg, DecodeTier.EmbeddedPreview, "test-preview", "test", 6000, 4000);
await fx.StartEngineAsync();
var light = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id]));
await fx.WaitForAsync(light.Id, JobState.Done);
var probed = Chronos.Core.Archiving.ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath).Frames[3];
Assert.Equal((FrameFlags.PreviewOnly, (int?)6000, (int?)4000), (probed.Flags, probed.PreviewW, probed.PreviewH));
// A small render decodes the smallest preview that reaches its size (1620×1080): that is not the largest.
for (var i = 0; i < Frames; i++) fx.Decoder.PreviewDecodes[ProcessingFixture.FileName(i)] = (1620, 1080);
var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
await fx.WaitForAsync(job.Id, JobState.Done);
var frames = Chronos.Core.Archiving.ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath).Frames;
Assert.True(frames.All(f => f is { Tier: DecodeTier.EmbeddedPreview, PreviewW: 6000, PreviewH: 4000 }), "sidecar preview sizes kept");
var rows = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct);
Assert.True(rows.All(f => (f.PreviewWidth, f.PreviewHeight) == (6000, 4000)), "index preview sizes kept");
Assert.Equal((6000, 4000), (Chronos.Core.Advice.SequenceContext.FromFrames(rows).PreviewWidth, Chronos.Core.Advice.SequenceContext.FromFrames(rows).PreviewHeight));
}
[Test]
public void FramesARenderNodeReceivesKeepTheirTierFields()
{
var dto = new Chronos.Protocol.FrameDto(Guid.CreateVersion7(), Guid.CreateVersion7(), 3, "IMG_0004.CR3", 25_000_000, new string('a', 64),
new DateTime(2026, 9, 28, 18, 0, 20), 120, 1.0 / 250, 8, 100, 24, 6, 6000, 4000, "cr3",
(long)(FrameFlags.PreviewOnly | FrameFlags.Intervalometer), 1620, 1080);
var frame = RenderNodeClient.ToFrame(dto);
Assert.Equal((ImageFileFormat.Cr3, FrameFlags.PreviewOnly | FrameFlags.Intervalometer, 1620, 1080),
(frame.Format, frame.Flags, frame.PreviewWidth, frame.PreviewHeight));
// The node's advisor caps a portrait sequence at its previews, oriented like the main image.
var tiers = Chronos.Core.Advice.SequenceContext.FromFrames([frame]);
Assert.Equal((1, 1080, 1620), (tiers.PreviewOnlyCount, tiers.PreviewWidth, tiers.PreviewHeight));
}
[Test]
public async Task OnlyFramesTheRenderShowsGetAStandIn()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
foreach (var i in new[] { 1, 3 })
await File.WriteAllBytesAsync(Path.Combine(fx.Archive.Paths.Root, "Synthetic", ProcessingFixture.FileName(i)), new byte[5000], fx.Ct);
var decoder = new ImageDecoder([fx.Decoder]);
var frames = await SequenceFrames.LoadAsync(fx.Db, fx.Archive.Archive, sequence, useProxies: false, decoder, fx.Ct);
// A plan that skips the odd frames (a speed-up): the read-ahead meets frames 1 and 3, the pipeline never asks.
var source = new SequenceFrameSource(frames, frames.LightFrames(), decoder, new FrameWindowOptions { Ahead = 4 })
{
Originals = true,
RenderExcluded = new HashSet<int>(),
};
await using (source)
{
foreach (var index in new[] { 0, 2, 4, 6 })
{
using var frame = await source.GetFrameAsync(index, 64, DecodePurpose.Render, fx.Ct);
Assert.Equal(64, frame.Width);
}
// The read-ahead decodes in the background: wait until it has met frame 4 (index 3).
for (var waited = 0; !source.Observed.ContainsKey(3) && waited < 200; waited++) await Task.Delay(50, fx.Ct);
}
Assert.Equal(DecodeTier.Unavailable, source.Observed[3].Tier);
Assert.Empty(source.StandIns);
// Asked for, a damaged frame shows its neighbour and is listed.
await using var asked = new SequenceFrameSource(frames, frames.LightFrames(), new ImageDecoder([fx.Decoder]))
{
RenderExcluded = new HashSet<int>(),
};
using (var standIn = await asked.GetFrameAsync(3, 64, DecodePurpose.Render, fx.Ct))
Assert.Equal(64, standIn.Width);
Assert.SequenceEqual([3], asked.StandIns);
}
[Test]
public async Task ARenderInterruptedByACrashStartsItsOutputAgainCleanly()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
var exports = fx.Folder(sequence).Exports;
var output = Path.Combine(exports, "clip-MJPEG.avi");
// The crashed attempt left its encoder's temporary file and a finished output it never recorded.
Directory.CreateDirectory(exports);
await File.WriteAllBytesAsync(Path.Combine(exports, ".clip-MJPEG.avi.0badf00d.tmp"), new byte[100], fx.Ct);
await File.WriteAllBytesAsync(output, new byte[100], fx.Ct);
await fx.StartEngineAsync();
var job = await fx.Archive.EnqueueWithCheckpointAsync(fx.Engine, ProcessingJobs.Render(sequence, Mjpeg with { Batch = ["gif-preview"] }, 96, 64),
new RenderCheckpoint([], 0, output).ToJson());
await fx.WaitForAsync(job.Id, JobState.Done);
Assert.SequenceEqual(["clip-GIF.gif", "clip-MJPEG.avi"], Entries(exports));
Assert.Equal("RIFF", Encoding.ASCII.GetString((await File.ReadAllBytesAsync(output, fx.Ct))[..4]));
Assert.Count(2, await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct));
}
[Test]
public async Task ARenderInterruptedAfterRecordingAnOutputResumesWithTheNextOne()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
var exports = fx.Folder(sequence).Exports;
var output = Path.Combine(exports, "clip-MJPEG.avi");
Directory.CreateDirectory(exports);
var recorded = Encoding.ASCII.GetBytes("RIFF recorded before the crash");
await File.WriteAllBytesAsync(output, recorded, fx.Ct);
// The crash came after the job recorded its first entry and before its checkpoint said so.
var jobId = Guid.CreateVersion7();
await fx.Db.Renders.InsertAsync(RecordedRender(sequence, Chronos.Data.Processing.Jobs.RenderJobHandler.RenderId(jobId, 0)), fx.Ct);
await fx.StartEngineAsync();
var job = await fx.Archive.EnqueueWithCheckpointAsync(fx.Engine, ProcessingJobs.Render(sequence, Mjpeg with { Batch = ["gif-preview"] }, 96, 64),
new RenderCheckpoint([], 0, output).ToJson(), jobId);
await fx.WaitForAsync(job.Id, JobState.Done);
var kept = await File.ReadAllBytesAsync(output, fx.Ct);
Assert.BytesEqual(recorded, kept);
Assert.SequenceEqual(["clip-GIF.gif", "clip-MJPEG.avi"], Entries(exports));
Assert.Count(2, await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct));
}
[Test]
public async Task ARenderResumedAfterAnotherRecordedItsNameKeepsThatOutput()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
var exports = fx.Folder(sequence).Exports;
var output = Path.Combine(exports, "clip-MJPEG.avi");
Directory.CreateDirectory(exports);
// While this job waited to resume, another render of the same preset finished under the name it had chosen.
var other = Encoding.ASCII.GetBytes("RIFF another render's output");
await File.WriteAllBytesAsync(output, other, fx.Ct);
await fx.Db.Renders.InsertAsync(RecordedRender(sequence, Guid.CreateVersion7()), fx.Ct);
await fx.StartEngineAsync();
var job = await fx.Archive.EnqueueWithCheckpointAsync(fx.Engine, ProcessingJobs.Render(sequence, Mjpeg, 96, 64), new RenderCheckpoint([], 0, output).ToJson());
await fx.WaitForAsync(job.Id, JobState.Done);
var kept = await File.ReadAllBytesAsync(output, fx.Ct);
Assert.BytesEqual(other, kept);
Assert.SequenceEqual(["clip-MJPEG (2).avi", "clip-MJPEG.avi"], Entries(exports));
Assert.Count(2, await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct));
}
[Test]
public async Task ARenderResumedAfterItsFolderMovedNeverDeletesWhatAnotherSequenceBroughtIntoIt()
{
// The crashed attempt's checkpoint names the folder it chose the output in, or (written by an older version) does not.
foreach (var named in new[] { true, false })
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
var stranger = await fx.AddAsync(Scene(), "Other");
var output = Path.Combine(fx.Folder(sequence).Exports, "clip-MJPEG.avi");
// The crashed attempt left its encoder's temporary file and a finished output it never recorded; the other
// sequence has an export of that name, and a hidden file that looks like such a leftover.
Directory.CreateDirectory(fx.Folder(sequence).Exports);
await File.WriteAllBytesAsync(Path.Combine(fx.Folder(sequence).Exports, ".clip-MJPEG.avi.0badf00d.tmp"), new byte[100], fx.Ct);
await File.WriteAllBytesAsync(output, new byte[100], fx.Ct);
Directory.CreateDirectory(fx.Folder(stranger).Exports);
await File.WriteAllBytesAsync(Path.Combine(fx.Folder(stranger).Exports, ".clip-MJPEG.avi.0ddba11.tmp"), new byte[50], fx.Ct);
await File.WriteAllBytesAsync(Path.Combine(fx.Folder(stranger).Exports, "clip-MJPEG.avi"), Encoding.ASCII.GetBytes("RIFF theirs"), fx.Ct);
// Another index moves the folder away, the leftovers along, then the other sequence into the folder it left; this
// index's scan finds both before the job resumes.
var (server, archive) = await fx.Archive.OtherWriterAsync();
var plan = await server.PlanRenameAsync(archive.Id, [sequence.Id], "Moved/{date}", null, fx.Ct);
await server.ApplyRenameAsync(plan, null, fx.Ct);
await server.ApplyRenameAsync(await server.PlanRenameAsync(archive.Id, [stranger.Id], sequence.Folder, null, fx.Ct), null, fx.Ct);
await fx.Archive.ScanAsync();
var before = ArchiveFixture.FilesOf(fx.Folder(sequence).Folder);
await fx.StartEngineAsync();
var job = await fx.Archive.EnqueueWithCheckpointAsync(fx.Engine, ProcessingJobs.Render(sequence, Mjpeg, 96, 64),
new RenderCheckpoint([], 0, output, named ? sequence.Folder : null).ToJson());
await fx.WaitForAsync(job.Id, JobState.Done);
Assert.SequenceEqual(before, ArchiveFixture.FilesOf(fx.Folder(sequence).Folder), $"{named}: the other sequence's files are untouched");
// Its leftovers are removed where the folder took them when the checkpoint says where it chose the output, and
// stay otherwise: the render then takes another name.
var moved = fx.Archive.Paths.Sequence(plan.Steps.Single().ToFolder).Exports;
Assert.SequenceEqual(named ? ["clip-MJPEG.avi"] : [".clip-MJPEG.avi.0badf00d.tmp", "clip-MJPEG (2).avi", "clip-MJPEG.avi"], Entries(moved), named.ToString());
var rendered = (await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct)).Single().OutputPath;
Assert.Equal(plan.Steps.Single().ToFolder + (named ? "/exports/clip-MJPEG.avi" : "/exports/clip-MJPEG (2).avi"), rendered);
Assert.Equal("RIFF", Encoding.ASCII.GetString((await File.ReadAllBytesAsync(fx.Archive.Paths.FromRelative(rendered), fx.Ct))[..4]));
}
}
[Test]
public async Task ConcurrentRendersOfOnePresetKeepBothOutputs()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
await fx.StartEngineAsync(workers: 2);
var first = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
var second = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
await fx.WaitForAsync(first.Id, JobState.Done);
await fx.WaitForAsync(second.Id, JobState.Done);
Assert.SequenceEqual(["clip-MJPEG (2).avi", "clip-MJPEG.avi"], Entries(fx.Folder(sequence).Exports));
var renders = await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct);
Assert.Equal(2, renders.Select(r => r.OutputPath).Distinct().Count());
}
[Test]
public void ANameBeingWrittenIsNotPickedAgain()
{
var folder = context.CreateTempDirectory("exports");
var name = new ExportNameContext { Date = new DateTime(2026, 6, 1), Title = "Roof", PresetName = "MJPEG", Codec = VideoCodec.Mjpeg };
using var a = new OutputNames();
using var b = new OutputNames();
var first = a.Reserve(taken => ExportNaming.OutputPath(folder, "{title}", name, taken));
var second = b.Reserve(taken => ExportNaming.OutputPath(folder, "{title}", name, taken));
Assert.Equal((Path.Combine(folder, "Roof.avi"), Path.Combine(folder, "Roof (2).avi")), (first, second));
Assert.False(b.TryReserve(first), "held by the first render");
a.Dispose();
using var c = new OutputNames();
Assert.Equal(first, c.Reserve(taken => ExportNaming.OutputPath(folder, "{title}", name, taken)));
}
[Test]
public void RenderRowIdsAreStablePerJobAndEntry()
{
var job = Guid.CreateVersion7();
var id = Chronos.Data.Processing.Jobs.RenderJobHandler.RenderId(job, 0);
Assert.Equal(id, Chronos.Data.Processing.Jobs.RenderJobHandler.RenderId(job, 0));
Assert.NotEqual(id, Chronos.Data.Processing.Jobs.RenderJobHandler.RenderId(job, 1));
Assert.NotEqual(id, Chronos.Data.Processing.Jobs.RenderJobHandler.RenderId(Guid.CreateVersion7(), 0));
Assert.Equal(8, id.Version);
}
[Test]
public async Task AServerTakesOnlyTheRendersItsManagedEncodersProduce()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var server = new ProcessingService(fx.Db, new ProcessingOptions { Host = EncoderHost.Server, AwaitsRenderNode = (_, _, _) => Task.FromResult(true) });
var handler = Assert.NotNull(server.CreateJobHandlers().Single(h => h.Kind == JobKind.Render) as ISelectiveJobHandler);
var sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = Guid.CreateVersion7(), Folder = "Synthetic" };
Job Job(ExportSettings export) => new() { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = ProcessingJobs.Render(sequence, export, 96, 64).ParametersJson };
async Task<bool> AcceptsAsync(string? parametersJson) =>
await handler.AcceptsAsync(new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = parametersJson }, fx.Ct);
Assert.True(await handler.AcceptsAsync(Job(Mjpeg with { Batch = ["png-sequence", "gif-preview"] }), fx.Ct), "managed outputs only");
Assert.False(await handler.AcceptsAsync(Job(Hevc), fx.Ct), "HEVC waits for a render node");
Assert.False(await handler.AcceptsAsync(Job(Mjpeg with { Batch = ["1080p-h264"] }), fx.Ct), "one MP4 in the batch");
Assert.True(await handler.AcceptsAsync(Job(Hevc) with { Checkpoint = """{"done":[],"pending":0}""" }, fx.Ct), "started here: resumes here");
Assert.True(await AcceptsAsync("{}"), "unreadable: fails with its reason");
Assert.True(await AcceptsAsync(WithPreset("""{"preset":{},"Preset":{}}""")), "a member given twice: fails with its reason");
Assert.True(await AcceptsAsync(WithPreset("""{"preset":{"isAuto":false,"manual":"mjpeg-avi"},"batch":null}""")), "a null batch is no batch");
var client = new ProcessingService(fx.Db).CreateJobHandlers().Single(h => h.Kind == JobKind.Render);
Assert.False(client is ISelectiveJobHandler, "a client renders whatever it is given");
}
[Test]
public async Task AServerTakesMp4JobsOnceNoRenderNodeCanAndOnlyThen()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = Guid.CreateVersion7(), Folder = "Synthetic" };
var job = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = ProcessingJobs.Render(sequence, Hevc, 96, 64).ParametersJson };
async Task<bool> AcceptsAsync(Func<Job, Chronos.Protocol.RenderJobParameters, CancellationToken, Task<bool>>? awaits)
{
var server = new ProcessingService(fx.Db, new ProcessingOptions { Host = EncoderHost.Server, AwaitsRenderNode = awaits });
return await ((ISelectiveJobHandler)server.CreateJobHandlers().Single(h => h.Kind == JobKind.Render)).AcceptsAsync(job, fx.Ct);
}
Chronos.Protocol.RenderJobParameters? asked = null;
Assert.True(await AcceptsAsync((_, p, _) => { asked = p; return Task.FromResult(false); }), "no node can take it");
Assert.Equal(("hevc", 96, 64), (asked!.Codec, asked.Width, asked.Height));
Assert.False(await AcceptsAsync(null), "without a node check, MP4 jobs always wait");
Assert.False(await AcceptsAsync((_, _, _) => throw new InvalidOperationException("database unreachable")), "unknown: keeps waiting");
}
[Test]
public async Task AServerRendersAsMjpegWhatNoRenderNodeTookAndSaysSo()
{
await using var fx = await ProcessingFixture.CreateAsync(context, o => o with { Host = EncoderHost.Server, AwaitsRenderNode = (_, _, _) => Task.FromResult(false) });
var sequence = await fx.AddAsync(Scene());
await fx.StartEngineAsync();
var request = ProcessingJobs.Render(sequence, Hevc, 96, 64);
var job = await fx.WaitForAsync((await fx.EnqueueAsync(request)).Id, JobState.Done);
var avi = await File.ReadAllBytesAsync(Path.Combine(fx.Folder(sequence).Exports, "clip-MJPEG.avi"), fx.Ct);
Assert.Equal(("RIFF", "AVI "), (Encoding.ASCII.GetString(avi, 0, 4), Encoding.ASCII.GetString(avi, 8, 4)));
var render = (await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct)).Single();
var preset = Loc.L("Export.Preset.Custom");
Assert.Equal((preset, "Mjpeg", 96, 64), (render.PresetName, render.Codec, render.Width, render.Height));
Assert.Contains(Loc.L("Export.Log.NoRenderNode", preset, "HEVC", "MJPEG"), job.LogTail);
}
[Test]
public void ABatchForAServerBecomesOneJobPerPreset()
{
var sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = Guid.CreateVersion7(), Folder = "Synthetic", Title = "Roof" };
var jobs = ProcessingJobs.RenderEach(sequence, Mjpeg with { Batch = ["mjpeg-avi", "gif-preview", "GIF-preview", "4k-hevc"] }, 96, 64);
var parameters = jobs.Select(j => System.Text.Json.JsonSerializer.Deserialize(j.ParametersJson!, Chronos.Protocol.ProtocolJson.Default.RenderJobParameters)!).ToList();
Assert.SequenceEqual(["mjpeg", "gif", "hevc"], parameters.Select(p => p.Codec));
Assert.True(jobs.All(j => j.Kind == JobKind.Render && j.Targets.SequenceEqual([sequence.Id])), "render jobs of the sequence");
Assert.True(parameters.All(p => ProcessingJobs.ReadExport(p.PresetJson).Batch.Count == 0), "no job carries a batch");
Assert.Equal((3840, 2160), (parameters[2].Width, parameters[2].Height));
// A posted batch written by hand may hold blank entries: they name no preset.
var posted = System.Text.Json.JsonSerializer.Deserialize(WithPreset("""{"preset":{"isAuto":false,"manual":"mjpeg-avi"},"batch":[null," ","gif-preview"]}"""),
Chronos.Protocol.ProtocolJson.Default.RenderJobParameters)!;
Assert.SequenceEqual(["mjpeg", "gif"], ProcessingJobs.Split(posted, 96, 64).Select(p => p.Codec));
}
/// <summary>MJPEG job parameters whose preset JSON is <paramref name="presetJson"/>.</summary>
private static string WithPreset(string presetJson) => System.Text.Json.JsonSerializer.Serialize(
new Chronos.Protocol.RenderJobParameters("MJPEG", presetJson, "mjpeg", 96, 64, false, null), Chronos.Protocol.ProtocolJson.Default.RenderJobParameters);
private static Render RecordedRender(Sequence sequence, Guid id) => new()
{
Id = id,
SequenceId = sequence.Id,
PresetName = "MJPEG",
OutputPath = "Synthetic/exports/clip-MJPEG.avi",
Created = DateTimeOffset.UtcNow,
};
private static List<string> Entries(string folder) =>
Directory.Exists(folder) ? [.. Directory.EnumerateFileSystemEntries(folder).Select(e => Path.GetFileName(e)).Order(StringComparer.Ordinal)] : [];
/// <summary>The engine marks a job cancelled before its handler has unwound; wait until the worker let it go.</summary>
private static async Task WaitUntilIdleAsync(ProcessingFixture fx, Guid jobId)
{
var until = DateTime.UtcNow.AddSeconds(30);
while (fx.Engine.RunningJobIds.Contains(jobId))
{
Assert.True(DateTime.UtcNow < until, "the render did not stop");
await Task.Delay(20, fx.Ct);
}
}
}