Files
Chronos/tests/Titano.Data.Tests/Archiving/ArchiveIndexTests.cs
T
Claude 2b2d341497 fix(data): trash moves finish whatever is cancelled, archive-wide jobs hold their sequences
Once a folder has moved into the trash (or back), its index step runs to the
end with no cancellation and a failed step moves the folder back, so trash and
index never disagree. Running or paused archive-wide jobs (scan, rebuild,
import, maintenance, a verify of everything) block a delete of every sequence
of their archive; a verify of named sequences blocks those. A row whose folder
is gone is refused (FolderMissing) instead of losing its settings. Delete
revalidates the plan cheaply (index row, jobs, sidecar id from its head)
instead of measuring every folder again; the dry run reports progress. A
restored folder without a sidecar gets the record's title, tags, rating and
notes and drops a hero frame it no longer has. EditSequenceInfoAsync changes
one field from the current sidecar values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-05 22:06:31 +00:00

199 lines
10 KiB
C#

using Titano.Core.Archiving;
using Titano.Core.Model;
using Titano.Data.Archiving;
using Titano.Testing;
using static Titano.Data.Tests.Archiving.ArchiveFixture;
namespace Titano.Data.Tests.Archiving;
[Category("archive")]
public sealed class ArchiveIndexTests(TestContext context)
{
/// <summary>Two folders adopted in place and one folder in the layout whose sidecar was lost.</summary>
private static void Populate(ArchivePaths paths)
{
Photos.WriteSeries(Path.Combine(paths.Root, "Trips", "Harbour sunset"), Start, 6);
Photos.WriteSeries(Path.Combine(paths.Root, "Trips", "Comet"), Start.AddDays(1), 4, firstSeed: 100);
Photos.WriteSeries(Path.Combine(paths.Root, "2026", "2026-09-30 Canon EOS R5", "originals"), Start.AddDays(2), 5, firstSeed: 200);
}
[Test]
public async Task AdoptionIndexesFoldersInPlaceWithoutTouchingPhotos()
{
await using var fx = await CreateAsync(context);
Populate(fx.Paths);
var photos = Directory.EnumerateFiles(fx.Paths.Root, "*.JPG", SearchOption.AllDirectories).ToDictionary(p => p, File.ReadAllBytes);
var summary = await fx.ScanAsync(ScanMode.Full);
Assert.Equal(3, summary.Adopted);
Assert.Empty(summary.Failed);
var stats = await fx.Db.Sequences.GetStatsAsync(fx.Archive.Id);
Assert.Equal(3, stats.Sequences);
Assert.Equal(15L, stats.Photos);
var harbour = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(l => l.Folder == "Trips/Harbour sunset");
Assert.Equal("Harbour sunset", harbour.Title);
var frames = await fx.Db.Frames.ListBySequenceAsync(harbour.Id);
Assert.Equal("Trips/Harbour sunset/IMG_0001.JPG", frames[0].RelativePath);
Assert.Equal(Start, frames[0].CapturedAt);
Assert.Equal(10.0, (await fx.Db.Sequences.GetAsync(harbour.Id))!.MedianInterval);
Assert.FileExists(Path.Combine(fx.Paths.Root, "Trips", "Harbour sunset", ".titano", "sequence.json"));
foreach (var (path, bytes) in photos) Assert.BytesEqual(bytes, File.ReadAllBytes(path));
}
[Test]
public async Task AnUnchangedArchiveRescansWithoutReadingSidecars()
{
await using var fx = await CreateAsync(context);
Populate(fx.Paths);
await fx.ScanAsync();
var again = await fx.ScanAsync();
Assert.Equal(3, again.Folders);
Assert.Equal(0, again.Indexed);
// A photo dropped into a folder and a title edited in a sidecar by another machine.
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "Trips", "Comet"), Start.AddDays(1).AddMinutes(1), 1, firstSeed: 150, firstNumber: 5);
var sidecar = fx.Paths.Sequence("Trips/Harbour sunset").ManifestPath;
var manifest = ManifestStore.ReadSequence(sidecar);
ManifestStore.WriteSequence(sidecar, manifest with { Title = "Harbour at dusk", Versions = manifest.Versions with { Settings = 5 } });
var changed = await fx.ScanAsync();
Assert.Equal(2, changed.Indexed);
var locations = await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id);
Assert.Equal("Harbour at dusk", locations.Single(l => l.Folder == "Trips/Harbour sunset").Title);
Assert.Equal(5, (await fx.Db.Sequences.GetAsync(locations.Single(l => l.Folder == "Trips/Comet").Id))!.FrameCount);
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
}
[Test]
public async Task RebuildingFromSidecarsGivesTheSameIndex()
{
await using var fx = await CreateAsync(context);
Populate(fx.Paths);
await fx.ScanAsync(ScanMode.Full);
var comet = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(l => l.Folder == "Trips/Comet");
await fx.Service.SetSequenceInfoAsync(comet.Id, "Comet", ["sky", "night"], 5, "Clear", fx.Ct);
var before = await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id);
await fx.ReopenWithEmptyIndexAsync();
var rebuilt = await fx.Service.Indexer.ScanAsync(fx.Archive, ScanMode.Rebuild, null, _ => ValueTask.CompletedTask, fx.Ct);
Assert.Equal(0, rebuilt.Adopted);
Assert.Equal(before, await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id));
// A second rebuild reads the snapshot written by the first and still yields the same index.
Assert.NotNull(IndexSnapshot.TryLoad(fx.Paths.SnapshotPath));
await fx.ReopenWithEmptyIndexAsync();
await fx.Service.Indexer.ScanAsync(fx.Archive, ScanMode.Rebuild, null, _ => ValueTask.CompletedTask, fx.Ct);
Assert.Equal(before, await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id));
}
[Test]
public async Task RebuildDropsRowsWhoseSidecarIsGoneAndScanKeepsThem()
{
await using var fx = await CreateAsync(context);
Populate(fx.Paths);
await fx.ScanAsync();
Directory.Move(Path.Combine(fx.Paths.Root, "Trips", "Comet"), Path.Combine(context.CreateTempDirectory("elsewhere"), "Comet"));
await fx.ScanAsync();
Assert.Equal(3, (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Count);
var orphans = await fx.Service.FindOrphansAsync(fx.Archive.Id, fx.Ct);
Assert.Equal("Trips/Comet", orphans.MissingSequences.Single().Folder);
var summary = await fx.ScanAsync(ScanMode.Rebuild);
Assert.Equal(1, summary.Removed);
Assert.Equal(2, (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Count);
}
[Test]
public async Task ADamagedSidecarIsKeptAsideAndRebuiltWithTheSameIds()
{
await using var fx = await CreateAsync(context);
Populate(fx.Paths);
await fx.ScanAsync();
var harbour = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(l => l.Folder == "Trips/Harbour sunset");
var frameIds = (await fx.Db.Frames.ListBySequenceAsync(harbour.Id)).Select(f => f.Id).ToList();
var sidecar = fx.Paths.Sequence(harbour.Folder).ManifestPath;
File.WriteAllText(sidecar, "{ \"id\": \"broken");
var summary = await fx.ScanAsync();
Assert.Empty(summary.Failed);
Assert.Equal(harbour.Id, ManifestStore.ReadSequence(sidecar).Id);
Assert.SequenceEqual(frameIds, (await fx.Db.Frames.ListBySequenceAsync(harbour.Id)).Select(f => f.Id));
Assert.Count(1, Directory.GetFiles(Path.GetDirectoryName(sidecar)!, "sequence.json.damaged-*"));
}
[Test]
public async Task ACopiedSequenceFolderGetsItsOwnIdentity()
{
await using var fx = await CreateAsync(context);
Populate(fx.Paths);
await fx.ScanAsync();
var source = Path.Combine(fx.Paths.Root, "Trips", "Comet");
var copy = Path.Combine(fx.Paths.Root, "Trips", "Comet copy");
Directory.CreateDirectory(Path.Combine(copy, ".titano"));
foreach (var file in Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories))
File.Copy(file, Path.Combine(copy, Path.GetRelativePath(source, file)));
await fx.ScanAsync();
var locations = await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id);
Assert.Equal(4, locations.Count);
var original = locations.Single(l => l.Folder == "Trips/Comet");
var copied = locations.Single(l => l.Folder == "Trips/Comet copy");
Assert.NotEqual(original.Id, copied.Id);
Assert.Equal(4, (await fx.Db.Frames.ListBySequenceAsync(original.Id)).Count);
Assert.Equal(4, (await fx.Db.Frames.ListBySequenceAsync(copied.Id)).Count);
Assert.Equal(copied.Id, ManifestStore.ReadSequence(fx.Paths.Sequence("Trips/Comet copy").ManifestPath).Id);
}
[Test]
public async Task AnOfflineArchiveIsLeftAsItWas()
{
var root = context.CreateTempDirectory("archive");
await using var fx = await CreateAsync(context, root);
Populate(fx.Paths);
await fx.ScanAsync();
var before = await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id);
var unplugged = root + "-unplugged";
Directory.Move(root, unplugged);
Assert.Equal(ArchiveAvailability.Offline, ArchiveService.GetAvailability(fx.Archive));
await Assert.ThrowsAsync<ArchiveOfflineException>(() => fx.ScanAsync(ScanMode.Rebuild));
Assert.Equal(before, await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id));
Directory.Move(unplugged, root);
Assert.Equal(ArchiveAvailability.Online, ArchiveService.GetAvailability(fx.Archive));
}
[Test]
public async Task SequenceInfoIsWrittenToSidecarAndIndexTogether()
{
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 updated = await fx.Service.SetSequenceInfoAsync(comet.Id, " Comet 2026 ", ["Night"], 4, "Tripod", fx.Ct);
Assert.Equal("Comet 2026", updated.Title);
var manifest = ManifestStore.ReadSequence(fx.Paths.Sequence(comet.Folder).ManifestPath);
Assert.Equal("Comet 2026", manifest.Title);
Assert.SequenceEqual(["Night"], manifest.Tags);
Assert.Equal(updated.SettingsVersion, manifest.Versions.Settings);
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));
}
}