fix(copies): address the owner review 6 findings

The review of the found-copies work showed pairs that could stick, jobs that
could act on a pair the user had answered otherwise, and detection gaps:

- forgetting a link on an address-keyed server deletes the pair and Candidate
  rows of that remote and resets the probe, so the sequence is found again
  when it is back; a Linked/Integrated row with no link is asked again
- 404s count only on id-keyed servers, at most once per recheck interval
- probes store the server's newest update time (copy_probes.server_newest,
  migration 0012) instead of comparing with this PC's clock; the lookup
  sample carries hashed photos by size and capture time too, for servers
  that have not hashed their copy
- the older-server gate no longer re-compares unchanged manifests
- a failed Integrate keeps its job id so a retry resumes only an untouched
  pair; cancel and stale plans release the pair (ReleaseAsync)
- automatic photo transfers never create or retarget a sequence
- a first upload files photos the server holds elsewhere into the new
  sequence
- Unlink re-compares before keeping apart; Trash here names only true last
  copies; Drop... trashes one version on both sides after re-checking the
  other
- a merge keeping the pair's other sequence swaps the dialog's columns and
  re-keys Different choices; Upload all reads every pair

Each fix has a regression test where it makes sense.

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 13:12:00 +01:00
1 parent 96b5b4078b
commit fe752a0a21
33 files changed
+957 -123

No files matched your search

