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>
181 lines
10 KiB
C#
181 lines
10 KiB
C#
using Chronos.Core.Project;
|
|
using Chronos.Data.Copies;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Data.Tests.Copies;
|
|
|
|
/// <summary>The detection rules (owner review 6, Q1; design §3.1, §3.2, §3.6): pairs, the Link matches setting, 1.1's gate, asking again.</summary>
|
|
[Category("sync")]
|
|
public sealed class CopyRulesTests
|
|
{
|
|
private static readonly DateTime First = new(2026, 9, 28, 18, 0, 0, 400);
|
|
|
|
private static CopyScore Identical(int shared = 10, int unhashed = 0) =>
|
|
CopyScore.None with { Shared = shared, SharedUnhashed = unhashed, LocalPhotos = shared + unhashed, OtherPhotos = shared + unhashed, Containment = 1000, Overlap = 1000 };
|
|
|
|
private static CopyScore Partial(int shared, int onlyHere) =>
|
|
CopyScore.None with { Shared = shared, OnlyHere = onlyHere, LocalPhotos = shared + onlyHere, OtherPhotos = shared, Containment = 1000 };
|
|
|
|
/// <summary>1.1's gate passing: both unlinked, same second, same camera, half of 10 shared.</summary>
|
|
private static SameOriginalsInput Gate(int shared = 5) => new(true, First, First.AddMilliseconds(300), "EOS R5", "eos r5", 10, 10, shared);
|
|
|
|
private static readonly SameOriginalsInput Closed = new(false, null, null, null, null, 0, 0, 0);
|
|
|
|
private static FoundCopy KeptApart(int answeredShared, string? fingerprint = null) => new()
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Kind = CopyKind.LocalServer,
|
|
LocalId = Guid.NewGuid(),
|
|
Server = "https://nas.test:8443",
|
|
RemoteId = Guid.NewGuid(),
|
|
Evidence = CopyEvidence.Hashes,
|
|
State = CopyState.KeptApart,
|
|
Score = Partial(answeredShared, 5),
|
|
Fingerprint = "f",
|
|
AnsweredShared = answeredShared,
|
|
AnsweredFingerprint = fingerprint,
|
|
};
|
|
|
|
[Test]
|
|
public void APairNeedsALinkASessionKeyOrEnoughShared()
|
|
{
|
|
Assert.True(CopyRules.IsCopy(CopyEvidence.Link, CopyScore.None));
|
|
Assert.True(CopyRules.IsCopy(CopyEvidence.SessionKey, CopyScore.None));
|
|
Assert.False(CopyRules.IsCopy(CopyEvidence.Hashes, CopyScore.None));
|
|
Assert.True(CopyRules.IsCopy(CopyEvidence.Hashes, CopyScore.None with { Shared = 1, Containment = 100 }));
|
|
Assert.False(CopyRules.IsCopy(CopyEvidence.Hashes, CopyScore.None with { Shared = 1, Containment = 99 }));
|
|
// Tombstoned content is evidence too.
|
|
Assert.True(CopyRules.IsCopy(CopyEvidence.Hashes, CopyScore.None with { SharedRemoved = 3, Containment = 300 }));
|
|
Assert.True(CopyRules.IsCopy(CopyEvidence.SizeAndTime, CopyScore.None with { SharedUnhashed = 2, Containment = 1000 }));
|
|
}
|
|
|
|
[Test]
|
|
public void AskAlwaysNeverLinks()
|
|
{
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(CopyLinking.AskAlways, CopyKind.LocalServer, Identical(), Gate()));
|
|
Assert.Equal(CopyDecision.None, CopyRules.Decide(CopyLinking.AskAlways, CopyKind.LocalServer, CopyScore.None, Gate()));
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(CopyLinking.AskAlways, CopyKind.LocalServer, CopyScore.None, Closed, CopyEvidence.SessionKey));
|
|
}
|
|
|
|
[Test]
|
|
public void WhenIdenticalLinksOnlyTheSamePhotosKnownByHash()
|
|
{
|
|
Assert.Equal(CopyDecision.Link, CopyRules.Decide(CopyLinking.WhenIdentical, CopyKind.LocalServer, Identical(), Closed));
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(CopyLinking.WhenIdentical, CopyKind.LocalServer, Partial(10, 1), Gate()));
|
|
// A size and time identity always asks.
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(CopyLinking.WhenIdentical, CopyKind.LocalServer, Identical(8, 2), Closed));
|
|
// Photos deleted on one side only are no identity.
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(CopyLinking.WhenIdentical, CopyKind.LocalServer, Identical() with { DeletedThere = 1, SharedRemoved = 1 }, Closed));
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(CopyLinking.WhenIdentical, CopyKind.LocalServer, Identical() with { DeletedHere = 1, SharedRemoved = 1 }, Closed));
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(CopyLinking.WhenIdentical, CopyKind.LocalServer, Identical() with { Differs = 1 }, Closed));
|
|
}
|
|
|
|
[Test]
|
|
public void IdenticalNeedsNothingDeletedOnOneSide()
|
|
{
|
|
Assert.True(Identical().Identical);
|
|
Assert.False((Identical() with { DeletedHere = 1 }).Identical);
|
|
Assert.False((Identical() with { DeletedThere = 1 }).Identical);
|
|
Assert.False(CopyScore.None.Identical);
|
|
}
|
|
|
|
[Test]
|
|
public void WhenHalfSharedKeeps11sGates()
|
|
{
|
|
Assert.Equal(CopyDecision.Link, CopyRules.Decide(CopyLinking.WhenHalfShared, CopyKind.LocalServer, Partial(5, 5), Gate()));
|
|
// A different first-capture second, or another camera: asked.
|
|
var otherSecond = Gate() with { RemoteFirst = First.AddSeconds(1) };
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(CopyLinking.WhenHalfShared, CopyKind.LocalServer, Partial(5, 5), otherSecond));
|
|
var otherCamera = Gate() with { RemoteCamera = "EOS R6" };
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(CopyLinking.WhenHalfShared, CopyKind.LocalServer, Partial(5, 5), otherCamera));
|
|
// Fewer than half of the smaller side, nothing hashed here, or a side linked already: asked.
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(CopyLinking.WhenHalfShared, CopyKind.LocalServer, Partial(4, 6), Gate(4)));
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(CopyLinking.WhenHalfShared, CopyKind.LocalServer, Partial(5, 5), Gate() with { LocalHashed = 0 }));
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(CopyLinking.WhenHalfShared, CopyKind.LocalServer, Partial(5, 5), Gate() with { BothUnlinked = false }));
|
|
}
|
|
|
|
[Test]
|
|
public void SameOriginalsIsThe11Rule()
|
|
{
|
|
Assert.True(CopyRules.SameOriginals(Gate()));
|
|
// Half of the smaller side: 3 of a 6-frame server sequence is enough against 10 local hashes.
|
|
Assert.True(CopyRules.SameOriginals(Gate(3) with { RemoteFrames = 6 }));
|
|
Assert.False(CopyRules.SameOriginals(Gate(0) with { RemoteFrames = 0 }));
|
|
Assert.False(CopyRules.SameOriginals(Gate() with { LocalFirst = null }));
|
|
// Both without a camera model count as equal, as in 1.1.
|
|
Assert.True(CopyRules.SameOriginals(Gate() with { LocalCamera = null, RemoteCamera = null }));
|
|
}
|
|
|
|
[Test]
|
|
public void ALocalPairIsNeverLinkedByItself()
|
|
{
|
|
foreach (var linking in Enum.GetValues<CopyLinking>())
|
|
Assert.Equal(CopyDecision.Ask, CopyRules.Decide(linking, CopyKind.LocalLocal, Identical(), Gate()));
|
|
}
|
|
|
|
[Test]
|
|
public void AKeptApartPairAsksAgainOnlyWhenItGrewALotOrBecameIdentical()
|
|
{
|
|
// Ten more shared photos at least, and a quarter of the answered count.
|
|
Assert.False(CopyRules.AsksAgain(KeptApart(20), Partial(29, 5)));
|
|
Assert.True(CopyRules.AsksAgain(KeptApart(20), Partial(30, 5)));
|
|
Assert.False(CopyRules.AsksAgain(KeptApart(100), Partial(124, 5)));
|
|
Assert.True(CopyRules.AsksAgain(KeptApart(100), Partial(125, 5)));
|
|
// Identical now, and not when answered.
|
|
Assert.True(CopyRules.AsksAgain(KeptApart(20, CopyRules.Fingerprint(Partial(20, 5), 1, 1)), Identical(20)));
|
|
// Kept apart while identical: an identity is no news.
|
|
Assert.False(CopyRules.AsksAgain(KeptApart(20, CopyRules.Fingerprint(Identical(20), 1, 1)), Identical(20)));
|
|
// Only kept-apart pairs ask again.
|
|
Assert.False(CopyRules.AsksAgain(KeptApart(20) with { State = CopyState.Linked }, Partial(90, 5)));
|
|
}
|
|
|
|
[Test]
|
|
public void FingerprintsFollowTheScoreAndTheVersions()
|
|
{
|
|
var a = CopyRules.Fingerprint(Partial(5, 5), 3, 4);
|
|
Assert.Equal(a, CopyRules.Fingerprint(Partial(5, 5), 3, 4));
|
|
Assert.NotEqual(a, CopyRules.Fingerprint(Partial(5, 5), 3, 5));
|
|
Assert.NotEqual(a, CopyRules.Fingerprint(Partial(6, 5), 3, 4));
|
|
var plan = CopyRules.PlanFingerprint(["send a", "fetch b"]);
|
|
Assert.Equal(64, plan.Length);
|
|
Assert.Equal(plan, CopyRules.PlanFingerprint(["send a", "fetch b"]));
|
|
Assert.NotEqual(plan, CopyRules.PlanFingerprint(["fetch b", "send a"]));
|
|
Assert.Equal("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", CopyRules.PlanFingerprint([]));
|
|
}
|
|
|
|
[Test]
|
|
public void AProbeIsEvenlySpacedWithTheFirstAndTheLast()
|
|
{
|
|
var photos = Enumerable.Range(0, 1000).Select(i => new FramePhoto(i.ToString("x64"), i, null)).ToList();
|
|
var probe = CopyRules.Probe(photos);
|
|
Assert.Count(64, probe);
|
|
Assert.Same(photos[0], probe[0]);
|
|
Assert.Same(photos[^1], probe[^1]);
|
|
Assert.Count(64, probe.Distinct());
|
|
var gaps = probe.Zip(probe.Skip(1), (a, b) => b.Size - a.Size).ToList();
|
|
Assert.InRange(gaps.Min(), 15, 16);
|
|
Assert.InRange(gaps.Max(), 15, 16);
|
|
|
|
Assert.Count(10, CopyRules.Probe(photos.Take(10).ToList()));
|
|
Assert.SequenceEqual([photos[0], photos[999]], CopyRules.Probe(photos, 2));
|
|
Assert.Empty(CopyRules.Probe([]));
|
|
}
|
|
|
|
[Test]
|
|
public void HintsScoreRangeCameraIntervalAndTitle()
|
|
{
|
|
var start = new DateTime(2026, 9, 28, 18, 0, 0);
|
|
var a = new CopyHintsInput(start, start.AddHours(2), "EOS R5", 5, "Harbour sunset");
|
|
Assert.Equal(100, CopyRules.Hints(a, a with { Camera = "eos r5", Title = "harbour" }));
|
|
// Half of the shorter range shared: 20.
|
|
Assert.Equal(20, CopyRules.Hints(a, new CopyHintsInput(start.AddHours(1), start.AddHours(3), null, null, null)));
|
|
Assert.Equal(25, CopyRules.Hints(a, new CopyHintsInput(null, null, "EOS R5", null, null)));
|
|
Assert.Equal(20, CopyRules.Hints(a, new CopyHintsInput(null, null, null, 5.4, null)));
|
|
Assert.Equal(0, CopyRules.Hints(a, new CopyHintsInput(null, null, null, 6, "Sun")));
|
|
// A null Last reads as First: a sequence with its first capture inside the other's range overlaps fully.
|
|
Assert.Equal(40, CopyRules.Hints(a, new CopyHintsInput(start.AddMinutes(30), null, null, null, null)));
|
|
Assert.Equal(0, CopyRules.Hints(a, new CopyHintsInput(start.AddHours(3), null, null, null, null)));
|
|
Assert.Equal(40, CopyRules.Hints(new CopyHintsInput(start, null, null, null, null), new CopyHintsInput(start, null, null, null, null)));
|
|
}
|
|
}
|