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

131 lines
6.5 KiB
C#

using Chronos.Core.Model;
using Chronos.Data.Copies;
using Chronos.Data.Sync;
using Chronos.Data.Tests.Infrastructure;
using Chronos.Data.Tests.Sync;
using Chronos.Protocol;
using Chronos.Testing;
namespace Chronos.Data.Tests.Copies;
/// <summary>The two sides of a comparison read from the index and from a server, and the last-copy check (owner review 6, #73; design §4.2).</summary>
[Category("sync")]
public abstract class CopySidesTests(TestContext context) : RepositoryTest(context)
{
private static string Sha(int n) => Convert.ToHexStringLower(BitConverter.GetBytes((long)n)).PadLeft(64, 'c');
private async Task<(Archive Archive, Sequence Sequence)> SequenceAsync(ChronosDatabase db, Archive? archive = null)
{
if (archive is null)
{
archive = TestData.Archive("A" + Guid.NewGuid().ToString("N")[..6]);
await db.Archives.UpsertAsync(archive, Ct);
}
var sequence = TestData.Sequence(archive.Id);
await db.Sequences.UpsertAsync(sequence, Ct);
return (archive, sequence);
}
private async Task<Frame> FrameAsync(ChronosDatabase db, Guid sequenceId, int index, string? sha, FrameFlags flags = FrameFlags.None)
{
var frame = TestData.Frame(sequenceId, index) with { Sha256 = sha, Flags = flags };
await db.Frames.InsertAsync(frame, Ct);
return frame;
}
[Test]
public async Task ALocalSideHoldsItsFramesTombstonesAndUnusableFlags()
{
var db = await OpenChronosAsync();
var (_, sequence) = await SequenceAsync(db);
var frames = new[]
{
await FrameAsync(db, sequence.Id, 0, Sha(0)),
await FrameAsync(db, sequence.Id, 1, Sha(1), FrameFlags.Missing),
await FrameAsync(db, sequence.Id, 2, Sha(2), FrameFlags.Corrupt | FrameFlags.Excluded),
await FrameAsync(db, sequence.Id, 3, null),
await FrameAsync(db, sequence.Id, 4, Sha(4), FrameFlags.Excluded),
};
await db.FrameRemovals.SyncSequenceAsync(sequence.Id,
[
new FrameRemoval(sequence.Id, Sha(8), null, RemovalReason.Deleted, TestData.Now),
new FrameRemoval(sequence.Id, Sha(9), Sha(4), RemovalReason.Replaced, TestData.Now.AddMinutes(1)),
], Ct);
var side = await new CopySides(db).LocalAsync(sequence.Id, Ct);
Assert.SequenceEqual(frames.Select(f => f.Id), side.Photos.Select(p => p.FrameId));
Assert.SequenceEqual([false, true, true, false, false], side.Photos.Select(p => p.Unusable));
Assert.Null(side.Photos[3].Sha256);
Assert.Equal((frames[0].FileName, frames[0].Size, frames[0].CapturedAt), (side.Photos[0].FileName, side.Photos[0].Size, side.Photos[0].CapturedAt));
Assert.SequenceEqual([(Sha(8), (string?)null, "deleted"), (Sha(9), Sha(4), "replaced")], side.Removed.Select(r => (r.Sha256, r.Replacement, r.Reason)));
// Compared with a copy holding every photo, the unusable frames are repaired and the converted one is the same photo.
var there = new CopySideInput([.. Enumerable.Range(0, 5).Select(i => new CopyPhotoInput(Guid.NewGuid(), i, frames[i].FileName,
i == 4 ? Sha(9) : Sha(i), frames[i].Size, frames[i].CapturedAt, false))], []);
var result = CopyComparer.Compare(side, there);
Assert.Equal((2, 1, 2), (result.Score.Shared, result.Score.SharedUnhashed, result.Score.OnlyThere));
Assert.True(result.Of(PhotoClass.OnlyThere).All(p => p.RepairsHere));
Assert.Equal(Sha(9), Assert.Contains(result.Photos, p => p.Class == PhotoClass.Converted).There!.Sha256);
}
[Test]
public async Task ARemoteSideTakesCaptureTimesFromTheFrameRows()
{
var db = await OpenChronosAsync();
using var remote = new FakeSyncRemote();
var at = new DateTime(2026, 9, 28, 18, 0, 0, 120);
var sequence = remote.Add("Harbour", []);
sequence.AddFrame([1, 2, 3], "IMG_0001.JPG", at);
sequence.AddFrame([4, 5, 6], "IMG_0002.JPG", at.AddSeconds(5));
sequence.Removed.Add(new RemovedPhotoDto(Sha(7), null, "deleted", TestData.Now));
var side = await new CopySides(db).RemoteAsync(remote, sequence.Id, Ct);
Assert.SequenceEqual(sequence.Frames.Select(f => (f.Id, f.FileName, f.Sha256, (long)f.Bytes.Length, f.CapturedAt)),
side.Photos.Select(p => (p.FrameId, p.FileName, p.Sha256!, p.Size, p.CapturedAt)));
Assert.True(side.Photos.All(p => !p.Unusable));
Assert.SequenceEqual([Sha(7)], side.Removed.Select(r => r.Sha256));
// A server that never hashed its originals lists none: they compare by size and time.
sequence.Unhashed = true;
Assert.True((await new CopySides(db).RemoteAsync(remote, sequence.Id, Ct)).Photos.All(p => p.Sha256 is null));
}
[Test]
public async Task HeldElsewhereLooksAcrossEveryArchiveOfTheIndex()
{
var db = await OpenChronosAsync();
var (_, mine) = await SequenceAsync(db);
var (other, inOther) = await SequenceAsync(db);
var (_, alsoInOther) = await SequenceAsync(db, other);
await FrameAsync(db, mine.Id, 0, Sha(1));
await FrameAsync(db, mine.Id, 1, Sha(2));
await FrameAsync(db, mine.Id, 2, Sha(3));
await FrameAsync(db, inOther.Id, 0, Sha(1));
await FrameAsync(db, alsoInOther.Id, 0, Sha(2), FrameFlags.Missing);
await FrameAsync(db, alsoInOther.Id, 1, Sha(4));
var sides = new CopySides(db);
var held = await sides.HeldElsewhereAsync([Sha(1).ToUpperInvariant(), Sha(2), Sha(3), Sha(4), string.Empty], [mine.Id], Ct);
// Photo 2's other frame is missing: it holds nothing. Photo 3 is only in the compared sequence.
Assert.True(held.SetEquals([Sha(1), Sha(4)]));
Assert.True((await sides.HeldElsewhereAsync([Sha(1), Sha(4)], [mine.Id, inOther.Id, alsoInOther.Id], Ct)).Count == 0);
Assert.True((await sides.HeldElsewhereAsync([Sha(1)], [], Ct)).SetEquals([Sha(1)]));
Assert.Empty(await sides.HeldElsewhereAsync([], [mine.Id], Ct));
// Batched: more hashes than one statement takes.
var many = Enumerable.Range(100, 1200).Select(Sha).Append(Sha(4)).ToList();
Assert.True((await sides.HeldElsewhereAsync(many, [mine.Id], Ct)).SetEquals([Sha(4)]));
}
}
public sealed class SqliteCopySidesTests(TestContext context) : CopySidesTests(context)
{
protected override Backend Backend => Backend.SqliteMemory;
}
[RequiresEnvironment(PgTestServer.Variable)]
public sealed class PostgresCopySidesTests(TestContext context) : CopySidesTests(context)
{
protected override Backend Backend => Backend.Postgres;
}