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>
140 lines
7.8 KiB
C#
140 lines
7.8 KiB
C#
using System.Globalization;
|
||
using Chronos.Core.Project;
|
||
using Chronos.Data.Copies;
|
||
using Chronos.Data.Sync;
|
||
using Chronos.Testing;
|
||
|
||
namespace Chronos.Data.Tests.Copies;
|
||
|
||
/// <summary>The details of two copies line by line and the one payload both get (owner review 6, Q4; design §4.3, §5e).</summary>
|
||
[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));
|
||
|
||
/// <summary>Photos 0–19 at indices 0–19.</summary>
|
||
private static readonly FramePhotos Here = new(Enumerable.Range(0, 20).Select(i => (i, Photo(i))));
|
||
|
||
/// <summary>Two photos more before them (100, 101): photo n at index n + 2.</summary>
|
||
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<string, CopySide> Choose(IEnumerable<DetailLine> 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<string, CopySide> { ["sequence.title"] = CopySide.Other };
|
||
Assert.Throws<ArgumentException>(() => 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<InvalidDataException>(() => 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));
|
||
}
|
||
}
|