fix(copies): close the sixth verification round of owner review 6

A merge that finds the other changed drops its journal so the retry plans
again. Unlink and Keep apart name the local side and leave a link that now
joins another sequence alone; Link only and the Integrate job refuse to
touch a link or a pair another integration holds. A sweep of 404s asks the
server once. To trash... trashes from the kept sequence after a swap and
every trash re-checks for new last copies. The Compare dialog and Settings
choices fit in every language, Details segments say which value they take,
twins need their sibling at the other home, and a linked row keeps three
buttons. Captures updated for the dialog and Settings scenes.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-17 21:58:58 +01:00
1 parent bf7701d279
commit 38764972ad
77 files changed
+965 -123

No files matched your search

@@ -687,6 +687,47 @@ public abstract class SequenceMergerTests(TestContext context) : DatabaseTest(co
}
}
[Test]
public async Task AFrameRemovedFromTheOtherAfterAStoppedTrashMakesTheNextRetryPlanAgain()
{
var fx = await FixtureAsync();
var merger = Merger(fx);
var workspace = new FakeWorkspace();
var (engine, handler) = Engine(fx, merger, new FakeProjects(), workspace);
await engine.StartAsync(Ct);
var keep = await SequenceAsync(fx, "keep", [(1, 0, "IMG_0001.JPG")]);
var drop = await SequenceAsync(fx, "drop", [(1, 0, "IMG_0001.JPG"), (2, 10_000, "IMG_0002.JPG"), (3, 20_000, "IMG_0003.JPG")]);
var copy = await PairAsync(fx, keep.Id, drop.Id);
handler.BeforeStage = stage =>
{
if (stage == MergeStage.Trash) workspace.Unsent.Add(drop.Id);
return Task.CompletedTask;
};
var plan = await merger.PlanAsync(copy.Id, keep.Id, drop.Id, await ChoicesAsync(fx, keep.Id, drop.Id, CopyPhotoPolicy.Union), NoContext, Ct);
var job = await merger.EnqueueAsync(engine, new FoundCopyStore(fx.Db), plan, keep.DisplayTitle, Ct);
await fx.WaitForAsync(job.Id, JobState.Failed);
Assert.True(File.Exists(MergeJournal.PathFor(fx.Paths, job.Id)), "an unsent workspace keeps the journal");
// A frame of the other goes to the trash while the merge waits: its frames version never goes back.
var gone = (await fx.Db.Frames.ListBySequenceAsync(drop.Id)).First(f => f.Sha256 == Sha(3, 20_000));
var trash = await fx.Service.FrameTrash.PlanDeleteAsync(drop.Id, [gone.Id], null, fx.Ct, job.Id);
await fx.Service.FrameTrash.DeleteAsync(trash, null, null, fx.Ct, job.Id);
handler.BeforeStage = null;
workspace.Unsent.Clear();
await engine.ResumeAsync(job.Id, Ct);
var failed = await fx.WaitForAsync(job.Id, JobState.Failed);
Assert.Equal(Chronos.Core.Localization.Loc.L("Errors.Copies.Changed", drop.DisplayTitle), failed.Error);
Assert.NotNull(await fx.Db.Sequences.GetAsync(drop.Id));
Assert.False(File.Exists(MergeJournal.PathFor(fx.Paths, job.Id)), "the next run plans again");
// The next Retry plans again and asks for a new comparison instead of the same error.
await engine.ResumeAsync(job.Id, Ct);
failed = await fx.WaitForAsync(job.Id, JobState.Failed);
Assert.Equal(Chronos.Core.Localization.Loc.L("Errors.Copies.StalePlan"), failed.Error);
Assert.NotNull(await fx.Db.Sequences.GetAsync(drop.Id));
}
[Test]
public async Task APhotoSettledThroughAThirdSequenceThatIsGoneByTheTrashKeepsTheOther()
{
@@ -50,6 +50,21 @@ public sealed class CopyEngineTests(TestContext context)
Fingerprint = "f",
}, f.Ct);
/// <summary>A paused Integrate job of <paramref name="localId"/>: a pair naming it reads Integrating.</summary>
private static async Task<Guid> PausedIntegrateAsync(SyncFixture f, Guid localId)
{
var id = Guid.CreateVersion7();
await f.Db.Jobs.EnqueueAsync(new Job
{
Id = id,
Kind = JobKind.Integrate,
TargetIds = [localId],
State = JobState.Paused,
Created = DateTimeOffset.UtcNow,
}, f.Ct);
return id;
}
private static async Task<CopyState> StateAsync(SyncFixture f, Guid copyId) => (await f.Sync.Copies.GetAsync(copyId, f.Ct))!.State;
[Test]
@@ -119,6 +134,36 @@ public sealed class CopyEngineTests(TestContext context)
Assert.False(kept.IsListed && kept.State == CopyState.Candidate);
}
[Test]
public async Task UnlinkOfAPairThatNoLongerHoldsTheLinkLeavesTheOtherPairsLinkAndIntegration()
{
await using var f = await FixtureAsync(context);
var photos = f.NewPhotos(2);
var first = await f.LocalAsync("Comet", photos);
var second = await f.LocalAsync("Comet copy", photos);
var remote = f.Remote.Add("Comet NAS", photos);
// (second, R) was unlinked earlier; then (first, R) was linked and its Integrate began.
var old = await PairAsync(f, second.Id, remote.Id);
await f.Sync.Copies.SetStateAsync(old.Id, null, CopyState.KeptApart, null, f.Ct);
await f.Sync.LinkAsync(f.Server, remote.Id, first.Id, f.Ct);
var linked = await f.Sync.Copies.EnsureLinkedAsync(f.Server, remote.Id, first.Id, f.Ct);
var job = await PausedIntegrateAsync(f, first.Id);
Assert.True(await f.Sync.Copies.SetStateAsync(linked.Id, null, CopyState.Integrating, new CopyAnswer(null, null, job, null, DateTimeOffset.UtcNow), f.Ct));
using var engine = Engine(f);
// The stale row still offers Unlink.
await engine.UnlinkAsync(old.Id, f.Remote, f.Ct);
Assert.Equal(CopyState.KeptApart, await StateAsync(f, old.Id));
Assert.Equal(first.Id, (await f.Sync.Links.GetAsync(f.Server, remote.Id, f.Ct))!.LocalId);
var integrating = (await f.Sync.Copies.GetAsync(linked.Id, f.Ct))!;
Assert.Equal((CopyState.Integrating, (Guid?)job), (integrating.State, integrating.JobId));
// The pair being integrated refuses an Unlink of its own.
await Assert.ThrowsAsync<InvalidOperationException>(() => engine.UnlinkAsync(linked.Id, f.Remote, f.Ct));
await Assert.ThrowsAsync<InvalidOperationException>(() => f.Sync.UnlinkAsync(f.Server, remote.Id, first.Id, f.Ct));
Assert.Equal(first.Id, (await f.Sync.Links.GetAsync(f.Server, remote.Id, f.Ct))!.LocalId);
}
[Test]
public async Task KeepApartOfALinkedPairUnlinksIt()
{
@@ -169,16 +214,17 @@ public sealed class CopyEngineTests(TestContext context)
var (gone, restored) = (remote.Frames[1], remote.Frames[2]);
await f.Remote.DeleteFramesAsync(remote.Id, new FrameDeleteRequest([gone.Id, restored.Id], null, false), f.Ct);
var (plan, lasts) = await engine.PlanTrashHereAsync(pair.Id, [Sha(photos[1]), Sha(photos[2])], f.Ct);
var trash = await engine.PlanTrashHereAsync(pair.Id, local.Id, [Sha(photos[1]), Sha(photos[2])], f.Ct);
var (plan, lasts) = trash;
Assert.Equal(2, plan.Frames.Count);
// No other sequence of this PC holds them: the trash takes their last copies, and the confirmation says so.
Assert.SequenceEqual(new[] { Sha(photos[1]), Sha(photos[2]) }.Order(StringComparer.Ordinal), lasts.Select(l => l.Key).Order(StringComparer.Ordinal));
// Restored on the server after the dry run: nothing moves, all or nothing.
remote.AddFrame(photos[2], restored.FileName);
await Assert.ThrowsAsync<StaleCopyPlanException>(() => engine.TrashHereAsync(pair.Id, plan, f.Remote, f.Ct));
await Assert.ThrowsAsync<StaleCopyPlanException>(() => engine.TrashHereAsync(pair.Id, trash, f.Remote, f.Ct));
Assert.Count(3, await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct));
var again = (await engine.PlanTrashHereAsync(pair.Id, [Sha(photos[1])], f.Ct)).Plan;
var again = await engine.PlanTrashHereAsync(pair.Id, local.Id, [Sha(photos[1])], f.Ct);
Assert.Equal(1, await engine.TrashHereAsync(pair.Id, again, f.Remote, f.Ct));
var left = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
Assert.SequenceEqual(new[] { Sha(photos[0]), Sha(photos[2]) }.Order(StringComparer.Ordinal), left.Select(fr => fr.Sha256!).Order(StringComparer.Ordinal));
@@ -217,17 +263,52 @@ public sealed class CopyEngineTests(TestContext context)
public async Task TrashHereNamesNoLastCopyForAPhotoAnotherSequenceHolds()
{
await using var f = await FixtureAsync(context);
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Comet", photos);
await f.LocalAsync("Comet backup", [photos[1]]);
var remote = f.Remote.Add("Comet NAS", photos);
var photos = f.NewPhotos(3);
var local = await f.LocalAsync("Comet", photos[..2]);
// The backup holds photo 1 and one of its own (a sequence keeps at least one frame).
var backup = await f.LocalAsync("Comet backup", [photos[1], photos[2]]);
var remote = f.Remote.Add("Comet NAS", photos[..2]);
var pair = await PairAsync(f, local.Id, remote.Id);
using var engine = Engine(f);
await f.Remote.DeleteFramesAsync(remote.Id, new FrameDeleteRequest([remote.Frames[1].Id], null, false), f.Ct);
var plan = await engine.PlanTrashHereAsync(pair.Id, [Sha(photos[1])], f.Ct);
var plan = await engine.PlanTrashHereAsync(pair.Id, local.Id, [Sha(photos[1])], f.Ct);
Assert.Equal((1, 0), (plan.Plan.Frames.Count, plan.LastCopies.Count));
// The other sequence goes to the trash while the confirmation is open: the photo is a last copy now, never moved unnamed.
var held = (await f.Db.Frames.ListBySequenceAsync(backup.Id, cancellationToken: f.Ct)).Single(r => r.Sha256 == Sha(photos[1]));
var other = await f.Archive.Service.FrameTrash.PlanDeleteAsync(backup.Id, [held.Id], (await f.Db.Sequences.GetAsync(backup.Id, f.Ct))!.FramesVersion, f.Ct);
await f.Archive.Service.FrameTrash.DeleteAsync(other, "test", null, f.Ct);
await Assert.ThrowsAsync<StaleCopyPlanException>(() => engine.TrashHereAsync(pair.Id, plan, f.Remote, f.Ct));
Assert.Count(2, await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct));
Assert.Count(1, (await engine.PlanTrashHereAsync(pair.Id, local.Id, [Sha(photos[1])], f.Ct)).LastCopies);
}
[Test]
public async Task TrashHereOfTwoLocalCopiesTrashesFromTheSequenceAsked()
{
await using var f = await FixtureAsync(context);
var photos = f.NewPhotos(3);
var first = await f.LocalAsync("Comet", photos);
var second = await f.LocalAsync("Comet copy", photos);
using var engine = Engine(f);
var pair = (await engine.FindAsync(null, f.Ct)).New.Single();
// The pair's first deletes a photo the second still holds (a merge keeping the second reads it "Deleted in other").
var (deleter, keeper) = pair.LocalId == first.Id ? (first, second) : (second, first);
var gone = (await f.Db.Frames.ListBySequenceAsync(deleter.Id, cancellationToken: f.Ct)).Single(fr => fr.Sha256 == Sha(photos[1]));
var deletion = await f.Archive.Service.FrameTrash.PlanDeleteAsync(deleter.Id, [gone.Id], (await f.Db.Sequences.GetAsync(deleter.Id, f.Ct))!.FramesVersion, f.Ct);
await f.Archive.Service.FrameTrash.DeleteAsync(deletion, "test", null, f.Ct);
var plan = await engine.PlanTrashHereAsync(pair.Id, keeper.Id, [Sha(photos[1])], f.Ct);
Assert.Equal(keeper.Id, plan.Plan.Sequence.Id);
Assert.Equal(1, await engine.TrashHereAsync(pair.Id, plan, null, f.Ct));
Assert.DoesNotContain(Sha(photos[1]), (await f.Db.Frames.ListBySequenceAsync(keeper.Id, cancellationToken: f.Ct)).Select(fr => fr.Sha256));
Assert.Count(2, await f.Db.Frames.ListBySequenceAsync(deleter.Id, cancellationToken: f.Ct));
// The sequence that deleted it holds nothing to trash: its own photos are refused.
await Assert.ThrowsAsync<InvalidOperationException>(() => engine.PlanTrashHereAsync(pair.Id, deleter.Id, [Sha(photos[1])], f.Ct));
await Assert.ThrowsAsync<ArgumentException>(() => engine.PlanTrashHereAsync(pair.Id, Guid.NewGuid(), [Sha(photos[1])], f.Ct));
}
[Test]
@@ -460,7 +541,10 @@ public sealed class CopyEngineTests(TestContext context)
IReadOnlyList<(Chronos.Core.Project.BundleHome Home, string Name)> projects =
[
(new(Chronos.Core.Project.BundleHome.ArchiveKind, linked.Id), "Grade"),
(new(Chronos.Core.Project.BundleHome.ServerKind, linkedRemote.Id, Server: ServerIdentity.IdKey(serverId)), "Grade NAS"),
(new(Chronos.Core.Project.BundleHome.ArchiveKind, linked.Id), "Vertical"),
(new(Chronos.Core.Project.BundleHome.ArchiveKind, linked.Id), "Slow pan"),
(new(Chronos.Core.Project.BundleHome.ServerKind, linkedRemote.Id, Server: ServerIdentity.IdKey(serverId)), "Grade"),
(new(Chronos.Core.Project.BundleHome.ServerKind, linkedRemote.Id, Server: ServerIdentity.IdKey(serverId)), "Slow pan (STUDIO-PC)"),
(new(Chronos.Core.Project.BundleHome.ArchiveKind, apart.Id), "Unpaired"),
];
using var engine = new CopyEngine(f.Sync, f.Archive.Service, new IntegratePlanner(f.Sync, sides, f.Archive.Service.Projects),
@@ -469,7 +553,14 @@ public sealed class CopyEngineTests(TestContext context)
var twin = (await engine.TwinsAsync(f.Ct)).Single();
Assert.Equal((pair.Id, linked.Id, "Grade", linkedRemote.Id, "Grade NAS"), (twin.CopyId, twin.LocalId, twin.LocalProject, twin.RemoteId, twin.ServerProject));
Assert.Equal((pair.Id, linked.Id, linkedRemote.Id), (twin.CopyId, twin.LocalId, twin.RemoteId));
Assert.SequenceEqual(["Grade", "Vertical", "Slow pan"], twin.LocalProjects);
Assert.SequenceEqual(["Grade", "Slow pan (STUDIO-PC)"], twin.ServerProjects);
// A project is a twin only with its sibling: the same name, or the name Copy across gave it at the other home.
Assert.Equal("Grade", twin.CounterpartOf("Grade", atServerHome: false));
Assert.Null(twin.CounterpartOf("Vertical", atServerHome: false));
Assert.Equal("Slow pan (STUDIO-PC)", twin.CounterpartOf("Slow pan", atServerHome: false));
Assert.Equal("Slow pan", twin.CounterpartOf("Slow pan (STUDIO-PC)", atServerHome: true));
Assert.Equal(f.Server, twin.Server);
// The Edit picker shows the server by its name, never by its key.
Assert.Equal("Studio NAS", twin.ServerName);
@@ -730,6 +730,36 @@ public sealed class IntegrateTests(TestContext context)
Assert.False(File.Exists(IntegrateJournal.PathFor(f.Archive.Paths, job.Id)));
}
[Test]
public async Task ALinkTheUserGaveTheServerSequenceAfterTheJournalIsNeverUndoneOnResume()
{
await using var f = await FixtureAsync(context);
var (engine, handler) = Engine(f);
await using var _ = engine;
await engine.StartAsync(f.Ct);
var (local, remote, pair) = await ScenarioAsync(f);
var other = await LocalAsync(f, "Comet copy", [(1, 0, "IMG_0001.JPG")]);
handler.BeforeStage = stage => stage == IntegrateStage.Link ? throw new IOException("crash before Link") : Task.CompletedTask;
var plan = await Planner(f).PlanAsync(f.Remote, pair, await ChoicesAsync(f, local.Id, remote.Id, CopyPhotoPolicy.Union), PlanContext(), f.Ct);
var job = await CopyJobs.EnqueueIntegrateAsync(engine, f.Sync.Copies, plan, local.DisplayTitle, f.Ct);
await f.Archive.WaitForAsync(job.Id, JobState.Failed);
Assert.True(File.Exists(IntegrateJournal.PathFor(f.Archive.Paths, job.Id)));
// Meanwhile the user links the server sequence to another copy of this PC.
await f.Sync.LinkAsync(f.Server, remote.Id, other.Id, f.Ct);
handler.BeforeStage = null;
var uploads = f.Remote.IntoUploads;
await engine.ResumeAsync(job.Id, f.Ct);
var failed = await f.Archive.WaitForAsync(job.Id, JobState.Failed);
Assert.Equal(Loc.L("Errors.Copies.StalePlan"), failed.Error);
Assert.Equal(other.Id, (await f.Sync.Links.GetAsync(f.Server, remote.Id, f.Ct))!.LocalId);
Assert.Null(await f.Sync.LinkOfAsync(f.Server, local.Id, f.Ct));
Assert.Equal(uploads, f.Remote.IntoUploads);
Assert.False(File.Exists(IntegrateJournal.PathFor(f.Archive.Paths, job.Id)));
Assert.NotEqual(CopyState.Integrating, (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!.State);
}
[Test]
public async Task ARetryAfterAFailureBeforeTheJournalRunsTheIntegrate()
{
@@ -127,7 +127,7 @@ public sealed class SyncServiceCopyTests(TestContext context)
await f.RefreshAsync();
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Comet")).Status);
await f.Sync.UnlinkAsync(f.Server, remote.Id, f.Ct);
await f.Sync.UnlinkAsync(f.Server, remote.Id, local.Id, f.Ct);
var link = await f.Sync.Links.GetAsync(f.Server, remote.Id, f.Ct);
Assert.NotNull(link);
@@ -244,6 +244,43 @@ public sealed class SyncServiceCopyTests(TestContext context)
Assert.True((await f.Sync.Copies.ListByServerAsync(nas.Server, f.Ct)).All(p => p.RemoteGoneHits == 0));
}
[Test]
public async Task ASweepOf404sConfirmsNoEarlierHitEither()
{
await using var f = await SyncFixture.CreateAsync(context);
var nas = new FakeSyncRemote { Server = HomeFolders.IdKey(Guid.NewGuid()) };
// One pair with a 404 counted more than ten minutes ago (its link already forgotten).
var first = f.NewPhotos(2);
var gone = await f.LocalAsync("Comet", first);
var goneRemote = nas.Add("Comet", first);
await f.Sync.LinkAsync(nas.Server, goneRemote.Id, gone.Id, f.Ct);
await f.Sync.RefreshAsync(nas, new SyncPolicy(), f.Ct);
nas.Sequences.Remove(goneRemote.Id);
await f.Sync.RefreshAsync(nas, new SyncPolicy(), f.Ct);
var pair = (await f.Sync.Copies.ListByServerAsync(nas.Server, f.Ct)).Single(p => p.LocalId == gone.Id);
Assert.Equal(1, pair.RemoteGoneHits);
await using (var session = await f.Db.Database.OpenAsync(f.Ct))
await session.ExecuteAsync("UPDATE found_copies SET remote_gone_at = $1 WHERE id = $2", [DateTimeOffset.UtcNow.AddMinutes(-11), pair.Id], f.Ct);
// Then the server serves something else: every linked sequence answers 404 at once.
var remotes = new List<FakeSequence>();
for (var i = 0; i < 5; i++)
{
var photos = f.NewPhotos(1);
var local = await f.LocalAsync($"Card {i}", photos);
var remote = nas.Add($"Card {i}", photos);
await f.Sync.LinkAsync(nas.Server, remote.Id, local.Id, f.Ct);
remotes.Add(remote);
}
foreach (var remote in remotes) nas.Sequences.Remove(remote.Id);
await f.Sync.RefreshAsync(nas, new SyncPolicy(), f.Ct);
pair = (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!;
Assert.Equal(1, pair.RemoteGoneHits);
Assert.False(pair.RemoteGone);
}
[Test]
public async Task AServerKnownOnlyByItsAddressMarksNothing()
{