Files
Chronos/tests/Chronos.Data.Tests/Sync/SyncAgentCopyTests.cs
T
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

147 lines
7.7 KiB
C#

using Chronos.Core.IO;
using Chronos.Core.Model;
using Chronos.Data.Copies;
using Chronos.Data.Jobs;
using Chronos.Data.Sync;
using Chronos.Testing;
namespace Chronos.Data.Tests.Sync;
/// <summary>
/// The new photos of a pair integrated with automatic photos (owner review 6, #73; design §7.3): after a comparison the
/// agent queues them both ways in the background; never for a pair only linked or integrated without automatic photos; a
/// failed transfer counts, waits for its backoff, and three in a row are told.
/// </summary>
[Category("sync")]
public sealed class SyncAgentCopyTests(TestContext context)
{
private static readonly SyncAgentOptions Quick = new()
{
RefreshInterval = TimeSpan.FromHours(1),
EventDelay = TimeSpan.FromMilliseconds(20),
MinBackoff = TimeSpan.FromMilliseconds(20),
MaxBackoff = TimeSpan.FromMilliseconds(200),
};
/// <summary>An engine that queues but never runs, so the queued jobs can be read and settled by the test.</summary>
private static JobEngine Queue(SyncFixture f) => new(f.Db, [.. f.Sync.CreateJobHandlers(new SyncFixture.Connector(f.Remote))],
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50) });
/// <summary>A local sequence and a server sequence sharing two photos, each with one the other lacks, linked, in <paramref name="state"/>.</summary>
private static async Task<(Sequence Local, FakeSequence Remote)> PairAsync(SyncFixture f, string title, CopyState state, bool autoPhotos)
{
var photos = f.NewPhotos(4);
var local = await f.LocalAsync(title, photos.Take(3).ToList());
var remote = f.Remote.Add(title + " NAS", [photos[0], photos[1], photos[3]]);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
var row = await f.Sync.Copies.EnsureLinkedAsync(f.Server, remote.Id, local.Id, f.Ct);
if (state != CopyState.Linked)
Assert.True(await f.Sync.Copies.SetStateAsync(row.Id, null, state, new CopyAnswer(null, null, null, autoPhotos, DateTimeOffset.UtcNow), f.Ct));
return (local, remote);
}
private static async Task<List<Job>> QueuedAsync(SyncFixture f, JobKind kind) =>
[.. (await f.Db.Jobs.ListAsync(JobState.Queued, kind, 50, cancellationToken: f.Ct)).Items];
[Test]
public async Task AnIntegratedPairWithAutomaticPhotosQueuesBothWaysInTheBackground()
{
await using var f = await SyncFixture.CreateAsync(context);
var (local, remote) = await PairAsync(f, "Comet", CopyState.Integrated, autoPhotos: true);
await using var engine = Queue(f);
await using var agent = new SyncAgent(f.Sync, f.Remote, () => new SyncPolicy(), engine, Quick);
await agent.RefreshAsync(f.Ct);
await agent.RefreshAsync(f.Ct);
var upload = (await QueuedAsync(f, JobKind.UploadCommit)).Single();
var download = (await QueuedAsync(f, JobKind.Download)).Single();
Assert.Equal((local.Id, JobPriority.Background), (upload.TargetIds.Single(), upload.Priority));
Assert.Equal((remote.Id, JobPriority.Background), (download.TargetIds.Single(), download.Priority));
Assert.Contains(local.ArchiveId.ToString(), download.ParametersJson ?? string.Empty);
Assert.Count(0, agent.PhotoFailures);
}
[Test]
public async Task APairOnlyLinkedOrWithoutAutomaticPhotosQueuesNothing()
{
await using var f = await SyncFixture.CreateAsync(context);
await PairAsync(f, "Linked", CopyState.Linked, autoPhotos: false);
await PairAsync(f, "Manual", CopyState.Integrated, autoPhotos: false);
await using var engine = Queue(f);
await using var agent = new SyncAgent(f.Sync, f.Remote, () => new SyncPolicy(), engine, Quick);
await agent.RefreshAsync(f.Ct);
Assert.Count(0, await QueuedAsync(f, JobKind.UploadCommit));
Assert.Count(0, await QueuedAsync(f, JobKind.Download));
}
[Test]
public async Task APairWhoseServerCopyIsGoneQueuesNoAutomaticPhotos()
{
await using var f = await SyncFixture.CreateAsync(context);
// A server keyed by its id, so a 404 counts against the pair (design §7.5).
var nas = new FakeSyncRemote { Server = HomeFolders.IdKey(Guid.NewGuid()) };
var photos = f.NewPhotos(3);
var local = await f.LocalAsync("Comet", photos);
var remote = nas.Add("Comet NAS", photos.Take(2));
await f.Sync.LinkAsync(nas.Server, remote.Id, local.Id, f.Ct);
var row = await f.Sync.Copies.EnsureLinkedAsync(nas.Server, remote.Id, local.Id, f.Ct);
Assert.True(await f.Sync.Copies.SetStateAsync(row.Id, null, CopyState.Integrated,
new CopyAnswer(null, null, null, true, DateTimeOffset.UtcNow), f.Ct));
nas.Drop(remote.Id);
await using var engine = new JobEngine(f.Db, [.. f.Sync.CreateJobHandlers(new SyncFixture.Connector(nas))],
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50) });
await using var agent = new SyncAgent(f.Sync, nas, () => new SyncPolicy(), engine, Quick);
await agent.RefreshAsync(f.Ct);
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), row.Id], f.Ct);
await agent.RefreshAsync(f.Ct);
var pair = (await f.Sync.Copies.GetAsync(row.Id, f.Ct))!;
Assert.True(pair.RemoteGoneHits > 0);
Assert.Equal((CopyState.Integrated, true), (pair.State, pair.AutoPhotos));
Assert.Count(0, await QueuedAsync(f, JobKind.UploadCommit));
Assert.Count(0, await QueuedAsync(f, JobKind.Download));
Assert.Count(0, agent.PhotoFailures);
}
[Test]
public async Task FailedTransfersCountWaitForTheirBackoffAndThreeInARowAreTold()
{
await using var f = await SyncFixture.CreateAsync(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.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
var row = await f.Sync.Copies.EnsureLinkedAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.Sync.Copies.SetStateAsync(row.Id, null, CopyState.Integrated, new CopyAnswer(null, null, null, true, DateTimeOffset.UtcNow), f.Ct);
await using var engine = Queue(f);
await using var agent = new SyncAgent(f.Sync, f.Remote, () => new SyncPolicy(), engine, Quick);
var told = new List<(Guid, AutoPhotoFailure)>();
agent.PhotosNotSent += (id, failure) => told.Add((id, failure));
for (var attempt = 1; attempt <= SyncAgent.NotSentAfter; attempt++)
{
await agent.RefreshAsync(f.Ct);
var upload = (await QueuedAsync(f, JobKind.UploadCommit)).Single();
await f.Db.Jobs.SetStateAsync(upload.Id, JobState.Failed, "Quota reached", f.Ct);
await agent.RefreshAsync(f.Ct);
Assert.Equal(new AutoPhotoFailure(attempt, "Quota reached"), agent.PhotoFailures[local.Id]);
// Within its backoff nothing new is queued.
Assert.Count(0, await QueuedAsync(f, JobKind.UploadCommit));
await Task.Delay(Quick.MaxBackoff + TimeSpan.FromMilliseconds(50), f.Ct);
}
Assert.Equal((local.Id, SyncAgent.NotSentAfter), (told.Single().Item1, told.Single().Item2.Count));
await agent.RefreshAsync(f.Ct);
var last = (await QueuedAsync(f, JobKind.UploadCommit)).Single();
await f.Db.Jobs.SetStateAsync(last.Id, JobState.Done, null, f.Ct);
await agent.RefreshAsync(f.Ct);
Assert.Count(0, agent.PhotoFailures);
}
}