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

127 lines
6.1 KiB
C#

using Chronos.Core.Import;
using Chronos.Core.Model;
using Chronos.Data.Archiving;
using Chronos.Data.Tests.Infrastructure;
using Chronos.Testing;
using static Chronos.Data.Tests.Archiving.ArchiveFixture;
namespace Chronos.Data.Tests.Archiving;
/// <summary>
/// Trash steps run inside the job that holds the sequence (an Integrate or a merge, owner review 6, #73): the job's own hold
/// does not make the sequence busy, another job's still does, and a frames version that moved refuses the step.
/// </summary>
[Category("archive")]
public abstract class TrashExceptJobTests(TestContext context) : DatabaseTest(context)
{
private async Task<ArchiveFixture> FixtureAsync() => Own(await CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true)));
private static async Task<Sequence> UploadedAsync(ArchiveFixture fx, int count, string session, int seed)
{
Guid? id = null;
for (var i = 0; i < count; i++)
{
var (path, size, sha) = fx.Stage(Start.AddSeconds(10 * i), seed + i);
var result = await fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, id, null, id is null ? session : null, path, $"IMG_{i + 1:0000}.JPG",
size, sha, null, null), fx.Ct);
id = result.SequenceId;
}
return (await fx.Db.Sequences.GetAsync(id!.Value))!;
}
private static async Task<Job> RunningAsync(ArchiveFixture fx, JobKind kind, params Guid[] targets)
{
var job = new Job
{
Id = Guid.CreateVersion7(),
Kind = kind,
TargetIds = targets,
State = JobState.Running,
NodeId = "test#0",
Created = DateTimeOffset.UtcNow,
Started = DateTimeOffset.UtcNow,
};
await fx.Db.Jobs.EnqueueAsync(job);
return job;
}
[Test]
public async Task APhotoTrashStepInsideItsOwnJobIsNotBusyAnotherJobsTargetIs()
{
var fx = await FixtureAsync();
var sequence = await UploadedAsync(fx, 3, "upload-1", 100);
var frames = await fx.Db.Frames.ListBySequenceAsync(sequence.Id);
var trash = fx.Service.FrameTrash;
var integrate = await RunningAsync(fx, JobKind.Integrate, sequence.Id);
Assert.SequenceEqual([FrameDeleteBlock.Busy], (await trash.PlanDeleteAsync(sequence.Id, [frames[0].Id], sequence.FramesVersion, fx.Ct)).Blocked);
var plan = await trash.PlanDeleteAsync(sequence.Id, [frames[0].Id], sequence.FramesVersion, fx.Ct, exceptJob: integrate.Id);
Assert.True(plan.CanDelete, string.Join(", ", plan.Blocked));
// The re-plan of the deletion itself must not count the job either.
await Assert.ThrowsAsync<StaleDeletePlanException>(() => trash.DeleteAsync(plan, "alberto", null, fx.Ct));
var result = await trash.DeleteAsync(plan, "alberto", null, fx.Ct, exceptJob: integrate.Id);
Assert.Equal(sequence.Id, result.Sequence.Id);
Assert.Count(2, await fx.Db.Frames.ListBySequenceAsync(sequence.Id));
await RunningAsync(fx, JobKind.Render, sequence.Id);
var now = (await fx.Db.Sequences.GetAsync(sequence.Id))!;
var held = await trash.PlanDeleteAsync(sequence.Id, [frames[1].Id], now.FramesVersion, fx.Ct, exceptJob: integrate.Id);
Assert.SequenceEqual([FrameDeleteBlock.Busy], held.Blocked);
}
[Test]
public async Task ASequenceTrashStepInsideItsOwnJobIsNotBusyAnotherJobsTargetIs()
{
var fx = await FixtureAsync();
var keep = await UploadedAsync(fx, 2, "upload-1", 100);
var drop = await UploadedAsync(fx, 2, "upload-2", 200);
var merge = await RunningAsync(fx, JobKind.MergeSequences, keep.Id, drop.Id);
Assert.Equal(DeleteBlock.Busy, (await fx.Service.Trash.PlanAsync([drop.Id], null, fx.Ct)).Blocked.Single().Reason);
var plan = await fx.Service.Trash.PlanAsync([drop.Id], null, fx.Ct, exceptJob: merge.Id);
Assert.Empty(plan.Blocked);
// Revalidated without the job's own hold too.
await Assert.ThrowsAsync<StaleDeletePlanException>(() => fx.Service.Trash.DeleteAsync(plan, null, null, fx.Ct));
Assert.NotNull(await fx.Db.Sequences.GetAsync(drop.Id));
var entries = await fx.Service.Trash.DeleteAsync(plan, "alberto", null, fx.Ct, exceptJob: merge.Id,
expectedFramesVersions: new Dictionary<Guid, int> { [drop.Id] = drop.FramesVersion });
Assert.Count(1, entries);
Assert.Null(await fx.Db.Sequences.GetAsync(drop.Id));
await RunningAsync(fx, JobKind.Render, keep.Id);
Assert.Equal(DeleteBlock.Busy, (await fx.Service.Trash.PlanAsync([keep.Id], null, fx.Ct, exceptJob: merge.Id)).Blocked.Single().Reason);
}
[Test]
public async Task AFramesVersionThatMovedRefusesTheStepUnderItsLeaseAndNothingMoves()
{
var fx = await FixtureAsync();
var drop = await UploadedAsync(fx, 2, "upload-1", 100);
var plan = await fx.Service.Trash.PlanAsync([drop.Id], null, fx.Ct);
var folder = fx.Paths.Sequence(drop.Folder).Folder;
// A photo added since the version the caller verified.
await Assert.ThrowsAsync<StaleDeletePlanException>(() => fx.Service.Trash.DeleteAsync(plan, null, null, fx.Ct,
expectedFramesVersions: new Dictionary<Guid, int> { [drop.Id] = drop.FramesVersion - 1 }));
Assert.NotNull(await fx.Db.Sequences.GetAsync(drop.Id));
Assert.True(Directory.Exists(folder));
// A sequence the map does not name is not checked; the one it names at its version goes.
var entries = await fx.Service.Trash.DeleteAsync(plan, null, null, fx.Ct,
expectedFramesVersions: new Dictionary<Guid, int> { [Guid.CreateVersion7()] = 99, [drop.Id] = drop.FramesVersion });
Assert.Count(1, entries);
Assert.False(Directory.Exists(folder));
}
}
public sealed class SqliteTrashExceptJobTests(TestContext context) : TrashExceptJobTests(context)
{
protected override Backend Backend => Backend.SqliteFile;
}
[RequiresEnvironment(PgTestServer.Variable)]
public sealed class PostgresTrashExceptJobTests(TestContext context) : TrashExceptJobTests(context)
{
protected override Backend Backend => Backend.Postgres;
}