Files
Chronos/tests/Chronos.App.Tests/Server/CopyWatchTests.cs
T
Alby96andClaude Opus 5.5 96b5b4078b feat(copies): engine composition, automatic photos, metered hold and App wiring (owner review 6, P5)
CopyEngine puts the planners together behind ICopyEngine so the App can find,
compare, integrate, merge, link, unlink and keep pairs apart. It also moves
photos that one side deleted to the other side's trash. Each trash step
re-reads both sides when applied, and nothing moves if any photo was restored
since the dry run. The sync agent now queues the new photos of pairs that were
integrated with automatic photos, both ways, at background priority. Failures
back off, and three in a row are reported so the pair can show "not sent". On
a metered connection, integrations are held like other server transfers.
AppServices builds the engine and registers the Integrate and Merge handlers.
A CopyWatch looks for local copies after start and after jobs that change
photos. Merges wait for workspace work that has not been sent home.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 12:03:56 +01:00

101 lines
3.7 KiB
C#

using Chronos.App.Server;
using Chronos.Core.Model;
using Chronos.Testing;
namespace Chronos.App.Tests.Server;
/// <summary>
/// The local detection that runs by itself (owner review 6, #73; design §3.4): once after the index opened, after a job that
/// changes photos ended, one at a time, and the requests that arrive meanwhile coalesce into one more.
/// </summary>
public sealed class CopyWatchTests
{
private static readonly TimeSpan Deadline = TimeSpan.FromSeconds(30);
private static readonly CopyWatchOptions Quick = new() { StartDelay = TimeSpan.FromMilliseconds(20), AfterJobDelay = TimeSpan.FromMilliseconds(50) };
private static Job Ended(JobKind kind, JobState state = JobState.Done) => new()
{
Id = Guid.CreateVersion7(),
Kind = kind,
State = state,
Created = DateTimeOffset.UtcNow,
};
private static async Task WaitAsync(Func<bool> condition, string what)
{
var until = DateTime.UtcNow + Deadline;
while (!condition())
{
if (DateTime.UtcNow > until) Assert.Fail($"timed out waiting for {what}");
await Task.Delay(10);
}
}
[Test]
public async Task ItLooksOnceAfterStartAndAfterAJobThatChangesPhotos()
{
var runs = 0;
await using var watch = new CopyWatch(_ =>
{
Interlocked.Increment(ref runs);
return Task.CompletedTask;
}, Quick);
watch.Start(null);
await WaitAsync(() => watch.Runs == 1, "the first detection");
watch.OnJobChanged(Ended(JobKind.Render));
watch.OnJobChanged(Ended(JobKind.Scan, JobState.Running));
await Task.Delay(200);
Assert.Equal(1, watch.Runs, "a render, or a scan still running, asks for none");
watch.OnJobChanged(Ended(JobKind.Scan));
watch.OnJobChanged(Ended(JobKind.Import, JobState.Failed));
watch.OnJobChanged(Ended(JobKind.MergeSequences, JobState.Cancelled));
await WaitAsync(() => watch.Runs == 2, "the detection after the jobs");
await Task.Delay(200);
Assert.Equal(2, Volatile.Read(ref runs), "jobs that end close together ask for one detection");
}
[Test]
public async Task RequestsWhileOneRunsGiveExactlyOneMore()
{
var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var running = 0;
var overlapped = false;
await using var watch = new CopyWatch(async _ =>
{
if (Interlocked.Increment(ref running) > 1) overlapped = true;
await release.Task;
Interlocked.Decrement(ref running);
}, Quick with { StartDelay = TimeSpan.Zero, AfterJobDelay = TimeSpan.Zero });
watch.Start(null);
await WaitAsync(() => Volatile.Read(ref running) == 1, "the first detection to start");
foreach (var kind in CopyWatch.Triggers) watch.OnJobChanged(Ended(kind));
await Task.Delay(100);
release.TrySetResult();
await WaitAsync(() => watch.Runs == 2, "the one more detection");
await Task.Delay(200);
Assert.Equal(2, watch.Runs);
Assert.False(overlapped, "never two at once");
}
[Test]
public async Task AFailedDetectionDoesNotStopTheWatch()
{
var calls = 0;
await using var watch = new CopyWatch(_ => Interlocked.Increment(ref calls) == 1 ? throw new InvalidOperationException("index busy") : Task.CompletedTask,
Quick);
watch.Start(null);
await WaitAsync(() => watch.Runs == 1, "the failed detection");
watch.OnJobChanged(Ended(JobKind.Download));
await WaitAsync(() => watch.Runs == 2, "the next detection");
Assert.Equal(2, Volatile.Read(ref calls));
}
}