Files
Chronos/tests/Chronos.Data.Tests/Videos/VideoLibraryTests.cs
T
Alby96andClaude Opus 5.5 03b4d0ef60 feat(P2): videos library, verified video trash and copy job, render hashes and still jobs
Renders now record their SHA-256 and kind, so copies, deletes and restores can
be verified. The new videos library lists every render with paging and
filters, deletes into the archive trash after a dry run whose fingerprint must
still match (journalled, so a crash mid-move is settled on open), restores by id
refusing a tampered file, and a VideoCopy job copies renders hash-verified into
a videos folder outside the archive, never overwriting. A still job saves one
frame as an image through an interim Data-side path until P1's Core still API
lands. Batch exports keep the playback and routing uses the job's own export,
so a playback job needs settings format 3 and an older node never claims it.

Refs #21

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

212 lines
12 KiB
C#

using Chronos.Core.Model;
using Chronos.Data.Videos;
using Chronos.Protocol;
using Chronos.Testing;
namespace Chronos.Data.Tests.Videos;
/// <summary>Owner review 1, R9: the videos library lists every render, deletes into the trash after a dry run and restores by id.</summary>
[Category("videos")]
public sealed class VideoLibraryTests(TestContext context)
{
private static readonly DateTimeOffset Day = new(2026, 10, 1, 12, 0, 0, TimeSpan.Zero);
[Test]
public async Task TheListPagesNewestFirstAndFilters()
{
await using var fx = await VideoFixture.CreateAsync(context);
var harbour = await fx.SequenceAsync("Harbour", 1);
var roof = await fx.SequenceAsync("Roof", 2);
var renders = new List<Render>();
for (var i = 0; i < 5; i++) renders.Add(await fx.RenderAsync(harbour, $"harbour-{i}.mp4", 1000 + (i * 100), Day.AddHours(i)));
var still = await fx.RenderAsync(roof, "roof - 0003.png", 5000, Day.AddDays(-400), codec: "PngSequence", kind: RenderKind.Still);
var gif = await fx.RenderAsync(roof, "roof.gif", 700, Day.AddMinutes(30), codec: "Gif");
var pngs = await fx.SequenceRenderAsync(roof, "roof-PNG", 3);
// Newest first, two at a time, with the total.
var seen = new List<Guid>();
string? cursor = null;
do
{
var page = await fx.Library.ListAsync(new VideoQuery { Limit = 3, Cursor = cursor }, fx.Ct);
Assert.Equal(8L, page.Total);
Assert.True(page.Items.Count <= 3);
seen.AddRange(page.Items.Select(i => i.Render.Id));
cursor = page.Next;
}
while (cursor is not null);
var all = renders.Append(still).Append(gif).Append(pngs).OrderByDescending(r => r.Created).Select(r => r.Id).ToList();
Assert.SequenceEqual(all, seen);
async Task<List<Guid>> Ids(VideoQuery query) => [.. (await fx.Library.ListAsync(query, fx.Ct)).Items.Select(i => i.Render.Id)];
Assert.SequenceEqual([still.Id], await Ids(new VideoQuery { Kind = RenderKind.Still }));
Assert.SequenceEqual([pngs.Id], await Ids(new VideoQuery { Kind = RenderKind.Sequence }));
Assert.Equal(6, (await Ids(new VideoQuery { Kind = RenderKind.Video })).Count);
Assert.SequenceEqual([gif.Id], await Ids(new VideoQuery { Codec = "gif" }));
Assert.SequenceEqual([still.Id], await Ids(new VideoQuery { Year = Day.AddDays(-400).Year }));
Assert.Equal(3, (await Ids(new VideoQuery { SequenceId = roof.Id })).Count);
Assert.SequenceEqual([renders[3].Id], await Ids(new VideoQuery { Text = "harbour-3" }));
Assert.SequenceEqual([still.Id, renders[4].Id], (await Ids(new VideoQuery { Sort = VideoSorts.Largest })).Take(2));
Assert.SequenceEqual([still.Id], (await Ids(new VideoQuery { Sort = VideoSorts.Oldest })).Take(1));
// The title sort pages too.
var titled = new List<Guid>();
cursor = null;
do
{
var page = await fx.Library.ListAsync(new VideoQuery { Sort = VideoSorts.Title, Limit = 3, Cursor = cursor }, fx.Ct);
titled.AddRange(page.Items.Select(i => i.Render.Id));
cursor = page.Next;
}
while (cursor is not null);
Assert.Equal(8, titled.Distinct().Count());
var item = (await fx.Library.GetAsync(still.Id, fx.Ct))!;
Assert.Equal((RenderKind.Still, false, fx.FileOf(still), "roof - 0003.png"), (item.Kind, item.Missing, item.FullPath, item.FileName));
File.Delete(fx.FileOf(gif));
Assert.True((await fx.Library.GetAsync(gif.Id, fx.Ct))!.Missing);
}
[Test]
public async Task TheDryRunFingerprintChangesWhenTheVideosDo()
{
await using var fx = await VideoFixture.CreateAsync(context);
var sequence = await fx.SequenceAsync();
var first = await fx.RenderAsync(sequence, "a.mp4", 1200);
var later = Guid.CreateVersion7();
var plan = await fx.Library.PlanDeleteAsync([first.Id, later], fx.Ct);
Assert.SequenceEqual([first.Id], plan.Items.Select(i => i.Render.Id));
Assert.Equal((later, VideoBlockReasons.NotFound), (plan.Blocked.Single().RenderId, plan.Blocked.Single().Reason));
Assert.Equal(plan.Fingerprint, (await fx.Library.PlanDeleteAsync([later, first.Id], fx.Ct)).Fingerprint, "the order asked does not matter");
// A render added since: the plan given back no longer holds, and nothing is deleted.
await fx.Db.Renders.InsertAsync(new Render { Id = later, SequenceId = sequence.Id, PresetName = "p", OutputPath = "Harbour/exports/b.mp4", Created = DateTimeOffset.UtcNow }, fx.Ct);
var replanned = await fx.Library.PlanDeleteAsync([first.Id, later], fx.Ct);
Assert.NotEqual(plan.Fingerprint, replanned.Fingerprint);
var refused = await Assert.ThrowsAsync<VideoLibraryException>(() => fx.Library.DeleteAsync([first.Id, later], plan.Fingerprint, "me", fx.Ct));
Assert.Equal(VideoErrors.PlanChanged, refused.Code);
Assert.True(File.Exists(fx.FileOf(first)));
Assert.NotNull(await fx.Db.Renders.GetAsync(first.Id, fx.Ct));
}
[Test]
public async Task ADeleteMovesTheFileAndTheRowIntoTheTrashAndARestoreBringsBothBack()
{
await using var fx = await VideoFixture.CreateAsync(context);
var sequence = await fx.SequenceAsync();
var kept = await fx.RenderAsync(sequence, "kept.mp4", 900);
var render = await fx.RenderAsync(sequence, "sunset.mp4", 4096, hashed: false);
Assert.Equal(2, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.RenderedCount);
var plan = await fx.Library.PlanDeleteAsync([render.Id], fx.Ct);
Assert.Equal(4096L, plan.Bytes);
var result = await fx.Library.DeleteAsync([render.Id], plan.Fingerprint, "alberto", fx.Ct);
var name = result.TrashEntries.Single();
Assert.True(VideoTrashNames.IsEntryName(name));
Assert.False(File.Exists(fx.FileOf(render)));
Assert.Null(await fx.Db.Renders.GetAsync(render.Id, fx.Ct));
Assert.Equal(1, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.RenderedCount);
Assert.NotNull(await fx.Db.Renders.GetAsync(kept.Id, fx.Ct));
// The record is published (no journal left) and the trash lists the entry.
var (folder, record) = VideoTrash.Location(fx.Archive.Paths, name);
Assert.True(File.Exists(record));
Assert.False(File.Exists(record + VideoTrash.PendingSuffix));
Assert.True(File.Exists(Path.Combine(folder, "sunset.mp4")));
var entry = (await fx.Archive.Service.Trash.ListAsync(fx.Archive.Archive, fx.Ct)).Single(e => e.Name == name);
Assert.Equal((Chronos.Data.Archiving.TrashKind.Videos, 4096L, "alberto"), (entry.Kind, entry.Bytes, entry.DeletedBy));
var restored = await fx.Library.RestoreAsync(fx.Archive.Archive.Id, name, fx.Ct);
Assert.Equal(render.Id, restored.Id);
Assert.Equal(render.OutputPath, restored.OutputPath);
Assert.NotNull(restored.Sha256);
Assert.True(File.Exists(fx.FileOf(render)));
Assert.Equal(restored.Sha256, (await fx.Db.Renders.GetAsync(render.Id, fx.Ct))!.Sha256);
Assert.Equal(2, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.RenderedCount);
Assert.False(File.Exists(record));
Assert.False(Directory.Exists(folder));
}
[Test]
public async Task ATamperedTrashedVideoIsNotRestored()
{
await using var fx = await VideoFixture.CreateAsync(context);
var sequence = await fx.SequenceAsync();
var render = await fx.RenderAsync(sequence, "sunset.mp4", 2048);
var plan = await fx.Library.PlanDeleteAsync([render.Id], fx.Ct);
var name = (await fx.Library.DeleteAsync([render.Id], plan.Fingerprint, null, fx.Ct)).TrashEntries.Single();
var trashed = Path.Combine(VideoTrash.Location(fx.Archive.Paths, name).Folder, "sunset.mp4");
var bytes = await File.ReadAllBytesAsync(trashed, fx.Ct);
bytes[100] ^= 0xFF;
await File.WriteAllBytesAsync(trashed, bytes, fx.Ct);
var refused = await Assert.ThrowsAsync<VideoLibraryException>(() => fx.Library.RestoreAsync(fx.Archive.Archive.Id, name, fx.Ct));
Assert.Equal(VideoErrors.HashMismatch, refused.Code);
Assert.True(File.Exists(trashed), "it stays in the trash");
Assert.False(File.Exists(fx.FileOf(render)));
Assert.Null(await fx.Db.Renders.GetAsync(render.Id, fx.Ct));
}
[Test]
public async Task ADeleteACrashInterruptedIsCompletedWhenTheArchiveOpens()
{
await using var fx = await VideoFixture.CreateAsync(context);
var sequence = await fx.SequenceAsync();
var render = await fx.RenderAsync(sequence, "sunset.mp4", 3000);
fx.Library.Trash.CrashAfterMove = true;
fx.Library.Trash.ForceCopy = true;
var plan = await fx.Library.PlanDeleteAsync([render.Id], fx.Ct);
await Assert.ThrowsAsync<IOException>(() => fx.Library.DeleteAsync([render.Id], plan.Fingerprint, null, fx.Ct));
fx.Library.Trash.CrashAfterMove = false;
// The journal is there, the row still is, and the trash does not list a delete under way.
var trash = VideoTrash.Directory(fx.Archive.Paths);
var pending = Directory.GetFiles(trash, "*" + VideoTrash.PendingSuffix).Single();
Assert.NotNull(await fx.Db.Renders.GetAsync(render.Id, fx.Ct));
Assert.Empty((await fx.Archive.Service.Trash.ListAsync(fx.Archive.Archive, fx.Ct)).Where(e => e.Kind == Chronos.Data.Archiving.TrashKind.Videos));
Assert.Equal(1, await fx.Library.RecoverAsync(fx.Archive.Archive, fx.Ct));
Assert.False(File.Exists(pending));
Assert.Null(await fx.Db.Renders.GetAsync(render.Id, fx.Ct));
Assert.False(File.Exists(fx.FileOf(render)));
Assert.Equal(0, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.RenderedCount);
var entry = (await fx.Archive.Service.Trash.ListAsync(fx.Archive.Archive, fx.Ct)).Single(e => e.Kind == Chronos.Data.Archiving.TrashKind.Videos);
Assert.Equal(render.Id, (await fx.Library.RestoreAsync(fx.Archive.Archive.Id, entry.Name, fx.Ct)).Id);
}
[Test]
public async Task AVideoWhoseFileIsMissingLosesOnlyItsRow()
{
await using var fx = await VideoFixture.CreateAsync(context);
var sequence = await fx.SequenceAsync();
var render = await fx.RenderAsync(sequence, "gone.mp4", 1500);
File.Delete(fx.FileOf(render));
var plan = await fx.Library.PlanDeleteAsync([render.Id], fx.Ct);
Assert.True(plan.Items.Single().Missing);
Assert.Equal(0L, plan.Bytes);
var result = await fx.Library.DeleteAsync([render.Id], plan.Fingerprint, null, fx.Ct);
Assert.SequenceEqual([render.Id], result.Deleted.Select(d => d.Render.Id));
Assert.Empty(result.TrashEntries);
Assert.Null(await fx.Db.Renders.GetAsync(render.Id, fx.Ct));
Assert.False(Directory.Exists(VideoTrash.Directory(fx.Archive.Paths)) && Directory.EnumerateFileSystemEntries(VideoTrash.Directory(fx.Archive.Paths)).Any());
}
[Test]
public async Task AnImageSequenceFolderGoesToTheTrashAndBackFileByFile()
{
await using var fx = await VideoFixture.CreateAsync(context);
var sequence = await fx.SequenceAsync();
var pngs = await fx.SequenceRenderAsync(sequence, "clip-PNG", 4);
var plan = await fx.Library.PlanDeleteAsync([pngs.Id], fx.Ct);
var name = (await fx.Library.DeleteAsync([pngs.Id], plan.Fingerprint, null, fx.Ct)).TrashEntries.Single();
Assert.False(Directory.Exists(fx.FileOf(pngs)));
var entry = (await fx.Archive.Service.Trash.ListAsync(fx.Archive.Archive, fx.Ct)).Single(e => e.Name == name);
Assert.Equal(4, entry.Files);
var restored = await fx.Library.RestoreAsync(fx.Archive.Archive.Id, name, fx.Ct);
Assert.Equal((pngs.Id, (string?)null), (restored.Id, restored.Sha256));
Assert.Equal(4, Directory.GetFiles(fx.FileOf(pngs)).Length);
}
}