using Chronos.Core.Model; using Chronos.Data.Videos; using Chronos.Protocol; using Chronos.Testing; namespace Chronos.Data.Tests.Videos; /// Owner review 1, R9: the videos library lists every render, deletes into the trash after a dry run and restores by id. [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(); 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(); 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> 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(); 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 AStillWhoseKindWasLostIsStillAStill() { // A still a server of before phase 5 stored (and a PC then synced) has no kind: its image codec, fps 0 and duration 0 tell it. await using var fx = await VideoFixture.CreateAsync(context); var roof = await fx.SequenceAsync("Roof", 2); var kindless = new Render { Id = Guid.CreateVersion7(), SequenceId = roof.Id, PresetName = "PNG", OutputPath = "Roof/exports/roof - 0007.png", Codec = "PngSequence", Width = 96, Height = 64, Created = Day, }; await fx.Db.Renders.InsertAsync(kindless, fx.Ct); var pngs = await fx.SequenceRenderAsync(roof, "roof-PNG", 2); Assert.Equal(RenderKind.Still, RenderKind.Of(kindless)); Assert.Equal(RenderKind.Sequence, RenderKind.Of(pngs), "an image sequence has a frame rate"); async Task> Ids(string kind) => [.. (await fx.Library.ListAsync(new VideoQuery { Kind = kind }, fx.Ct)).Items.Select(i => i.Render.Id)]; Assert.SequenceEqual([kindless.Id], await Ids(RenderKind.Still)); Assert.SequenceEqual([pngs.Id], await Ids(RenderKind.Sequence)); Assert.Empty(await Ids(RenderKind.Video)); Assert.Equal(RenderKind.Still, (await fx.Library.GetAsync(kindless.Id, fx.Ct))!.Kind); } [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(() => 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(() => 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(() => 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); } [Test] [WindowsOnly] public async Task AFileHeldOpenWhileItIsCopiedToTheTrashStaysWholeUntilTheNextStart() { await using var fx = await VideoFixture.CreateAsync(context); var sequence = await fx.SequenceAsync(); var render = await fx.RenderAsync(sequence, "sunset.mp4", 3000); var source = fx.FileOf(render); fx.Library.Trash.ForceCopy = true; var plan = await fx.Library.PlanDeleteAsync([render.Id], fx.Ct); // A player holds the file open: it copies and verifies, but the original cannot be deleted. string name; using (new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read)) name = (await fx.Library.DeleteAsync([render.Id], plan.Fingerprint, null, fx.Ct)).TrashEntries.Single(); // The delete is done (row gone, verified copy listed); the original stays whole and is marked as left over. var (folder, _) = VideoTrash.Location(fx.Archive.Paths, name); Assert.Null(await fx.Db.Renders.GetAsync(render.Id, fx.Ct)); Assert.Equal(3000L, new FileInfo(Path.Combine(folder, "sunset.mp4")).Length); Assert.Equal(3000L, new FileInfo(source).Length); Assert.True(File.Exists(folder + VideoTrash.LeftoverSuffix)); Assert.Equal(name, (await fx.Archive.Service.Trash.ListAsync(fx.Archive.Archive, fx.Ct)).Single(e => e.Kind == Chronos.Data.Archiving.TrashKind.Videos).Name); // The next start removes it, and the render comes back under its own name. Assert.Equal(1, await fx.Library.RecoverAsync(fx.Archive.Archive, fx.Ct)); Assert.False(File.Exists(source)); Assert.False(File.Exists(folder + VideoTrash.LeftoverSuffix)); Assert.Equal(render.OutputPath, (await fx.Library.RestoreAsync(fx.Archive.Archive.Id, name, fx.Ct)).OutputPath); } [Test] [WindowsOnly] public async Task AnImageSequenceWithAFileHeldOpenKeepsEveryFileUntilTheNextStart() { await using var fx = await VideoFixture.CreateAsync(context); var sequence = await fx.SequenceAsync(); var pngs = await fx.SequenceRenderAsync(sequence, "clip-PNG", 4); var source = fx.FileOf(pngs); var files = Directory.GetFiles(source).Order(StringComparer.Ordinal).ToList(); fx.Library.Trash.ForceCopy = true; var plan = await fx.Library.PlanDeleteAsync([pngs.Id], fx.Ct); string name; using (new FileStream(files[2], FileMode.Open, FileAccess.Read, FileShare.Read)) name = (await fx.Library.DeleteAsync([pngs.Id], plan.Fingerprint, null, fx.Ct)).TrashEntries.Single(); // Never half a folder: every file stays where it was, and every one is in the trash. var (folder, _) = VideoTrash.Location(fx.Archive.Paths, name); Assert.SequenceEqual(files, Directory.GetFiles(source).Order(StringComparer.Ordinal)); Assert.Equal(4, Directory.GetFiles(Path.Combine(folder, Path.GetFileName(source))).Length); Assert.True(File.Exists(folder + VideoTrash.LeftoverSuffix)); Assert.Equal(1, await fx.Library.RecoverAsync(fx.Archive.Archive, fx.Ct)); Assert.False(Directory.Exists(source)); Assert.Empty(Directory.GetDirectories(Path.GetDirectoryName(source)!, ".*removing-*")); Assert.Equal(4, Directory.GetFiles(Path.Combine(folder, Path.GetFileName(source))).Length, "the trash keeps its copy"); } [Test] public async Task ARestoreSkipsANameARenderIsWriting() { await using var fx = await VideoFixture.CreateAsync(context); var sequence = await fx.SequenceAsync(); var render = await fx.RenderAsync(sequence, "sunset.mp4", 2000); 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(); // A render of this process picked the free name meanwhile and is still encoding it. using var writing = new Chronos.Data.Processing.OutputNames(); Assert.True(writing.TryReserve(fx.FileOf(render))); var restored = await fx.Library.RestoreAsync(fx.Archive.Archive.Id, name, fx.Ct); Assert.Equal("sunset (2).mp4", Path.GetFileName(restored.OutputPath)); Assert.False(File.Exists(fx.FileOf(render))); } }