Files
Chronos/tests/Chronos.Data.Tests/Videos/VideoRenderTests.cs
T
Alby96andClaude Opus 5.5 1be273d0e4 fix: address the phase 5 review
Video trash moves across volumes commit once the verified copy has its
name and leave an undeletable source whole behind a .leftover marker that
the next start settles; deletes rehash under the lease. Restores honour
the names renders are writing, and encoders never overwrite an output.
Routing floors at the stored settings' format, kindless stills read as
stills, star trails restart per loop, and settings the encoder ignored
become render warnings. The Videos section fits beside the player, plays
downloaded server copies, names blocked videos and says why actions are
off; the admin panel offers no download for sequences or missing files
and names a filtered sequence from its record.

Refs #21

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

139 lines
8.6 KiB
C#

using System.Security.Cryptography;
using Chronos.Core.Diagnostics.Synthetic;
using Chronos.Core.Export;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Core.Render;
using Chronos.Core.Video;
using Chronos.Data.Processing;
using Chronos.Data.Tests.Processing;
using Chronos.Testing;
namespace Chronos.Data.Tests.Videos;
/// <summary>Owner review 1, phase 5: renders record their hash and kind, a still job saves one frame, playback routes as format 3.</summary>
[Category("videos")]
public sealed class VideoRenderTests(TestContext context)
{
private const int Frames = 8;
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 readonly PlaybackSettings Boomerang = new() { Direction = PlaybackDirection.Boomerang, Loops = 2 };
private static SyntheticSequence Scene() => SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = Frames, StartEv = 0.5, EndEv = -0.5 });
[Test]
public void ABatchPresetKeepsThePlaybackOfItsExport()
{
var export = Mjpeg with { Playback = Boomerang, Batch = ["gif-preview"] };
Assert.Equal(Boomerang, ProcessingJobs.BatchExport(export, "gif-preview").Playback);
}
[Test]
public void APlaybackJobNeedsFormatThreeAndANodeOfFormatTwoNeverGetsIt()
{
var sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = Guid.CreateVersion7(), Folder = "Synthetic", Title = "Roof" };
static Chronos.Protocol.RenderJobParameters Read(Chronos.Data.Archiving.Jobs.ArchiveJobRequest request) => ProcessingJobs.ReadRender(request.ParametersJson).Parameters;
Assert.Equal(1, Read(ProcessingJobs.Render(sequence, Mjpeg, 96, 64, new ProjectSettings())).RequiredSettingsFormat);
Assert.Equal(ProjectSettings.PlaybackVersion, Read(ProcessingJobs.Render(sequence, Mjpeg with { Playback = Boomerang }, 96, 64, new ProjectSettings())).RequiredSettingsFormat);
Assert.Equal(ProjectSettings.PlaybackVersion, Read(ProcessingJobs.Render(sequence, Mjpeg with { Playback = Boomerang }, 96, 64)).RequiredSettingsFormat,
"without settings too");
// Only a batch preset plays back: the job of that preset needs format 3, the plain one does not.
var split = ProcessingJobs.Split(Read(ProcessingJobs.Render(sequence, Mjpeg with { Batch = ["gif-preview"] }, 96, 64, new ProjectSettings())), 96, 64, new ProjectSettings());
Assert.SequenceEqual([1, 1], split.Select(p => p.RequiredSettingsFormat));
// Routed with settings whose own export plays back, the job's export does not, but the settings text the node reads is
// format 3: a node of format 2 could not parse it. The other way round, the job's export counts.
var plain = Read(ProcessingJobs.Render(sequence, Mjpeg, 96, 64, new ProjectSettings()));
var playing = new ProjectSettings { Export = Mjpeg with { Playback = Boomerang } };
Assert.Equal(ProjectSettings.PlaybackVersion, ProcessingJobs.Routed(plain, playing, 96, 64).RequiredSettingsFormat);
var posted = Read(ProcessingJobs.Render(sequence, Mjpeg with { Playback = Boomerang }, 96, 64, new ProjectSettings())) with { RequiredSettingsFormat = 1 };
Assert.Equal(ProjectSettings.PlaybackVersion, ProcessingJobs.Routed(posted, new ProjectSettings(), 96, 64).RequiredSettingsFormat);
Assert.Equal(ProjectSettings.PlaybackVersion, ProcessingJobs.RequiredFormat(new ProjectSettings(), Mjpeg with { Batch = ["gif-preview"], Playback = Boomerang }, 96, 64));
// A still needs format 3 whatever it renders.
var still = Read(ProcessingJobs.Still(sequence, Mjpeg, VideoCodec.PngSequence, 2, null, 96, 64, new ProjectSettings()));
Assert.Equal((RenderRules.StillFormat, (int?)2, true), (still.RequiredSettingsFormat, still.StillFrame, still.IsStill));
}
[Test]
public async Task AVideoRecordsItsHashAndNoKind()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
await fx.StartEngineAsync();
await fx.WaitForAsync((await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg with { Batch = ["png-sequence"] }, 96, 64))).Id, JobState.Done);
var renders = (await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct)).OrderBy(r => r.OutputPath, StringComparer.Ordinal).ToList();
var avi = Path.Combine(fx.Folder(sequence).Exports, "clip-MJPEG.avi");
Assert.Equal(Convert.ToHexStringLower(SHA256.HashData(await File.ReadAllBytesAsync(avi, fx.Ct))), renders[0].Sha256);
Assert.Null(renders[0].Kind);
Assert.Equal(((string?)null, (string?)null), (renders[1].Sha256, renders[1].Kind));
Assert.Equal(RenderKind.Sequence, RenderKind.Of(renders[1]));
}
[Test]
public async Task AStillJobSavesOneFrameAsAnImageAndRecordsIt()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
await fx.StartEngineAsync();
var request = ProcessingJobs.Still(sequence, Mjpeg with { NameTemplate = "still-{codec}" }, VideoCodec.PngSequence, 2, null, 96, 64);
await fx.WaitForAsync((await fx.EnqueueAsync(request)).Id, JobState.Done);
var exports = fx.Folder(sequence).Exports;
var image = Path.Combine(exports, "still-PNG - 0003.png");
Assert.True(File.Exists(image), string.Join(", ", Directory.EnumerateFileSystemEntries(exports)));
Assert.Equal(1, Directory.EnumerateFileSystemEntries(exports).Count());
using (var decoded = DecoderFactory.CreateServer().Decode(new DecodeRequest(image)))
Assert.Equal((96, 64), (Assert.NotNull(decoded).Pixels.Width, decoded.Pixels.Height));
var render = (await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct)).Single();
Assert.Equal(("Synthetic/exports/still-PNG - 0003.png", RenderKind.Still, 0.0, 0.0), (render.OutputPath, render.Kind, render.Fps, render.DurationSeconds));
Assert.Equal(Convert.ToHexStringLower(SHA256.HashData(await File.ReadAllBytesAsync(image, fx.Ct))), render.Sha256);
// The same frame again: another name, never over the first.
await fx.WaitForAsync((await fx.EnqueueAsync(request)).Id, JobState.Done);
Assert.True(File.Exists(Path.Combine(exports, "still-PNG - 0003 (2).png")));
// A frame outside the sequence fails the job and writes nothing.
var outside = ProcessingJobs.Still(sequence, Mjpeg, VideoCodec.JpegSequence, Frames + 5, null, 96, 64);
await fx.WaitForAsync((await fx.EnqueueAsync(outside)).Id, JobState.Failed);
Assert.Equal(2, Directory.EnumerateFileSystemEntries(exports).Count());
}
[Test]
public void AStillIsFoundByItsPhotoBeforeItsIndex()
{
var sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = Guid.CreateVersion7(), Folder = "S", Title = "S" };
var frames = Enumerable.Range(0, 4).Select(i => new Frame
{
Id = Guid.CreateVersion7(),
SequenceId = sequence.Id,
Index = i,
RelativePath = $"S/{i}.jpg",
FileName = $"{i}.jpg",
Sha256 = new string((char)('a' + i), 64),
}).ToList();
var set = new SequenceFrames(sequence, frames, frames.Select(f => (string?)f.RelativePath).ToList());
Assert.Equal(3, SequenceRender.StillIndex(set, 1, new string('d', 64)));
Assert.Equal(3, SequenceRender.StillIndex(set, 1, new string('D', 64)), "any case");
Assert.Null(SequenceRender.StillIndex(set, 1, new string('z', 64)), "a photo no longer there: its index names another photo now");
Assert.Equal(1, SequenceRender.StillIndex(set, 1, null), "no hash: the index");
Assert.Null(SequenceRender.StillIndex(set, 9, null));
// Named by the export's template and the frame number from 1; a taken name gets " (2)".
var folder = context.CreateTempDirectory("still");
var name = new ExportNameContext { Date = new DateTime(2026, 9, 28), Title = "Harbour", PresetName = "PNG", Codec = VideoCodec.PngSequence };
var export = new ExportSettings { NameTemplate = "{title} - {codec}" };
Assert.Equal(Path.Combine(folder, "Harbour - PNG - 0042.png"), SequenceRender.StillPath(export, folder, name, 41));
Assert.Equal(Path.Combine(folder, "Harbour - PNG - 0042 (2).png"), SequenceRender.StillPath(export, folder, name, 41, p => p.EndsWith("0042.png", StringComparison.Ordinal)));
}
}