diff --git a/src/Titano.Data/Archiving/ArchiveService.cs b/src/Titano.Data/Archiving/ArchiveService.cs index b338d554..92aff0f9 100644 --- a/src/Titano.Data/Archiving/ArchiveService.cs +++ b/src/Titano.Data/Archiving/ArchiveService.cs @@ -115,20 +115,37 @@ public sealed class ArchiveService : IJobHandlerProvider public static ArchiveAvailability GetAvailability(Archive archive) => ArchiveVolume.Probe(new ArchivePaths(archive.RootPath)); /// Changes title, tags, rating and notes in the sidecar and the index together (settings version + 1). - public async Task SetSequenceInfoAsync(Guid sequenceId, string? title, IReadOnlyList tags, int rating, string? notes, - CancellationToken cancellationToken = default) + public Task SetSequenceInfoAsync(Guid sequenceId, string? title, IReadOnlyList tags, int rating, string? notes, + CancellationToken cancellationToken = default) => + EditSequenceInfoAsync(sequenceId, _ => new SequenceInfo(title, tags, rating, notes), cancellationToken); + + /// + /// Changes some of title, tags, rating and notes from their current values — read under the sequence's lease from + /// the sidecar, or from the index when it is ahead (an edit made there only) — so fields + /// leaves alone keep any change made since the caller read the sequence. Sidecar and index change together + /// (settings version + 1). + /// + public async Task EditSequenceInfoAsync(Guid sequenceId, Func edit, CancellationToken cancellationToken = default) { + ArgumentNullException.ThrowIfNull(edit); var (archive, sequence) = await LoadSequenceAsync(sequenceId, cancellationToken).ConfigureAwait(false); var paths = new ArchivePaths(archive.RootPath); ArchiveVolume.EnsureWritable(paths); - var manifest = await _writer.UpdateAsync(paths, sequenceId, sequence.Folder, m => m with + var manifest = await _writer.UpdateAsync(paths, sequenceId, sequence.Folder, m => { - Title = string.IsNullOrWhiteSpace(title) ? null : title.Trim(), - Tags = tags, - Rating = rating, - Notes = notes, - Versions = m.Versions with { Settings = System.Math.Max(m.Versions.Settings, sequence.SettingsVersion) + 1 }, - UpdatedAt = DateTimeOffset.UtcNow, + var current = sequence.SettingsVersion > m.Versions.Settings + ? new SequenceInfo(sequence.Title, sequence.Tags, sequence.Rating, sequence.Notes) + : new SequenceInfo(m.Title, m.Tags, m.Rating, m.Notes); + var info = edit(current); + return m with + { + Title = string.IsNullOrWhiteSpace(info.Title) ? null : info.Title.Trim(), + Tags = info.Tags, + Rating = info.Rating, + Notes = info.Notes, + Versions = m.Versions with { Settings = System.Math.Max(m.Versions.Settings, sequence.SettingsVersion) + 1 }, + UpdatedAt = DateTimeOffset.UtcNow, + }; }, cancellationToken).ConfigureAwait(false); var folder = paths.Sequence(sequence.Folder); var updated = SequenceMapping.ToSequence(manifest, archive.Id, folder, sequence); @@ -165,9 +182,9 @@ public sealed class ArchiveService : IJobHandlerProvider await Renamer.ApplyAsync(await LoadArchiveAsync(plan.ArchiveId, cancellationToken).ConfigureAwait(false), plan, progress, cancellationToken) .ConfigureAwait(false); - /// Dry run of moving sequences to their archive's trash. - public Task PlanDeleteAsync(IReadOnlyList sequenceIds, CancellationToken cancellationToken = default) => - Trash.PlanAsync(sequenceIds, cancellationToken); + /// Dry run of moving sequences to their archive's trash; counts the sequences checked. + public Task PlanDeleteAsync(IReadOnlyList sequenceIds, IProgress? progress = null, CancellationToken cancellationToken = default) => + Trash.PlanAsync(sequenceIds, progress, cancellationToken); /// Moves exactly the planned sequences to the trash; returns the trash entries made. public Task> DeleteAsync(DeletePlan plan, string? deletedBy = null, IProgress? progress = null, @@ -222,6 +239,9 @@ public sealed class ArchiveService : IJobHandlerProvider } } +/// The fields of a sequence the user describes it with (sidecar and index). +public sealed record SequenceInfo(string? Title, IReadOnlyList Tags, int Rating, string? Notes); + /// Builds the archive jobs; enqueue with . public static class ArchiveJobs { diff --git a/src/Titano.Data/Archiving/SequenceTrash.cs b/src/Titano.Data/Archiving/SequenceTrash.cs index a25a8bbf..5e9c321b 100644 --- a/src/Titano.Data/Archiving/SequenceTrash.cs +++ b/src/Titano.Data/Archiving/SequenceTrash.cs @@ -5,6 +5,7 @@ using Titano.Core.Diagnostics; using Titano.Core.IO; using Titano.Core.Localization; using Titano.Core.Model; +using Titano.Data.Archiving.Jobs; namespace Titano.Data.Archiving; @@ -20,15 +21,18 @@ public enum DeleteBlock /// Other sequences live in folders below this one; moving it would take them along. ContainsSequences, - /// A job is running on the sequence, or paused in the middle of it. + /// A running or paused job works on the sequence, or on its whole archive (a scan, a rebuild, an import, a verify, maintenance). Busy, /// The folder's sidecar names another sequence: the index is out of date (a scan fixes it). ForeignSidecar, + + /// The index names a folder that is not on disk: there is nothing to keep in the trash, so the Orphans check removes the row. + FolderMissing, } /// One sequence of a delete plan: its folder moves to and its index rows go. -/// Archive-relative folder the sequence folder moves to; null when it has no folder on disk (only its rows go). +/// Archive-relative folder the sequence folder moves to; null for a sequence that has no folder yet (created on the server, no photo received), of which only the rows go. /// Files under the folder (originals, proxies, exports, sidecars). /// Their size on disk. public sealed record DeleteStep(Sequence Sequence, string? TrashFolder, int Files, long Bytes); @@ -61,7 +65,9 @@ public sealed class StaleDeletePlanException(Guid sequenceId) : InvalidOperation /// run; moves each sequence folder whole — originals, proxies, exports and its sidecars — into /// .titano/trash/<id> of its archive under the sequence's leases, next to a record of its index row and /// renders, then removes the index rows. moves a folder back and indexes it again from its -/// sidecar with the same ids. The trash is a hidden folder of the archive, so scans never index it. +/// sidecar with the same ids. The trash is a hidden folder of the archive, so scans never index it. Once a folder has +/// moved, its index step runs to the end whatever the caller cancels; when that step fails, the folder moves back, so +/// the trash and the index never disagree. /// public sealed class SequenceTrash { @@ -71,6 +77,9 @@ public sealed class SequenceTrash private const string RecordExtension = ".json"; private const int JobPage = 200; + /// Bytes of a sidecar read to find its id (written second, after the schema version). + private const int SidecarHead = 4096; + private readonly TitanoDatabase _db; private readonly SequenceWriter _writer; private readonly ArchiveIndexer _indexer; @@ -92,43 +101,40 @@ public sealed class SequenceTrash public static string TrashDirectory(ArchivePaths archive) => Path.Combine(archive.MetaDirectory, FolderName); /// The dry run: for each sequence, where its folder goes and how many files and bytes it holds, or why it cannot go. + /// Sequences checked so far. /// A sequence is not in the index. - public async Task PlanAsync(IReadOnlyList sequenceIds, CancellationToken cancellationToken = default) + public async Task PlanAsync(IReadOnlyList sequenceIds, IProgress? progress = null, CancellationToken cancellationToken = default) { - var busy = await BusySequencesAsync(cancellationToken).ConfigureAwait(false); - var archives = new Dictionary Locations)>(); + var busy = await BusyAsync(cancellationToken).ConfigureAwait(false); + var archives = new Dictionary(); var steps = new List(); var blocked = new List(); var claimed = new HashSet(StringComparer.OrdinalIgnoreCase); + var checkedCount = 0; foreach (var id in sequenceIds.Distinct()) { cancellationToken.ThrowIfCancellationRequested(); var sequence = await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false) ?? throw new ArgumentException($"Unknown sequence {id}.", nameof(sequenceIds)); - if (!archives.TryGetValue(sequence.ArchiveId, out var context)) - { - var archive = await _db.Archives.GetAsync(sequence.ArchiveId, cancellationToken).ConfigureAwait(false) - ?? throw new ArgumentException($"Unknown archive {sequence.ArchiveId}.", nameof(sequenceIds)); - var paths = new ArchivePaths(archive.RootPath); - context = (archive, paths, ArchiveVolume.Probe(paths) == ArchiveAvailability.Online, - await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false)); - archives[sequence.ArchiveId] = context; - } - if (Block(sequence, context.Paths, context.Online, context.Locations, busy) is { } reason) + var context = await ContextAsync(archives, sequence.ArchiveId, cancellationToken).ConfigureAwait(false) + ?? throw new ArgumentException($"Unknown archive {sequence.ArchiveId}.", nameof(sequenceIds)); + if (Block(sequence, context, busy) is { } reason) { blocked.Add(new BlockedDelete(sequence, reason)); - continue; } - var folder = sequence.Folder.Length == 0 ? null : context.Paths.Sequence(sequence.Folder).Folder; - if (folder is null || !Directory.Exists(folder)) + else if (sequence.Folder.Length == 0) { steps.Add(new DeleteStep(sequence, null, 0, 0)); - continue; } - var (files, bytes) = await Task.Run(() => Measure(folder, cancellationToken), cancellationToken).ConfigureAwait(false); - var name = EntryName(context.Paths, sequence.Id, claimed); - claimed.Add(name); - steps.Add(new DeleteStep(sequence, context.Paths.ToRelative(Path.Combine(TrashDirectory(context.Paths), name)), files, bytes)); + else + { + var folder = context.Paths.Sequence(sequence.Folder).Folder; + var (files, bytes) = await Task.Run(() => Measure(folder, cancellationToken), cancellationToken).ConfigureAwait(false); + var name = EntryName(context.Paths, sequence.Id, claimed); + claimed.Add(name); + steps.Add(new DeleteStep(sequence, context.Paths.ToRelative(Path.Combine(TrashDirectory(context.Paths), name)), files, bytes)); + } + progress?.Report(++checkedCount); } return new DeletePlan(steps, blocked); } @@ -138,26 +144,19 @@ public sealed class SequenceTrash /// next; cancelling stops between two sequences. Returns the trash entries made. /// /// Who asked (user name or client), kept in the trash record. + /// Sequences moved so far. /// A sequence moved, vanished or got busy since the plan; nothing was deleted. - /// An archive went offline; sequences before it are in the trash. + /// An archive is offline; sequences before it are in the trash. public async Task> DeleteAsync(DeletePlan plan, string? deletedBy = null, IProgress? progress = null, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(plan); - // A plan made before another change is refused before anything moves. - var fresh = await PlanAsync([.. plan.Steps.Select(s => s.Sequence.Id)], cancellationToken).ConfigureAwait(false); - if (fresh.Blocked is [var block, ..]) throw new StaleDeletePlanException(block.Sequence.Id); - foreach (var step in plan.Steps) - { - if (fresh.Steps.First(s => s.Sequence.Id == step.Sequence.Id).Sequence.Folder != step.Sequence.Folder) - throw new StaleDeletePlanException(step.Sequence.Id); - } - + await RevalidateAsync(plan, cancellationToken).ConfigureAwait(false); var entries = new List(plan.Steps.Count); - for (var i = 0; i < fresh.Steps.Count; i++) + for (var i = 0; i < plan.Steps.Count; i++) { cancellationToken.ThrowIfCancellationRequested(); - if (await DeleteOneAsync(fresh.Steps[i], deletedBy, cancellationToken).ConfigureAwait(false) is { } entry) entries.Add(entry); + if (await DeleteOneAsync(plan.Steps[i], deletedBy, cancellationToken).ConfigureAwait(false) is { } entry) entries.Add(entry); progress?.Report(i + 1); } return entries; @@ -186,7 +185,8 @@ public sealed class SequenceTrash /// /// Moves a sequence back from the trash into its folder (or, when that is taken, a free one next to it) and indexes /// it again from its sidecar with its ids, settings and renders. A folder without a sidecar is adopted under the - /// sequence's id, as a scan would adopt it. Returns the restored row. + /// sequence's id, as a scan would adopt it, with the title, tags, rating and notes the record kept. Returns the + /// restored row. When it cannot be indexed, the folder goes back into the trash with its record. /// /// The index already holds that sequence. /// The entry is not in the trash. @@ -211,36 +211,101 @@ public sealed class SequenceTrash var sequence = paths.Sequence(folder); await using (await _writer.LockAsync(paths, id, cancellationToken).ConfigureAwait(false)) { + cancellationToken.ThrowIfCancellationRequested(); Directory.CreateDirectory(Path.GetDirectoryName(sequence.Folder)!); Directory.Move(source, sequence.Folder); - Log.Info("archive", $"{record.Sequence.DisplayTitle} restored from the trash to {folder}"); - var manifest = File.Exists(sequence.ManifestPath) ? SequenceWriter.ReadLocked(sequence, id) : Adopt(sequence, id, cancellationToken); - var kept = record.Sequence with { ArchiveId = archive.Id, Folder = folder, RenderedCount = 0 }; - await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false)) + try { - await tx.Sequences.UpsertAsync(SequenceMapping.ToSequence(manifest, archive.Id, sequence, kept), cancellationToken).ConfigureAwait(false); - await tx.Frames.SyncSequenceAsync(id, SequenceMapping.ToFrames(manifest, sequence), cancellationToken).ConfigureAwait(false); - foreach (var key in manifest.SessionKeys) await tx.Sequences.MapSessionKeyAsync(archive.Id, key, id, cancellationToken).ConfigureAwait(false); - foreach (var render in record.Renders) - await tx.Renders.InsertAsync(render with { OutputPath = Rebase(render.OutputPath, record.Sequence.Folder, folder) }, cancellationToken).ConfigureAwait(false); - await tx.CommitAsync(cancellationToken).ConfigureAwait(false); + var manifest = File.Exists(sequence.ManifestPath) ? SequenceWriter.ReadLocked(sequence, id) : Adopt(sequence, record.Sequence, cancellationToken); + // The index step runs to the end once it starts: a cancel cannot leave half the rows written. + await IndexAsync(archive, sequence, manifest, record, CancellationToken.None).ConfigureAwait(false); } - await _indexer.RecordStampsAsync(archive, sequence, id, cancellationToken).ConfigureAwait(false); + catch (Exception ex) + { + Log.Warning("archive", $"{record.Sequence.DisplayTitle} could not be restored; its folder goes back into the trash", ex); + MoveBack(sequence.Folder, source); + throw; + } + Log.Info("archive", $"{record.Sequence.DisplayTitle} restored from the trash to {folder}"); + await RecordStampsAsync(archive, sequence, id).ConfigureAwait(false); File.Delete(recordPath); } - return await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false) + return await _db.Sequences.GetAsync(id, CancellationToken.None).ConfigureAwait(false) ?? throw new InvalidOperationException($"{record.Sequence.DisplayTitle} was restored but is not in the index."); } - /// A sidecar for a restored folder that had none (its frames get new ids; the sequence keeps its own). - private SequenceManifest Adopt(SequencePaths sequence, Guid id, CancellationToken cancellationToken) + /// + /// A sidecar for a restored folder that lost its own, built as a scan adopts a folder: its frames get new ids, the + /// sequence keeps its id, and the title, tags, rating and notes of the record go into it so sidecar and index agree. + /// + private SequenceManifest Adopt(SequencePaths sequence, Sequence kept, CancellationToken cancellationToken) { - var manifest = SequenceAdopter.Build(id, sequence, _writer.Options.Owner, cancellationToken); + var built = SequenceAdopter.Build(kept.Id, sequence, _writer.Options.Owner, cancellationToken); + var manifest = built with + { + Title = kept.Title, + Tags = kept.Tags, + Rating = kept.Rating, + Notes = kept.Notes, + Versions = built.Versions with { Settings = System.Math.Max(built.Versions.Settings, kept.SettingsVersion) }, + }; Directory.CreateDirectory(sequence.MetaDirectory); ManifestStore.WriteSequence(sequence.ManifestPath, manifest); return manifest; } + /// The restored sequence's rows: the sidecar's frames, the record's row and renders; a hero frame the sidecar no longer has is dropped. + private async Task IndexAsync(Archive archive, SequencePaths sequence, SequenceManifest manifest, TrashRecord record, CancellationToken cancellationToken) + { + var deleted = record.Sequence; + var hero = deleted.HeroFrameId is { } frame && manifest.Frames.Any(f => f.Id == frame) ? frame : (Guid?)null; + var kept = deleted with { ArchiveId = archive.Id, Folder = sequence.RelativeFolder, RenderedCount = 0, HeroFrameId = hero }; + await using var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false); + await tx.Sequences.UpsertAsync(SequenceMapping.ToSequence(manifest, archive.Id, sequence, kept), cancellationToken).ConfigureAwait(false); + await tx.Frames.SyncSequenceAsync(deleted.Id, SequenceMapping.ToFrames(manifest, sequence), cancellationToken).ConfigureAwait(false); + foreach (var key in manifest.SessionKeys) await tx.Sequences.MapSessionKeyAsync(archive.Id, key, deleted.Id, cancellationToken).ConfigureAwait(false); + foreach (var render in record.Renders) + { + var moved = render with { OutputPath = Rebase(render.OutputPath, deleted.Folder, sequence.RelativeFolder) }; + await tx.Renders.InsertAsync(moved, cancellationToken).ConfigureAwait(false); + } + await tx.CommitAsync(cancellationToken).ConfigureAwait(false); + } + + /// The next incremental scan skips the restored folder; when the stamps cannot be written it only reads the folder again. + private async Task RecordStampsAsync(Archive archive, SequencePaths sequence, Guid id) + { + try + { + await _indexer.RecordStampsAsync(archive, sequence, id, CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or DatabaseException) + { + Log.Warning("archive", $"Scan stamps of {sequence.RelativeFolder} not recorded; the next scan reads it again", ex); + } + } + + /// Checks the plan again just before anything moves, without measuring the folders again; any change refuses it whole. + private async Task RevalidateAsync(DeletePlan plan, CancellationToken cancellationToken) + { + var busy = await BusyAsync(cancellationToken).ConfigureAwait(false); + var archives = new Dictionary(); + foreach (var step in plan.Steps) + { + cancellationToken.ThrowIfCancellationRequested(); + var row = await _db.Sequences.GetAsync(step.Sequence.Id, cancellationToken).ConfigureAwait(false); + if (row is null || row.ArchiveId != step.Sequence.ArchiveId || row.Folder != step.Sequence.Folder) throw new StaleDeletePlanException(step.Sequence.Id); + if (await ContextAsync(archives, row.ArchiveId, cancellationToken).ConfigureAwait(false) is not { } context) throw new StaleDeletePlanException(row.Id); + switch (Block(row, context, busy)) + { + case DeleteBlock.Offline: + throw new ArchiveOfflineException(context.Paths.Root, context.Availability); + case not null: + throw new StaleDeletePlanException(row.Id); + } + } + } + private async Task DeleteOneAsync(DeleteStep step, string? deletedBy, CancellationToken cancellationToken) { var sequence = step.Sequence; @@ -248,74 +313,114 @@ public sealed class SequenceTrash ?? throw new StaleDeletePlanException(sequence.Id); var paths = new ArchivePaths(archive.RootPath); ArchiveVolume.EnsureWritable(paths); - TrashEntry? entry = null; await using (await _writer.LockAsync(paths, sequence.Id, cancellationToken).ConfigureAwait(false)) using (await LeaseFile.AcquireAsync(paths.LocksDirectory, $"thumbs-{sequence.Id:N}", _writer.Options.Owner, _writer.Options.LeaseWait, _writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false)) { var row = await _db.Sequences.GetAsync(sequence.Id, cancellationToken).ConfigureAwait(false); if (row is null || row.Folder != sequence.Folder) throw new StaleDeletePlanException(sequence.Id); - if (step.TrashFolder is not null && sequence.Folder.Length > 0 && Directory.Exists(paths.Sequence(sequence.Folder).Folder)) + if (step.TrashFolder is null) { - var folder = paths.Sequence(sequence.Folder); - if (File.Exists(folder.ManifestPath) && ManifestStore.ReadSequence(folder.ManifestPath).Id != sequence.Id) - throw new StaleDeletePlanException(sequence.Id); - var trash = TrashDirectory(paths); - Directory.CreateDirectory(trash); - var name = EntryName(paths, sequence.Id, new HashSet()); - var record = new TrashRecord(row, await _db.Renders.ListBySequenceAsync(sequence.Id, cancellationToken).ConfigureAwait(false), step.Files, - step.Bytes, DateTimeOffset.UtcNow, deletedBy); - // The record first: a crash before the move leaves a record without a folder, which the listing ignores. - var recordPath = Path.Combine(trash, name + RecordExtension); - AtomicFile.WriteAllText(recordPath, JsonSerializer.Serialize(record, TrashJson.Default.TrashRecord)); - try - { - Directory.Move(folder.Folder, Path.Combine(trash, name)); - } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) - { - // A file of the folder is open elsewhere: nothing moved, so the record goes and the rows stay. - File.Delete(recordPath); - throw; - } - entry = Entry(archive.Id, name, record); + await RemoveRowsAsync(archive.Id, row, cancellationToken).ConfigureAwait(false); + Log.Info("archive", $"{row.DisplayTitle} removed from the index (no folder yet)"); + return null; } - await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false)) + + var folder = paths.Sequence(sequence.Folder); + if (!Directory.Exists(folder.Folder) || (File.Exists(folder.ManifestPath) && SidecarId(folder.ManifestPath) != sequence.Id)) + throw new StaleDeletePlanException(sequence.Id); + var trash = TrashDirectory(paths); + Directory.CreateDirectory(trash); + var name = EntryName(paths, sequence.Id, new HashSet()); + var target = Path.Combine(trash, name); + var record = new TrashRecord(row, await _db.Renders.ListBySequenceAsync(sequence.Id, cancellationToken).ConfigureAwait(false), step.Files, + step.Bytes, DateTimeOffset.UtcNow, deletedBy); + cancellationToken.ThrowIfCancellationRequested(); + // The record first: a crash before the move leaves a record without a folder, which the listing ignores. + var recordPath = target + RecordExtension; + AtomicFile.WriteAllText(recordPath, JsonSerializer.Serialize(record, TrashJson.Default.TrashRecord)); + try { - await tx.Sequences.DeleteAsync(sequence.Id, cancellationToken).ConfigureAwait(false); - await tx.Archives.DeleteFolderStateAsync(archive.Id, sequence.Folder, cancellationToken).ConfigureAwait(false); - await tx.CommitAsync(cancellationToken).ConfigureAwait(false); + Directory.Move(folder.Folder, target); } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + // A file of the folder is open elsewhere: nothing moved, so the record goes and the rows stay. + File.Delete(recordPath); + throw; + } + try + { + // The folder has moved: its rows go too, to the end, whatever the caller cancels meanwhile. + await RemoveRowsAsync(archive.Id, row, CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) + { + Log.Warning("archive", $"{row.DisplayTitle} could not leave the index; its folder goes back", ex); + if (MoveBack(target, folder.Folder)) File.Delete(recordPath); + throw; + } + Log.Info("archive", $"{row.DisplayTitle} moved to the trash: {row.Folder} → {name} ({step.Files} files, {step.Bytes} bytes)"); + return Entry(archive.Id, name, record); } - Log.Info("archive", entry is null - ? $"{sequence.DisplayTitle} removed from the index (no folder on disk)" - : $"{sequence.DisplayTitle} moved to the trash: {sequence.Folder} → {entry.Name} ({step.Files} files, {step.Bytes} bytes)"); - return entry; } - private static DeleteBlock? Block(Sequence sequence, ArchivePaths paths, bool online, IReadOnlyList locations, - IReadOnlySet busy) + private async Task RemoveRowsAsync(Guid archiveId, Sequence sequence, CancellationToken cancellationToken) + { + await using var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false); + await tx.Sequences.DeleteAsync(sequence.Id, cancellationToken).ConfigureAwait(false); + // A sequence without a folder has no scan state; the root's belongs to a folder adopted in place. + if (sequence.Folder.Length > 0) await tx.Archives.DeleteFolderStateAsync(archiveId, sequence.Folder, cancellationToken).ConfigureAwait(false); + await tx.CommitAsync(cancellationToken).ConfigureAwait(false); + } + + /// Undoes a folder move after a failed index step; false (logged) when the folder cannot move back either. + private static bool MoveBack(string from, string to) { - if (!online) return DeleteBlock.Offline; - if (busy.Contains(sequence.Id)) return DeleteBlock.Busy; - // An empty folder is the archive root of a folder adopted in place, or a server sequence still waiting for its first frame. - if (sequence.Folder.Length == 0) return sequence.FrameCount > 0 ? DeleteBlock.ArchiveRoot : null; - var prefix = sequence.Folder + "/"; - if (locations.Any(l => l.Id != sequence.Id && l.Folder.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))) return DeleteBlock.ContainsSequences; - var sidecar = paths.Sequence(sequence.Folder).ManifestPath; try { - if (File.Exists(sidecar) && ManifestStore.ReadSequence(sidecar).Id != sequence.Id) return DeleteBlock.ForeignSidecar; + Directory.Move(from, to); + return true; } - catch (InvalidDataException) + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { - return DeleteBlock.ForeignSidecar; + Log.Error("archive", $"{from} could not move back to {to}; it stays where it is", ex); + return false; } - return null; } - /// Sequences a running or paused job works on (an import, a render, a transfer, a proxy pass). - private async Task> BusySequencesAsync(CancellationToken cancellationToken) + private async Task ContextAsync(Dictionary known, Guid archiveId, CancellationToken cancellationToken) + { + if (known.TryGetValue(archiveId, out var context)) return context; + if (await _db.Archives.GetAsync(archiveId, cancellationToken).ConfigureAwait(false) is { } archive) + { + var paths = new ArchivePaths(archive.RootPath); + context = new ArchiveContext(paths, ArchiveVolume.Probe(paths), + await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false)); + } + known[archiveId] = context; + return context; + } + + private static DeleteBlock? Block(Sequence sequence, ArchiveContext context, IReadOnlySet busy) + { + if (context.Availability != ArchiveAvailability.Online) return DeleteBlock.Offline; + if (busy.Contains(sequence.Id) || busy.Contains(sequence.ArchiveId)) return DeleteBlock.Busy; + // An empty folder is the archive root of a folder adopted in place, or a server sequence still waiting for its first frame. + if (sequence.Folder.Length == 0) return sequence.FrameCount > 0 ? DeleteBlock.ArchiveRoot : null; + var folder = context.Paths.Sequence(sequence.Folder); + if (!Directory.Exists(folder.Folder)) return DeleteBlock.FolderMissing; + var prefix = sequence.Folder + "/"; + if (context.Locations.Any(l => l.Id != sequence.Id && l.Folder.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))) return DeleteBlock.ContainsSequences; + return File.Exists(folder.ManifestPath) && SidecarId(folder.ManifestPath) != sequence.Id ? DeleteBlock.ForeignSidecar : null; + } + + /// + /// What running and paused jobs hold: the sequences they target (renders, analyses, proxies, transfers) and the + /// archives of archive-wide ones (scan, rebuild, import, maintenance, a verify of everything). An import's folders + /// are known only to its plan, so it holds its whole archive; a verify of named sequences holds only those. + /// + private async Task> BusyAsync(CancellationToken cancellationToken) { var busy = new HashSet(); foreach (var state in new[] { JobState.Running, JobState.Paused }) @@ -324,7 +429,7 @@ public sealed class SequenceTrash do { var page = await _db.Jobs.ListAsync(state, JobPage, cursor, cancellationToken).ConfigureAwait(false); - foreach (var job in page.Items) busy.UnionWith(job.TargetIds); + foreach (var job in page.Items) busy.UnionWith(Holds(job)); cursor = page.NextCursor; } while (cursor is not null); @@ -332,6 +437,50 @@ public sealed class SequenceTrash return busy; } + private static IReadOnlyList Holds(Job job) + { + if (job.Kind != JobKind.Verify) return job.TargetIds; + try + { + return ArchiveJobJson.Read(job, ArchiveJobJson.Default.VerifyParameters, new VerifyParameters(null)).SequenceIds ?? job.TargetIds; + } + catch (JsonException) + { + return job.TargetIds; + } + } + + /// + /// The id a sidecar names, from its head (the id is written second), so checking a 35 MB sidecar does not parse it; + /// the whole file is read only when the head does not hold the id. Null when the file is unreadable. + /// + private static Guid? SidecarId(string path) + { + try + { + var head = new byte[SidecarHead]; + int length; + using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)) + length = stream.ReadAtLeast(head, head.Length, throwOnEndOfStream: false); + var reader = new Utf8JsonReader(head.AsSpan(0, length), isFinalBlock: length < head.Length, state: default); + while (reader.Read()) + { + if (reader.TokenType != JsonTokenType.PropertyName || reader.CurrentDepth != 1) continue; + if (reader.ValueTextEquals("id"u8)) + { + if (!reader.Read()) break; + return reader.TokenType == JsonTokenType.String && reader.TryGetGuid(out var id) ? id : null; + } + if (!reader.TrySkip()) break; + } + return ManifestStore.ReadSequence(path).Id; + } + catch (Exception ex) when (ex is JsonException or InvalidDataException or IOException or UnauthorizedAccessException) + { + return null; + } + } + private static (int Files, long Bytes) Measure(string folder, CancellationToken cancellationToken) { int files = 0; @@ -385,6 +534,9 @@ public sealed class SequenceTrash return null; } } + + /// An archive as the plan sees it: where it is, whether it can be written, and where its sequences are. + private sealed record ArchiveContext(ArchivePaths Paths, ArchiveAvailability Availability, IReadOnlyList Locations); } /// What the trash keeps next to a deleted folder: the index row and renders the sidecar does not carry. diff --git a/tests/Titano.Data.Tests/Archiving/ArchiveIndexTests.cs b/tests/Titano.Data.Tests/Archiving/ArchiveIndexTests.cs index 71d41e21..1d6c4542 100644 --- a/tests/Titano.Data.Tests/Archiving/ArchiveIndexTests.cs +++ b/tests/Titano.Data.Tests/Archiving/ArchiveIndexTests.cs @@ -177,4 +177,22 @@ public sealed class ArchiveIndexTests(TestContext context) Assert.SequenceEqual(["Night"], (await fx.Db.Sequences.GetAsync(comet.Id))!.Tags); Assert.Equal(0, (await fx.ScanAsync()).Indexed); } + + [Test] + public async Task AnEditOfOneFieldKeepsWhatChangedSinceTheRowWasRead() + { + await using var fx = await CreateAsync(context); + Populate(fx.Paths); + await fx.ScanAsync(); + var comet = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(l => l.Folder == "Trips/Comet"); + var stale = (await fx.Db.Sequences.GetAsync(comet.Id))!; + var edited = await fx.Service.SetSequenceInfoAsync(comet.Id, "Comet 2026", ["night"], 2, "Tripod", fx.Ct); + + var rated = await fx.Service.EditSequenceInfoAsync(stale.Id, info => info with { Rating = 5 }, fx.Ct); + + Assert.Equal(("Comet 2026", "night", 5, "Tripod"), (rated.Title, rated.Tags.Single(), rated.Rating, rated.Notes)); + Assert.Equal(edited.SettingsVersion + 1, rated.SettingsVersion); + var manifest = ManifestStore.ReadSequence(fx.Paths.Sequence(comet.Folder).ManifestPath); + Assert.Equal(("Comet 2026", "night", 5, "Tripod"), (manifest.Title, manifest.Tags.Single(), manifest.Rating, manifest.Notes)); + } } diff --git a/tests/Titano.Data.Tests/Archiving/SequenceTrashTests.cs b/tests/Titano.Data.Tests/Archiving/SequenceTrashTests.cs index c2dc3eb0..8d06282a 100644 --- a/tests/Titano.Data.Tests/Archiving/SequenceTrashTests.cs +++ b/tests/Titano.Data.Tests/Archiving/SequenceTrashTests.cs @@ -57,6 +57,40 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con return render; } + private static Job Running(JobKind kind, IReadOnlyList targets, string? parameters = null) => new() + { + Id = Guid.CreateVersion7(), + Kind = kind, + TargetIds = targets, + ParametersJson = parameters, + State = JobState.Running, + NodeId = "test#0", + Created = DateTimeOffset.UtcNow, + Started = DateTimeOffset.UtcNow, + }; + + /// Makes the next write of on the sequences table fail, as a lost database connection would. + private async Task FailSequencesAsync(ArchiveFixture fx, string operation) + { + await using var session = await fx.Db.Database.OpenAsync(fx.Ct); + await session.ExecuteScriptAsync(ForDialect( + $"CREATE TRIGGER fail_{operation} BEFORE {operation} ON sequences BEGIN SELECT RAISE(ABORT, 'injected'); END;", + $"CREATE FUNCTION fail_{operation}() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'injected'; END $$; " + + $"CREATE TRIGGER fail_{operation} BEFORE {operation} ON sequences FOR EACH ROW EXECUTE FUNCTION fail_{operation}();"), fx.Ct); + } + + private async Task HealSequencesAsync(ArchiveFixture fx, string operation) + { + await using var session = await fx.Db.Database.OpenAsync(fx.Ct); + await session.ExecuteScriptAsync(ForDialect($"DROP TRIGGER fail_{operation};", $"DROP TRIGGER fail_{operation} ON sequences;"), fx.Ct); + } + + /// Reports on the calling thread, so a test can act between two sequences. + private sealed class Inline(Action report) : IProgress + { + public void Report(int value) => report(value); + } + [Test] public async Task TheDryRunCountsWhatMovesAndChangesNothing() { @@ -65,7 +99,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con var before = await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id); var folder = fx.Paths.Sequence(sequences[0].Folder).Folder; - var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id, sequences[1].Id], fx.Ct); + var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id, sequences[1].Id], null, fx.Ct); Assert.Empty(plan.Blocked); Assert.Count(2, plan.Steps); var (files, bytes) = OnDisk(folder); @@ -88,7 +122,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con var folder = fx.Paths.Sequence(victim.Folder).Folder; var (files, bytes) = OnDisk(folder); - var plan = await fx.Service.PlanDeleteAsync([victim.Id], fx.Ct); + var plan = await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct); var entries = await fx.Service.DeleteAsync(plan, "alberto", null, fx.Ct); var entry = Assert.Contains(entries, e => e.SequenceId == victim.Id); @@ -123,7 +157,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con var before = await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id); var settings = (await fx.Db.Sequences.GetAsync(victim.Id))!.SettingsJson; - var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], fx.Ct), null, null, fx.Ct)).Single(); + var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct)).Single(); var restored = await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct); Assert.Equal(victim.Folder, restored.Folder); @@ -136,7 +170,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con Assert.Equal(0, (await fx.ScanAsync()).Indexed); // Deleted again: the trash takes it again under the same name. - var again = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], fx.Ct), null, null, fx.Ct)).Single(); + var again = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct)).Single(); Assert.Equal(entry.Name, again.Name); } @@ -147,7 +181,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con var sequences = await PopulateAsync(fx); var victim = sequences[2]; await RenderAsync(fx, victim); - var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], fx.Ct), null, null, fx.Ct)).Single(); + var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct)).Single(); Photos.WriteSeries(Path.Combine(fx.Paths.Root, "inbox", "c"), Start.AddDays(5), 2, firstSeed: 40); await fx.ScanAsync(); @@ -165,14 +199,20 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con { var fx = await FixtureAsync(); var sequences = await PopulateAsync(fx); - var victim = sequences[1]; - var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], fx.Ct), null, null, fx.Ct)).Single(); + var victim = await fx.Service.SetSequenceInfoAsync(sequences[1].Id, "Ridge", ["fog"], 4, "Tripod", fx.Ct); + var hero = (await fx.Db.Frames.ListBySequenceAsync(victim.Id)).Last().Id; + await fx.Db.Sequences.UpsertAsync(victim with { HeroFrameId = hero }, fx.Ct); + var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct)).Single(); var trashed = fx.Paths.Sequence(fx.Paths.ToRelative(Path.Combine(SequenceTrash.TrashDirectory(fx.Paths), entry.Name))); File.Delete(trashed.ManifestPath); var restored = await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct); Assert.Equal(victim.Id, restored.Id); + Assert.Null(restored.HeroFrameId, "the frames were adopted with new ids"); + var sidecar = ManifestStore.ReadSequence(fx.Paths.Sequence(victim.Folder).ManifestPath); + Assert.Equal(("Ridge", "fog", 4, "Tripod"), (sidecar.Title, sidecar.Tags.Single(), sidecar.Rating, sidecar.Notes)); + Assert.Equal(("Ridge", 4), (restored.Title, restored.Rating)); Assert.Equal(victim.Folder, restored.Folder); Assert.Equal(3, restored.FrameCount); Assert.Count(3, await fx.Db.Frames.ListBySequenceAsync(victim.Id)); @@ -187,21 +227,11 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con var sequences = await PopulateAsync(fx); Photos.WriteSeries(Path.Combine(fx.Paths.Root, "inbox", "a", "night"), Start.AddDays(3), 2, firstSeed: 30); await fx.ScanAsync(); - var running = new Job - { - Id = Guid.CreateVersion7(), - Kind = JobKind.Proxies, - TargetIds = [sequences[1].Id], - State = JobState.Running, - NodeId = "test#0", - Created = DateTimeOffset.UtcNow, - Started = DateTimeOffset.UtcNow, - }; - await fx.Db.Jobs.EnqueueAsync(running); + await fx.Db.Jobs.EnqueueAsync(Running(JobKind.Proxies, [sequences[1].Id])); // The folder of the third sequence now holds the sidecar of another sequence (copied over by hand). File.Copy(fx.Paths.Sequence(sequences[0].Folder).ManifestPath, fx.Paths.Sequence(sequences[2].Folder).ManifestPath, overwrite: true); - var plan = await fx.Service.PlanDeleteAsync([.. sequences.Select(s => s.Id)], fx.Ct); + var plan = await fx.Service.PlanDeleteAsync([.. sequences.Select(s => s.Id)], null, fx.Ct); Assert.Empty(plan.Steps); Assert.SequenceEqual([DeleteBlock.ContainsSequences, DeleteBlock.Busy, DeleteBlock.ForeignSidecar], plan.Blocked.OrderBy(b => b.Sequence.Folder, StringComparer.Ordinal).Select(b => b.Reason)); @@ -217,7 +247,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con var location = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(); Assert.Equal(string.Empty, location.Folder); - var plan = await fx.Service.PlanDeleteAsync([location.Id], fx.Ct); + var plan = await fx.Service.PlanDeleteAsync([location.Id], null, fx.Ct); Assert.Equal(DeleteBlock.ArchiveRoot, plan.Blocked.Single().Reason); } @@ -226,7 +256,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con { var fx = await FixtureAsync(); var sequences = await PopulateAsync(fx); - var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id, sequences[1].Id], fx.Ct); + var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id, sequences[1].Id], null, fx.Ct); await fx.Service.ApplyRenameAsync(await fx.Service.PlanRenameAsync(fx.Archive.Id, [sequences[1].Id], "moved/{date}", null, fx.Ct), null, fx.Ct); var stale = await Assert.ThrowsAsync(() => fx.Service.DeleteAsync(plan, null, null, fx.Ct)); @@ -236,16 +266,136 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con } [Test] - public async Task ARowWithoutAFolderLeavesOnlyTheIndex() + public async Task ARowWhoseFolderIsMissingStaysForTheOrphansCheck() { var fx = await FixtureAsync(); var sequences = await PopulateAsync(fx); Directory.Delete(fx.Paths.Sequence(sequences[0].Folder).Folder, recursive: true); - var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id], fx.Ct); + var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id], null, fx.Ct); + Assert.Empty(plan.Steps); + Assert.Equal(DeleteBlock.FolderMissing, plan.Blocked.Single().Reason); + Assert.NotNull(await fx.Db.Sequences.GetAsync(sequences[0].Id)); + } + + [Test] + public async Task ASequenceWithoutAFolderYetLosesOnlyItsRows() + { + var fx = await FixtureAsync(); + var empty = new Sequence + { + Id = Guid.CreateVersion7(), + ArchiveId = fx.Archive.Id, + Title = "Waiting for photos", + Folder = string.Empty, + Origin = SequenceOrigin.Server, + SyncState = SyncState.InSync, + UpdatedAt = DateTimeOffset.UtcNow, + }; + await fx.Db.Sequences.UpsertAsync(empty, fx.Ct); + + var plan = await fx.Service.PlanDeleteAsync([empty.Id], null, fx.Ct); Assert.Null(plan.Steps.Single().TrashFolder); Assert.Empty(await fx.Service.DeleteAsync(plan, null, null, fx.Ct)); + Assert.Null(await fx.Db.Sequences.GetAsync(empty.Id)); + } + + [Test] + public async Task JobsOnTheWholeArchiveHoldEverySequenceOfIt() + { + var fx = await FixtureAsync(); + var sequences = await PopulateAsync(fx); + var ids = sequences.Select(s => s.Id).ToList(); + + // A verify of one named sequence holds only that one. + var verifyOne = Running(JobKind.Verify, [fx.Archive.Id], ArchiveJobs.Verify(fx.Archive, [sequences[2].Id]).ParametersJson); + await fx.Db.Jobs.EnqueueAsync(verifyOne); + var plan = await fx.Service.PlanDeleteAsync(ids, null, fx.Ct); + Assert.Equal(sequences[2].Id, plan.Blocked.Single(b => b.Reason == DeleteBlock.Busy).Sequence.Id); + Assert.Count(2, plan.Steps); + + // An import (whose folders only its plan knows) holds the whole archive, and so does a paused one. + var import = Running(JobKind.Import, [fx.Archive.Id]) with { State = JobState.Paused }; + await fx.Db.Jobs.EnqueueAsync(import); + plan = await fx.Service.PlanDeleteAsync(ids, null, fx.Ct); + Assert.Empty(plan.Steps); + Assert.True(plan.Blocked.All(b => b.Reason == DeleteBlock.Busy)); + + // A plan made before the job started is refused when it runs. + await fx.Db.Jobs.SetStateAsync(import.Id, JobState.Done, null, fx.Ct); + var before = await fx.Service.PlanDeleteAsync([sequences[0].Id], null, fx.Ct); + await fx.Db.Jobs.EnqueueAsync(Running(JobKind.Scan, [fx.Archive.Id])); + await Assert.ThrowsAsync(() => fx.Service.DeleteAsync(before, null, null, fx.Ct)); + Assert.True(Directory.Exists(fx.Paths.Sequence(sequences[0].Folder).Folder)); + } + + [Test] + public async Task WhenTheIndexCannotLetGoTheFolderComesBack() + { + var fx = await FixtureAsync(); + var sequences = await PopulateAsync(fx); + var victim = sequences[0]; + var folder = fx.Paths.Sequence(victim.Folder).Folder; + var before = await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id); + var plan = await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct); + + await FailSequencesAsync(fx, "DELETE"); + await Assert.ThrowsAsync(() => fx.Service.DeleteAsync(plan, null, null, fx.Ct)); + + Assert.True(Directory.Exists(folder), "the folder is back where it was"); + Assert.Equal(before, await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id)); + Assert.Empty(Directory.EnumerateFileSystemEntries(SequenceTrash.TrashDirectory(fx.Paths))); + await HealSequencesAsync(fx, "DELETE"); + Assert.Count(1, await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct)); + } + + [Test] + public async Task ARestoreThatCannotBeIndexedLeavesTheEntryInTheTrash() + { + var fx = await FixtureAsync(); + var sequences = await PopulateAsync(fx); + var victim = sequences[1]; + var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct)).Single(); + + await FailSequencesAsync(fx, "INSERT"); + await Assert.ThrowsAsync(() => fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct)); + + Assert.False(Directory.Exists(fx.Paths.Sequence(victim.Folder).Folder)); + Assert.Null(await fx.Db.Sequences.GetAsync(victim.Id)); + Assert.Equal(entry.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name); + await HealSequencesAsync(fx, "INSERT"); + Assert.Equal(victim.Folder, (await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct)).Folder); + } + + [Test] + public async Task CancellingStopsBetweenTwoWholeSequences() + { + var fx = await FixtureAsync(); + var sequences = await PopulateAsync(fx); + var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id, sequences[1].Id], null, fx.Ct); + using var cancel = CancellationTokenSource.CreateLinkedTokenSource(fx.Ct); + + await Assert.ThrowsAsync(() => fx.Service.DeleteAsync(plan, null, new Inline(_ => cancel.Cancel()), cancel.Token)); + Assert.Null(await fx.Db.Sequences.GetAsync(sequences[0].Id)); + Assert.Count(1, await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)); + Assert.NotNull(await fx.Db.Sequences.GetAsync(sequences[1].Id)); + Assert.True(Directory.Exists(fx.Paths.Sequence(sequences[1].Folder).Folder)); + } + + [Test] + public async Task ACancelledDryRunStopsAndReportsWhatItChecked() + { + var fx = await FixtureAsync(); + var sequences = await PopulateAsync(fx); + var checkedCounts = new List(); + var plan = await fx.Service.PlanDeleteAsync([.. sequences.Select(s => s.Id)], new Inline(checkedCounts.Add), fx.Ct); + Assert.SequenceEqual([1, 2, 3], checkedCounts); + Assert.Count(3, plan.Steps); + + using var cancel = CancellationTokenSource.CreateLinkedTokenSource(fx.Ct); + await Assert.ThrowsAsync(() => + fx.Service.PlanDeleteAsync([.. sequences.Select(s => s.Id)], new Inline(_ => cancel.Cancel()), cancel.Token)); } }