Owner review 2, phase 4a review findings on the renamer. - File rename recovery is safe to run again from wherever it stopped: once every file is at its temporary name the journal says phase 2, as Run does, so a second recovery (after a failure, a crash or a sidecar that could not be written) never moves a file that reached its new name. A new name something else took meanwhile is never written over: that photo and its companions go back to their old names, or to the first free _2, _3, recorded in the journal before they move, and the frame names where its photo is. A journal is never deleted while a temporary file of it is still there. - Every check that can refuse a step runs under the step's leases before anything of it moves: where its sequences are, strangers inside, its new folder taken since the plan (a restore, an upload), a sidecar of a newer format, a title edited since the plan (compare-and-set against the planned from-title, sidecar and index), and its file renames, compared by a digest of every move at the planned frames version (a verify that hashes photos does not bump the version). Once the folder moved, every index row follows it before any file is renamed, and a photo in use or files found changed keep their names: the step completes. - The same checks run up front for a held plan, so its retry either fits or is given up. - A failed move job holds its plan, as a pause does: an archive open no longer applies the rest unasked; only the job's retry does. File renames a step left half-way are still completed at an archive open. - Each sequence changed is told as its step is journaled, so a pause or a failure never keeps it untold; the plan's journal keeps the tally of every run, so the job log counts the whole plan. - A selected sequence deleted while the job waited is named in the log and the plan refused with the localized stale-plan error, not a raw ArgumentException. - The dry run renames no file of a newer-format sidecar. New log lines and error (en, it): title and file names kept, file names kept because a photo was in use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 1d1c888120edd709dbb5341c1922bf85085c4857)
371 lines
22 KiB
C#
371 lines
22 KiB
C#
using System.Text;
|
|
using Chronos.Core.Archiving;
|
|
using Chronos.Core.Naming;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Core.Tests.Archiving;
|
|
|
|
/// <summary>Reorganize (owner review 2, R7): file names planned from a template, and the two-phase renames of a sequence's files.</summary>
|
|
[Category("archive")]
|
|
public sealed class ReorganizeTests(TestContext context)
|
|
{
|
|
private static readonly DateTime Start = new(2026, 9, 28, 18, 0, 0);
|
|
|
|
/// <summary>Frames in index order, every one hashed, ten seconds apart.</summary>
|
|
private static SequenceManifest Sequence(params string[] files) => new()
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Camera = new SidecarCamera { Make = "Canon", Model = "Canon EOS R5" },
|
|
Versions = new ManifestVersions { Frames = 4 },
|
|
Frames = [.. files.Select((f, i) => new SidecarFrame
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
I = i,
|
|
File = f,
|
|
Original = $"IMG_{i + 1:0000}{Path.GetExtension(f)}",
|
|
Sha256 = new string((char)('a' + i % 6), 64),
|
|
CapturedAt = Start.AddSeconds(10 * i),
|
|
})],
|
|
};
|
|
|
|
private static FileRenamePlan Plan(SequenceManifest manifest, string template, string? title = null, IEnumerable<string>? extra = null) =>
|
|
FileRenames.Plan(manifest, title, PathTemplate.Parse(template), [.. manifest.Frames.Select(f => f.File), .. extra ?? []]);
|
|
|
|
[Test]
|
|
public void FramesAreNamedInIndexOrderAndNamesTakenGetNumbersAsFilingDoes()
|
|
{
|
|
var manifest = Sequence("originals/c.JPG", "originals/a.JPG", "originals/b.JPG", "originals/d.JPG");
|
|
var plan = Plan(manifest, "{date}");
|
|
Assert.SequenceEqual(["originals/2026-09-28.JPG", "originals/2026-09-28_2.JPG", "originals/2026-09-28_3.JPG", "originals/2026-09-28_4.JPG"],
|
|
plan.Moves.Select(m => m.To));
|
|
Assert.SequenceEqual(manifest.Frames.Select(f => f.Id), plan.Moves.Select(m => m.FrameId));
|
|
Assert.Equal(4, plan.Frames);
|
|
Assert.SequenceEqual(plan.Moves.Take(FileRenames.SampleCount), plan.Samples);
|
|
Assert.Empty(plan.Blocked);
|
|
}
|
|
|
|
[Test]
|
|
public void AChainOfNamesIsPlannedAndAFileNoFrameListsKeepsItsName()
|
|
{
|
|
// Frame 1 now holds the name frame 0 gets, frame 2 the one frame 1 gets: 2→1, 3→2, 4→3. A file no frame lists holds "4".
|
|
var manifest = Sequence("originals/2.JPG", "originals/3.JPG", "originals/4.JPG");
|
|
var plan = Plan(manifest, "{n:0}");
|
|
Assert.SequenceEqual(["originals/2.JPG>originals/1.JPG", "originals/3.JPG>originals/2.JPG", "originals/4.JPG>originals/3.JPG"],
|
|
plan.Moves.Select(m => $"{m.From}>{m.To}"));
|
|
|
|
var stray = Plan(manifest, "{n:0}", extra: ["originals/1.jpg", "originals/notes.txt"]);
|
|
Assert.Equal("originals/1_2.JPG", stray.Moves[0].To);
|
|
}
|
|
|
|
[Test]
|
|
public void PhotosInPlaceUnhashedOrMissingKeepTheirNamesAndHoldThem()
|
|
{
|
|
var manifest = Sequence("IMG_0001.JPG", "originals/IMG_0002.JPG", "originals/IMG_0003.JPG", "originals/IMG_0004.JPG");
|
|
manifest = manifest with { Frames = [manifest.Frames[0], manifest.Frames[1] with { Sha256 = null }, manifest.Frames[2], manifest.Frames[3]] };
|
|
// The third photo is not on disk; the fourth would take its name.
|
|
var plan = FileRenames.Plan(manifest, "IMG_0003", PathTemplate.Parse("{title}"), ["IMG_0001.JPG", "originals/IMG_0002.JPG", "originals/IMG_0004.JPG"]);
|
|
Assert.SequenceEqual([(RenameBlock.InPlace, 1), (RenameBlock.Unhashed, 1), (RenameBlock.Missing, 1)], plan.Blocked.Select(b => (b.Reason, b.Frames)));
|
|
Assert.Equal("originals/IMG_0004.JPG>originals/IMG_0003_2.JPG", plan.Moves.Select(m => $"{m.From}>{m.To}").Single());
|
|
Assert.Equal(1, plan.Frames);
|
|
}
|
|
|
|
[Test]
|
|
public void CompanionsFollowTheirPhotoAndTheirNamesAreKeptFree()
|
|
{
|
|
var manifest = Sequence("originals/A.CR2", "originals/B.CR2");
|
|
var plan = Plan(manifest, "{n:0000}", extra: ["originals/A.xmp", "originals/B.CR2.xmp", "originals/0001.xmp"]);
|
|
// "0001.xmp" belongs to no frame: the first photo, whose companion would take it, gets the next free name.
|
|
Assert.SequenceEqual(["originals/A.CR2>originals/0001_2.CR2", "originals/A.xmp>originals/0001_2.xmp",
|
|
"originals/B.CR2>originals/0002.CR2", "originals/B.CR2.xmp>originals/0002.CR2.xmp"], plan.Moves.Select(m => $"{m.From}>{m.To}"));
|
|
Assert.SequenceEqual([false, true, false, true], plan.Moves.Select(m => m.Companion));
|
|
Assert.Equal(2, plan.Frames);
|
|
Assert.Count(2, plan.Samples);
|
|
}
|
|
|
|
[Test]
|
|
public void TheTrashAndReorganizeFindTheSameCompanionsWhateverTheirCase()
|
|
{
|
|
var sequence = Folder(out _);
|
|
var manifest = Sequence("originals/A.CR2", "originals/B.CR2");
|
|
foreach (var file in new[] { "originals/A.CR2", "originals/B.CR2", "originals/a.XMP", "originals/b.cr2.Xmp", "originals/c.xmp" }) Write(sequence, file, file);
|
|
var present = FileRenames.ListPresent(sequence, manifest);
|
|
var plan = FileRenames.Plan(manifest, null, PathTemplate.Parse("{n:0000}"), present);
|
|
Assert.SequenceEqual(["originals/A.CR2>originals/0001.CR2", "originals/a.XMP>originals/0001.XMP", "originals/B.CR2>originals/0002.CR2",
|
|
"originals/b.cr2.Xmp>originals/0002.cr2.Xmp"], plan.Moves.Select(m => $"{m.From}>{m.To}"));
|
|
var index = new FrameCompanions.Index(sequence);
|
|
Assert.SequenceEqual(["originals/a.XMP"], index.Of("originals/A.CR2"));
|
|
Assert.SequenceEqual(["originals/b.cr2.Xmp"], index.Of("originals/B.CR2"));
|
|
Assert.SequenceEqual(plan.Moves.Where(m => m.Companion).Select(m => m.From), manifest.Frames.SelectMany(f => FrameCompanions.Of(sequence, f.File)));
|
|
}
|
|
|
|
[Test]
|
|
public void NamesGivenOnceStayAsTheyAre()
|
|
{
|
|
var manifest = Sequence("originals/x.JPG", "originals/y.JPG");
|
|
var plan = Plan(manifest, "{date}_{n:000}");
|
|
var renamed = FileRenameJournal.Applied(manifest, FileRenameJournal.Create(manifest.Versions.Frames, plan.Moves, "t"))!;
|
|
Assert.Equal(manifest.Versions.Frames + 1, renamed.Versions.Frames);
|
|
Assert.SequenceEqual(manifest.Frames.Select(f => f.Id), renamed.Frames.Select(f => f.Id));
|
|
Assert.Empty(Plan(renamed, "{date}_{n:000}").Moves);
|
|
Assert.Null(FileRenameJournal.Applied(renamed, FileRenameJournal.Create(manifest.Versions.Frames, plan.Moves, "t")));
|
|
}
|
|
|
|
[Test]
|
|
public void APlanWithAFileTemplateCountsSamplesAndListsWhatKeepsItsName()
|
|
{
|
|
var renamed = Sequence("originals/a.JPG", "originals/b.JPG");
|
|
var adopted = Sequence("a.JPG");
|
|
var subjects = new[]
|
|
{
|
|
new RenameSubject(renamed.Id, "s/one", "Trip", Start, "Canon", "Canon EOS R5", null) { Files = new SequenceFiles(renamed, [.. renamed.Frames.Select(f => f.File)]) },
|
|
new RenameSubject(adopted.Id, "s/two", "Adopted", Start, "Canon", "Canon EOS R5", null) { Files = new SequenceFiles(adopted, ["a.JPG"]) },
|
|
new RenameSubject(Guid.NewGuid(), "s/three", null, Start, "Canon", "Canon EOS R5", null),
|
|
};
|
|
var plan = RenamePlanner.Plan(Guid.NewGuid(), subjects, null, "{title} 2026", ["s/one", "s/two", "s/three"], _ => false, fileTemplate: "{title}_{n:00}");
|
|
Assert.Equal("{title}_{n:00}", plan.FileTemplate);
|
|
Assert.SequenceEqual(subjects.Select(s => s.SequenceId), plan.Subjects);
|
|
var one = plan.Steps.Single(s => s.SequenceId == renamed.Id);
|
|
Assert.Equal((2, 4), (one.FileChanges, one.FramesVersion));
|
|
// The new title names the files.
|
|
Assert.SequenceEqual(["originals/Trip 2026_01.JPG", "originals/Trip 2026_02.JPG"], one.FileSamples.Select(f => f.To));
|
|
var two = plan.Steps.Single(s => s.SequenceId == adopted.Id);
|
|
Assert.Equal((0, 0), (two.FileChanges, two.FramesVersion));
|
|
Assert.Equal((RenameBlock.InPlace, 1), (two.Blocked.Single().Reason, two.Blocked.Single().Frames));
|
|
Assert.True(two.Changes, "its title changes");
|
|
var three = plan.Steps.Single(s => s.SequenceId == subjects[2].SequenceId);
|
|
Assert.Equal(RenameBlock.Unreadable, three.Blocked.Single().Reason);
|
|
|
|
// Nothing but kept names: listed, changes nothing.
|
|
var keptOnly = RenamePlanner.Plan(Guid.NewGuid(), [subjects[1]], null, null, ["s/two"], _ => false, fileTemplate: "{n}");
|
|
Assert.False(keptOnly.Steps.Single().Changes);
|
|
}
|
|
|
|
[Test]
|
|
public void FoldersTheTemplateGaveAlreadyStayAndASecondRunPlansNothing()
|
|
{
|
|
// Two sequences of one day filed by the template, the second beside the first; a third elsewhere, a fourth whose
|
|
// folder differs only in case, a fifth with a number filing never gives.
|
|
const string Template = "{year}/{date} {title}";
|
|
RenameSubject At(string folder) => new(Guid.NewGuid(), folder, "Sunset", Start, null, null, null);
|
|
RenameSubject[] subjects = [At("2026/2026-09-28 Sunset"), At("2026/2026-09-28 Sunset (2)"), At("inbox/c"), At("2026/2026-09-28 sunset (4)"),
|
|
At("2026/2026-09-28 Sunset (03)")];
|
|
var plan = RenamePlanner.Plan(Guid.NewGuid(), subjects, Template, null, subjects.Select(s => s.Folder), _ => false);
|
|
Assert.SequenceEqual([(subjects[2].SequenceId, "2026/2026-09-28 Sunset (3)"), (subjects[3].SequenceId, "2026/2026-09-28 Sunset (4)"),
|
|
(subjects[4].SequenceId, "2026/2026-09-28 Sunset (5)")], plan.Steps.Select(s => (s.SequenceId, s.ToFolder)));
|
|
|
|
// Where the plan put them, the same template changes nothing.
|
|
var moved = plan.Steps.ToDictionary(s => s.SequenceId, s => s.ToFolder);
|
|
RenameSubject[] after = [.. subjects.Select(s => moved.TryGetValue(s.SequenceId, out var to) ? s with { Folder = to } : s)];
|
|
var onDisk = after.Select(s => s.Folder).ToHashSet(StringComparer.OrdinalIgnoreCase);
|
|
Assert.Empty(RenamePlanner.Plan(Guid.NewGuid(), after, Template, null, onDisk, onDisk.Contains).Steps);
|
|
// Nor in another order.
|
|
Assert.Empty(RenamePlanner.Plan(Guid.NewGuid(), [.. after.Reverse()], Template, null, onDisk, onDisk.Contains).Steps);
|
|
}
|
|
|
|
[Test]
|
|
public void TheFingerprintFollowsWhatThePlanChanges()
|
|
{
|
|
var manifest = Sequence("originals/a.JPG");
|
|
RenamePlan Make(SequenceManifest m) => RenamePlanner.Plan(Guid.NewGuid(), [new RenameSubject(m.Id, "s", "T", Start, null, null, null)
|
|
{ Files = new SequenceFiles(m, [.. m.Frames.Select(f => f.File)]) }], null, null, ["s"], _ => false, fileTemplate: "{n:000}");
|
|
var plan = Make(manifest);
|
|
Assert.Equal(64, plan.Fingerprint().Length);
|
|
Assert.Equal(plan.Fingerprint(), Make(manifest).Fingerprint());
|
|
Assert.NotEqual(plan.Fingerprint(), Make(manifest with { Versions = new ManifestVersions { Frames = 5 } }).Fingerprint());
|
|
}
|
|
|
|
[Test]
|
|
public void TemplatesAreCheckedAsTheArchiveSettingsAre()
|
|
{
|
|
Assert.Null(new RenameTemplates("{year}/{title}", "{date}_{n}", "{title} 2026").Problem());
|
|
Assert.Equal("folderTemplate", new RenameTemplates(Folder: " ").Problem());
|
|
Assert.Equal("fileTemplate", new RenameTemplates(File: "{nope}").Problem());
|
|
Assert.Equal("fileTemplate", new RenameTemplates(File: "{date:%}").Problem());
|
|
Assert.Equal("titleTemplate", new RenameTemplates(Title: "{title").Problem());
|
|
Assert.True(new RenameTemplates().IsEmpty);
|
|
}
|
|
|
|
private SequencePaths Folder(out ArchivePaths archive)
|
|
{
|
|
archive = new ArchivePaths(context.CreateTempDirectory("archive"));
|
|
var sequence = archive.Sequence("s");
|
|
Directory.CreateDirectory(sequence.Originals);
|
|
Directory.CreateDirectory(sequence.MetaDirectory);
|
|
return sequence;
|
|
}
|
|
|
|
private static void Write(SequencePaths sequence, string file, string text) => File.WriteAllText(sequence.File(file), text, Encoding.UTF8);
|
|
|
|
private static string Read(SequencePaths sequence, string file) => File.ReadAllText(sequence.File(file), Encoding.UTF8);
|
|
|
|
[Test]
|
|
public void FilesMoveInTwoPhasesSoChainsAndSwapsNeverMeet()
|
|
{
|
|
var sequence = Folder(out _);
|
|
Write(sequence, "originals/a.jpg", "A");
|
|
Write(sequence, "originals/b.jpg", "B");
|
|
Write(sequence, "originals/c.jpg", "C");
|
|
Guid x = Guid.NewGuid(), y = Guid.NewGuid(), z = Guid.NewGuid();
|
|
// A swap (a↔b) and a chain into a name that frees up (c→a's place is taken by b, so c→d).
|
|
var journal = FileRenameJournal.Create(7, [new FileRename(x, "originals/a.jpg", "originals/b.jpg"), new FileRename(y, "originals/b.jpg", "originals/a.jpg"),
|
|
new FileRename(z, "originals/c.jpg", "originals/d.jpg")], "t1");
|
|
FileRenameJournal.Write(sequence, journal);
|
|
var done = FileRenameJournal.Run(sequence, journal);
|
|
Assert.Equal(2, done.Phase);
|
|
Assert.Equal(("A", "B", "C"), (Read(sequence, "originals/b.jpg"), Read(sequence, "originals/a.jpg"), Read(sequence, "originals/d.jpg")));
|
|
Assert.FileMissing(sequence.File("originals/c.jpg"));
|
|
Assert.True(journal.Files.All(f => !File.Exists(sequence.File(f.Temp))));
|
|
Assert.True(journal.Files.All(f => Path.GetFileName(f.Temp).StartsWith(".chronos-rename-t1-", StringComparison.Ordinal)), "hidden, never a photo");
|
|
}
|
|
|
|
[Test]
|
|
public void AFailedMovePutsEveryFileBackAndChangesNothing()
|
|
{
|
|
var sequence = Folder(out _);
|
|
Write(sequence, "originals/a.jpg", "A");
|
|
Write(sequence, "originals/b.jpg", "B");
|
|
var journal = FileRenameJournal.Create(1, [new FileRename(Guid.NewGuid(), "originals/a.jpg", "originals/x.jpg"),
|
|
new FileRename(Guid.NewGuid(), "originals/b.jpg", "originals/y.jpg")], "t2");
|
|
FileRenameJournal.Write(sequence, journal);
|
|
// Another file took the second name after the plan: nothing is written over it, and the first file goes back.
|
|
Write(sequence, "originals/y.jpg", "other");
|
|
Assert.Throws<IOException>(() => FileRenameJournal.Run(sequence, journal));
|
|
Assert.Equal(("A", "B", "other"), (Read(sequence, "originals/a.jpg"), Read(sequence, "originals/b.jpg"), Read(sequence, "originals/y.jpg")));
|
|
Assert.FileMissing(sequence.File("originals/x.jpg"));
|
|
Assert.Null(FileRenameJournal.Read(sequence));
|
|
}
|
|
|
|
[Theory]
|
|
[Case(1)]
|
|
[Case(2)]
|
|
public void ARenameACrashInterruptedIsCompleted(int phase)
|
|
{
|
|
var sequence = Folder(out _);
|
|
var manifest = Sequence("originals/2.jpg", "originals/3.jpg", "originals/4.jpg");
|
|
foreach (var f in manifest.Frames) Write(sequence, f.File, f.Id.ToString());
|
|
var plan = Plan(manifest, "{n:0}");
|
|
var journal = FileRenameJournal.Create(manifest.Versions.Frames, plan.Moves, "t3") with { Phase = phase };
|
|
FileRenameJournal.Write(sequence, journal);
|
|
// Phase 1: the first file reached its temporary name. Phase 2: every file did, and the first its new name.
|
|
File.Move(sequence.File(journal.Files[0].From), sequence.File(journal.Files[0].Temp));
|
|
if (phase == 2)
|
|
{
|
|
foreach (var f in journal.Files.Skip(1)) File.Move(sequence.File(f.From), sequence.File(f.Temp));
|
|
File.Move(sequence.File(journal.Files[0].Temp), sequence.File(journal.Files[0].To));
|
|
}
|
|
|
|
var recovered = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
|
|
Assert.SequenceEqual(["originals/1.jpg", "originals/2.jpg", "originals/3.jpg"], recovered.Frames.Select(f => f.File));
|
|
Assert.Equal(manifest.Versions.Frames + 1, recovered.Versions.Frames);
|
|
foreach (var f in recovered.Frames) Assert.Equal(f.Id.ToString(), Read(sequence, f.File));
|
|
Assert.FileMissing(sequence.File("originals/4.jpg"));
|
|
Assert.True(journal.Files.All(f => !File.Exists(sequence.File(f.Temp))));
|
|
}
|
|
|
|
/// <summary>
|
|
/// A rename of <paramref name="shape"/> (a chain of names that free up, a swap, a cycle of three) a crash interrupted
|
|
/// in its first phase, once its first file reached its temporary name; each file holds its frame's id.
|
|
/// </summary>
|
|
private static (SequenceManifest Manifest, FileRenameJournal Journal) Interrupted(SequencePaths sequence, string shape)
|
|
{
|
|
string[][] names = shape switch
|
|
{
|
|
"chain" => [["originals/2.jpg", "originals/3.jpg", "originals/4.jpg"], ["originals/1.jpg", "originals/2.jpg", "originals/3.jpg"]],
|
|
"swap" => [["originals/a.jpg", "originals/b.jpg"], ["originals/b.jpg", "originals/a.jpg"]],
|
|
_ => [["originals/a.jpg", "originals/b.jpg", "originals/c.jpg"], ["originals/b.jpg", "originals/c.jpg", "originals/a.jpg"]],
|
|
};
|
|
var (from, to) = (names[0], names[1]);
|
|
var manifest = Sequence(from);
|
|
foreach (var f in manifest.Frames) Write(sequence, f.File, f.Id.ToString());
|
|
var journal = FileRenameJournal.Create(manifest.Versions.Frames, [.. manifest.Frames.Select((f, i) => new FileRename(f.Id, f.File, to[i]))], "t4");
|
|
FileRenameJournal.Write(sequence, journal);
|
|
File.Move(sequence.File(journal.Files[0].From), sequence.File(journal.Files[0].Temp));
|
|
return (manifest, journal);
|
|
}
|
|
|
|
private static void AssertEveryFrameHoldsItsPhoto(SequencePaths sequence, SequenceManifest recovered)
|
|
{
|
|
foreach (var f in recovered.Frames) Assert.Equal(f.Id.ToString(), Read(sequence, f.File));
|
|
Assert.Empty(Directory.EnumerateFiles(sequence.Originals, ".chronos-rename-*"));
|
|
}
|
|
|
|
[Theory]
|
|
[Case("chain")]
|
|
[Case("swap")]
|
|
[Case("cycle")]
|
|
public void ARecoveryRunAgainLeavesEveryFileWithItsFrame(string shape)
|
|
{
|
|
var sequence = Folder(out _);
|
|
var (manifest, _) = Interrupted(sequence, shape);
|
|
// Completed, but the sidecar that follows was never written (a full disk, a crash before it): the journal is found again.
|
|
var first = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
|
|
Assert.Equal(2, Assert.NotNull(FileRenameJournal.Read(sequence)).Phase);
|
|
var again = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
|
|
Assert.SequenceEqual(first.Frames.Select(f => f.File), again.Frames.Select(f => f.File));
|
|
Assert.False(manifest.Frames.Select(f => f.File).SequenceEqual(again.Frames.Select(f => f.File)), "every frame has its new name");
|
|
AssertEveryFrameHoldsItsPhoto(sequence, again);
|
|
}
|
|
|
|
[Test]
|
|
[WindowsOnly]
|
|
public void ARecoveryCutShortWhileFilesTakeTheirNewNamesGoesOnFromThere()
|
|
{
|
|
var sequence = Folder(out _);
|
|
var (manifest, journal) = Interrupted(sequence, "chain");
|
|
// The last file reached its temporary name too, and another program holds it: the recovery stops at it.
|
|
File.Move(sequence.File(journal.Files[2].From), sequence.File(journal.Files[2].Temp));
|
|
using (new FileStream(sequence.File(journal.Files[2].Temp), FileMode.Open, FileAccess.Read, FileShare.None))
|
|
Assert.Throws<IOException>(() => FileRenameJournal.Recover(sequence, manifest));
|
|
Assert.Equal("originals/1.jpg", journal.Files[0].To);
|
|
Assert.Equal(manifest.Frames[0].Id.ToString(), Read(sequence, "originals/1.jpg"));
|
|
|
|
// Run again, it goes on from there: no file that took its new name moves again.
|
|
var recovered = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
|
|
Assert.SequenceEqual(["originals/1.jpg", "originals/2.jpg", "originals/3.jpg"], recovered.Frames.Select(f => f.File));
|
|
AssertEveryFrameHoldsItsPhoto(sequence, recovered);
|
|
}
|
|
|
|
[Test]
|
|
public void ANewNameTakenMeanwhileNeverStrandsAPhoto()
|
|
{
|
|
var sequence = Folder(out _);
|
|
var (manifest, journal) = Interrupted(sequence, "chain");
|
|
// Copied in under the name the second file gets after the crash: never written over. Its own old name is the third
|
|
// file's new one, so it takes the first free number of that name.
|
|
Write(sequence, "originals/2.jpg", "other");
|
|
FileRenameJournal.Delete(sequence);
|
|
Assert.NotNull(FileRenameJournal.Read(sequence));
|
|
|
|
var recovered = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
|
|
Assert.SequenceEqual(["originals/1.jpg", "originals/3_2.jpg", "originals/3.jpg"], recovered.Frames.Select(f => f.File));
|
|
Assert.Equal("other", Read(sequence, "originals/2.jpg"));
|
|
AssertEveryFrameHoldsItsPhoto(sequence, recovered);
|
|
// The new name is in the journal, so a recovery run again finds every file where it is.
|
|
Assert.Equal("originals/3_2.jpg", Assert.NotNull(FileRenameJournal.Read(sequence)).Files[1].To);
|
|
Assert.SequenceEqual(recovered.Frames.Select(f => f.File), Assert.NotNull(FileRenameJournal.Recover(sequence, manifest)).Frames.Select(f => f.File));
|
|
AssertEveryFrameHoldsItsPhoto(sequence, recovered);
|
|
Assert.Equal(journal.Files.Count, Directory.EnumerateFiles(sequence.Originals).Count() - 1);
|
|
}
|
|
|
|
[Test]
|
|
public void APhotoWhoseCompanionsNewNameIsTakenStaysWithItsCompanion()
|
|
{
|
|
var sequence = Folder(out _);
|
|
var manifest = Sequence("originals/A.CR2");
|
|
Write(sequence, "originals/A.CR2", "photo");
|
|
Write(sequence, "originals/A.xmp", "sidecar");
|
|
var id = manifest.Frames[0].Id;
|
|
FileRename[] moves = [new(id, "originals/A.CR2", "originals/0001.CR2"), new(id, "originals/A.xmp", "originals/0001.xmp", Companion: true)];
|
|
var journal = FileRenameJournal.Create(manifest.Versions.Frames, moves, "t5") with { Phase = 2 };
|
|
FileRenameJournal.Write(sequence, journal);
|
|
foreach (var f in journal.Files) File.Move(sequence.File(f.From), sequence.File(f.Temp));
|
|
Write(sequence, "originals/0001.xmp", "other");
|
|
|
|
// Both go back to their names, together; the frame keeps its file.
|
|
Assert.Null(FileRenameJournal.Recover(sequence, manifest));
|
|
Assert.Equal(("photo", "sidecar", "other"), (Read(sequence, "originals/A.CR2"), Read(sequence, "originals/A.xmp"), Read(sequence, "originals/0001.xmp")));
|
|
Assert.FileMissing(sequence.File("originals/0001.CR2"));
|
|
Assert.Empty(Directory.EnumerateFiles(sequence.Originals, ".chronos-rename-*"));
|
|
}
|
|
}
|