Files
Alby96andClaude Opus 5.5 f33bcac32e
CI / Build and test (push) Successful in 13m44s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 16s
Release / Build, test and publish (push) Successful in 26m34s
CI / PostgreSQL 16 integration (push) Failing after 17m36s
CI / PostgreSQL 17 integration (push) Failing after 15m36s
CI / Docker image (push) Successful in 1m12s
Release / Server image (push) Failing after 15s
feat(copies): matches between this PC and the server, Integrate and merge (owner review 6, v1.2.0) (#74)
The same time-lapse on a server and on this PC under different ids, or twice on this PC, is recognised,
compared and settled as the owner asked in owner review 6.

Matches are found after each sync refresh, through the server's new photo lookup, and after the jobs that
change photos. The default is to ask every time. Server › Matches offers:
- Keep apart, which is remembered;
- Link only and Unlink;
- the Compare dialog: photos, details line by line, projects and the plan.

Integrate completes both sides. Photos default to Union, projects are copied across, and a losing version
goes to the trash only after a hash check. Afterwards new photos move by themselves, and deletions wait for
the user.

A merge of two local sequences verifies every photo before the other sequence goes to its archive's trash.
Upload all local skips suspected matches and flags them.

Migration 0012 adds the found_copies and copy_probes tables. Jobs 25 (Integrate) and 26 (MergeSequences) are
new, and so are the server capabilities photo-lookup and upload-into.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-18 15:20:29 +01:00

593 lines
32 KiB
C#

using System.Security.Cryptography;
using Chronos.Core.Model;
using Chronos.Data.Archiving;
using Chronos.Data.Copies;
using Chronos.Data.Sync;
using Chronos.Data.Tests.Archiving;
using Chronos.Data.Tests.Sync;
using Chronos.Protocol;
using Chronos.Testing;
namespace Chronos.Data.Tests.Copies;
/// <summary>
/// The matches engine of a PC (owner review 6, #73; design §5, §6): what Find matches finds and tells, the facts listed,
/// Link, Unlink, Keep apart and Ask again, and the trash steps of photos one side deleted, re-checked when applied.
/// </summary>
[Category("sync")]
public sealed class CopyEngineTests(TestContext context)
{
private static readonly CopyPlanContext Context =
new("STUDIO-PC", "NAS", true, true, true, true, true, false, null, new HashSet<Guid>(), new HashSet<Guid>());
private static async Task<SyncFixture> FixtureAsync(TestContext context)
{
var f = await SyncFixture.CreateAsync(context);
f.Remote.Capabilities.Add(ServerCapabilities.UploadInto);
f.Remote.Capabilities.Add(ServerCapabilities.PhotoLookup);
return f;
}
private static CopyEngine Engine(SyncFixture f)
{
var sides = new CopySides(f.Db);
return new CopyEngine(f.Sync, f.Archive.Service, new IntegratePlanner(f.Sync, sides, f.Archive.Service.Projects),
new SequenceMerger(f.Db, f.Archive.Service, sides), new CopyReviewBuilder(f.Db, f.Archive.Service.Projects, sides), _ => Context,
serverNames: key => key == f.Server ? "NAS" : null);
}
private static string Sha(byte[] bytes) => Convert.ToHexStringLower(SHA256.HashData(bytes));
private static Task<FoundCopy> PairAsync(SyncFixture f, Guid localId, Guid remoteId) =>
f.Sync.Copies.UpsertFoundAsync(new FoundCopy
{
Id = Guid.NewGuid(),
Kind = CopyKind.LocalServer,
LocalId = localId,
Server = f.Server,
RemoteId = remoteId,
Evidence = CopyEvidence.Hashes,
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]
public async Task FindMatchesTellsAndListsTheServerPairWithTheFactsOfBothSides()
{
await using var f = await FixtureAsync(context);
var photos = f.NewPhotos(4);
var local = await f.LocalAsync("Comet", photos);
var remote = f.Remote.Add("Comet (selection)", photos.Take(2));
using var engine = Engine(f);
var told = new List<CopyFindResult>();
engine.Found += told.Add;
var result = await engine.FindAsync(f.Remote, f.Ct);
var pair = result.New.Single();
Assert.Equal((local.Id, remote.Id), (pair.LocalId, pair.RemoteId!.Value));
Assert.True(told.Any(t => t.New.Any(n => n.Id == pair.Id)));
var view = (await engine.ListAsync(f.Ct)).Single();
Assert.Equal(("Comet", 4, 4), (view.Here.Title, view.Here.Photos, view.Here.Hashed));
Assert.Equal(("Comet (selection)", "NAS", 2), (view.There.Title, view.There.Where, view.There.Photos));
}
[Test]
public async Task FindMatchesWithoutAServerLooksAmongThisPcsArchives()
{
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 result = await engine.FindAsync(null, f.Ct);
var pair = result.New.Single();
Assert.Equal(CopyKind.LocalLocal, pair.Kind);
Assert.SequenceEqual(new[] { first.Id, second.Id }.Order(), new[] { pair.LocalId, pair.OtherId!.Value }.Order());
// Asked again later, the pair already known is not new.
Assert.True((await engine.FindLocalAsync(f.Ct)).New.Count == 0);
}
[Test]
public async Task LinkUnlinkKeepApartAndAskAgainMoveThePair()
{
await using var f = await FixtureAsync(context);
var photos = f.NewPhotos(3);
var local = await f.LocalAsync("Comet", photos);
var remote = f.Remote.Add("Comet NAS", photos.Take(2));
await f.RefreshAsync(new SyncPolicy { Linking = CopyLinking.AskAlways });
var pair = (await f.Sync.Copies.ListAsync(f.Ct)).Single();
using var engine = Engine(f);
await engine.LinkAsync(pair.Id, f.Remote, f.Ct);
Assert.Equal(CopyState.Linked, await StateAsync(f, pair.Id));
Assert.Equal(remote.Id, (await f.Sync.LinkOfAsync(f.Server, local.Id, f.Ct))!.RemoteId);
await engine.UnlinkAsync(pair.Id, f.Remote, f.Ct);
Assert.Equal(CopyState.KeptApart, await StateAsync(f, pair.Id));
Assert.Null(await f.Sync.LinkOfAsync(f.Server, local.Id, f.Ct));
await engine.AskAgainAsync(pair.Id, f.Ct);
Assert.Equal(CopyState.Candidate, await StateAsync(f, pair.Id));
await engine.KeepApartAsync(pair.Id, f.Ct);
var kept = (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!;
Assert.Equal(CopyState.KeptApart, kept.State);
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()
{
await using var f = await FixtureAsync(context);
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Comet", photos);
var remote = f.Remote.Add("Comet NAS", photos);
await f.RefreshAsync(new SyncPolicy { Linking = CopyLinking.AskAlways });
var pair = (await f.Sync.Copies.ListAsync(f.Ct)).Single();
using var engine = Engine(f);
await engine.LinkAsync(pair.Id, f.Remote, f.Ct);
await engine.KeepApartAsync(pair.Id, f.Ct);
Assert.Equal(CopyState.KeptApart, await StateAsync(f, pair.Id));
Assert.Null(await f.Sync.LinkOfAsync(f.Server, local.Id, f.Ct));
Assert.NotNull(await f.Sync.Links.GetAsync(f.Server, remote.Id, f.Ct));
}
[Test]
public async Task KeepHereOfASequenceDeletedOnTheServerIsLeftOutOfUploadAllLocal()
{
await using var f = await FixtureAsync(context);
var local = await f.LocalAsync("Comet", f.NewPhotos(2));
var gone = Guid.CreateVersion7();
var pair = await f.Sync.Copies.EnsureLinkedAsync(f.Server, gone, local.Id, f.Ct);
// Deleted on the server: two 404s the re-check apart, read as deleted on server.
var at = DateTimeOffset.UtcNow.AddHours(-2);
await f.Sync.Copies.MarkRemoteGoneAsync(f.Server, gone, local.Id, Guid.NewGuid(), at, f.Ct);
await f.Sync.Copies.MarkRemoteGoneAsync(f.Server, gone, local.Id, Guid.NewGuid(), at.AddHours(1), f.Ct);
using var engine = Engine(f);
await engine.KeepHereAsync(pair.Id, f.Ct);
// Kept here: Upload all local leaves it out, and the answer stays.
var row = (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!;
Assert.Equal((CopyState.KeptApart, true), (row.State, row.RemoteGone));
var entry = new SyncEntry(local, null, SyncStatus.LocalOnly);
var (upload, skipped) = UploadAllRule.Select([entry], await f.Sync.Copies.ListAsync(f.Ct), new HashSet<Guid>(), new HashSet<Guid>());
Assert.Empty(upload);
Assert.Equal(local.Id, skipped.Single().Entry.Local!.Id);
}
[Test]
public async Task LinkIsRefusedWhenTheLocalCopyIsLinkedToAnotherServerSequence()
{
await using var f = await FixtureAsync(context);
var photos = f.NewPhotos(3);
var local = await f.LocalAsync("Comet", photos);
var linked = f.Remote.Add("Comet", photos);
var other = f.Remote.Add("Comet selection", photos.Take(2));
await f.RefreshAsync(new SyncPolicy { Linking = CopyLinking.AskAlways });
await f.Sync.LinkAsync(f.Server, linked.Id, local.Id, f.Ct);
var pair = await PairAsync(f, local.Id, other.Id);
using var engine = Engine(f);
await Assert.ThrowsAsync<CopyLinkTakenException>(() => engine.LinkAsync(pair.Id, f.Remote, f.Ct));
Assert.Equal(linked.Id, (await f.Sync.LinkOfAsync(f.Server, local.Id, f.Ct))!.RemoteId);
Assert.Equal(CopyState.Candidate, await StateAsync(f, pair.Id));
}
[Test]
public async Task TrashHereMovesWhatTheServerDeletedAndRefusesAPhotoRestoredThere()
{
await using var f = await FixtureAsync(context);
var photos = f.NewPhotos(3);
var local = await f.LocalAsync("Comet", photos);
var remote = f.Remote.Add("Comet NAS", photos);
var pair = await PairAsync(f, local.Id, remote.Id);
using var engine = Engine(f);
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 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, 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, 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));
}
[Test]
public async Task TrashThereSendsThePlansVersionAndAStalePlanMovesNothing()
{
await using var f = await FixtureAsync(context);
var photos = f.NewPhotos(3);
var local = await f.LocalAsync("Comet", photos);
var remote = f.Remote.Add("Comet NAS", photos);
var pair = await PairAsync(f, local.Id, remote.Id);
using var engine = Engine(f);
var frames = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
var deleted = frames.Single(fr => fr.Sha256 == Sha(photos[2]));
var sequence = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
var local3 = await f.Archive.Service.FrameTrash.PlanDeleteAsync(local.Id, [deleted.Id], sequence.FramesVersion, f.Ct);
await f.Archive.Service.FrameTrash.DeleteAsync(local3, "test", null, f.Ct);
var plan = await engine.PlanTrashThereAsync(pair.Id, f.Remote, [Sha(photos[2])], f.Ct);
Assert.Equal((1, remote.FramesVersion), (plan.Photos, plan.FramesVersion));
// The photo is in this PC's trash only: the server's is its last copy.
Assert.Count(1, plan.LastCopies);
remote.AddFrame(f.NewPhotos(1)[0], "IMG_0009.JPG");
await Assert.ThrowsAsync<StaleCopyPlanException>(() => engine.TrashThereAsync(plan, f.Remote, f.Ct));
Assert.Count(0, f.Remote.Trashed);
var fresh = await engine.PlanTrashThereAsync(pair.Id, f.Remote, [Sha(photos[2])], f.Ct);
Assert.Equal(1, await engine.TrashThereAsync(fresh, f.Remote, f.Ct));
Assert.Equal(Sha(photos[2]), f.Remote.Trashed.Single().Frame.Sha256);
}
[Test]
public async Task TrashHereNamesNoLastCopyForAPhotoAnotherSequenceHolds()
{
await using var f = await FixtureAsync(context);
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, 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]
public async Task UnlinkKeepsApartAgainstWhatTheTwoHoldNowNotAtTheLastComparison()
{
await using var f = await FixtureAsync(context);
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Comet", photos);
var remote = f.Remote.Add("Comet NAS", [photos[0]]);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.Sync.Copies.EnsureLinkedAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync(new SyncPolicy { Linking = CopyLinking.AskAlways });
// The server copy grows to hold every photo: the pair's score follows at the next comparison.
remote.AddFrame(photos[1], "IMG_0002.JPG");
await f.RefreshAsync(new SyncPolicy { Linking = CopyLinking.AskAlways });
var pair = (await f.Sync.Copies.FindRemoteAsync(local.Id, f.Server, remote.Id, f.Ct))!;
Assert.Equal((2, 0, 0), (pair.Score.Shared, pair.Score.OnlyHere, pair.Score.OnlyThere));
using var engine = Engine(f);
await engine.UnlinkAsync(pair.Id, f.Remote, f.Ct);
await f.RefreshAsync(new SyncPolicy { Linking = CopyLinking.AskAlways });
// Answered against two identical copies: nothing grew since, so the pair is not asked about again.
var kept = (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!;
Assert.Equal((CopyState.KeptApart, 2), (kept.State, kept.AnsweredShared ?? -1));
}
[Test]
public async Task DropVersionTrashesOneVersionOnBothSidesAndIsRefusedOnceTheOtherIsGone()
{
await using var f = await FixtureAsync(context);
var (local, remote, a, b) = await VersionsAsync(f);
var pair = await PairAsync(f, local.Id, remote.Id);
using var engine = Engine(f);
var review = await engine.CompareAsync(pair.Id, f.Remote, f.Ct);
Assert.Count(2, review.Photos.Of(PhotoClass.Versions));
var plan = await engine.PlanDropVersionAsync(pair.Id, Sha(a), f.Remote, f.Ct);
Assert.Equal((Sha(b), 2), (plan.PartnerKey, plan.Photos));
Assert.Equal(2, await engine.DropVersionAsync(plan, f.Remote, f.Ct));
var left = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
Assert.SequenceEqual(new[] { Sha(b) }, left.Select(fr => fr.Sha256!).Where(s => s == Sha(a) || s == Sha(b)));
Assert.False(remote.Frames.Any(fr => fr.Sha256 == Sha(a)));
Assert.True(remote.Frames.Any(fr => fr.Sha256 == Sha(b)));
Assert.Equal(Sha(a), f.Remote.Trashed.Single().Frame.Sha256);
}
[Test]
public async Task DropVersionIsRefusedWhenTheOtherVersionLeftOneSide()
{
await using var f = await FixtureAsync(context);
var (local, remote, a, b) = await VersionsAsync(f);
var pair = await PairAsync(f, local.Id, remote.Id);
using var engine = Engine(f);
var plan = await engine.PlanDropVersionAsync(pair.Id, Sha(a), f.Remote, f.Ct);
// The other version leaves the server after the dry run: dropping this one would leave the server without the photo.
await f.Remote.DeleteFramesAsync(remote.Id, new FrameDeleteRequest([remote.Frames.Single(fr => fr.Sha256 == Sha(b)).Id], null, false), f.Ct);
await Assert.ThrowsAsync<StaleCopyPlanException>(() => engine.DropVersionAsync(plan, f.Remote, f.Ct));
Assert.True(remote.Frames.Any(fr => fr.Sha256 == Sha(a)));
Assert.Count(1, f.Remote.Trashed);
Assert.True((await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct)).Any(fr => fr.Sha256 == Sha(a)));
}
[Test]
public async Task DropOwnVersionTrashesOneVersionOfALocalSequenceAndIsRefusedOnceTheOtherIsGone()
{
// Two versions a Union merge left in one sequence, its pair gone (owner Q3): listed, and each can be dropped.
await using var f = await FixtureAsync(context);
var (local, _, a, b) = await VersionsAsync(f);
using var engine = Engine(f);
var photo = (await engine.VersionsAsync(local.Id, f.Ct)).Single();
var (frameA, frameB) = photo.FirstSha256 == Sha(a) ? (photo.FirstFrameId, photo.SecondFrameId) : (photo.SecondFrameId, photo.FirstFrameId);
Assert.True(photo.CapturedAt is not null && photo.FirstSize > 0 && photo.SecondSize > 0);
var dropA = await engine.PlanDropOwnVersionAsync(local.Id, frameA, f.Ct);
Assert.Equal((1, frameB, Sha(b)), (dropA.Plan.Frames.Count, dropA.PartnerFrameId, dropA.PartnerSha256));
Assert.Equal(Sha(a), dropA.LastCopies.Single().Key);
var dropB = await engine.PlanDropOwnVersionAsync(local.Id, frameB, f.Ct);
Assert.Equal(1, await engine.DropOwnVersionAsync(dropB, f.Ct));
var left = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
Assert.True(left.Any(fr => fr.Sha256 == Sha(a)) && !left.Any(fr => fr.Sha256 == Sha(b)));
Assert.Empty(await engine.VersionsAsync(local.Id, f.Ct));
// The other version is gone now: dropping this one too would lose the photo, so nothing moves.
await Assert.ThrowsAsync<StaleCopyPlanException>(() => engine.DropOwnVersionAsync(dropA, f.Ct));
Assert.True((await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct)).Any(fr => fr.Sha256 == Sha(a)));
}
/// <summary>The photos Main leaves out of <paramref name="sequenceId"/>, by content.</summary>
private static async Task<IReadOnlySet<string>> ExcludedAsync(SyncFixture f, Guid sequenceId)
{
var row = (await f.Db.Sequences.GetAsync(sequenceId))!;
return CopyPayload.ExcludedKeys(CopyPayload.Portable(SyncPayload.Of(row), await SequencePayloadWriter.PhotosAsync(f.Db, sequenceId, null, f.Ct)));
}
/// <summary>Main of <paramref name="sequenceId"/> leaves out <paramref name="key"/>, as a Union keep-both leaves the incoming version.</summary>
private static async Task ExcludeAsync(SyncFixture f, Guid sequenceId, string key)
{
var row = (await f.Db.Sequences.GetAsync(sequenceId))!;
var portable = CopyPayload.Portable(SyncPayload.Of(row), await SequencePayloadWriter.PhotosAsync(f.Db, sequenceId, null, f.Ct));
await new SequencePayloadWriter(f.Db, f.Archive.Service).ApplyAsync(sequenceId, CopyPayload.ExcludeVersions(portable, [key]), row.SettingsVersion, f.Ct);
}
[Test]
public async Task DroppingTheVersionMainUsesLetsMainUseTheOneThatStays()
{
// A Union keep-both excluded version B in Main; the user drops A, the one Main used: Main must not be left without
// the photo (owner review 6, verification round 5).
await using var f = await FixtureAsync(context);
var (local, _, a, b) = await VersionsAsync(f);
await ExcludeAsync(f, local.Id, Sha(b));
Assert.SequenceEqual([Sha(b)], await ExcludedAsync(f, local.Id));
using var engine = Engine(f);
var photo = (await engine.VersionsAsync(local.Id, f.Ct)).Single();
var (frameA, aExcluded, bExcluded) = photo.FirstSha256 == Sha(a)
? (photo.FirstFrameId, photo.FirstExcluded, photo.SecondExcluded)
: (photo.SecondFrameId, photo.SecondExcluded, photo.FirstExcluded);
Assert.Equal((false, true), (aExcluded, bExcluded));
var plan = await engine.PlanDropOwnVersionAsync(local.Id, frameA, f.Ct);
Assert.Equal(1, await engine.DropOwnVersionAsync(plan, f.Ct));
Assert.Empty(await ExcludedAsync(f, local.Id));
Assert.True((await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct)).Any(fr => fr.Sha256 == Sha(b)));
}
[Test]
public async Task KeepVersionMovesTheExclusionHeroAndKeyframesToTheVersionThatStays()
{
await using var f = await FixtureAsync(context);
var (local, _, a, b) = await VersionsAsync(f);
await ExcludeAsync(f, local.Id, Sha(b));
var row = (await f.Db.Sequences.GetAsync(local.Id))!;
var portable = CopyPayload.Portable(SyncPayload.Of(row), await SequencePayloadWriter.PhotosAsync(f.Db, local.Id, null, f.Ct));
// Main used A and left B out: once A goes, B is used.
var kept = CopyPayload.KeepVersion(portable, Sha(b), Sha(a));
Assert.NotNull(kept);
Assert.Empty(CopyPayload.ExcludedKeys(kept!));
// Dropping the excluded one changes nothing in Main.
Assert.Null(CopyPayload.KeepVersion(portable, Sha(a), Sha(b)));
}
[Test]
public async Task DropVersionOfTwoLocalSequencesCountsAPhotoOnEachSide()
{
// Finding 9 said a pair of this PC's sequences listed "0 photos": the plan counts each local side's version.
await using var f = await FixtureAsync(context);
var (local, _, a, b) = await VersionsAsync(f);
var at = ArchiveFixture.Start.AddSeconds(5).AddMilliseconds(250);
var plainAt = ArchiveFixture.Start;
var other = await f.LocalAsync("Comet copy", [Photos.Jpeg(plainAt, 7001), a, b], capturedAt: [plainAt, at, at]);
var pair = await f.Sync.Copies.UpsertFoundAsync(new FoundCopy
{
Id = Guid.NewGuid(),
Kind = CopyKind.LocalLocal,
LocalId = local.Id,
OtherId = other.Id,
Evidence = CopyEvidence.Hashes,
Fingerprint = "f",
}, f.Ct);
await ExcludeAsync(f, local.Id, Sha(b));
await ExcludeAsync(f, other.Id, Sha(b));
using var engine = Engine(f);
var plan = await engine.PlanDropVersionAsync(pair.Id, Sha(a), null, f.Ct);
Assert.Equal((Sha(b), 2), (plan.PartnerKey, plan.Photos));
Assert.Equal(2, await engine.DropVersionAsync(plan, null, f.Ct));
// Both sides' Main now use the version that stays.
Assert.Empty(await ExcludedAsync(f, local.Id));
Assert.Empty(await ExcludedAsync(f, other.Id));
}
/// <summary>
/// A pair holding one photo in two versions (a Union keep-both): two files of one sub-second capture time, on both sides,
/// besides one plain photo.
/// </summary>
private static async Task<(Sequence Local, FakeSequence Remote, byte[] A, byte[] B)> VersionsAsync(SyncFixture f)
{
var plainAt = ArchiveFixture.Start;
var at = ArchiveFixture.Start.AddSeconds(5).AddMilliseconds(250);
var (plain, a, b) = (Photos.Jpeg(plainAt, 7001), Photos.Jpeg(at, 7002), Photos.Jpeg(at, 7003));
var local = await f.LocalAsync("Comet", [plain, a, b], capturedAt: [plainAt, at, at]);
var remote = f.Remote.Add("Comet NAS", []);
remote.AddFrame(plain, "IMG_0001.JPG", plainAt);
remote.AddFrame(a, "IMG_0002.JPG", at);
remote.AddFrame(b, "IMG_0003.JPG", at);
return (local, remote, a, b);
}
[Test]
public async Task TrashThereIsRefusedForAPhotoThisPcStillHolds()
{
await using var f = await FixtureAsync(context);
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Comet", photos);
var remote = f.Remote.Add("Comet NAS", photos);
var pair = await PairAsync(f, local.Id, remote.Id);
using var engine = Engine(f);
var plan = await engine.PlanTrashThereAsync(pair.Id, f.Remote, [Sha(photos[1])], f.Ct);
await Assert.ThrowsAsync<StaleCopyPlanException>(() => engine.TrashThereAsync(plan, f.Remote, f.Ct));
Assert.Count(0, f.Remote.Trashed);
}
[Test]
public async Task TwinsNameTheProjectsOfBothHomesOfALinkedPairEvenUnderTheServersIdKey()
{
// Owner Q5's alternative: projects left where they are; Edit's picker marks the two homes' projects as twins. The
// server home is keyed by the server's id while the pair still carries the address key it was found under.
var serverId = Guid.NewGuid();
var address = string.Empty;
await using var f = await SyncFixture.CreateAsync(context, () => new ServerIdentity([new KnownServerKey(serverId, [address])]));
address = f.Server;
var photos = f.NewPhotos(2);
var linked = await f.LocalAsync("Comet", photos);
var apart = await f.LocalAsync("Lighthouse", f.NewPhotos(2));
var linkedRemote = f.Remote.Add("Comet NAS", photos);
var apartRemote = f.Remote.Add("Lighthouse NAS", photos);
var pair = await PairAsync(f, linked.Id, linkedRemote.Id);
Assert.True(await f.Sync.Copies.SetStateAsync(pair.Id, CopyState.Candidate, CopyState.Linked, null, f.Ct));
await PairAsync(f, apart.Id, apartRemote.Id);
var sides = new CopySides(f.Db);
IReadOnlyList<(Chronos.Core.Project.BundleHome Home, string Name)> projects =
[
(new(Chronos.Core.Project.BundleHome.ArchiveKind, linked.Id), "Grade"),
(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),
new SequenceMerger(f.Db, f.Archive.Service, sides), new CopyReviewBuilder(f.Db, f.Archive.Service.Projects, sides), _ => Context,
serverNames: key => key == f.Server ? "Studio NAS" : null, workspaceProjects: _ => Task.FromResult(projects));
var twin = (await engine.TwinsAsync(f.Ct)).Single();
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);
}
}