@@ -3,6 +3,7 @@ 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;
@@ -168,14 +169,16 @@ 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 = await engine.PlanTrashHereAsync(pair.Id, [Sha(photos[1]), Sha(photos[2])], f.Ct);
var (plan, lasts) = await engine.PlanTrashHereAsync(pair.Id, [Sha(photos[1]), Sha(photos[2])], f.Ct);
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));
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);
var again = (await engine.PlanTrashHereAsync(pair.Id, [Sha(photos[1])], f.Ct)).Plan;
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));
@@ -210,6 +213,104 @@ public sealed class CopyEngineTests(TestContext context)
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(2);
var local = await f.LocalAsync("Comet", photos);
await f.LocalAsync("Comet backup", [photos[1]]);
var remote = f.Remote.Add("Comet NAS", photos);
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);
Assert.Equal((1, 0), (plan.Plan.Frames.Count, plan.LastCopies.Count));
}
[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)));
}
/// <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()
{
@@ -170,6 +170,43 @@ public sealed class CopyFinderServerTests(TestContext context)
Assert.Equal(CopyEvidence.SizeAndTime, (await PairsAsync(f)).Single().Evidence);
}
[Test]
public async Task AServerCopyNotHashedYetIsFoundByTheSizeAndTimeOfHashedPhotos()
{
await using var f = await LookupAsync(context);
var photos = f.NewPhotos(3);
var local = await f.LocalAsync("Comet", photos);
// Adopted on the server and not verified there: it lists no hash, so only size and capture time can find it.
var remote = f.Remote.Add("Comet", photos);
remote.Unhashed = true;
await f.RefreshAsync();
var pair = (await PairsAsync(f)).Single();
Assert.Equal((local.Id, remote.Id), (pair.LocalId, pair.RemoteId!.Value));
Assert.Equal(CopyState.Candidate, pair.State);
}
[Test]
public async Task ASequenceAddedOnAServerWhoseClockIsBehindIsStillLookedFor()
{
await using var f = await LookupAsync(context);
var photos = f.NewPhotos(3);
await f.LocalAsync("Comet", photos);
var other = f.Remote.Add("Harbour", f.NewPhotos(2));
other.UpdatedAt = DateTimeOffset.UtcNow.AddHours(-3);
await f.RefreshAsync();
Assert.Count(1, f.Remote.Lookups);
// Added after the probe by a server whose clock is three hours behind this PC's: later than the newest it showed.
var remote = f.Remote.Add("Comet", photos);
remote.UpdatedAt = other.UpdatedAt.AddMinutes(1);
await f.RefreshAsync();
Assert.Count(2, f.Remote.Lookups);
Assert.Equal(remote.Id, (await PairsAsync(f)).Single().RemoteId!.Value);
}
[Test]
public async Task OneRefreshComparesAtMostFiftyPairs()
{
@@ -488,6 +488,66 @@ public sealed class IntegrateTests(TestContext context)
Assert.True((await Planner(f).PlanAsync(f.Remote, after, plan.Choices, PlanContext(), f.Ct)).CanApply);
}
[Test]
public async Task ARetryAfterThePairWasAnsweredOtherwiseRefusesAndTouchesNothing()
{
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);
handler.BeforeStage = stage => stage == IntegrateStage.Photos ? throw new IOException("crash before Photos") : 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);
// The failed job still names the pair, so a retry may resume it while nobody answered otherwise.
var held = (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!;
Assert.Equal((CopyState.Linked, (Guid?)job.Id), (held.State, held.JobId));
// Kept apart meanwhile: the retry refuses, sends nothing and forgets its journal.
Assert.True(await f.Sync.Copies.SetStateAsync(pair.Id, null, CopyState.KeptApart, new CopyAnswer(pair.Fingerprint, null, null, false, DateTimeOffset.UtcNow), 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(uploads, f.Remote.IntoUploads);
Assert.Count(3, await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct));
Assert.Equal(CopyState.KeptApart, (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!.State);
Assert.False(File.Exists(IntegrateJournal.PathFor(f.Archive.Paths, job.Id)));
}
[Test]
public async Task ACancelAfterTheLinkLeavesThePairLinkedAndFreeOfTheJob()
{
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 running = new TaskCompletionSource<Guid>(TaskCreationOptions.RunContinuationsAsynchronously);
handler.BeforeStage = async stage =>
{
if (stage == IntegrateStage.Photos) await engine.CancelAsync(await running.Task, CancellationToken.None);
};
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);
running.SetResult(job.Id);
await f.Archive.WaitForAsync(job.Id, JobState.Cancelled);
var until = DateTime.UtcNow.AddSeconds(10);
var after = (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!;
while ((after.State != CopyState.Linked || after.JobId is not null) && DateTime.UtcNow < until)
{
await Task.Delay(20, f.Ct);
after = (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!;
}
// The link the job made stays (both sides are joined); the pair no longer waits for the cancelled job.
Assert.Equal((CopyState.Linked, (Guid?)null), (after.State, after.JobId));
Assert.Equal(local.Id, (await f.Sync.Links.GetAsync(f.Server, remote.Id, f.Ct))?.LocalId);
}
[Test]
public async Task ProjectsAreCopiedAcrossBothWaysUnderFreeNames()
{
+6 -3
View File
@@ -54,8 +54,11 @@ internal sealed class SyncFixture : IAsyncDisposable
return photos;
}
/// <summary>A local sequence made through the archive ingest (hashed frames, a real sidecar).</summary>
public async Task<Sequence> LocalAsync(string title, IReadOnlyList<byte[]> originals, string? settingsJson = null)
/// <summary>
/// A local sequence made through the archive ingest (hashed frames, a real sidecar), each frame captured 10 s after the
/// one before from <see cref="ArchiveFixture.Start"/>, or at <paramref name="capturedAt"/>.
/// </summary>
public async Task<Sequence> LocalAsync(string title, IReadOnlyList<byte[]> originals, string? settingsJson = null, IReadOnlyList<DateTime>? capturedAt = null)
{
Guid? id = null;
var staging = Path.Combine(Archive.Paths.StagingDirectory, "test");
@@ -66,7 +69,7 @@ internal sealed class SyncFixture : IAsyncDisposable
await File.WriteAllBytesAsync(path, originals[i], Ct);
var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(originals[i]));
var result = await Archive.Service.Ingest.CommitAsync(new IngestRequest(Archive.Archive.Id, id, id is null ? title : null, null, path,
$"IMG_{i + 1:0000}.JPG", originals[i].Length, sha, ArchiveFixture.Start.AddSeconds(10 * i), null), Ct);
$"IMG_{i + 1:0000}.JPG", originals[i].Length, sha, capturedAt?[i] ?? ArchiveFixture.Start.AddSeconds(10 * i), null), Ct);
id = result.SequenceId;
}
var sequence = (await Db.Sequences.GetAsync(id!.Value, Ct))!;
@@ -239,6 +239,51 @@ public sealed class SyncServiceCopyTests(TestContext context)
Assert.True((await f.Sync.Copies.ListByServerAsync(f.Server, f.Ct)).All(p => p.RemoteGoneHits == 0));
}
[Test]
public async Task ASequenceForgottenOnAServerKnownByItsAddressIsFoundAgainWhenItIsBack()
{
await using var f = await SyncFixture.CreateAsync(context);
f.Remote.Capabilities.Add(ServerCapabilities.PhotoLookup);
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Comet", photos);
var remote = f.Remote.Add("Comet", photos);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
f.Remote.Sequences.Remove(remote.Id);
await f.RefreshAsync();
Assert.Null(await f.Sync.LinkOfAsync(f.Server, local.Id, f.Ct));
// Back on the server: nothing remembers the forgotten link, so the two are found and asked about again.
f.Remote.Sequences[remote.Id] = remote;
await f.RefreshAsync();
var pair = (await f.Sync.Copies.ListByServerAsync(f.Server, f.Ct)).Single();
Assert.Equal((local.Id, remote.Id, CopyState.Candidate), (pair.LocalId, pair.RemoteId!.Value, pair.State));
Assert.Null(await f.Sync.LinkOfAsync(f.Server, local.Id, f.Ct));
}
[Test]
public async Task ALinkedPairWhoseLinkIsGoneIsAskedAboutAgain()
{
await using var f = await SyncFixture.CreateAsync(context);
f.Remote.Capabilities.Add(ServerCapabilities.PhotoLookup);
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Comet", photos);
var remote = f.Remote.Add("Comet", photos);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
// The link removed by another hand, the pair's row left as it was.
await f.Sync.Links.DeleteAsync(f.Server, remote.Id, f.Ct);
var raised = new List<CopyFindResult>();
f.Sync.CopiesFound += (_, result) => raised.Add(result);
await f.RefreshAsync();
var pair = (await f.Sync.Copies.ListByServerAsync(f.Server, f.Ct)).Single();
Assert.Equal((CopyState.Candidate, false), (pair.State, pair.AutoPhotos));
Assert.Contains(pair.Id, raised.SelectMany(r => r.New).Select(p => p.Id));
}
[Test]
public async Task DeletionCountsFollowEachComparison()
{
@@ -83,32 +83,128 @@ public sealed class SyncTransferCopyTests(TestContext context)
[Test]
public async Task AServerSequenceGoneMarksThePairAndSendsNowhereElse()
{
await using var f = await SyncFixture.CreateAsync(context);
// A server known by its id: a 404 there is that server's word (an address may name another server tomorrow).
var nas = new FakeSyncRemote { Server = ServerIdentity.IdKey(Guid.NewGuid()) };
nas.Capabilities.Add(ServerCapabilities.UploadInto);
nas.Capabilities.Add(ServerCapabilities.PhotoLookup);
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Comet", photos);
var remote = nas.Add("Comet", [photos[0]]);
var pair = await PairAsync(f, nas.Server, local.Id, remote.Id);
nas.Sequences.Remove(remote.Id);
var job = await ContextAsync(f);
var transfers = new SyncTransfers(f.Sync);
await Assert.ThrowsAsync<RemoteGoneException>(() => transfers.SendPhotosAsync(job, nas, local.Id, remote.Id, [Sha(photos[1])]));
await Assert.ThrowsAsync<RemoteGoneException>(() => transfers.FetchPhotosAsync(job, nas, remote.Id, local.Id, [Sha(photos[0])]));
Assert.Empty(nas.Sequences);
Assert.Equal(0, nas.IntoUploads);
Assert.Equal(1, (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!.RemoteGoneHits);
// A refresh right after asks again but does not count a second 404 within the recheck interval.
await f.Sync.RefreshAsync(nas, new SyncPolicy(), f.Ct);
Assert.Equal(1, (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!.RemoteGoneHits);
}
[Test]
public async Task AGoneAnswerFromAServerKnownByItsAddressMarksNothing()
{
await using var f = Capable(await SyncFixture.CreateAsync(context));
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Comet", photos);
var remote = f.Remote.Add("Comet", [photos[0]]);
var pair = await f.Sync.Copies.UpsertFoundAsync(new FoundCopy
var pair = await PairAsync(f, f.Server, local.Id, remote.Id);
f.Remote.Sequences.Remove(remote.Id);
var job = await ContextAsync(f);
await Assert.ThrowsAsync<RemoteGoneException>(() => new SyncTransfers(f.Sync).SendPhotosAsync(job, f.Remote, local.Id, remote.Id, [Sha(photos[1])]));
Assert.Equal(0, (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!.RemoteGoneHits);
}
[Test]
public async Task AFirstUploadWhoseFirstPhotoTheServerHoldsElsewhereEndsWithEveryPhotoInTheNewSequence()
{
await using var f = Capable(await SyncFixture.CreateAsync(context));
var photos = f.NewPhotos(3);
var local = await f.LocalAsync("Comet", photos);
// The first photo is already on the server, in another sequence.
f.Remote.Add("Elsewhere", [photos[0]]);
var engine = await f.StartEngineAsync();
var job = await f.Sync.EnqueueUploadAsync(engine, f.Server, local);
await f.Archive.WaitForAsync(job.Id, JobState.Done);
var link = Assert.NotNull(await f.Sync.Links.FindByLocalAsync(f.Server, local.Id, f.Ct));
var target = f.Remote.Sequences[link.RemoteId];
Assert.True(target.Frames.Select(fr => fr.Sha256).ToHashSet().SetEquals(photos.Select(Sha)));
}
[Test]
public async Task AnAutomaticUploadAfterTheServerCopyIsGoneFailsAndCreatesNothing()
{
await using var f = Capable(await SyncFixture.CreateAsync(context));
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Comet", photos);
var remote = f.Remote.Add("Comet", [photos[0]]);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
var pair = await PairAsync(f, f.Server, local.Id, remote.Id);
Assert.True(await f.Sync.Copies.SetStateAsync(pair.Id, null, CopyState.Integrated, new CopyAnswer(null, null, null, true, DateTimeOffset.UtcNow), f.Ct));
f.Remote.Sequences.Remove(remote.Id);
var engine = await f.StartEngineAsync();
var job = await f.Sync.EnqueueAutomaticUploadAsync(engine, f.Server, local, f.Ct);
await f.Archive.WaitForAsync(job.Id, JobState.Failed);
// Never a new server sequence: the automatic photos move only between the two the pair joins.
Assert.Empty(f.Remote.Sequences);
}
[Test]
public async Task AnAutomaticUploadOfAPairNoLongerIntegratedSendsNothing()
{
await using var f = Capable(await SyncFixture.CreateAsync(context));
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Comet", photos);
var remote = f.Remote.Add("Comet", [photos[0]]);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await PairAsync(f, f.Server, local.Id, remote.Id, CopyState.Linked);
var engine = await f.StartEngineAsync();
var job = await f.Sync.EnqueueAutomaticUploadAsync(engine, f.Server, local, f.Ct);
await f.Archive.WaitForAsync(job.Id, JobState.Failed);
Assert.Count(1, remote.Frames);
}
[Test]
public async Task AnAutomaticDownloadWithoutTheLocalCopyFailsAndCreatesNoSequence()
{
await using var f = Capable(await SyncFixture.CreateAsync(context));
var remote = f.Remote.Add("Comet", f.NewPhotos(2));
var engine = await f.StartEngineAsync();
var job = await f.Sync.EnqueueAutomaticDownloadAsync(engine, f.Server, remote.Id, "Comet", f.Archive.Archive.Id, f.Ct);
await f.Archive.WaitForAsync(job.Id, JobState.Failed);
Assert.Equal(0, await f.Db.Sequences.CountAsync(new() { ArchiveId = f.Archive.Archive.Id }, f.Ct));
}
private static Task<FoundCopy> PairAsync(SyncFixture f, string server, Guid localId, Guid remoteId, CopyState state = CopyState.Candidate) =>
f.Sync.Copies.UpsertFoundAsync(new FoundCopy
{
Id = Guid.NewGuid(),
Kind = CopyKind.LocalServer,
LocalId = local.Id,
Server = f.Server,
RemoteId = remote.Id,
LocalId = localId,
Server = server,
RemoteId = remoteId,
Evidence = CopyEvidence.Hashes,
Fingerprint = "f",
State = state,
}, f.Ct);
f.Remote.Sequences.Remove(remote.Id);
var job = await ContextAsync(f);
var transfers = new SyncTransfers(f.Sync);
await Assert.ThrowsAsync<RemoteGoneException>(() => transfers.SendPhotosAsync(job, f.Remote, local.Id, remote.Id, [Sha(photos[1])]));
await Assert.ThrowsAsync<RemoteGoneException>(() => transfers.FetchPhotosAsync(job, f.Remote, remote.Id, local.Id, [Sha(photos[0])]));
Assert.Empty(f.Remote.Sequences);
Assert.Equal(0, f.Remote.IntoUploads);
Assert.True((await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!.RemoteGoneHits >= 1);
}
[Test]
public async Task ALinkedUploadFilesIntoItsTargetWhenTheServerCan()