Files
Alby96andClaude Opus 5.5 728ffa35cb
CI / Build and test (push) Successful in 9m41s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 9s
Release / Build, test and publish (push) Successful in 14m49s
CI / PostgreSQL 16 integration (push) Successful in 8m51s
CI / PostgreSQL 17 integration (push) Successful in 9m48s
CI / Docker image (push) Successful in 54s
Release / Server image (push) Successful in 1m39s
feat: export panel and Render menu, playback options, videos library (owner review 1 phase 5) (#27)
Owner review 1 R3, R8, R9. A grouped Export step and Render menu; encoding, playback (reverse, boomerang, loops, fades, hold) and still frames; one videos library on the desktop and the server with a verified trash and copies to a videos folder.

Closes #21

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-05 09:18:40 +01:00

130 lines
7.7 KiB
C#

using System.Security.Cryptography;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Videos;
using Chronos.Testing;
namespace Chronos.Data.Tests.Videos;
/// <summary>Owner review 1, R9, decision 16: a VideoCopy job copies renders into the videos folder, verified, never overwriting.</summary>
[Category("videos")]
public sealed class VideoCopyTests(TestContext context)
{
[Test]
public async Task ACopyIsVerifiedAndRecordedAndATakenNameGetsANumber()
{
await using var fx = await VideoFixture.CreateAsync(context);
var sequence = await fx.SequenceAsync();
var render = await fx.RenderAsync(sequence, "sunset.mp4", 5000);
var old = await fx.RenderAsync(sequence, "old.mp4", 3000, hashed: false);
await File.WriteAllTextAsync(Path.Combine(fx.VideosFolder, "sunset.mp4"), "someone else's", fx.Ct);
await fx.StartEngineAsync();
var request = fx.Library.CopyJob(new VideoCopyParameters([render.Id, old.Id, render.Id], fx.VideosFolder, RequestedBy: "alberto"));
Assert.Equal((JobKind.VideoCopy, 2), (request.Kind, request.Targets.Count));
var job = await fx.EnqueueAsync(request);
await fx.Archive.WaitForAsync(job.Id, JobState.Done);
var copy = Path.Combine(fx.VideosFolder, "sunset (2).mp4");
Assert.SequenceEqual(await File.ReadAllBytesAsync(fx.FileOf(render), fx.Ct), await File.ReadAllBytesAsync(copy, fx.Ct));
Assert.Equal("someone else's", await File.ReadAllTextAsync(Path.Combine(fx.VideosFolder, "sunset.mp4"), fx.Ct), "never overwritten");
Assert.True(File.Exists(Path.Combine(fx.VideosFolder, "old.mp4")));
Assert.Empty(Directory.EnumerateFileSystemEntries(fx.VideosFolder, ".incoming-*"));
var copies = await fx.Db.RenderCopies.ListByRenderAsync(render.Id, fx.Ct);
Assert.Equal((copy, render.Sha256, 5000L, "alberto"), (copies.Single().Path, copies.Single().Sha256, copies.Single().Bytes, copies.Single().CopiedBy));
// A render of before migration 0010 is hashed by its first copy, and remembered.
var hashed = (await fx.Db.Renders.GetAsync(old.Id, fx.Ct))!.Sha256;
Assert.Equal(Convert.ToHexStringLower(SHA256.HashData(await File.ReadAllBytesAsync(fx.FileOf(old), fx.Ct))), hashed);
Assert.Equal(hashed, (await fx.Db.RenderCopies.ListByRenderAsync(old.Id, fx.Ct)).Single().Sha256);
Assert.Equal(1, (await fx.Library.GetAsync(render.Id, fx.Ct))!.Copies.Count);
}
[Test]
public async Task ACopyThatDoesNotMatchIsRemovedAndTheRenderFails()
{
await using var fx = await VideoFixture.CreateAsync(context);
var sequence = await fx.SequenceAsync();
var good = await fx.RenderAsync(sequence, "good.mp4", 2000);
var bad = await fx.RenderAsync(sequence, "bad.mp4", 2000);
// The file changed since it was recorded: its copy cannot match the render's hash.
await File.WriteAllBytesAsync(fx.FileOf(bad), VideoFixture.Content("other", 2000), fx.Ct);
await fx.StartEngineAsync();
var job = await fx.EnqueueAsync(fx.Library.CopyJob(new VideoCopyParameters([bad.Id, good.Id], fx.VideosFolder)));
var failed = await fx.Archive.WaitForAsync(job.Id, JobState.Failed);
Assert.Equal(Loc.L("Errors.Render.Failed", 1, 2), failed.Error);
Assert.SequenceEqual(["good.mp4"], Directory.GetFileSystemEntries(fx.VideosFolder).Select(Path.GetFileName));
Assert.Empty(await fx.Db.RenderCopies.ListByRenderAsync(bad.Id, fx.Ct));
Assert.True(File.Exists(fx.FileOf(bad)), "the render itself is never touched");
}
[Test]
public async Task ACopyThatCannotFitFailsBeforeWritingAnything()
{
await using var fx = await VideoFixture.CreateAsync(context, freeSpace: 6000);
var sequence = await fx.SequenceAsync();
var first = await fx.RenderAsync(sequence, "first.mp4", 4000);
var second = await fx.RenderAsync(sequence, "second.mp4", 4000);
await fx.StartEngineAsync();
var job = await fx.EnqueueAsync(fx.Library.CopyJob(new VideoCopyParameters([first.Id, second.Id], fx.VideosFolder)));
var failed = await fx.Archive.WaitForAsync(job.Id, JobState.Failed);
Assert.Contains(Loc.L("Errors.Videos.NoSpace", Chronos.Data.Housekeeping.CleanupSteps.Megabytes(2000)), failed.Error!);
Assert.Empty(Directory.EnumerateFileSystemEntries(fx.VideosFolder));
Assert.Empty(await fx.Db.RenderCopies.ListByRenderAsync(first.Id, fx.Ct));
}
[Test]
public async Task AVideosFolderInsideTheArchiveIsRefused()
{
await using var fx = await VideoFixture.CreateAsync(context);
var sequence = await fx.SequenceAsync();
var render = await fx.RenderAsync(sequence, "sunset.mp4", 1000);
var inside = Path.Combine(fx.Archive.Paths.Root, "Videos");
Assert.Equal(VideoErrors.FolderInvalid, fx.Library.FolderProblem(inside));
Assert.Equal(VideoErrors.FolderInvalid, fx.Library.FolderProblem(fx.Archive.Paths.Root));
Assert.Equal(VideoErrors.FolderUnset, fx.Library.FolderProblem(" "));
Assert.Equal(VideoErrors.FolderInvalid, fx.Library.FolderProblem("relative/videos"));
Assert.Null(fx.Library.FolderProblem(fx.VideosFolder));
var refused = Assert.Throws<VideoLibraryException>(() => fx.Library.CopyJob(new VideoCopyParameters([render.Id], inside)));
Assert.Equal((VideoErrors.FolderInvalid, Loc.L("Errors.Videos.FolderInsideArchive")), (refused.Code, refused.Message));
// A workspace the host protects is refused too, and so is a folder that became one after the job was queued.
var workspace = context.CreateTempDirectory("workspace");
fx.Library.ProtectedFolders = () => [workspace];
Assert.Equal(VideoErrors.FolderInvalid, fx.Library.FolderProblem(Path.Combine(workspace, "out")));
fx.Library.ProtectedFolders = null;
var request = fx.Library.CopyJob(new VideoCopyParameters([render.Id], Path.Combine(workspace, "out")));
fx.Library.ProtectedFolders = () => [workspace];
await fx.StartEngineAsync();
var failed = await fx.Archive.WaitForAsync((await fx.EnqueueAsync(request)).Id, JobState.Failed);
Assert.Equal(Loc.L("Errors.Videos.FolderInsideArchive"), failed.Error);
Assert.False(Directory.Exists(Path.Combine(workspace, "out")));
}
[Test]
public async Task AnImageSequenceIsCopiedAndVerifiedFileByFile()
{
await using var fx = await VideoFixture.CreateAsync(context);
var sequence = await fx.SequenceAsync();
var pngs = await fx.SequenceRenderAsync(sequence, "clip-PNG", 5);
Directory.CreateDirectory(Path.Combine(fx.VideosFolder, "clip-PNG"));
await fx.StartEngineAsync();
var job = await fx.EnqueueAsync(fx.Library.CopyJob(new VideoCopyParameters([pngs.Id], fx.VideosFolder)));
await fx.Archive.WaitForAsync(job.Id, JobState.Done);
var copy = Path.Combine(fx.VideosFolder, "clip-PNG (2)");
var sources = Directory.GetFiles(fx.FileOf(pngs)).Order(StringComparer.Ordinal).ToList();
var copied = Directory.GetFiles(copy).Order(StringComparer.Ordinal).ToList();
Assert.SequenceEqual(sources.Select(Path.GetFileName), copied.Select(Path.GetFileName));
for (var i = 0; i < sources.Count; i++)
Assert.SequenceEqual(await File.ReadAllBytesAsync(sources[i], fx.Ct), await File.ReadAllBytesAsync(copied[i], fx.Ct));
var record = (await fx.Db.RenderCopies.ListByRenderAsync(pngs.Id, fx.Ct)).Single();
Assert.Equal((copy, (string?)null, pngs.Bytes), (record.Path, record.Sha256, record.Bytes));
Assert.Null((await fx.Db.Renders.GetAsync(pngs.Id, fx.Ct))!.Sha256, "a folder records no hash of its own");
}
}