using Chronos.Core.Project; using Chronos.Data.Sync; using Chronos.Protocol; using Chronos.Testing; namespace Chronos.Data.Tests.Sync; /// The photo-by-photo comparison of two copies (owner review 6, #73; design ยง4.1). [Category("sync")] public sealed class CopyComparisonTests { private static readonly DateTime Start = new(2026, 9, 28, 18, 0, 0, 250); private static string Sha(int n) => Convert.ToHexStringLower(BitConverter.GetBytes((long)n)).PadLeft(64, 'a'); /// Photo n: its own SHA-256, size and a sub-second capture time 5 s after the one before. private static CopyPhotoInput Photo(int n, int index = -1, bool hashed = true, bool unusable = false, string? name = null, DateTime? at = null, long? size = null) => new(Guid.NewGuid(), index < 0 ? n : index, name ?? $"IMG_{n:0000}.JPG", hashed ? Sha(n) : null, size ?? 1000 + n, at ?? Start.AddSeconds(5 * n), unusable); private static CopySideInput Side(params CopyPhotoInput[] photos) => new(photos, []); private static CopySideInput Side(IEnumerable photos, params RemovedPhotoDto[] removed) => new([.. photos], removed); private static RemovedPhotoDto Deleted(int n) => new(Sha(n), null, "deleted", DateTimeOffset.UnixEpoch); private static RemovedPhotoDto Replaced(int n, int by) => new(Sha(n), Sha(by), "replaced", DateTimeOffset.UnixEpoch); private static int Count(CopyComparisonResult result, PhotoClass c) => result.Of(c).Count(); [Test] public void IdenticalCopiesAreAllSame() { var result = CopyComparer.Compare(Side(Photo(1), Photo(2), Photo(3)), Side(Photo(1), Photo(2), Photo(3))); Assert.Equal(3, Count(result, PhotoClass.Same)); Assert.Equal((3, 0, 0, 1000, 1000), (result.Score.Shared, result.Score.OnlyHere, result.Score.OnlyThere, result.Score.Containment, result.Score.Overlap)); Assert.True(result.Score.Identical); Assert.True(result.Score.ByHash); Assert.Count(9, result.Totals); } [Test] public void ASubsetReadsFullyContainedAndASupersetToo() { // The Uploader sent part of the card: the server holds a subset. var all = Enumerable.Range(1, 10).Select(n => Photo(n)).ToArray(); var part = Enumerable.Range(1, 4).Select(n => Photo(n)).ToArray(); var subset = CopyComparer.Compare(Side(all), Side(part)); Assert.Equal((4, 6, 0, 1000, 400), (subset.Score.Shared, subset.Score.OnlyHere, subset.Score.OnlyThere, subset.Score.Containment, subset.Score.Overlap)); Assert.False(subset.Score.Identical); var superset = CopyComparer.Compare(Side(part), Side(all)); Assert.Equal((4, 0, 6, 1000), (superset.Score.Shared, superset.Score.OnlyHere, superset.Score.OnlyThere, superset.Score.Containment)); } [Test] public void AConvertedPhotoIsConvertedNotOnlyOnOneSide() { // This PC replaced photo 2 by a converted file (photo 50's content); the server still holds the original. var here = Side([Photo(1), Photo(50, index: 1, at: Start.AddSeconds(10))], Replaced(2, 50)); var result = CopyComparer.Compare(here, Side(Photo(1), Photo(2))); var converted = Assert.Contains(result.Photos, p => p.Class == PhotoClass.Converted); Assert.Equal((Sha(50), Sha(2)), (converted.Here!.Sha256, converted.There!.Sha256)); Assert.Equal((2, 0, 0), (result.Score.Shared, result.Score.OnlyHere, result.Score.OnlyThere)); Assert.True(result.Score.Identical); } [Test] public void ATombstonedPhotoIsDeletedOnThatSideNotOnlyOnTheOther() { var result = CopyComparer.Compare(Side([Photo(1), Photo(2)], Deleted(3)), Side([Photo(1), Photo(3)], Deleted(2))); Assert.Equal(Sha(2), Assert.Contains(result.Photos, p => p.Class == PhotoClass.DeletedThere).Key); Assert.Equal(Sha(3), Assert.Contains(result.Photos, p => p.Class == PhotoClass.DeletedHere).Key); Assert.Equal((0, 0, 1, 1, 2), (result.Score.OnlyHere, result.Score.OnlyThere, result.Score.DeletedHere, result.Score.DeletedThere, result.Score.SharedRemoved)); Assert.False(result.Score.Identical); Assert.Equal(1000, result.Score.Containment); } [Test] public void CopiesOfOnePhotoInsideASideCountOnce() { var copy = Photo(2) with { FrameId = Guid.NewGuid(), Index = 5, FileName = "IMG_0002 (1).JPG" }; var result = CopyComparer.Compare(Side(Photo(1), Photo(2), copy), Side(Photo(1), Photo(2))); Assert.Equal((2, 2, 2), (result.Score.Shared, result.Score.LocalPhotos, result.Score.OtherPhotos)); var same = Assert.Contains(result.Photos, p => p.Key == Sha(2)); Assert.Equal((2, 1), (same.HereOccurrences, same.ThereOccurrences)); Assert.True(result.Score.Identical); } [Test] public void AnUnhashedPhotoMatchesBySizeAndMillisecond() { var result = CopyComparer.Compare(Side(Photo(1), Photo(2, hashed: false)), Side(Photo(1), Photo(2))); var unhashed = Assert.Contains(result.Photos, p => p.Class == PhotoClass.SameUnhashed); Assert.Equal(Sha(2), unhashed.Key); Assert.Equal((1, 1), (result.Score.Shared, result.Score.SharedUnhashed)); Assert.True(result.Score.Identical); Assert.False(result.Score.ByHash); } [Test] public void UnhashedLeftoversStayOnTheirSide() { // Another size at the same time is another photo: unlike CompareOriginals, nothing is assumed to match. var result = CopyComparer.Compare(Side(Photo(1), Photo(2, hashed: false, size: 7)), Side(Photo(1), Photo(3, hashed: false))); Assert.Equal((1, 0, 1, 1), (result.Score.Shared, result.Score.SharedUnhashed, result.Score.OnlyHere, result.Score.OnlyThere)); Assert.True(Assert.Contains(result.Photos, p => p.Class == PhotoClass.OnlyHere).Key.StartsWith("u:7:", StringComparison.Ordinal)); } [Test] public void DiffersPairsOnePhotoPerSideWithTheSameNameAndSubSecondInstant() { // Photo 2 edited on one side: another SHA-256, same name, same sub-second capture time. var edited = Photo(2) with { Sha256 = Sha(99), Size = 4242 }; var result = CopyComparer.Compare(Side(Photo(1), Photo(2)), Side(Photo(1), edited)); var differs = Assert.Contains(result.Photos, p => p.Class == PhotoClass.Differs); Assert.Equal((Sha(2), Sha(99), 1002L), (differs.Here!.Sha256, differs.There!.Sha256, differs.Bytes)); Assert.Equal((1, 0, 0), (result.Score.Differs, result.Score.OnlyHere, result.Score.OnlyThere)); Assert.False(result.Score.Identical); var total = Assert.Contains(result.Totals, t => t.Class == PhotoClass.Differs); Assert.Equal((1002L, 4242L), (total.HereBytes, total.ThereBytes)); // Another name, or another millisecond, is no pair. Assert.Equal(0, CopyComparer.Compare(Side(Photo(2)), Side(edited with { FileName = "DSC_0002.JPG" })).Score.Differs); Assert.Equal(0, CopyComparer.Compare(Side(Photo(2)), Side(edited with { CapturedAt = edited.CapturedAt!.Value.AddMilliseconds(1) })).Score.Differs); } [Test] public void WholeSecondCaptureTimesNeverPairAsDiffers() { // A camera that records whole seconds: two different photos at one second with one name are not one photo edited. var at = new DateTime(2026, 9, 28, 18, 0, 0); var result = CopyComparer.Compare(Side(Photo(1, at: at, name: "IMG_0001.JPG")), Side(Photo(2, at: at, name: "IMG_0001.JPG"))); Assert.Equal((0, 1, 1), (result.Score.Differs, result.Score.OnlyHere, result.Score.OnlyThere)); } [Test] public void BurstsNeverPairAsDiffers() { // Two photos of a burst at one instant on each side: which is which cannot be told, so nothing pairs. var at = Start.AddMilliseconds(130); var result = CopyComparer.Compare( Side(Photo(1, at: at, name: "IMG_0001.JPG"), Photo(2, at: at, name: "IMG_0001.JPG")), Side(Photo(3, at: at, name: "IMG_0001.JPG"), Photo(4, at: at, name: "IMG_0001.JPG"))); Assert.Equal((0, 2, 2), (result.Score.Differs, result.Score.OnlyHere, result.Score.OnlyThere)); } [Test] public void TwoVersionsAfterAKeepBothAreVersions() { // A Union keep-both left both versions of photo 2 on both sides, at its one capture instant. var version = Photo(2) with { FrameId = Guid.NewGuid(), Index = 3, Sha256 = Sha(99), Size = 4242 }; var result = CopyComparer.Compare(Side(Photo(1), Photo(2), version), Side(Photo(1), Photo(2), version with { FrameId = Guid.NewGuid() })); Assert.Equal(2, Count(result, PhotoClass.Versions)); Assert.Equal((3, 2), (result.Score.Shared, result.Score.Versions)); Assert.True(result.Score.Identical); // At a whole second they stay Same. var at = new DateTime(2026, 9, 28, 18, 0, 10); var whole = CopyComparer.Compare(Side(Photo(2, at: at), version with { CapturedAt = at }), Side(Photo(2, at: at), version with { CapturedAt = at })); Assert.Equal((2, 0), (Count(whole, PhotoClass.Same), whole.Score.Versions)); } [Test] public void AnUnusableFrameHoldsNothingAndMarksTheRepair() { var result = CopyComparer.Compare(Side(Photo(1), Photo(2, unusable: true)), Side(Photo(1), Photo(2), Photo(3, unusable: true))); var brought = Assert.Contains(result.Photos, p => p.Key == Sha(2)); Assert.Equal((PhotoClass.OnlyThere, true, false), (brought.Class, brought.RepairsHere, brought.RepairsThere)); Assert.DoesNotContain(Sha(3), result.Photos.Select(p => p.Key)); Assert.Equal((1, 2), (result.Score.LocalPhotos, result.Score.OtherPhotos)); // A Missing or Corrupt frame there is repaired by this PC's copy. var other = CopyComparer.Compare(Side(Photo(1), Photo(2)), Side(Photo(1), Photo(2, unusable: true))); var sent = Assert.Contains(other.Photos, p => p.Key == Sha(2)); Assert.Equal((PhotoClass.OnlyHere, true), (sent.Class, sent.RepairsThere)); } [Test] public void BytesArePerClassAndSide() { var result = CopyComparer.Compare(Side(Photo(1), Photo(2)), Side(Photo(1), Photo(3))); Assert.Equal((1001L, 1001L), Totals(PhotoClass.Same)); Assert.Equal((1002L, 0L), Totals(PhotoClass.OnlyHere)); Assert.Equal((0L, 1003L), Totals(PhotoClass.OnlyThere)); Assert.Equal(1003L, Assert.Contains(result.Photos, p => p.Class == PhotoClass.OnlyThere).Bytes); (long, long) Totals(PhotoClass c) { var total = result.Totals.Single(t => t.Class == c); return (total.HereBytes, total.ThereBytes); } } [Test] public void TheResultDoesNotDependOnTheOrderOfTheInputs() { var edited = Photo(7) with { Sha256 = Sha(97) }; CopyPhotoInput[] here = [Photo(1), Photo(2), Photo(3, hashed: false), Photo(4), Photo(5), Photo(6), Photo(7), Photo(8, unusable: true)]; CopyPhotoInput[] there = [Photo(1), Photo(3), Photo(4, hashed: false), Photo(6), edited, Photo(8), Photo(9)]; var removedHere = new[] { Deleted(9) }; var removedThere = new[] { Deleted(5) }; var a = CopyComparer.Compare(Side(here, removedHere), Side(there, removedThere)); var b = CopyComparer.Compare(Side(Enumerable.Reverse(here), removedHere), Side(Enumerable.Reverse(there), removedThere)); Assert.Equal(a.Score, b.Score); Assert.SequenceEqual(a.Photos.Select(p => (p.Class, p.Key, p.Here?.FrameId, p.There?.FrameId)), b.Photos.Select(p => (p.Class, p.Key, p.Here?.FrameId, p.There?.FrameId))); Assert.Equal((2, 2, 1, 1, 1, 1, 1), (a.Score.Shared, a.Score.SharedUnhashed, a.Score.Differs, a.Score.DeletedHere, a.Score.DeletedThere, a.Score.OnlyHere, a.Score.OnlyThere)); } [Test] public void DeletionsCountWhatTheOtherSideTombstonedAndNoLongerHolds() { var tombstones = SyncTombstones.FromDtos([Deleted(3)], [Deleted(2), Deleted(1)]); FramePhoto[] here = [new(Sha(1), 1, null), new(Sha(2), 2, null), new(Sha(2), 2, null)]; FramePhoto[] there = [new(Sha(1), 1, null), new(Sha(3), 3, null)]; Assert.Equal((1, 1), CopyComparer.Deletions(here, there, tombstones)); Assert.Equal((0, 0), CopyComparer.Deletions(here, there, SyncTombstones.None)); } [Test] public void TombstonesFromDtosReadBothSides() { var tombstones = SyncTombstones.FromDtos([Replaced(2, 50), Deleted(4)], [Deleted(5)]); Assert.Equal(tombstones.Canonical(Sha(50)), tombstones.Canonical(Sha(2))); Assert.True(tombstones.DeletedLocally(Sha(4))); Assert.False(tombstones.DeletedLocally(Sha(2))); Assert.True(tombstones.DeletedRemotely(Sha(5))); Assert.True(SyncTombstones.FromDtos([], null).IsEmpty); } }