diff --git a/src/Chronos.Data/Copies/CopyPayload.cs b/src/Chronos.Data/Copies/CopyPayload.cs new file mode 100644 index 00000000..9bcf7f28 --- /dev/null +++ b/src/Chronos.Data/Copies/CopyPayload.cs @@ -0,0 +1,236 @@ +using System.Globalization; +using System.Text.Json; +using System.Text.Json.Nodes; +using Chronos.Core.Project; +using Chronos.Data.Sync; + +namespace Chronos.Data.Copies; + +/// +/// The details of two copies line by line (owner review 6, Q4; design §4.3): title, tags, rating, notes and settings, the +/// settings compared in their portable form (frame positions by photo), so the same choice on the same photo at another +/// position is no difference; and the one payload both sides get once every line is chosen. Pure. +/// +public static class CopyPayload +{ + /// The one line every difference of the frame positions (exclusions, hero, keyframes by photo) collapses into. + public const string PendingFramesPath = "pendingFrames"; + + private const string Title = SyncRules.MetadataPath + "title"; + private const string Tags = SyncRules.MetadataPath + "tags"; + private const string Rating = SyncRules.MetadataPath + "rating"; + private const string Notes = SyncRules.MetadataPath + "notes"; + + /// + /// The payload with its settings in portable form ( on + /// , the frames its positions name). Settings that are not a project are kept normalised as they are. + /// + public static SyncPayload Portable(SyncPayload payload, FramePhotos photos) + { + ArgumentNullException.ThrowIfNull(payload); + ArgumentNullException.ThrowIfNull(photos); + var settings = SyncRules.NormalizeSettings(payload.SettingsJson); + try + { + return payload with { SettingsJson = ProjectContent.ToPortable(settings, photos) }; + } + catch (JsonException) + { + return payload with { SettingsJson = settings }; + } + } + + /// + /// Every difference of title, tags, rating, notes and settings, settings compared in their portable form (positions by + /// photo, each side's settings made portable with its own photos), so the same exclusion at index 10 here and 12 there is + /// no line. Lines under "pendingFrames" collapse into one line "pendingFrames" (Frame positions). + /// + public static IReadOnlyList Lines(SyncPayload here, FramePhotos herePhotos, SyncPayload there, FramePhotos therePhotos) + { + ArgumentNullException.ThrowIfNull(here); + ArgumentNullException.ThrowIfNull(there); + var mine = Portable(here, herePhotos); + var theirs = Portable(there, therePhotos); + return Collapse(SyncRules.Differences(mine, theirs, int.MaxValue), mine, theirs); + } + + /// + /// The one payload both sides get, in portable form: here's values, with there's at every path chosen + /// . Every line must be chosen. Each side places it on its own frames at write time + /// (, under its lease). + /// + /// A line has no choice. + /// The merged settings are not a project this Chronos reads: nothing is to be applied. + public static SyncPayload Merge(SyncPayload herePortable, SyncPayload therePortable, IReadOnlyDictionary choices, + IReadOnlyList lines) + { + ArgumentNullException.ThrowIfNull(herePortable); + ArgumentNullException.ThrowIfNull(therePortable); + ArgumentNullException.ThrowIfNull(choices); + ArgumentNullException.ThrowIfNull(lines); + if (lines.FirstOrDefault(l => !choices.ContainsKey(l.Path)) is { } unchosen) + throw new ArgumentException($"The detail '{unchosen.Path}' has no side chosen.", nameof(choices)); + + var (title, tags, rating, notes) = (herePortable.Title, herePortable.Tags, herePortable.Rating, herePortable.Notes); + var settings = Parse(SyncRules.NormalizeSettings(herePortable.SettingsJson)); + var other = Parse(SyncRules.NormalizeSettings(therePortable.SettingsJson)); + foreach (var line in lines.Where(l => choices[l.Path] == CopySide.Other).DistinctBy(l => l.Path, StringComparer.Ordinal)) + { + switch (line.Path) + { + case Title: + title = therePortable.Title; + break; + case Tags: + tags = therePortable.Tags; + break; + case Rating: + rating = therePortable.Rating; + break; + case Notes: + notes = therePortable.Notes; + break; + case "settings": + settings = other?.DeepClone(); + break; + default: + settings = Set(settings, other, line.Path); + break; + } + } + + var json = settings?.ToJsonString() ?? string.Empty; + string merged; + try + { + // Read as a project (never a newer Chronos's: its values may mean something else), then written canonically. + _ = ProjectSettings.FromJson(json); + merged = ProjectContent.Canonical(json); + } + catch (JsonException ex) + { + throw new InvalidDataException("The merged settings are not a project.", ex); + } + return new SyncPayload(title, tags, rating, notes, merged); + } + + /// + /// Adds the excluded flag, by photo, for the incoming versions of a Union keep-both (design §5e): each key (a canonical + /// SHA-256, or "u:<size>:<capture ms>") becomes an excluded pending frame, or marks the one already naming it. + /// + /// The settings are of a newer Chronos. + /// The settings are not a project. + public static SyncPayload ExcludeVersions(SyncPayload portable, IReadOnlyCollection photoKeys) + { + ArgumentNullException.ThrowIfNull(portable); + ArgumentNullException.ThrowIfNull(photoKeys); + if (photoKeys.Count == 0) return portable; + var settings = ProjectSettings.FromJson(SyncRules.NormalizeSettings(portable.SettingsJson)); + var pending = (settings.PendingFrames ?? []).ToList(); + foreach (var key in photoKeys.Distinct(StringComparer.Ordinal)) + { + if (Photo(key) is not { } photo) continue; + var at = pending.FindIndex(p => p.Occurrence == 0 && Names(p, photo)); + if (at >= 0) pending[at] = pending[at] with { Excluded = true }; + else pending.Add(new PendingFrame { Sha256 = photo.Sha256, Size = photo.Size, CapturedAt = photo.CapturedAt, Excluded = true }); + } + return portable with { SettingsJson = ProjectContent.Canonical((settings with { PendingFrames = pending }).ToJson()) }; + } + + /// Paths whose value moved between two payloads of one side (the apply-time check of design §5c step 5). + public static IReadOnlySet Moved(SyncPayload before, SyncPayload after) + { + ArgumentNullException.ThrowIfNull(before); + ArgumentNullException.ThrowIfNull(after); + return Collapse(SyncRules.Differences(before, after, int.MaxValue), before, after).Select(l => l.Path).ToHashSet(StringComparer.Ordinal); + } + + private static List Collapse(IReadOnlyList differences, SyncPayload here, SyncPayload there) + { + var lines = new List(differences.Count); + var pending = false; + foreach (var difference in differences) + { + if (difference.Path == PendingFramesPath || difference.Path.StartsWith(PendingFramesPath + ".", StringComparison.Ordinal)) + { + if (pending) continue; + pending = true; + lines.Add(new DetailLine(PendingFramesPath, PendingOf(here), PendingOf(there))); + continue; + } + lines.Add(new DetailLine(difference.Path, difference.Mine, difference.Theirs)); + } + return lines; + + static string? PendingOf(SyncPayload payload) => + Parse(SyncRules.NormalizeSettings(payload.SettingsJson)) is JsonObject project && project[PendingFramesPath] is { } node ? node.ToJsonString() : null; + } + + private static JsonNode? Parse(string json) + { + try + { + return JsonNode.Parse(json); + } + catch (JsonException) + { + return JsonValue.Create(json); + } + } + + /// Sets of to the value it has in ; removes it where the source has none. + private static JsonNode? Set(JsonNode? target, JsonNode? source, string path) + { + var names = path.Split('.'); + var value = source; + var present = true; + foreach (var name in names) + { + if (value is JsonObject o && o.TryGetPropertyValue(name, out var child)) + { + value = child; + continue; + } + present = false; + break; + } + if (target is not JsonObject root) + { + if (!present) return target; + root = new JsonObject(); + target = root; + } + var parent = root; + for (var i = 0; i < names.Length - 1; i++) + { + if (parent[names[i]] is JsonObject next) + { + parent = next; + continue; + } + if (!present) return target; + var created = new JsonObject(); + parent[names[i]] = created; + parent = created; + } + if (present) parent[names[^1]] = value?.DeepClone(); + else parent.Remove(names[^1]); + return target; + } + + /// The photo a key names (); null for an unhashed photo without a capture time. + private static FramePhoto? Photo(string key) + { + if (!key.StartsWith(CopyComparer.UnhashedPrefix, StringComparison.Ordinal)) return key.Length == 0 ? null : new FramePhoto(key.ToLowerInvariant(), 0, null); + var parts = key[CopyComparer.UnhashedPrefix.Length..].Split(':'); + return parts.Length == 2 && long.TryParse(parts[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out var size) + && long.TryParse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out var ms) + ? new FramePhoto(null, size, new DateTime(ms * TimeSpan.TicksPerMillisecond, DateTimeKind.Unspecified)) + : null; + } + + private static bool Names(PendingFrame entry, FramePhoto photo) => photo.IsHashed + ? string.Equals(entry.Sha256, photo.Sha256, StringComparison.OrdinalIgnoreCase) + : string.IsNullOrEmpty(entry.Sha256) && entry.Size == photo.Size && entry.CapturedAt is { } at + && CopyComparer.Millisecond(at) == CopyComparer.Millisecond(photo.CapturedAt!.Value); +} diff --git a/src/Chronos.Data/Copies/CopyRules.cs b/src/Chronos.Data/Copies/CopyRules.cs new file mode 100644 index 00000000..9de364bb --- /dev/null +++ b/src/Chronos.Data/Copies/CopyRules.cs @@ -0,0 +1,186 @@ +using System.Globalization; +using System.Security.Cryptography; +using System.Text; +using Chronos.Core.Project; + +namespace Chronos.Data.Copies; + +/// +/// The decisions of detection that need no I/O (owner review 6, #73; design §3.1, §3.2): whether two copies are a pair, what +/// the Link matches setting does with one, 1.1's same-originals gate, when a kept-apart pair asks again, fingerprints, hints +/// and the probe sample. Pure and thread-safe. +/// +public static class CopyRules +{ + /// A pair at all needs this much of the smaller side shared (per mille), besides at least one photo. + public const int MinContainment = 100; + + /// The photos a probe samples of a sequence by default. + public const int ProbeSize = 64; + + private const string FingerprintVersion = "c1"; + + /// A pair at all: a link, a session key, or (Shared + SharedUnhashed + SharedRemoved) ≥ 1 and Containment ≥ 100. + public static bool IsCopy(CopyEvidence evidence, CopyScore score) + { + ArgumentNullException.ThrowIfNull(score); + if (evidence is CopyEvidence.Link or CopyEvidence.SessionKey) return true; + return score.Shared + score.SharedUnhashed + score.SharedRemoved >= 1 && score.Containment >= MinContainment; + } + + /// + /// What detection does with a pair that has no answer (state Candidate or new), by the setting (design §3.2): a local↔local + /// pair is never linked (it would need a merge); WhenIdentical links exactly the same photos known by SHA-256 on both sides; + /// WhenHalfShared links by 1.1's rule (). Everything else that is a pair asks. + /// + /// The pair's evidence (a back-reference makes a pair without shared photos); Hashes by default. + public static CopyDecision Decide(CopyLinking linking, CopyKind kind, CopyScore score, SameOriginalsInput gate, CopyEvidence evidence = CopyEvidence.Hashes) + { + ArgumentNullException.ThrowIfNull(score); + ArgumentNullException.ThrowIfNull(gate); + if (kind == CopyKind.LocalServer) + { + var link = linking switch + { + CopyLinking.WhenIdentical => score.Identical && score.ByHash, + CopyLinking.WhenHalfShared => SameOriginals(gate), + _ => false, + }; + if (link) return CopyDecision.Link; + } + return IsCopy(evidence, score) ? CopyDecision.Ask : CopyDecision.None; + } + + /// + /// 1.1's SyncService.SameOriginalsAsync rule with the gates of MatchByContentAsync, ported exactly (owner Q1 + /// "today's rule"): both unlinked, the same first-capture second, camera models equal ignoring case, at least one local + /// hash, and at least half of the smaller side's originals shared by canonical SHA-256. + /// + public static bool SameOriginals(SameOriginalsInput gate) + { + ArgumentNullException.ThrowIfNull(gate); + if (!gate.BothUnlinked || gate.LocalFirst is not { } local || gate.RemoteFirst is not { } remote) return false; + if (Second(local) != Second(remote)) return false; + if (!string.Equals(gate.LocalCamera, gate.RemoteCamera, StringComparison.OrdinalIgnoreCase)) return false; + if (gate.LocalHashed <= 0) return false; + return gate.SharedCanonical > 0 && 2L * gate.SharedCanonical >= Math.Min(gate.LocalHashed, gate.RemoteFrames); + } + + /// + /// A kept-apart pair asks again (design §3.6): it became Identical (it was not when the user answered), or Shared grew by + /// at least max(10, 25 % of the shared count the user answered). + /// + public static bool AsksAgain(FoundCopy answered, CopyScore now) + { + ArgumentNullException.ThrowIfNull(answered); + ArgumentNullException.ThrowIfNull(now); + if (answered.State != CopyState.KeptApart) return false; + var wasIdentical = WasIdentical(answered.AnsweredFingerprint) ?? answered.Score.Identical; + if (now.Identical && !wasIdentical) return true; + var before = answered.AnsweredShared ?? answered.Score.Shared; + var grown = now.Shared - before; + return grown >= 10 && 4L * grown >= before; + } + + /// + /// What a comparison found, as text: the counts and both sides' frames versions. Equal fingerprints mean nothing moved + /// since (detection skips the comparison; a kept-apart answer stands). + /// + public static string Fingerprint(CopyScore score, int localFramesVersion, int otherFramesVersion) + { + ArgumentNullException.ThrowIfNull(score); + return string.Join(':', FingerprintVersion, + Text(score.Shared), Text(score.SharedUnhashed), Text(score.SharedRemoved), Text(score.OnlyHere), Text(score.OnlyThere), Text(score.Differs), + Text(score.DeletedHere), Text(score.DeletedThere), Text(score.Versions), Text(score.LocalPhotos), Text(score.OtherPhotos), + Text(localFramesVersion), Text(otherFramesVersion)); + + static string Text(int value) => value.ToString(CultureInfo.InvariantCulture); + } + + /// The SHA-256 (lower-case hex) of a plan's lines joined by '\n': equal plans have equal fingerprints. + public static string PlanFingerprint(IEnumerable lines) + { + ArgumentNullException.ThrowIfNull(lines); + return Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(string.Join('\n', lines)))); + } + + /// + /// How alike two sequences look, 0–100 (design §3.1): 40 × the capture-range overlap fraction of the shorter range, 25 when + /// the camera models are equal ignoring case, 20 when the median intervals are within 10 %, 15 when the titles share a word + /// of at least 4 letters (ignoring case). A null Last is read as First (a sequence without a last capture yet). + /// + public static int Hints(CopyHintsInput a, CopyHintsInput b) + { + ArgumentNullException.ThrowIfNull(a); + ArgumentNullException.ThrowIfNull(b); + var score = (int)Math.Round(40 * Overlap(a, b), MidpointRounding.AwayFromZero); + if (!string.IsNullOrWhiteSpace(a.Camera) && string.Equals(a.Camera.Trim(), b.Camera?.Trim(), StringComparison.OrdinalIgnoreCase)) score += 25; + if (a.MedianInterval is > 0 and var x && b.MedianInterval is > 0 and var y && Math.Abs(x - y) <= 0.1 * Math.Max(x, y)) score += 20; + if (Words(a.Title).Overlaps(Words(b.Title))) score += 15; + return Math.Clamp(score, 0, 100); + } + + /// Up to photo keys, evenly spaced over the sequence, the first and the last included. + public static IReadOnlyList Probe(IReadOnlyList photos, int max = ProbeSize) + { + ArgumentNullException.ThrowIfNull(photos); + if (max <= 0 || photos.Count == 0) return []; + if (photos.Count <= max) return [.. photos]; + if (max == 1) return [photos[0]]; + var sample = new FramePhoto[max]; + var last = photos.Count - 1; + for (var i = 0; i < max; i++) sample[i] = photos[(int)((long)i * last / (max - 1))]; + return sample; + } + + private static DateTime Second(DateTime time) => new(time.Ticks - time.Ticks % TimeSpan.TicksPerSecond, DateTimeKind.Unspecified); + + /// Whether a fingerprint () records an Identical score; null for one in another form. + private static bool? WasIdentical(string? fingerprint) + { + if (fingerprint is null) return null; + var parts = fingerprint.Split(':'); + if (parts.Length != 14 || parts[0] != FingerprintVersion) return null; + var counts = new int[parts.Length - 1]; + for (var i = 1; i < parts.Length; i++) + { + if (!int.TryParse(parts[i], NumberStyles.Integer, CultureInfo.InvariantCulture, out counts[i - 1])) return null; + } + // shared, shared unhashed, removed, only here, only there, differs, deleted here, deleted there, versions, … + return counts[3] == 0 && counts[4] == 0 && counts[5] == 0 && counts[6] == 0 && counts[7] == 0 && counts[0] + counts[1] > 0; + } + + /// The fraction of the shorter capture range the two ranges share (a single instant within the other's range: 1). + private static double Overlap(CopyHintsInput a, CopyHintsInput b) + { + if (a.First is not { } aFirst || b.First is not { } bFirst) return 0; + var (aFrom, aTo) = Range(aFirst, a.Last); + var (bFrom, bTo) = Range(bFirst, b.Last); + var from = aFrom > bFrom ? aFrom : bFrom; + var to = aTo < bTo ? aTo : bTo; + if (to < from) return 0; + var shorter = Math.Min((aTo - aFrom).Ticks, (bTo - bFrom).Ticks); + return shorter <= 0 ? 1 : Math.Min(1, (double)(to - from).Ticks / shorter); + + static (DateTime From, DateTime To) Range(DateTime first, DateTime? last) => last is { } l && l >= first ? (first, l) : (first, first); + } + + /// The words of at least 4 letters of a title, lower-case. + private static HashSet Words(string? title) + { + var words = new HashSet(StringComparer.Ordinal); + if (string.IsNullOrWhiteSpace(title)) return words; + var word = new StringBuilder(); + foreach (var c in title + " ") + { + if (char.IsLetterOrDigit(c)) + { + word.Append(char.ToLowerInvariant(c)); + continue; + } + if (word.ToString() is var w && w.Count(char.IsLetter) >= 4) words.Add(w); + word.Clear(); + } + return words; + } +} diff --git a/src/Chronos.Data/Copies/CopySides.cs b/src/Chronos.Data/Copies/CopySides.cs new file mode 100644 index 00000000..b5be7a04 --- /dev/null +++ b/src/Chronos.Data/Copies/CopySides.cs @@ -0,0 +1,74 @@ +using Chronos.Core.Model; +using Chronos.Data.Repositories; +using Chronos.Data.Sync; +using Chronos.Protocol; + +namespace Chronos.Data.Copies; + +/// +/// The two sides of a comparison as reads them (owner review 6, #73; design §4.2): a local +/// sequence's frames and tombstones, a server sequence's manifest with its frames' capture times, and the last-copy check +/// the planners make before anything goes to a trash. +/// +public sealed class CopySides(ChronosDatabase db) +{ + /// Frames whose file holds nothing usable: the other side's copy repairs them. + public const FrameFlags UnusableFlags = FrameFlags.Missing | FrameFlags.Corrupt; + + /// Parameters per statement of . + private const int Batch = 500; + + private readonly ChronosDatabase _db = db ?? throw new ArgumentNullException(nameof(db)); + private readonly DbAccess _access = new(db.Database); + + /// A local sequence: every frame row in index order and its tombstones (). + public async Task LocalAsync(Guid id, CancellationToken ct) + { + var frames = await Processing.SequenceFrames.ListAsync(_db, id, ct).ConfigureAwait(false); + var removals = await _db.FrameRemovals.ListBySequenceAsync(id, ct).ConfigureAwait(false); + return new CopySideInput( + [.. frames.Select(f => new CopyPhotoInput(f.Id, f.Index, f.FileName, f.Sha256, f.Size, f.CapturedAt, (f.Flags & UnusableFlags) != 0))], + SyncTombstones.ToDto(removals)); + } + + /// + /// A server sequence: its manifest (content, flags, tombstones) with the capture times of its frame rows (the manifest + /// has none). A frame the listing no longer has keeps no capture time. + /// + public async Task RemoteAsync(ISyncRemote remote, Guid id, CancellationToken ct) + { + ArgumentNullException.ThrowIfNull(remote); + var manifest = await remote.GetManifestAsync(id, ct).ConfigureAwait(false); + var rows = await remote.ListFramesAsync(id, ct).ConfigureAwait(false); + var captured = new Dictionary(rows.Count); + foreach (var row in rows) captured[row.Id] = row.CapturedAt; + return new CopySideInput( + [.. manifest.Frames.Select(f => new CopyPhotoInput(f.Id, f.Index, f.FileName, f.Sha256.Length == 0 ? null : f.Sha256, f.Size, + captured.GetValueOrDefault(f.Id), (f.Flags & (long)UnusableFlags) != 0))], + manifest.Removed ?? []); + } + + /// + /// The raw SHA-256 (lower-case) of that a usable frame (not Missing or Corrupt) of any sequence + /// of this index other than holds: what is not the last copy of its photo. Batched. + /// + public async Task> HeldElsewhereAsync(IReadOnlyCollection sha256, IReadOnlyCollection except, CancellationToken ct) + { + ArgumentNullException.ThrowIfNull(sha256); + ArgumentNullException.ThrowIfNull(except); + var held = new HashSet(StringComparer.Ordinal); + var wanted = sha256.Where(s => !string.IsNullOrEmpty(s)).Select(s => s.ToLowerInvariant()).Distinct(StringComparer.Ordinal).ToList(); + var excluded = except.Distinct().ToList(); + foreach (var chunk in wanted.Chunk(Math.Max(1, Batch - excluded.Count))) + { + var p = new SqlParameters(); + var not = excluded.Count == 0 ? string.Empty : $" AND sequence_id NOT IN ({string.Join(", ", excluded.Select(id => p.Add(id)))})"; + var hashes = string.Join(", ", chunk.Select(sha => p.Add(sha))); + var rows = await _access.ListAsync( + $"SELECT DISTINCT sha256 FROM frames WHERE sha256 IN ({hashes}) AND (flags & {(int)UnusableFlags}) = 0{not}", + p.ToArray(), r => r.GetString(0), ct).ConfigureAwait(false); + foreach (var sha in rows) held.Add(sha.ToLowerInvariant()); + } + return held; + } +} diff --git a/src/Chronos.Data/Sync/CopyComparison.cs b/src/Chronos.Data/Sync/CopyComparison.cs new file mode 100644 index 00000000..f6630fbc --- /dev/null +++ b/src/Chronos.Data/Sync/CopyComparison.cs @@ -0,0 +1,280 @@ +using System.Globalization; +using Chronos.Core.Project; +using Chronos.Data.Copies; + +namespace Chronos.Data.Sync; + +/// +/// The photo-by-photo comparison of two copies of one time-lapse (owner review 6, #73; design §4.1): which photos both hold, +/// which only one holds, which one side deleted, which differ and which a Union kept in two versions. Detection, the Compare +/// dialog, Integrate and Merge all read this one comparison. Pure: no I/O, thread-safe, O(n log n). +/// +public static class CopyComparer +{ + /// The unhashed key prefix: "u:<size>:<capture millisecond>". + internal const string UnhashedPrefix = "u:"; + + /// + /// Compares (this PC, or the kept sequence of a merge) with (the server, + /// or the other local sequence). Each photo lands in exactly one class; unusable frames (Missing or Corrupt) hold nothing + /// and mark the photo the other side brings as a repair. + /// + public static CopyComparisonResult Compare(CopySideInput here, CopySideInput there) + { + ArgumentNullException.ThrowIfNull(here); + ArgumentNullException.ThrowIfNull(there); + // 1. Tombstones of both sides; unusable frames left out of their side's holdings. + var tombstones = SyncTombstones.FromDtos(here.Removed ?? [], there.Removed); + var mine = Holdings(here.Photos, tombstones); + var theirs = Holdings(there.Photos, tombstones); + var brokenHere = Broken(here.Photos, tombstones); + var brokenThere = Broken(there.Photos, tombstones); + var theirsByKey = theirs.ToDictionary(h => h.Key, StringComparer.Ordinal); + var done = new HashSet(ReferenceEqualityComparer.Instance); + var photos = new List(mine.Count + theirs.Count); + + // 3. Same and Converted: one canonical key on both sides. + foreach (var h in mine) + { + if (!h.Hashed || !theirsByKey.TryGetValue(h.Key, out var t)) continue; + var (hereFrame, thereFrame, same) = Pick(h, t); + photos.Add(new ComparedPhoto(same ? PhotoClass.Same : PhotoClass.Converted, h.Key, hereFrame, thereFrame, h.Occurrences, t.Occurrences, hereFrame.Size)); + done.Add(h); + done.Add(t); + } + + // 4. SameUnhashed: one side not hashed, the same size and capture millisecond (FramePhoto.Matches), one to one. + var bySizeAndTime = new Dictionary<(long Size, long Ms), List>(); + foreach (var t in theirs) + { + if (done.Contains(t) || t.Ms is not { } ms) continue; + if (!bySizeAndTime.TryGetValue((t.First.Size, ms), out var list)) bySizeAndTime[(t.First.Size, ms)] = list = []; + list.Add(t); + } + foreach (var h in mine) + { + if (done.Contains(h) || h.Ms is not { } ms || !bySizeAndTime.TryGetValue((h.First.Size, ms), out var candidates)) continue; + var t = candidates.FirstOrDefault(c => !done.Contains(c) && (!h.Hashed || !c.Hashed)); + if (t is null) continue; + var key = h.Hashed ? h.Key : t.Hashed ? t.Key : h.Key; + photos.Add(new ComparedPhoto(PhotoClass.SameUnhashed, key, h.First, t.First, h.Occurrences, t.Occurrences, h.First.Size)); + done.Add(h); + done.Add(t); + } + + // 5. Deleted there / deleted here: held by one side, tombstoned by the other, which does not hold it. + foreach (var h in mine.Where(h => !done.Contains(h) && h.Hashed && tombstones.DeletedRemotely(h.Key))) + { + photos.Add(new ComparedPhoto(PhotoClass.DeletedThere, h.Key, h.First, null, h.Occurrences, 0, h.First.Size)); + done.Add(h); + } + foreach (var t in theirs.Where(t => !done.Contains(t) && t.Hashed && tombstones.DeletedLocally(t.Key))) + { + photos.Add(Brought(PhotoClass.DeletedHere, t, brokenHere)); + done.Add(t); + } + + // 6. Differs, strictly: one hashed photo per side at one sub-second instant, with the same file name. + var hereAt = HashedAt(mine.Where(h => !done.Contains(h))); + var thereAt = HashedAt(theirs.Where(t => !done.Contains(t))); + foreach (var (ms, onHere) in hereAt) + { + if (onHere.Count != 1 || !SubSecond(ms) || !thereAt.TryGetValue(ms, out var onThere) || onThere.Count != 1) continue; + var (h, t) = (onHere[0], onThere[0]); + if (!string.Equals(h.First.FileName, t.First.FileName, StringComparison.OrdinalIgnoreCase)) continue; + photos.Add(new ComparedPhoto(PhotoClass.Differs, h.Key, h.First, t.First, h.Occurrences, t.Occurrences, h.First.Size)); + done.Add(h); + done.Add(t); + } + + // 7. Versions: a sub-second instant holding exactly the same two shared photos on both sides (a Union keep-both). + MarkVersions(photos, mine, theirs); + + // 8. The rest is on one side only (unhashed leftovers are not assumed to match). + foreach (var h in mine.Where(h => !done.Contains(h))) + photos.Add(new ComparedPhoto(PhotoClass.OnlyHere, h.Key, h.First, null, h.Occurrences, 0, h.First.Size, RepairsThere: Repairs(h, brokenThere))); + foreach (var t in theirs.Where(t => !done.Contains(t))) photos.Add(Brought(PhotoClass.OnlyThere, t, brokenHere)); + + photos.Sort(Order); + return new CopyComparisonResult(photos, Totals(photos), Score(photos, mine.Count, theirs.Count)); + } + + /// + /// For linked pairs at each reconcile: only the deletion counts, cheaply. Deleted there: distinct photos this side holds + /// that the other side tombstoned and does not hold; deleted here the other way round (as + /// leaves them out). + /// + public static (int DeletedHere, int DeletedThere) Deletions(IReadOnlyCollection here, IReadOnlyCollection there, SyncTombstones tombstones) + { + ArgumentNullException.ThrowIfNull(here); + ArgumentNullException.ThrowIfNull(there); + ArgumentNullException.ThrowIfNull(tombstones); + if (tombstones.IsEmpty) return (0, 0); + var mine = here.Select(tombstones.Canonical).ToList(); + var theirs = there.Select(tombstones.Canonical).ToList(); + var minePhotos = new FramePhotos(mine.Select((p, i) => (i, p))); + var theirPhotos = new FramePhotos(theirs.Select((p, i) => (i, p))); + return (Count(theirs, tombstones.DeletedLocally, minePhotos), Count(mine, tombstones.DeletedRemotely, theirPhotos)); + + static int Count(List side, Func deleted, FramePhotos other) => + side.Where(p => p.IsHashed && deleted(p.Sha256) && other.IndexOf(p) is null).Select(p => p.Sha256!.ToLowerInvariant()).Distinct(StringComparer.Ordinal).Count(); + } + + /// The key of an unhashed photo by its size and capture time; a photo without a time is keyed by its frame (it matches nothing). + internal static string UnhashedKey(long size, DateTime? capturedAt, Guid frameId) => capturedAt is { } at + ? string.Create(CultureInfo.InvariantCulture, $"{UnhashedPrefix}{size}:{Millisecond(at)}") + : string.Create(CultureInfo.InvariantCulture, $"{UnhashedPrefix}{size}:{frameId:N}"); + + internal static long Millisecond(DateTime time) => time.Ticks / TimeSpan.TicksPerMillisecond; + + /// A capture millisecond with a sub-second part: the camera records real sub-second times. + private static bool SubSecond(long ms) => ms % 1000 != 0; + + /// One photo of one side: its key and the frames holding it, in index order (the first stands for it). + private sealed class Holding(string key, bool hashed, List frames) + { + public string Key { get; } = key; + + public bool Hashed { get; } = hashed; + + public List Frames { get; } = frames; + + public CopyPhotoInput First => Frames[0]; + + public int Occurrences => Frames.Count; + + public long? Ms => First.CapturedAt is { } at ? Millisecond(at) : null; + } + + /// 2. A side's usable frames grouped by photo key (copies of one photo inside a side count once), in index order. + private static List Holdings(IReadOnlyList photos, SyncTombstones tombstones) + { + ArgumentNullException.ThrowIfNull(photos); + var result = new List(); + var byKey = new Dictionary(StringComparer.Ordinal); + foreach (var frame in photos.Where(p => p is { Unusable: false }).OrderBy(p => p.Index).ThenBy(p => p.FrameId)) + { + var hashed = !string.IsNullOrEmpty(frame.Sha256); + var key = Key(frame, tombstones); + if (byKey.TryGetValue(key, out var holding)) + { + holding.Frames.Add(frame); + continue; + } + byKey[key] = holding = new Holding(key, hashed, [frame]); + result.Add(holding); + } + return result; + } + + private static string Key(CopyPhotoInput frame, SyncTombstones tombstones) => string.IsNullOrEmpty(frame.Sha256) + ? UnhashedKey(frame.Size, frame.CapturedAt, frame.FrameId) + : tombstones.Canonical(frame.Sha256)!; + + /// The keys of a side's unusable frames: canonical SHA-256 and size and capture time, so a photo either way marks a repair. + private static HashSet Broken(IReadOnlyList photos, SyncTombstones tombstones) + { + var broken = new HashSet(StringComparer.Ordinal); + foreach (var frame in photos.Where(p => p is { Unusable: true })) + { + broken.Add(Key(frame, tombstones)); + if (frame.CapturedAt is not null) broken.Add(UnhashedKey(frame.Size, frame.CapturedAt, frame.FrameId)); + } + return broken; + } + + private static bool Repairs(Holding brought, HashSet broken) => + broken.Count > 0 && (broken.Contains(brought.Key) || (brought.First.CapturedAt is not null + && broken.Contains(UnhashedKey(brought.First.Size, brought.First.CapturedAt, brought.First.FrameId)))); + + /// A photo only the other side holds (or holds and this side deleted). + private static ComparedPhoto Brought(PhotoClass c, Holding t, HashSet brokenHere) => + new(c, t.Key, null, t.First, 0, t.Occurrences, t.First.Size, RepairsHere: Repairs(t, brokenHere)); + + /// The frames standing for a photo both hold: one with the same raw SHA-256 on each side when there is one (Same), else the first (Converted). + private static (CopyPhotoInput Here, CopyPhotoInput There, bool Same) Pick(Holding h, Holding t) + { + foreach (var mine in h.Frames) + { + var theirs = t.Frames.FirstOrDefault(f => string.Equals(f.Sha256, mine.Sha256, StringComparison.OrdinalIgnoreCase)); + if (theirs is not null) return (mine, theirs, true); + } + return (h.First, t.First, false); + } + + /// The hashed photos left at each capture millisecond. + private static SortedDictionary> HashedAt(IEnumerable holdings) + { + var at = new SortedDictionary>(); + foreach (var h in holdings) + { + if (!h.Hashed || h.Ms is not { } ms) continue; + if (!at.TryGetValue(ms, out var list)) at[ms] = list = []; + list.Add(h); + } + return at; + } + + private static void MarkVersions(List photos, List mine, List theirs) + { + var hereAt = KeysAt(mine); + var thereAt = KeysAt(theirs); + var shared = photos.Where(p => p.Class is PhotoClass.Same or PhotoClass.Converted).Select(p => p.Key).ToHashSet(StringComparer.Ordinal); + for (var i = 0; i < photos.Count; i++) + { + var p = photos[i]; + if (p.Class is not (PhotoClass.Same or PhotoClass.Converted) || p.Here!.CapturedAt is not { } hereTime || p.There!.CapturedAt is not { } thereTime) continue; + var ms = Millisecond(hereTime); + if (ms != Millisecond(thereTime) || !SubSecond(ms)) continue; + if (!hereAt.TryGetValue(ms, out var onHere) || !thereAt.TryGetValue(ms, out var onThere)) continue; + if (onHere.Count == 2 && onThere.Count == 2 && onHere.SetEquals(onThere) && onHere.All(shared.Contains)) photos[i] = p with { Class = PhotoClass.Versions }; + } + + static Dictionary> KeysAt(List side) + { + var at = new Dictionary>(); + foreach (var h in side) + { + if (h.Ms is not { } ms) continue; + if (!at.TryGetValue(ms, out var keys)) at[ms] = keys = new HashSet(StringComparer.Ordinal); + keys.Add(h.Key); + } + return at; + } + } + + /// Capture order, then key and class: the same for any order of the inputs. + private static int Order(ComparedPhoto a, ComparedPhoto b) + { + var byTime = ((a.Here ?? a.There)!.CapturedAt ?? DateTime.MaxValue).CompareTo((b.Here ?? b.There)!.CapturedAt ?? DateTime.MaxValue); + if (byTime != 0) return byTime; + var byKey = string.CompareOrdinal(a.Key, b.Key); + return byKey != 0 ? byKey : a.Class.CompareTo(b.Class); + } + + private static PhotoClassTotal[] Totals(List photos) => + [ + .. Enum.GetValues().Select(c => + { + var of = photos.Where(p => p.Class == c).ToList(); + return new PhotoClassTotal(c, of.Count, of.Sum(p => p.Here?.Size ?? 0), of.Sum(p => p.There?.Size ?? 0)); + }), + ]; + + /// 10. The counts and ratios of design §3.2 (Versions counted in Shared). + private static CopyScore Score(List photos, int localPhotos, int otherPhotos) + { + int Of(PhotoClass c) => photos.Count(p => p.Class == c); + var versions = Of(PhotoClass.Versions); + var shared = Of(PhotoClass.Same) + Of(PhotoClass.Converted) + versions; + var unhashed = Of(PhotoClass.SameUnhashed); + var (deletedHere, deletedThere) = (Of(PhotoClass.DeletedHere), Of(PhotoClass.DeletedThere)); + var removed = deletedHere + deletedThere; + var smaller = Math.Min(localPhotos, otherPhotos); + var union = localPhotos + otherPhotos - (shared + unhashed); + var containment = smaller == 0 ? 0 : (int)Math.Min(1000, 1000L * (shared + unhashed + removed) / smaller); + var overlap = union <= 0 ? 0 : (int)Math.Min(1000, 1000L * (shared + unhashed) / union); + return new CopyScore(shared, unhashed, removed, Of(PhotoClass.OnlyHere), Of(PhotoClass.OnlyThere), Of(PhotoClass.Differs), deletedHere, deletedThere, + versions, localPhotos, otherPhotos, containment, overlap, 0); + } +} diff --git a/src/Chronos.Data/Sync/SyncTombstones.cs b/src/Chronos.Data/Sync/SyncTombstones.cs index 8d0db261..c708fa9e 100644 --- a/src/Chronos.Data/Sync/SyncTombstones.cs +++ b/src/Chronos.Data/Sync/SyncTombstones.cs @@ -42,6 +42,18 @@ public sealed class SyncTombstones /// No photo removed on either side. public static SyncTombstones None { get; } = new([], null); + /// + /// The tombstones of two copies both given as the manifest carries them (): a local pair compared + /// with each other (owner review 6, #73), standing for "this PC". + /// + public static SyncTombstones FromDtos(IEnumerable here, IEnumerable? there) + { + ArgumentNullException.ThrowIfNull(here); + return new SyncTombstones(here.Where(r => r is not null).Select(r => new FrameRemoval(Guid.Empty, r.Sha256, r.Replacement, + string.Equals(r.Reason, Wire(RemovalReason.Replaced), StringComparison.OrdinalIgnoreCase) ? RemovalReason.Replaced : RemovalReason.Deleted, r.At)), + there?.Where(r => r is not null)); + } + /// Whether either side removed or replaced anything. public bool IsEmpty => _deletedLocally.Count == 0 && _deletedRemotely.Count == 0 && _canonical.Count == 0; diff --git a/tests/Chronos.Data.Tests/Copies/CopyPayloadTests.cs b/tests/Chronos.Data.Tests/Copies/CopyPayloadTests.cs new file mode 100644 index 00000000..d9edb208 --- /dev/null +++ b/tests/Chronos.Data.Tests/Copies/CopyPayloadTests.cs @@ -0,0 +1,139 @@ +using System.Globalization; +using Chronos.Core.Project; +using Chronos.Data.Copies; +using Chronos.Data.Sync; +using Chronos.Testing; + +namespace Chronos.Data.Tests.Copies; + +/// The details of two copies line by line and the one payload both get (owner review 6, Q4; design §4.3, §5e). +[Category("sync")] +public sealed class CopyPayloadTests +{ + 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, 'b'); + + private static FramePhoto Photo(int n) => new(Sha(n), 1000 + n, Start.AddSeconds(5 * n)); + + /// Photos 0–19 at indices 0–19. + private static readonly FramePhotos Here = new(Enumerable.Range(0, 20).Select(i => (i, Photo(i)))); + + /// Two photos more before them (100, 101): photo n at index n + 2. + private static readonly FramePhotos There = new(new[] { (0, Photo(100)), (1, Photo(101)) }.Concat(Enumerable.Range(0, 20).Select(i => (i + 2, Photo(i))))); + + private static SyncPayload Payload(ProjectSettings settings, string? title = "Harbour", params string[] tags) => + new(title, tags, 3, null, settings.ToJson()); + + private static IReadOnlyDictionary Choose(IEnumerable lines, CopySide side, params (string Path, CopySide Side)[] except) + { + var choices = lines.ToDictionary(l => l.Path, _ => side, StringComparer.Ordinal); + foreach (var (path, chosen) in except) choices[path] = chosen; + return choices; + } + + [Test] + public void TheSameExclusionAtAnotherIndexIsNoLine() + { + var here = Payload(new ProjectSettings { ExcludedFrames = [10], HeroFrame = 3 }); + var there = Payload(new ProjectSettings { ExcludedFrames = [12], HeroFrame = 5 }); + Assert.Empty(CopyPayload.Lines(here, Here, there, There)); + + // Another photo excluded there: one line, the frame positions. + var other = Payload(new ProjectSettings { ExcludedFrames = [11], HeroFrame = 5 }); + var lines = CopyPayload.Lines(here, Here, other, There); + var line = Assert.Contains(lines, l => l.Path == CopyPayload.PendingFramesPath); + Assert.Count(1, lines); + Assert.Contains(Sha(10), line.Here); + Assert.Contains(Sha(9), line.There); + } + + [Test] + public void FramePositionsCollapseIntoOneLine() + { + var here = Payload(new ProjectSettings { ExcludedFrames = [1, 2], HeroFrame = 3 }, "Harbour", "dusk"); + var there = Payload(new ProjectSettings { ExcludedFrames = [9], HeroFrame = 7, Stack = new StackSettings { Length = 15 } }, "Harbour at dusk", "dusk", "sea"); + var lines = CopyPayload.Lines(here, Here, there, There); + Assert.Count(4, lines); + Assert.True(lines.Select(l => l.Path).ToHashSet().SetEquals(["sequence.title", "sequence.tags", CopyPayload.PendingFramesPath, "stack.length"])); + var stack = lines.Single(l => l.Path == "stack.length"); + Assert.Equal(("9", "15"), (stack.Here, stack.There)); + } + + [Test] + public void MergeTakesTheChosenSideOfEachLine() + { + var here = Payload(new ProjectSettings { ExcludedFrames = [1], Stack = new StackSettings { Length = 9 }, Export = new ExportSettings { NameTemplate = "{title}" } }, + "Harbour", "dusk"); + var there = Payload(new ProjectSettings { ExcludedFrames = [9], Stack = new StackSettings { Length = 15 }, Export = new ExportSettings { NameTemplate = "{date}" } }, + "Harbour at dusk", "dusk", "sea"); + var lines = CopyPayload.Lines(here, Here, there, There); + var choices = Choose(lines, CopySide.ThisPc, ("sequence.title", CopySide.Other), ("stack.length", CopySide.Other), (CopyPayload.PendingFramesPath, CopySide.Other)); + var merged = CopyPayload.Merge(CopyPayload.Portable(here, Here), CopyPayload.Portable(there, There), choices, lines); + + Assert.Equal("Harbour at dusk", merged.Title); + Assert.SequenceEqual(["dusk"], merged.Tags); + var settings = ProjectSettings.FromJson(merged.SettingsJson!); + Assert.Equal((15, "{title}"), (settings.Stack.Length, settings.Export.NameTemplate)); + // Portable: placed on this PC's frames, the exclusion is there's photo 7 (index 9 there). + Assert.SequenceEqual([7], ProjectSettings.FromJson(ProjectContent.Place(merged.SettingsJson!, Here)).ExcludedFrames); + Assert.SequenceEqual([9], ProjectSettings.FromJson(ProjectContent.Place(merged.SettingsJson!, There)).ExcludedFrames); + + // Every line this PC's: the merged payload is this PC's. + var mine = CopyPayload.Merge(CopyPayload.Portable(here, Here), CopyPayload.Portable(there, There), Choose(lines, CopySide.ThisPc), lines); + Assert.Empty(CopyPayload.Lines(mine, new FramePhotos([]), CopyPayload.Portable(here, Here), new FramePhotos([]))); + } + + [Test] + public void AnUnchosenLineThrows() + { + var here = Payload(new ProjectSettings(), "Harbour"); + var there = Payload(new ProjectSettings { Stack = new StackSettings { Length = 15 } }, "Harbour at dusk"); + var lines = CopyPayload.Lines(here, Here, there, There); + var choices = new Dictionary { ["sequence.title"] = CopySide.Other }; + Assert.Throws(() => CopyPayload.Merge(here, there, choices, lines)); + } + + [Test] + public void AMergeThatIsNoProjectIsRefused() + { + var here = Payload(new ProjectSettings()); + var there = new SyncPayload("Harbour", [], 3, null, "not a project"); + var lines = CopyPayload.Lines(here, Here, there, There); + Assert.Equal("settings", Assert.Contains(lines, l => l.Path == "settings").Path); + Assert.Throws(() => CopyPayload.Merge(here, there, Choose(lines, CopySide.Other), lines)); + var kept = CopyPayload.Merge(here, there, Choose(lines, CopySide.ThisPc), lines); + Assert.Equal(9, ProjectSettings.FromJson(kept.SettingsJson!).Stack.Length); + } + + [Test] + public void ExcludeVersionsExcludesByPhoto() + { + var portable = CopyPayload.Portable(Payload(new ProjectSettings { HeroFrame = 4 }), Here); + var unhashed = string.Create(CultureInfo.InvariantCulture, $"u:777:{Start.AddSeconds(500).Ticks / TimeSpan.TicksPerMillisecond}"); + var excluded = CopyPayload.ExcludeVersions(portable, [Sha(99), Sha(4), unhashed]); + var pending = ProjectSettings.FromJson(excluded.SettingsJson!).PendingFrames!; + Assert.True(pending.Single(p => p.Sha256 == Sha(99)).Excluded); + // The hero's photo keeps its hero flag and is excluded too. + var hero = pending.Single(p => p.Sha256 == Sha(4)); + Assert.True(hero.Excluded && hero.Hero); + var byTime = pending.Single(p => string.IsNullOrEmpty(p.Sha256)); + Assert.Equal((777L, Start.AddSeconds(500)), (byTime.Size, byTime.CapturedAt!.Value)); + + // Placed on frames holding the version, it is excluded there. + var frames = new FramePhotos([(0, Photo(1)), (1, new FramePhoto(Sha(99), 5, Start)), (2, Photo(4))]); + var placed = ProjectSettings.FromJson(ProjectContent.Place(excluded.SettingsJson!, frames)); + Assert.SequenceEqual([1, 2], placed.ExcludedFrames.Order()); + Assert.Equal(2, placed.HeroFrame); + Assert.Same(portable, CopyPayload.ExcludeVersions(portable, [])); + } + + [Test] + public void MovedNamesThePathsThatChanged() + { + var before = CopyPayload.Portable(Payload(new ProjectSettings { ExcludedFrames = [1] }), Here); + var after = CopyPayload.Portable(Payload(new ProjectSettings { ExcludedFrames = [2], Stack = new StackSettings { Length = 3 } }, "Pier"), Here); + Assert.True(CopyPayload.Moved(before, after).SetEquals(["sequence.title", CopyPayload.PendingFramesPath, "stack.length"])); + Assert.Empty(CopyPayload.Moved(before, before)); + } +} diff --git a/tests/Chronos.Data.Tests/Copies/CopyRulesTests.cs b/tests/Chronos.Data.Tests/Copies/CopyRulesTests.cs new file mode 100644 index 00000000..c7ce11be --- /dev/null +++ b/tests/Chronos.Data.Tests/Copies/CopyRulesTests.cs @@ -0,0 +1,180 @@ +using Chronos.Core.Project; +using Chronos.Data.Copies; +using Chronos.Testing; + +namespace Chronos.Data.Tests.Copies; + +/// 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. +[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 }; + + /// 1.1's gate passing: both unlinked, same second, same camera, half of 10 shared. + 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()) + 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))); + } +} diff --git a/tests/Chronos.Data.Tests/Copies/CopySidesTests.cs b/tests/Chronos.Data.Tests/Copies/CopySidesTests.cs new file mode 100644 index 00000000..1f4138d5 --- /dev/null +++ b/tests/Chronos.Data.Tests/Copies/CopySidesTests.cs @@ -0,0 +1,130 @@ +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; + +/// 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). +[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 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; +} diff --git a/tests/Chronos.Data.Tests/Sync/CopyComparisonTests.cs b/tests/Chronos.Data.Tests/Sync/CopyComparisonTests.cs new file mode 100644 index 00000000..57242345 --- /dev/null +++ b/tests/Chronos.Data.Tests/Sync/CopyComparisonTests.cs @@ -0,0 +1,228 @@ +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); + } +}