Files
Chronos/tests/Chronos.Data.Tests/Copies/CopyPayloadTests.cs
T
Alby96andClaude Opus 5.5 f33bcac32e
CI / Build and test (push) Successful in 13m44s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 16s
Release / Build, test and publish (push) Successful in 26m34s
CI / PostgreSQL 16 integration (push) Failing after 17m36s
CI / PostgreSQL 17 integration (push) Failing after 15m36s
CI / Docker image (push) Successful in 1m12s
Release / Server image (push) Failing after 15s
feat(copies): matches between this PC and the server, Integrate and merge (owner review 6, v1.2.0) (#74)
The same time-lapse on a server and on this PC under different ids, or twice on this PC, is recognised,
compared and settled as the owner asked in owner review 6.

Matches are found after each sync refresh, through the server's new photo lookup, and after the jobs that
change photos. The default is to ask every time. Server › Matches offers:
- Keep apart, which is remembered;
- Link only and Unlink;
- the Compare dialog: photos, details line by line, projects and the plan.

Integrate completes both sides. Photos default to Union, projects are copied across, and a losing version
goes to the trash only after a hash check. Afterwards new photos move by themselves, and deletions wait for
the user.

A merge of two local sequences verifies every photo before the other sequence goes to its archive's trash.
Upload all local skips suspected matches and flags them.

Migration 0012 adds the found_copies and copy_probes tables. Jobs 25 (Integrate) and 26 (MergeSequences) are
new, and so are the server capabilities photo-lookup and upload-into.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-18 15:20:29 +01:00

140 lines
7.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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));
}
}