Files
Chronos/tests/Chronos.Data.Tests/Sync/SyncAgentCopyTests.cs
T
Alby96andClaude Opus 5.5 d10577b175 fix(copies): close the owner review 6 gaps with tests
Photos not sent: automatic photos of an integrated pair that failed three
times in a row are now told with an error toast that shows the row, and the
row reads "not sent" with the last reason. The matches-found and linked toasts
of background detections run from their Windows notification too.

Maintenance now clears a merge's or an integration's journal whose job is done,
cancelled, or deleted without a cancel and untouched for the history's days.

Tests pin the matches jobs in the priority queue, the owner's defaults (ask
always, union, copy across), the dialog starting from the settings, Choose
each preselecting nothing, no automatic photos for a pair whose server copy
is gone, the repair of a corrupt local frame by an Integrate, and the owner's
whole scenario end to end against a real in-process server.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 14:03:23 +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);
}
}