From f4e1d02c1d460842bb42b4a10050e6c7a8cdbe84 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 3 Oct 2026 16:49:10 +0000 Subject: [PATCH] fix(data): restore a sequence whose folder lost its sidecar Restore left a folder without a sidecar out of the index until the next scan, so Undo and the trash's Restore seemed to do nothing; such a folder is now adopted under the sequence's own id, as a scan would adopt it, and comes back at once with its settings and renders. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Data/Archiving/ArchiveService.cs | 4 +-- src/Titano.Data/Archiving/SequenceTrash.cs | 25 +++++++++++-------- .../Archiving/SequenceTrashTests.cs | 24 ++++++++++++++++-- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/Titano.Data/Archiving/ArchiveService.cs b/src/Titano.Data/Archiving/ArchiveService.cs index f474a4ca..b338d554 100644 --- a/src/Titano.Data/Archiving/ArchiveService.cs +++ b/src/Titano.Data/Archiving/ArchiveService.cs @@ -178,8 +178,8 @@ public sealed class ArchiveService : IJobHandlerProvider public async Task> ListTrashAsync(Guid archiveId, CancellationToken cancellationToken = default) => await Trash.ListAsync(await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false), cancellationToken).ConfigureAwait(false); - /// Moves a sequence back from the trash and indexes it again; null when its folder has no sidecar (a scan adopts it). - public async Task RestoreAsync(Guid archiveId, string entryName, CancellationToken cancellationToken = default) => + /// Moves a sequence back from the trash and indexes it again. + public async Task RestoreAsync(Guid archiveId, string entryName, CancellationToken cancellationToken = default) => await Trash.RestoreAsync(await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false), entryName, cancellationToken).ConfigureAwait(false); /// Plans a card or folder import into an archive (nothing is copied). diff --git a/src/Titano.Data/Archiving/SequenceTrash.cs b/src/Titano.Data/Archiving/SequenceTrash.cs index 87c72033..a25a8bbf 100644 --- a/src/Titano.Data/Archiving/SequenceTrash.cs +++ b/src/Titano.Data/Archiving/SequenceTrash.cs @@ -185,12 +185,12 @@ 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. Returns the restored row; null when the folder has - /// no sidecar (the next scan adopts it). + /// 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. /// /// The index already holds that sequence. /// The entry is not in the trash. - public async Task RestoreAsync(Archive archive, string name, CancellationToken cancellationToken = default) + public async Task RestoreAsync(Archive archive, string name, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(archive); ArgumentException.ThrowIfNullOrWhiteSpace(name); @@ -214,12 +214,7 @@ public sealed class SequenceTrash Directory.CreateDirectory(Path.GetDirectoryName(sequence.Folder)!); Directory.Move(source, sequence.Folder); Log.Info("archive", $"{record.Sequence.DisplayTitle} restored from the trash to {folder}"); - if (!File.Exists(sequence.ManifestPath)) - { - File.Delete(recordPath); - return null; - } - var manifest = SequenceWriter.ReadLocked(sequence, id); + 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)) { @@ -233,7 +228,17 @@ public sealed class SequenceTrash await _indexer.RecordStampsAsync(archive, sequence, id, cancellationToken).ConfigureAwait(false); File.Delete(recordPath); } - return await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false); + return await _db.Sequences.GetAsync(id, cancellationToken).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) + { + var manifest = SequenceAdopter.Build(id, sequence, _writer.Options.Owner, cancellationToken); + Directory.CreateDirectory(sequence.MetaDirectory); + ManifestStore.WriteSequence(sequence.ManifestPath, manifest); + return manifest; } private async Task DeleteOneAsync(DeleteStep step, string? deletedBy, CancellationToken cancellationToken) diff --git a/tests/Titano.Data.Tests/Archiving/SequenceTrashTests.cs b/tests/Titano.Data.Tests/Archiving/SequenceTrashTests.cs index 0d5632a5..c2dc3eb0 100644 --- a/tests/Titano.Data.Tests/Archiving/SequenceTrashTests.cs +++ b/tests/Titano.Data.Tests/Archiving/SequenceTrashTests.cs @@ -124,7 +124,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con 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 restored = Assert.NotNull(await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct)); + var restored = await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct); Assert.Equal(victim.Folder, restored.Folder); Assert.Equal(settings, restored.SettingsJson); @@ -151,7 +151,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con Photos.WriteSeries(Path.Combine(fx.Paths.Root, "inbox", "c"), Start.AddDays(5), 2, firstSeed: 40); await fx.ScanAsync(); - var restored = Assert.NotNull(await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct)); + var restored = await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct); Assert.Equal("inbox/c (2)", restored.Folder); var output = (await fx.Db.Renders.ListBySequenceAsync(victim.Id)).Single().OutputPath; Assert.Equal("inbox/c (2)/exports/preview.mp4", output); @@ -160,6 +160,26 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con Assert.Count(4, await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)); } + [Test] + public async Task AFolderThatLostItsSidecarComesBackUnderItsId() + { + 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 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.Equal(victim.Folder, restored.Folder); + Assert.Equal(3, restored.FrameCount); + Assert.Count(3, await fx.Db.Frames.ListBySequenceAsync(victim.Id)); + Assert.Equal(victim.Id, ManifestStore.ReadSequence(fx.Paths.Sequence(victim.Folder).ManifestPath).Id); + Assert.Equal(0, (await fx.ScanAsync()).Indexed, "the index and the new sidecar agree"); + } + [Test] public async Task SequencesThatCannotMoveAreBlockedAndUntouched() {