Files
Chronos/tests/Chronos.Data.Tests/Copies/CopySidesTests.cs
T
Alby96andClaude Opus 5.5 f6f81e2c37 feat(copies): pure comparison, detection rules, details payload and comparison sides
Detection, Integrate and Merge need one comparison of two copies of a time-lapse, so
this adds it: CopyComparer classifies every photo (same, converted, same unhashed,
deleted here/there, differs, two versions, only here/there) with strict Differs and
Versions rules so whole-second capture times and bursts never pair, unusable frames
held as nothing and marked for repair, and the score of design 3.2.
CopyRules ports 1.1's same-originals gate exactly for "Link when half shared" and
holds the Decide table, ask-again thresholds, fingerprints, hints and the probe
sample. CopyPayload compares details in portable form (same photo at another index is
no line) and merges the chosen sides into one payload that must parse as a project.
CopySides reads both sides from the index and a server and answers the last-copy
check across every archive. SyncTombstones gains FromDtos for local pairs.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:58:07 +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;
}