From eac4a36a7b60a7f990ef7702c68da6401fcc90a7 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 3 Nov 2026 10:05:27 +0100 Subject: [PATCH] fix(projects): one photo merges as one, newer settings are never merged A workspace copied before the home hashed or converted a photo named it differently from the versions the home made since, so a pull merged the same photo as two: frame edits were silently undone and Keep mine placed the library's value. Before replaying, the workspace media now learn the home's hashes (same frame id, size and capture time) and conversion aliases, so every side names each photo alike. Rebind also gives a renamed entry the occurrence a placement looks for, so positions on burst photos keep applying once hashed, and folds entries of one photo (in Rebind and in the merge's keyed form) instead of keeping one. Settings of a newer Chronos are no longer merged member by member with this one's: ProjectMerge refuses when both sides changed. A workspace pull keeps its local versions and working state untouched and records a conflict of format that only Save both settles (into a new project of this format); with nothing local it takes the newer head read-only. Main's catch-up leaves a newer head and settings written meanwhile as they are. When Main's catch-up merge overrides values another writer stored, that writer's settings are kept as an External version before the Merge one, so nothing is lost from the history. Unknown members inside Auto values, camera keyframes and curve points, and on pending entries once placed, are kept like ProjectSettings' Extra. Co-Authored-By: Claude Opus 5.5 --- src/Chronos.Core/Project/Auto.cs | 10 + src/Chronos.Core/Project/FramePhotos.cs | 6 +- src/Chronos.Core/Project/ProjectContent.cs | 82 ++++- src/Chronos.Core/Project/ProjectSettings.cs | 47 ++- src/Chronos.Data/Projects/ProjectMerge.cs | 57 ++-- src/Chronos.Data/Projects/ProjectService.cs | 28 +- src/Chronos.Data/Workspaces/WorkspaceMedia.cs | 40 +++ src/Chronos.Data/Workspaces/WorkspaceStore.cs | 11 +- src/Chronos.Data/Workspaces/WorkspaceSync.cs | 63 +++- .../Project/ProjectContentTests.cs | 120 ++++++++ .../Projects/ProjectMergeTests.cs | 192 ++++++++++++ .../Workspaces/WorkspaceMergeTests.cs | 287 ++++++++++++++++++ 12 files changed, 905 insertions(+), 38 deletions(-) create mode 100644 tests/Chronos.Data.Tests/Projects/ProjectMergeTests.cs create mode 100644 tests/Chronos.Data.Tests/Workspaces/WorkspaceMergeTests.cs diff --git a/src/Chronos.Core/Project/Auto.cs b/src/Chronos.Core/Project/Auto.cs index 962486e7..b9a3d34e 100644 --- a/src/Chronos.Core/Project/Auto.cs +++ b/src/Chronos.Core/Project/Auto.cs @@ -1,3 +1,6 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + namespace Chronos.Core.Project; /// @@ -24,6 +27,13 @@ public sealed record Auto public IReadOnlyList? ReasonArgs { get; init; } + /// + /// Members this Chronos does not know (written by a newer one, such as the advisor's confidence): kept as they are, so + /// they survive a round trip and every edit of the parameter (owner review 2, R8). + /// + [JsonExtensionData] + public Dictionary? Extra { get; set; } + /// The value in force: manual when set by the user, otherwise the advisor's, otherwise . public T Resolve(T fallback) => !IsAuto && Manual is not null ? Manual : HasAdvice && AutoValue is not null ? AutoValue : fallback; diff --git a/src/Chronos.Core/Project/FramePhotos.cs b/src/Chronos.Core/Project/FramePhotos.cs index 7d23fbb5..e5c740d8 100644 --- a/src/Chronos.Core/Project/FramePhotos.cs +++ b/src/Chronos.Core/Project/FramePhotos.cs @@ -55,7 +55,8 @@ public sealed class FramePhotos public FramePhotos(IEnumerable<(int Index, FramePhoto Photo)> frames, IEnumerable? aliases) { ArgumentNullException.ThrowIfNull(frames); - _canonical = PhotoAlias.Classes(aliases); + Aliases = aliases is null ? [] : [.. aliases.Where(a => a is not null)]; + _canonical = PhotoAlias.Classes(Aliases); var list = frames.Where(f => f.Index >= 0).ToList(); _byIndex = new FramePhoto?[list.Count == 0 ? 0 : list.Max(f => f.Index) + 1]; foreach (var (index, photo) in list) _byIndex[index] = photo; @@ -82,6 +83,9 @@ public sealed class FramePhotos public int Count => _byIndex.Length; + /// The links between encodings of one photo these frames were given (empty without any): another copy's frames can take them. + public IReadOnlyList Aliases { get; } + /// The photo at ; null when no frame has that index. public FramePhoto? this[int index] => (uint)index < (uint)_byIndex.Length ? _byIndex[index] : null; diff --git a/src/Chronos.Core/Project/ProjectContent.cs b/src/Chronos.Core/Project/ProjectContent.cs index 0e5534aa..41fbfff5 100644 --- a/src/Chronos.Core/Project/ProjectContent.cs +++ b/src/Chronos.Core/Project/ProjectContent.cs @@ -57,9 +57,12 @@ public static class ProjectContent /// /// The portable text with each pending frame whose photo hold named as they hold it now: an /// encoding a conversion replaced by the one the frame holds (, also its undo), a photo parked - /// before it was hashed by its SHA-256, size and capture time. The same choices on the same photos then read as the same - /// text (), whichever encoding or hashing state they were saved against; a placement finds the - /// same frames either way. Canonical when anything is renamed; the text as it is otherwise, and for a newer format. + /// before it was hashed by its SHA-256, size and capture time, and which copy of that photo it is among the frames + /// holding it under that name (a photo of a burst told apart by its order while unhashed is unique once hashed). Entries + /// that name one photo once renamed become one (). The same choices on the same photos then read as + /// the same text (), whichever encoding or hashing state they were saved against; a placement + /// finds the same frames either way. Canonical when anything is renamed or folded; the text as it is otherwise, and for + /// a newer format. /// /// The text is not a project object. public static string Rebind(string portableJson, FramePhotos frames) @@ -80,19 +83,90 @@ public static class ProjectContent var occurrence = entry["occurrence"] is JsonValue o && o.TryGetValue(out var n) && n > 0 ? n : 0; // Only a frame that is hashed names a photo better than the entry does. if (frames.IndexOf(new FramePhoto(sha, size, captured), occurrence) is not { } at || frames[at] is not { IsHashed: true } held) continue; + // The copy a placement looks for under the new name: among the frames holding that SHA-256, not those of that size and time. + var copy = frames.Occurrence(at); var sameTime = held.CapturedAt is { } heldAt ? captured is { } entryAt && FramePhoto.Millisecond(heldAt) == FramePhoto.Millisecond(entryAt) : captured is null; - if (string.Equals(held.Sha256, sha, StringComparison.OrdinalIgnoreCase) && held.Size == size && sameTime) continue; + if (string.Equals(held.Sha256, sha, StringComparison.OrdinalIgnoreCase) && held.Size == size && sameTime && copy == occurrence) continue; entry["sha256"] = held.Sha256!.ToLowerInvariant(); entry["size"] = held.Size; if (held.CapturedAt is { } heldTime) entry["capturedAt"] = heldTime; else entry.Remove("capturedAt"); + if (copy > 0) entry["occurrence"] = copy; + else entry.Remove("occurrence"); renamed = true; } + renamed |= FoldAlike(pending); return renamed ? Canonical(project.ToJsonString()) : portableJson; } + /// + /// The photo a pending entry names and which copy of it: "sha:<sha-256>#<occurrence>", or + /// "photo:<size>@<capture ticks>#<occurrence>" for a photo not hashed; null for an entry naming no photo. + /// Entries of one key are one photo's positions (). + /// + public static string? PhotoKey(JsonNode? entry) + { + if (entry is not JsonObject e) return null; + var occurrence = e["occurrence"] is JsonValue o && o.TryGetValue(out var n) ? n : 0; + if (e["sha256"] is JsonValue s && s.TryGetValue(out var sha) && sha.Length > 0) + return string.Create(CultureInfo.InvariantCulture, $"sha:{sha.ToLowerInvariant()}#{occurrence}"); + var size = e["size"] is JsonValue z && z.TryGetValue(out var bytes) ? bytes : 0; + return e["capturedAt"] is JsonValue c && c.TryGetValue(out var at) + ? string.Create(CultureInfo.InvariantCulture, $"photo:{size}@{at.Ticks}#{occurrence}") + : null; + } + + /// + /// Folds into , two pending entries of one photo (saved under two names, + /// alike since): excluded or hero when either is, the keyframes of both (each once), and the members this Chronos does not + /// know of both (those of where both have one). Nothing either held is dropped. + /// + public static void Fold(JsonObject into, JsonObject entry) + { + ArgumentNullException.ThrowIfNull(into); + ArgumentNullException.ThrowIfNull(entry); + if (Flag(entry, "excluded")) into["excluded"] = true; + if (Flag(entry, "hero")) into["hero"] = true; + if (entry["keyframes"] is JsonArray { Count: > 0 } keys) + { + if (into["keyframes"] is not JsonArray mine) into["keyframes"] = mine = new JsonArray(); + foreach (var key in keys.ToList()) + { + if (!mine.Any(k => JsonNode.DeepEquals(k, key))) mine.Add(key?.DeepClone()); + } + } + foreach (var (name, value) in entry.ToList()) + { + if (!ProjectSettings.PendingMembers.Contains(name) && !into.ContainsKey(name)) into[name] = value?.DeepClone(); + } + } + + /// Folds the pending entries naming one photo into the first of them (); whether any were. + private static bool FoldAlike(JsonArray pending) + { + var first = new Dictionary(StringComparer.Ordinal); + var folded = new List(); + foreach (var entry in pending.OfType()) + { + if (PhotoKey(entry) is not { } key) continue; + if (first.TryGetValue(key, out var into)) + { + Fold(into, entry); + folded.Add(entry); + } + else + { + first[key] = entry; + } + } + foreach (var entry in folded) pending.Remove(entry); + return folded.Count > 0; + } + + private static bool Flag(JsonObject entry, string name) => entry[name] is JsonValue value && value.TryGetValue(out var set) && set; + /// /// Whether two portable texts hold the same choices on the frames of : the same text, or the /// same once their pending frames are named as the frames hold their photos now (). diff --git a/src/Chronos.Core/Project/ProjectSettings.cs b/src/Chronos.Core/Project/ProjectSettings.cs index f6eec582..2654249c 100644 --- a/src/Chronos.Core/Project/ProjectSettings.cs +++ b/src/Chronos.Core/Project/ProjectSettings.cs @@ -44,10 +44,20 @@ public enum StackSetting } /// A control point of the time-remap curve: output fraction → source fraction (both 0..1). -public sealed record CurvePoint(double X, double Y); +public sealed record CurvePoint(double X, double Y) +{ + /// Members this Chronos does not know (written by a newer one): kept as they are, so they survive a round trip. + [JsonExtensionData] + public Dictionary? Extra { get; set; } +} /// Virtual-camera keyframe in normalised coordinates. -public sealed record CameraKey(int Frame, double CenterX, double CenterY, double Zoom, double Rotation, string Easing = "EaseInOut"); +public sealed record CameraKey(int Frame, double CenterX, double CenterY, double Zoom, double Rotation, string Easing = "EaseInOut") +{ + /// Members this Chronos does not know (written by a newer one): kept as they are, so they survive a round trip. + [JsonExtensionData] + public Dictionary? Extra { get; set; } +} /// /// The frame positions of a project that name a photo this copy of the sequence does not hold: settings taken from a @@ -564,14 +574,21 @@ public sealed record ProjectSettings return changed; } - /// Gives the pending frames whose photo holds their positions back. + /// + /// Gives the pending frames whose photo holds their positions back. An entry with members this + /// Chronos does not know (a newer one's values of the photo) stays, without its positions: those members are not this + /// Chronos's to drop, and the next portable form parks the positions in it again. + /// private static bool PlacePending(JsonObject project, FramePhotos to, List pending) { var changed = false; foreach (var parked in pending) { if (to.IndexOf(parked.Photo, parked.Occurrence) is not { } at) continue; - parked.Placed = changed = true; + var kept = HasUnknownMembers(parked.Entry); + if (kept && !HoldsPosition(parked.Entry)) continue; + parked.Placed = !kept; + changed = true; if (Flag(parked.Entry, "excluded")) { if (project["excludedFrames"] is not JsonArray positions) project["excludedFrames"] = positions = new JsonArray(); @@ -592,6 +609,12 @@ public sealed record ProjectSettings keyframes.Add(copy); } } + if (kept) + { + parked.Entry["excluded"] = false; + parked.Entry["hero"] = false; + parked.Entry["keyframes"] = new JsonArray(); + } } if (project["heroFrame"] is null) return changed; // A hero is chosen: none waits any more, and an entry left with nothing to place goes. @@ -651,8 +674,20 @@ public sealed record ProjectSettings private static bool Flag(JsonObject entry, string name) => entry[name] is JsonValue value && value.TryGetValue(out var set) && set; - /// A pending entry still has a position to give its photo. - private static bool Waits(JsonObject entry) => Flag(entry, "excluded") || Flag(entry, "hero") || entry["keyframes"] is JsonArray { Count: > 0 }; + /// The members of a pending entry this Chronos knows: its photo, which copy of it, and the positions on it. + internal static readonly HashSet PendingMembers = new(StringComparer.OrdinalIgnoreCase) + { + "sha256", "size", "capturedAt", "occurrence", "excluded", "hero", "keyframes", + }; + + /// A pending entry has a position to give its photo. + private static bool HoldsPosition(JsonObject entry) => Flag(entry, "excluded") || Flag(entry, "hero") || entry["keyframes"] is JsonArray { Count: > 0 }; + + /// A pending entry holds members this Chronos does not know (written by a newer one). + private static bool HasUnknownMembers(JsonObject entry) => entry.Any(p => !PendingMembers.Contains(p.Key)); + + /// A pending entry still holds something: a position to give its photo, or members that are not this Chronos's to drop. + private static bool Waits(JsonObject entry) => HoldsPosition(entry) || HasUnknownMembers(entry); private static bool IsPosition(JsonNode? node, out int position) { diff --git a/src/Chronos.Data/Projects/ProjectMerge.cs b/src/Chronos.Data/Projects/ProjectMerge.cs index d59fd3d1..a65c0d66 100644 --- a/src/Chronos.Data/Projects/ProjectMerge.cs +++ b/src/Chronos.Data/Projects/ProjectMerge.cs @@ -22,6 +22,12 @@ public sealed record ProjectReplay(IReadOnlyList Commits, IRead public bool HasConflicts => Conflicts.Count > 0; } +/// +/// A merge both sides of which changed met settings of a newer Chronos (): this +/// Chronos never merges them, as their known members may mean something else there (owner review 2, R8). Nothing was merged. +/// +public sealed class NewerSettingsException(string message) : InvalidOperationException(message); + /// /// Three-way merge of projects: the editor's edit (ours) and a change stored meanwhile by someone else /// (theirs: an analysis filling Auto values, another client through the server, another workspace through a @@ -31,15 +37,24 @@ public sealed record ProjectReplay(IReadOnlyList Commits, IRead /// user instead (a server sequence, BRIEF §14.5; a workspace project, owner review 2, R8). Pending frames (frame /// positions parked by photo, the portable form of ) merge photo by photo and flag by flag, so /// two people excluding different photos never conflict; two heroes chosen on both sides do (path ). -/// Members this Chronos does not know merge like the others and are kept. +/// Entries of one photo in one project (saved under two names, alike since) merge as one, keeping the positions of both. +/// Members this Chronos does not know merge like the others and are kept. Settings of a newer Chronos are never merged: +/// a side that did not change takes the other's as they are, anything else is refused (). /// public static class ProjectMerge { /// The path of the conflict of two heroes chosen among pending frames on both sides. public const string HeroPath = "hero"; + /// + /// The path of the settings format (): a difference there is settings of a newer + /// Chronos that this one cannot merge with its own (a workspace's conflict of format). + /// + public const string FormatPath = "version"; + private const string PendingPath = "pendingFrames"; + /// Both sides changed, and one of the three is of a newer format. public static ProjectSettings Merge(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs) => MergeWithConflicts(basis, ours, theirs).Merged; @@ -48,6 +63,7 @@ public static class ProjectMerge /// conflicts. A conflict whose path is in takes theirs (the user chose "take server" /// there), every other one ours. /// + /// Both sides changed, and one of the three is of a newer format. public static ProjectMergeResult MergeWithConflicts(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs, IReadOnlySet? takeTheirs = null) { ArgumentNullException.ThrowIfNull(basis); @@ -56,6 +72,14 @@ public static class ProjectMerge var conflicts = new List(); var choice = takeTheirs ?? EmptySet; var (b, o, t) = (Keyed(basis), Keyed(ours), Keyed(theirs)); + if (basis.IsNewerFormat || ours.IsNewerFormat || theirs.IsNewerFormat) + { + // Their known members may mean something else there: only a side that did not change gives way. + if (JsonNode.DeepEquals(o, b) || JsonNode.DeepEquals(t, o)) return new ProjectMergeResult(Read(theirs.ToJson()), []); + if (JsonNode.DeepEquals(t, b)) return new ProjectMergeResult(Read(ours.ToJson()), []); + throw new NewerSettingsException(string.Create(CultureInfo.InvariantCulture, + $"Settings of format {Math.Max(basis.Version, Math.Max(ours.Version, theirs.Version))} are not merged by this Chronos (format {ProjectSettings.CurrentVersion}).")); + } var merged = MergeNode(b, o, t, string.Empty, conflicts, choice) as JsonObject ?? new JsonObject(); OneHero(merged, o, t, conflicts, choice); return new ProjectMergeResult(FromKeyed(merged), conflicts); @@ -75,6 +99,7 @@ public static class ProjectMerge /// history stays linear and the last one holds the net change. Conflicts are those of the last commit; their paths in /// take theirs in every commit. /// + /// A commit and theirs both changed, and one of them (or the basis) is of a newer format. public static ProjectReplay Replay(ProjectSettings basis, IReadOnlyList mine, ProjectSettings theirs, IReadOnlySet? takeTheirs = null) { ArgumentNullException.ThrowIfNull(mine); @@ -157,14 +182,22 @@ public static class ProjectMerge private static string? HeroOf(JsonObject project) => project[PendingPath] is JsonObject pending ? pending.FirstOrDefault(p => p.Value is JsonObject entry && Flag(entry, "hero")).Key : null; - /// The project as JSON with its pending frames as an object keyed by photo (). + /// + /// The project as JSON with its pending frames as an object keyed by photo (); entries of one photo + /// (saved under two names, alike since) are one, with the positions of both (). + /// private static JsonObject Keyed(ProjectSettings settings) { var project = JsonNode.Parse(settings.ToJson())!.AsObject(); if (project[PendingPath] is JsonArray pending) { var keyed = new JsonObject(); - foreach (var entry in pending) keyed[KeyOf(entry)] = entry?.DeepClone(); + foreach (var entry in pending) + { + var key = KeyOf(entry); + if (keyed[key] is JsonObject known && entry is JsonObject other) ProjectContent.Fold(known, other); + else keyed[key] = entry?.DeepClone(); + } project[PendingPath] = keyed; } return project; @@ -188,22 +221,10 @@ public static class ProjectMerge } /// - /// The key of a pending entry: its photo and which copy of it ("sha:…#0", or "photo:size@ticks#0" for a photo not hashed), - /// or its JSON for an entry naming no photo. + /// The key of a pending entry: its photo and which copy of it (: "sha:…#0", or + /// "photo:size@ticks#0" for a photo not hashed), or its JSON for an entry naming no photo. /// - private static string KeyOf(JsonNode? entry) - { - if (entry is JsonObject e) - { - var occurrence = e["occurrence"] is JsonValue o && o.TryGetValue(out var n) ? n : 0; - if (e["sha256"] is JsonValue s && s.TryGetValue(out var sha) && sha.Length > 0) - return string.Create(CultureInfo.InvariantCulture, $"sha:{sha.ToLowerInvariant()}#{occurrence}"); - var size = e["size"] is JsonValue z && z.TryGetValue(out var bytes) ? bytes : 0; - if (e["capturedAt"] is JsonValue c && c.TryGetValue(out var at)) - return string.Create(CultureInfo.InvariantCulture, $"photo:{size}@{at.Ticks}#{occurrence}"); - } - return "raw:" + (entry?.ToJsonString() ?? "null"); - } + private static string KeyOf(JsonNode? entry) => ProjectContent.PhotoKey(entry) ?? "raw:" + (entry?.ToJsonString() ?? "null"); /// The entry of the photo of holding no position; null for an entry naming no photo. private static JsonNode? Unplaced(JsonNode? template) diff --git a/src/Chronos.Data/Projects/ProjectService.cs b/src/Chronos.Data/Projects/ProjectService.cs index e04f257d..432fcb1d 100644 --- a/src/Chronos.Data/Projects/ProjectService.cs +++ b/src/Chronos.Data/Projects/ProjectService.cs @@ -392,10 +392,30 @@ public sealed class ProjectService return; } // Both moved: the settings written meanwhile merge with the head (the version made in a workspace wins where both changed), - // each photo named as the frames hold it now so the same photo merges as one. - var merged = ProjectMerge.Merge(ProjectMerge.Read(ProjectContent.Rebind(mirrored.SettingsJson, frames.Photos)), - ProjectMerge.Read(ProjectContent.Rebind(head.SettingsJson, frames.Photos)), ProjectMerge.Read(ProjectContent.Rebind(current, frames.Photos))); - var version = await AppendDerivedAsync(context, main, head, ProjectContent.Canonical(merged.ToJson()), ProjectVersionKind.Merge, cancellationToken) + // each photo named as the frames hold it now so the same photo merges as one. Where both changed, the settings written + // meanwhile become an external version first, so the values the merge leaves out stay in the history to restore. + ProjectMergeResult merged; + try + { + merged = ProjectMerge.MergeWithConflicts(ProjectMerge.Read(ProjectContent.Rebind(mirrored.SettingsJson, frames.Photos)), + ProjectMerge.Read(ProjectContent.Rebind(head.SettingsJson, frames.Photos)), ProjectMerge.Read(ProjectContent.Rebind(current, frames.Photos))); + } + catch (NewerSettingsException ex) + { + // A newer Chronos's version is never merged with this one's settings: both are left as they are, recorded as seen. + Log.Warning("projects", $"Version {head.Number} of Main of sequence {context.Row.Id} and its settings written meanwhile are left as they are", ex); + await _db.Projects.SetMirroredAsync(main.Id, head.Id, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false); + return; + } + var parent = head; + if (merged.HasConflicts) + { + parent = await AppendDerivedAsync(context, main, head, current, ProjectVersionKind.External, cancellationToken).ConfigureAwait(false); + main = (await _db.Projects.GetAsync(main.Id, cancellationToken).ConfigureAwait(false))!; + Log.Info("projects", $"Version {head.Number} of Main of sequence {context.Row.Id} and its settings written meanwhile both changed " + + $"{string.Join(", ", merged.Conflicts.Select(c => c.Path))}: version {head.Number}'s values are kept, the others are version {parent.Number}"); + } + var version = await AppendDerivedAsync(context, main, parent, ProjectContent.Canonical(merged.Merged.ToJson()), ProjectVersionKind.Merge, cancellationToken) .ConfigureAwait(false); main = (await _db.Projects.GetAsync(main.Id, cancellationToken).ConfigureAwait(false))!; await MirrorAsync(context, main, version, cancellationToken).ConfigureAwait(false); diff --git a/src/Chronos.Data/Workspaces/WorkspaceMedia.cs b/src/Chronos.Data/Workspaces/WorkspaceMedia.cs index a33cc224..df45aee6 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceMedia.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceMedia.cs @@ -4,6 +4,7 @@ using Chronos.Core.Model; using Chronos.Core.Project; using Chronos.Core.Render; using Chronos.Data.Processing; +using Chronos.Data.Projects; namespace Chronos.Data.Workspaces; @@ -104,6 +105,45 @@ public sealed record WorkspaceMedia(string Format, int FormatVersion, string Kin /// The frames held, as the processing engine reads them. public IReadOnlyList ToFrames() => [.. Frames.Select(f => f.ToFrame(SequenceId))]; + + /// + /// These media with their photos named as names them now (owner review 2, R8): a frame copied + /// before the home hashed its photo (a folder adopted, verified since) takes the SHA-256 the home's frame of that id has + /// for the same photo (the same size and capture time; a conversion that gave the frame another file is not the same + /// photo), and the encodings the home's conversions joined since the copy are added to . Versions + /// the home made since name those photos so; named alike here, one photo merges as one. The frames, their files and the + /// frames version stay as they are. This instance when there is nothing to learn. + /// + public WorkspaceMedia NamedAsAt(HomeFrames home) + { + ArgumentNullException.ThrowIfNull(home); + var there = new Dictionary(home.Frames.Count); + foreach (var frame in home.Frames) there[frame.Id] = frame; + var learned = false; + var frames = new List(Frames.Count); + foreach (var frame in Frames) + { + if (string.IsNullOrEmpty(frame.Sha256) && there.TryGetValue(frame.Id, out var held) && !string.IsNullOrEmpty(held.Sha256) + && held.Size == frame.Size && Millisecond(held.CapturedAt) == Millisecond(frame.CapturedAt)) + { + frames.Add(frame with { Sha256 = held.Sha256.ToLowerInvariant() }); + learned = true; + } + else + { + frames.Add(frame); + } + } + var known = Aliases ?? []; + var links = known.Select(Link).ToHashSet(); + var added = home.Photos.Aliases.Where(a => links.Add(Link(a))).ToList(); + if (!learned && added.Count == 0) return this; + return this with { Frames = learned ? frames : Frames, Aliases = added.Count > 0 ? [.. known, .. added] : Aliases }; + + static long? Millisecond(DateTime? time) => time is { } at ? at.Ticks / TimeSpan.TicksPerMillisecond : null; + // A media index read back may hold a damaged link: compared as it is, and left out of the classes like any other. + static (string?, string?) Link(PhotoAlias alias) => (alias.Sha256?.ToLowerInvariant(), alias.Replacement?.ToLowerInvariant()); + } } /// diff --git a/src/Chronos.Data/Workspaces/WorkspaceStore.cs b/src/Chronos.Data/Workspaces/WorkspaceStore.cs index b3448e14..d049ca34 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceStore.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceStore.cs @@ -27,7 +27,16 @@ public sealed record WorkspaceSyncState(Guid? RestoredFrom, WorkspaceConflict? C /// taking the server's (each member or all), or saving both (). /// public sealed record WorkspaceConflict(Guid TheirsVersionId, int TheirsNumber, Guid BaseVersionId, IReadOnlyList Mine, - IReadOnlyList Differences, DateTimeOffset Found); + IReadOnlyList Differences, DateTimeOffset Found) +{ + /// + /// The home's head holds settings of a newer Chronos (a difference at ): nothing of + /// this workspace's is merged into them. Only Save both settles it (this workspace's versions go to a new project in this + /// Chronos's format), or a Chronos that reads them; keeping mine or taking the server's is refused. + /// + [JsonIgnore] + public bool NewerFormat => Differences.Any(d => d.Path == ProjectMerge.FormatPath); +} /// /// Parameters of a job: target = the home's sequence; the workspace folder, the diff --git a/src/Chronos.Data/Workspaces/WorkspaceSync.cs b/src/Chronos.Data/Workspaces/WorkspaceSync.cs index 77ab7612..dad0487f 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceSync.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceSync.cs @@ -1,3 +1,4 @@ +using System.Globalization; using System.Text.Json; using Chronos.Core.Model; using Chronos.Core.Project; @@ -81,9 +82,12 @@ public sealed class WorkspaceSyncException(PushOutcome outcome, string message) /// (, new ids, the old files kept in versions/.replaced), and the working state is /// merged too (); settings both sides changed to different values stop /// there as a conflict () for the user to settle (). Versions -/// travel in the portable form (), so frames inserted at the home never move a position. Every -/// operation holds the project's lease; nothing is ever deleted: a restore or save-both first saves uncommitted changes -/// as a version. +/// travel in the portable form (), so frames inserted at the home never move a position; before +/// a merge the media learn how the home names its photos now (hashed or converted there since the copy), so every side +/// names one photo alike. Settings of a newer Chronos are never merged with this one's: a project with nothing of its own +/// moves onto them read-only, any other waits on a conflict of format that only Save both settles +/// (). Every operation holds the project's lease; nothing is ever deleted: a +/// restore or save-both first saves uncommitted changes as a version. /// public sealed class WorkspaceSync(WorkspaceStore store, string clientName) { @@ -357,8 +361,10 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) var tipVersion = locals.Count > 0 ? locals[^1] : basis; if (ours.Count > 0) basis = theirs.First(v => v.Id == ours[^1].Id); locals = [.. locals.Skip(ours.Count)]; - var media = project.ReadMedia(); + // The media first learn how the home names its photos now (hashed or converted there since the copy). + var media = await MediaNamedAsAtHomeAsync(project, home, cancellationToken).ConfigureAwait(false); var photos = media?.Photos ?? NoFrames; + if (IsNewer(head) || IsNewer(basis)) return NewerAtHome(project, bundle, media, tipVersion, locals, basis, head, theirs, takeTheirs); // Each side names a photo as the frames held it when it was saved: named as the media hold it now (a conversion or a // hash since), the same photo merges as one. var basisSettings = basis is null ? new ProjectSettings() : ProjectMerge.Read(ProjectContent.Rebind(basis.SettingsJson, photos)); @@ -406,6 +412,55 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) return (moved, null, theirs); } + /// + /// The workspace's media with their photos named as the home names them now (), + /// written back when the home taught them anything, so the versions, the working state and every comparison after this + /// one name each photo alike. Null while the workspace holds no media. + /// + private static async Task MediaNamedAsAtHomeAsync(WorkspaceProject project, IProjectHome home, CancellationToken cancellationToken) + { + if (project.ReadMedia() is not { } media) return null; + var named = media.NamedAsAt(await home.FramesAsync(cancellationToken).ConfigureAwait(false)); + if (!ReferenceEquals(named, media)) project.WriteMedia(named); + return named; + } + + private static bool IsNewer(ProjectVersion? version) => version is not null && ProjectContent.FormatOf(version.SettingsJson) > ProjectSettings.CurrentVersion; + + /// + /// The home's head, or the base, holds settings of a newer Chronos: nothing is merged into or out of them, as their known + /// members may mean something else there. With nothing of this workspace's own (no version to send, the working state the + /// last version's), the project moves onto the head as it is, read-only when that is the newer one. Otherwise the local + /// versions and the working state stay as they are and a conflict of format waits (): + /// Save both sends them to a new project made from the base, in this Chronos's format. A choice to merge them + /// ( set: keep mine, take the server's) is refused, the conflict left recorded. + /// + /// A choice to merge was made. + private static (ProjectBundle Bundle, WorkspaceConflict? Conflict, IReadOnlyList Pulled) NewerAtHome(WorkspaceProject project, + ProjectBundle bundle, WorkspaceMedia? media, ProjectVersion? tipVersion, IReadOnlyList locals, ProjectVersion? basis, ProjectVersion head, + IReadOnlyList theirs, IReadOnlySet? takeTheirs) + { + var photos = media?.Photos ?? NoFrames; + var tip = tipVersion?.SettingsJson ?? ProjectContent.ToPortable(new ProjectSettings(), photos); + if (locals.Count == 0 && (bundle.IsReadOnly + || ProjectContent.SameChoices(tip, Portable(bundle.Project, media) ?? throw MediaMissing(project.ProjectId), photos))) + { + var placed = ProjectSettings.FromJson(ProjectContent.Place(head.SettingsJson, photos), allowNewer: true); + var moved = WithBase(bundle, head) with { Project = placed, Saved = DateTimeOffset.UtcNow }; + project.WriteBundle(moved); + return (moved, null, theirs); + } + var newer = Math.Max(ProjectContent.FormatOf(head.SettingsJson), basis is null ? 0 : ProjectContent.FormatOf(basis.SettingsJson)); + var format = new SettingsDifference(ProjectMerge.FormatPath, ProjectContent.FormatOf(tip).ToString(CultureInfo.InvariantCulture), + newer.ToString(CultureInfo.InvariantCulture)); + var conflict = new WorkspaceConflict(head.Id, head.Number, basis?.Id ?? Guid.Empty, [.. locals.Select(v => v.Id)], [format], DateTimeOffset.UtcNow); + project.WriteSync(project.ReadSync() with { Conflict = conflict }); + if (takeTheirs is not null) + throw new WorkspaceSyncException(PushOutcome.NewerFormat, + $"Version {head.Number} of project {project.ProjectId} holds settings of a newer Chronos: nothing of this workspace's is merged into it (save both, or update Chronos)."); + return (bundle, conflict, theirs); + } + /// Both sides kept: this workspace's versions to a new project at the home, this project onto the home's head. private async Task SaveBothAsync(WorkspaceProject project, ProjectBundle bundle, IProjectHome home, string? name, CancellationToken cancellationToken) { diff --git a/tests/Chronos.Core.Tests/Project/ProjectContentTests.cs b/tests/Chronos.Core.Tests/Project/ProjectContentTests.cs index 7150e567..44e9db62 100644 --- a/tests/Chronos.Core.Tests/Project/ProjectContentTests.cs +++ b/tests/Chronos.Core.Tests/Project/ProjectContentTests.cs @@ -135,6 +135,126 @@ public sealed class ProjectContentTests Assert.Equal(newer, ProjectContent.Rebind(newer, now)); } + [Test] + public void APhotoOfABurstToldApartByItsOrderKeepsItsPositionOnceHashed() + { + // A RAW burst of one size without sub-second times: three photos alike but for their SHA-256, told apart by their + // order while not hashed (frames 1 to 3); and a photo held twice, two copies of one file (frames 4 and 5). + FramePhoto Burst(int n, bool hashed) => new(hashed ? (500 + n).ToString("x64", System.Globalization.CultureInfo.InvariantCulture) : null, 25_000_000, Start); + FramePhotos Copy(bool hashed) => new([(0, Photo(1)), (1, Burst(0, hashed)), (2, Burst(1, hashed)), (3, Burst(2, hashed)), (4, Photo(2)), (5, Photo(2))]); + var (before, after) = (Copy(hashed: false), Copy(hashed: true)); + var choices = new ProjectSettings { ExcludedFrames = [3, 5], HeroFrame = 2, Camera = new CameraSettings { Keyframes = [new CameraKey(1, 0.3, 0.5, 1.2, 0)] } }; + var saved = ProjectContent.ToPortable(choices, before); + Assert.SequenceEqual([0, 1, 1, 2], ProjectSettings.FromJson(saved).PendingFrames!.Select(p => p.Occurrence).Order()); + + // Hashed since: each photo of the burst is the only one of its SHA-256, the copy of the photo held twice still the second. + var rebound = ProjectContent.Rebind(saved, after); + var named = ProjectSettings.FromJson(rebound).PendingFrames!; + Assert.True(named.All(p => p.Sha256 is not null)); + Assert.SequenceEqual([0, 0, 0, 1], named.Select(p => p.Occurrence).Order()); + Assert.Equal(ProjectContent.ToPortable(choices, after), rebound); + Assert.True(ProjectContent.SameChoices(saved, ProjectContent.ToPortable(choices, after), after)); + var placed = ProjectSettings.FromJson(ProjectContent.Place(rebound, after)); + Assert.SequenceEqual([3, 5], placed.ExcludedFrames); + Assert.Equal((int?)2, placed.HeroFrame); + Assert.Equal(1, placed.Camera.Keyframes.Single().Frame); + Assert.Null(placed.PendingFrames); + } + + [Test] + public void EntriesOfOnePhotoSavedUnderTwoNamesBecomeOneWithThePositionsOfBoth() + { + // Photo 2 was converted into 20 (owner review 2, R7). A version holds an exclusion parked on the old encoding, and a hero, a + // keyframe and a newer Chronos's rating parked on the new one: each copy named the photo as it held it. + var rating = JsonSerializer.SerializeToElement(4); + var split = new ProjectSettings + { + PendingFrames = + [ + new PendingFrame { Sha256 = Photo(2).Sha256, Size = Photo(2).Size, CapturedAt = Photo(2).CapturedAt, Excluded = true }, + new PendingFrame + { + Sha256 = Photo(20).Sha256, Size = Photo(20).Size, CapturedAt = Photo(2).CapturedAt, Hero = true, Keyframes = [new CameraKey(0, 0.8, 0.5, 1.2, 0)], + Extra = new Dictionary { ["rating"] = rating }, + }, + ], + }; + var text = ProjectContent.Canonical(split.ToJson()); + var now = new FramePhotos([(0, Photo(1)), (1, Photo(20)), (2, Photo(3))], [new PhotoAlias(Photo(2).Sha256!, Photo(20).Sha256!)]); + + var rebound = ProjectContent.Rebind(text, now); + var entry = Assert.NotNull(ProjectSettings.FromJson(rebound).PendingFrames).Single(); + Assert.Equal(Photo(20).Sha256, entry.Sha256); + Assert.True(entry.Excluded && entry.Hero); + Assert.Equal(0.8, entry.Keyframes.Single().CenterX); + Assert.Equal(4, entry.Extra!["rating"].GetInt32()); + // The same choices as the version made where the frames hold the new encoding. + Assert.True(ProjectContent.SameChoices(text, rebound, now)); + var placed = ProjectSettings.FromJson(ProjectContent.Place(rebound, now)); + Assert.SequenceEqual([1], placed.ExcludedFrames); + Assert.Equal((int?)1, placed.HeroFrame); + Assert.Equal(1, placed.Camera.Keyframes.Single().Frame); + // Two entries already named alike are one too. + var twice = ProjectContent.Canonical((split with { PendingFrames = [.. split.PendingFrames!.Select(p => p with { Sha256 = Photo(20).Sha256 })] }).ToJson()); + Assert.Equal(rebound, ProjectContent.Rebind(twice, now)); + } + + [Test] + public void MembersOfANewerChronosSurviveInsideParametersKeyframesAndCurvePoints() + { + var waiting = Photo(9).Sha256; + var json = $$""" + { + "version": 1, + "timing": { "remapCurve": [ { "x": 0, "y": 0, "handle": 0.25 }, { "x": 1, "y": 1 } ] }, + "exposure": { "deflicker": { "isAuto": true, "hasAdvice": true, "autoValue": true, "confidence": 0.8 } }, + "camera": { "keyframes": [ { "frame": 1, "centerX": 0.4, "centerY": 0.5, "zoom": 1.2, "rotation": 0, "tilt": 3 } ] }, + "pendingFrames": [ { "sha256": "{{waiting}}", "size": 1009, "keyframes": [ { "frame": 0, "centerX": 0.2, "centerY": 0.5, "zoom": 1, "rotation": 0, "tilt": 5 } ] } ] + } + """; + var settings = ProjectSettings.FromJson(json); + Assert.Equal(0.8, settings.Exposure.Deflicker.Extra!["confidence"].GetDouble()); + Assert.Equal(3, settings.Camera.Keyframes.Single().Extra!["tilt"].GetInt32()); + Assert.Equal(0.25, settings.Timing.RemapCurve[0].Extra!["handle"].GetDouble()); + // An edit of the parameter or the keyframe keeps them too. + Assert.Equal(0.8, settings.Exposure.Deflicker.WithManual(false).Extra!["confidence"].GetDouble()); + Assert.Equal(3, (settings.Camera.Keyframes[0] with { Zoom = 2 }).Extra!["tilt"].GetInt32()); + + var frames = Frames(Photo(1), Photo(2), Photo(3)); + var portable = ProjectContent.ToPortable(json, frames); + var placed = ProjectContent.Place(portable, frames); + foreach (var text in new[] { settings.ToJson(), portable, placed, ProjectContent.Canonical(portable), ProjectContent.Rebind(portable, frames) }) + { + var node = JsonNode.Parse(text)!; + Assert.Equal(0.8, node["exposure"]!["deflicker"]!["confidence"]!.GetValue()); + Assert.Equal(0.25, node["timing"]!["remapCurve"]![0]!["handle"]!.GetValue()); + var waits = node["pendingFrames"]!.AsArray().Single(p => p!["sha256"]!.GetValue() == waiting)!; + Assert.Equal(5, waits["keyframes"]![0]!["tilt"]!.GetValue()); + } + // The keyframe's member goes where the keyframe goes: parked with its photo, then placed again. + var parked = JsonNode.Parse(portable)!["pendingFrames"]!.AsArray().Single(p => p!["sha256"]!.GetValue() == Photo(2).Sha256)!; + Assert.Equal(3, parked["keyframes"]![0]!["tilt"]!.GetValue()); + Assert.Equal(3, JsonNode.Parse(placed)!["camera"]!["keyframes"]![0]!["tilt"]!.GetValue()); + Assert.Equal(portable, ProjectContent.ToPortable(placed, frames)); + } + + [Test] + public void AWaitingPhotoKeepsANewerChronosMembersOnceItsPositionsArePlaced() + { + var frames = Frames(Photo(1), Photo(2), Photo(3)); + var portable = ProjectContent.Canonical($$"""{ "pendingFrames": [ { "sha256": "{{Photo(2).Sha256}}", "size": {{Photo(2).Size}}, "excluded": true, "rating": 5 } ] }"""); + + var placed = ProjectSettings.FromJson(ProjectContent.Place(portable, frames)); + Assert.SequenceEqual([1], placed.ExcludedFrames); + var kept = Assert.NotNull(placed.PendingFrames).Single(); + Assert.False(kept.Excluded || kept.Hero || kept.Keyframes.Count > 0); + Assert.Equal(5, kept.Extra!["rating"].GetInt32()); + // Parked again, the rating is still the photo's: the same text as before. + Assert.Equal(portable, ProjectContent.ToPortable(placed, frames)); + // Nothing more to place: the settings stay as they are. + Assert.Equal(placed.ToJson(), ProjectContent.Place(placed.ToJson(), frames)); + } + [Test] public void TheSameSettingsGiveTheSameTextAndHashWhateverTheFrames() { diff --git a/tests/Chronos.Data.Tests/Projects/ProjectMergeTests.cs b/tests/Chronos.Data.Tests/Projects/ProjectMergeTests.cs new file mode 100644 index 00000000..9ce45bdc --- /dev/null +++ b/tests/Chronos.Data.Tests/Projects/ProjectMergeTests.cs @@ -0,0 +1,192 @@ +using System.Globalization; +using System.Text.Json.Nodes; +using Chronos.Core.Model; +using Chronos.Core.Project; +using Chronos.Data.Projects; +using Chronos.Data.Tests.Archiving; +using Chronos.Data.Tests.Infrastructure; +using Chronos.Testing; + +namespace Chronos.Data.Tests.Projects; + +/// The merge of projects (owner review 2, R8): one photo under two names, and settings of a newer Chronos. +public sealed class ProjectMergeTests +{ + private static readonly DateTime Start = new(2026, 9, 28, 18, 0, 0); + + private static FramePhoto Photo(int n) => new(n.ToString("x64", CultureInfo.InvariantCulture), 1000 + n, Start.AddSeconds(10 * n)); + + private static PendingFrame Waiting(FramePhoto photo, bool excluded = false, bool hero = false) => + new() { Sha256 = photo.Sha256, Size = photo.Size, CapturedAt = photo.CapturedAt, Excluded = excluded, Hero = hero }; + + [Test] + public void EntriesOfOnePhotoMergeAsOneWithThePositionsOfBoth() + { + var frames = new FramePhotos([(0, Photo(1)), (1, Photo(2)), (2, Photo(3))]); + var basis = new ProjectSettings(); + // A version stored with an exclusion and the hero of one photo in two entries (renamed alike since they were saved). + var ours = new ProjectSettings { PendingFrames = [Waiting(Photo(2), excluded: true), Waiting(Photo(2), hero: true)] }; + var theirs = basis with { Stack = new StackSettings { Mode = StackSetting.Median } }; + + var merged = ProjectMerge.MergeWithConflicts(basis, ours, theirs); + Assert.Empty(merged.Conflicts); + var entry = Assert.NotNull(merged.Merged.PendingFrames).Single(); + Assert.True(entry.Excluded && entry.Hero); + Assert.Equal(StackSetting.Median, merged.Merged.Stack.Mode); + var placed = ProjectSettings.FromJson(ProjectContent.Place(merged.Merged.ToJson(), frames)); + Assert.SequenceEqual([1], placed.ExcludedFrames); + Assert.Equal((int?)1, placed.HeroFrame); + + // The same through the merge's other side, and when the other side changed the photo's flags. + Assert.True(ProjectMerge.Merge(basis, theirs, ours).PendingFrames!.Single() is { Excluded: true, Hero: true }); + var unexcluded = new ProjectSettings { PendingFrames = [Waiting(Photo(2), hero: true)] }; + var both = ProjectMerge.MergeWithConflicts(ours, ours, unexcluded); + Assert.Empty(both.Conflicts); + Assert.True(both.Merged.PendingFrames!.Single() is { Excluded: false, Hero: true }); + } + + [Test] + public void APhotoNamedAlikeOnEverySideMergesAsOnePhoto() + { + // Saved here before the photo was hashed (by size and capture time) and stored at the home after (by SHA-256): once each + // side names it as the frames hold it now, undoing the exclusion here is a change of this side only. + var unhashed = new FramePhoto(null, Photo(2).Size, Photo(2).CapturedAt); + var frames = new FramePhotos([(0, Photo(1)), (1, Photo(2)), (2, Photo(3))]); + var basis = new ProjectSettings { PendingFrames = [Waiting(unhashed, excluded: true)] }; + var theirs = new ProjectSettings { PendingFrames = [Waiting(Photo(2), excluded: true)], Exposure = new ExposureSettings { ExposureOffsetEv = 0.7 } }; + var ours = new ProjectSettings(); + + ProjectSettings Named(ProjectSettings settings) => ProjectSettings.FromJson(ProjectContent.Rebind(ProjectContent.Canonical(settings.ToJson()), frames)); + var merged = ProjectMerge.MergeWithConflicts(Named(basis), Named(ours), Named(theirs)); + Assert.Empty(merged.Conflicts); + Assert.Null(merged.Merged.PendingFrames); + Assert.Equal(0.7, merged.Merged.Exposure.ExposureOffsetEv); + } + + [Test] + public void SettingsOfANewerChronosAreNeverMergedWithThisOnes() + { + var basis = new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 0.1 } }; + var newer = ProjectSettings.FromJson("""{ "version": 2, "exposure": { "exposureOffsetEv": 0.5 }, "grade": { "look": "film" } }""", allowNewer: true); + var mine = basis with { Stack = new StackSettings { Mode = StackSetting.Median } }; + + // Both changed: refused, whichever side the newer one is, in a replay too. + Assert.Throws(() => ProjectMerge.MergeWithConflicts(basis, mine, newer)); + Assert.Throws(() => ProjectMerge.Merge(basis, newer, mine)); + Assert.Throws(() => ProjectMerge.Replay(basis, [mine], newer)); + Assert.Throws(() => new ProjectMerge.Rebase(basis, newer).Apply(mine)); + + // A side that did not change takes the other's as they are: nothing is merged. + var taken = ProjectMerge.MergeWithConflicts(basis, basis, newer); + Assert.Empty(taken.Conflicts); + Assert.True(taken.Merged.IsNewerFormat); + Assert.Equal(newer.ToJson(), taken.Merged.ToJson()); + Assert.Equal(mine.ToJson(), ProjectMerge.Merge(newer, mine, newer).ToJson()); + Assert.Equal(basis.ToJson(), ProjectMerge.Merge(newer, newer, basis).ToJson()); + Assert.Equal(newer.ToJson(), ProjectMerge.Replay(basis, [basis], newer).Commits.Single().ToJson()); + } +} + +/// A newer Chronos sharing the archive (owner review 2, R8): what it writes when it stores a version. +internal static class NewerChronos +{ + /// + /// A version of settings format + 1 on top of , written + /// as that Chronos's push writes it: the version file, then the record's head (the index adopts it from the sidecars). + /// It holds a member this Chronos does not know (a grade) and an exposure offset of 1.5. + /// + public static ProjectVersion Push(ArchiveFixture fx, Sequence sequence, ProjectVersion head) + { + var folder = fx.Paths.Sequence(sequence.Folder); + var settings = JsonNode.Parse(head.SettingsJson)!.AsObject(); + settings["version"] = ProjectSettings.CurrentVersion + 1; + settings["grade"] = new JsonObject { ["look"] = "film" }; + settings["exposure"] = new JsonObject { ["exposureOffsetEv"] = 1.5 }; + var text = ProjectContent.Canonical(settings.ToJsonString()); + var version = new ProjectVersion + { + Id = Guid.CreateVersion7(), + ProjectId = head.ProjectId, + ParentId = head.Id, + Number = head.Number + 1, + Kind = ProjectVersionKind.Commit, + ClientName = "a newer Chronos", + SettingsJson = text, + ContentHash = ProjectContent.Hash(text), + SettingsFormat = ProjectSettings.CurrentVersion + 1, + FramesVersion = head.FramesVersion, + CreatedAt = DateTimeOffset.UtcNow, + StoredAt = DateTimeOffset.UtcNow, + }; + ProjectSidecars.WriteVersion(folder.VersionPath(head.ProjectId, version.Number, version.Id), version); + var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(head.ProjectId)); + ProjectSidecars.WriteRecord(folder.ProjectRecordPath(head.ProjectId), record with { Head = new ProjectHead(version.Id, version.Number), Updated = DateTimeOffset.UtcNow }); + return version; + } +} + +/// Main's catch-up merge (owner review 2, R8): a mirror cut short meets settings written meanwhile. +[Category("projects")] +public abstract class MainMergeTests(TestContext context) : DatabaseTest(context) +{ + private async Task FixtureAsync() => Own(await ArchiveFixture.CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true))); + + [Test] + public async Task TheValuesTheMergeLeavesOutStayInMainsHistory() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await ProjectServiceTests.UploadedAsync(fx, 4); + var projects = fx.Service.Projects; + var main = await projects.MainAsync(sequence.Id, fx.Ct); + var first = await projects.HeadAsync(main.Id, fx.Ct); + var pushed = (await projects.PushAsync(main.Id, ProjectServiceTests.Draft(first, new ProjectSettings + { + Stack = new StackSettings { Mode = StackSetting.Median }, + Exposure = new ExposureSettings { ExposureOffsetEv = 0.5 }, + }, frames), fx.Ct)).Version!; + + // The push's mirror cut short, and another path writes an offset of its own and an exclusion meanwhile. + var reverted = await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings()); + await fx.Db.Projects.SetMirroredAsync(main.Id, first.Id, reverted.SettingsVersion, fx.Ct); + await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 1.25 }, ExcludedFrames = [3] }); + + // The pushed version wins where both changed; the other writer's settings are the version before the merge. + var history = await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct); + Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind)); + Assert.Equal((history[1].Id, pushed.Id), (history[0].ParentId, history[1].ParentId)); + var external = ProjectSettings.FromJson(ProjectContent.Place(history[1].SettingsJson, ProjectServiceTests.Photos(frames))); + Assert.Equal((1.25, StackSetting.None), (external.Exposure.ExposureOffsetEv, external.Stack.Mode)); + Assert.SequenceEqual([3], external.ExcludedFrames); + var settings = ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!); + Assert.Equal((0.5, StackSetting.Median), (settings.Exposure.ExposureOffsetEv, settings.Stack.Mode)); + Assert.SequenceEqual([3], settings.ExcludedFrames); + } + + [Test] + public async Task ANewerChronosHeadOfMainIsNotMergedWithSettingsWrittenMeanwhile() + { + var fx = await FixtureAsync(); + var (sequence, _) = await ProjectServiceTests.UploadedAsync(fx, 3); + var projects = fx.Service.Projects; + var main = await projects.MainAsync(sequence.Id, fx.Ct); + var newer = NewerChronos.Push(fx, sequence, await projects.HeadAsync(main.Id, fx.Ct)); + await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { Stack = new StackSettings { Mode = StackSetting.Mean } }); + + // Both are left as they are: the newer head stays the head, the settings stay those written. + Assert.Equal(newer.Id, (await projects.HeadAsync(main.Id, fx.Ct)).Id); + Assert.Count(2, await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)); + var settings = ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!); + Assert.Equal((StackSetting.Mean, 0.0), (settings.Stack.Mode, settings.Exposure.ExposureOffsetEv)); + } +} + +public sealed class SqliteMainMergeTests(TestContext context) : MainMergeTests(context) +{ + protected override Backend Backend => Backend.SqliteFile; +} + +[RequiresEnvironment(PgTestServer.Variable)] +public sealed class PostgresMainMergeTests(TestContext context) : MainMergeTests(context) +{ + protected override Backend Backend => Backend.Postgres; +} diff --git a/tests/Chronos.Data.Tests/Workspaces/WorkspaceMergeTests.cs b/tests/Chronos.Data.Tests/Workspaces/WorkspaceMergeTests.cs new file mode 100644 index 00000000..d36d43ce --- /dev/null +++ b/tests/Chronos.Data.Tests/Workspaces/WorkspaceMergeTests.cs @@ -0,0 +1,287 @@ +using Chronos.Core.Archiving; +using Chronos.Core.Model; +using Chronos.Core.Project; +using Chronos.Data.Archiving; +using Chronos.Data.Projects; +using Chronos.Data.Tests.Archiving; +using Chronos.Data.Tests.Infrastructure; +using Chronos.Data.Tests.Projects; +using Chronos.Data.Workspaces; +using Chronos.Testing; + +namespace Chronos.Data.Tests.Workspaces; + +/// +/// What a workspace's sync merges (owner review 2, R8): one photo named alike on every side whatever the home did to it since +/// the copy (hashed, converted), and settings of a newer Chronos never merged with this one's. +/// +[Category("workspace")] +public abstract class WorkspaceMergeTests(TestContext context) : DatabaseTest(context) +{ + private async Task FixtureAsync(int frames = 5) + { + var archive = Own(await ArchiveFixture.CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true))); + return Own(await WorkspaceFixture.CreateAsync(Context, archive, frames)); + } + + private static async Task EditAsync(WorkspaceSync sync, Guid projectId, Func edit, CancellationToken ct) + { + var session = await sync.OpenAsync(projectId, ct); + await sync.SaveAsync(projectId, edit(session.Working), ct); + } + + /// The photo at not hashed at the home, as in a folder adopted and not verified yet: its sidecar, then a full scan. + private static async Task UnhashAsync(WorkspaceFixture fx, int index) + { + var folder = fx.Archive.Paths.Sequence(fx.Sequence.Folder); + var manifest = ManifestStore.ReadSequence(folder.ManifestPath); + ManifestStore.WriteSequence(folder.ManifestPath, manifest with { Frames = [.. manifest.Frames.Select((f, i) => i == index ? f with { Sha256 = null } : f)] }); + await fx.Archive.ScanAsync(ScanMode.Full); + Assert.Null((await fx.Db.Frames.ListBySequenceAsync(fx.Sequence.Id))[index].Sha256); + } + + /// The archive verified: the photos not hashed yet are hashed (the frames version stays, so no Update media is offered). + private static async Task VerifyAsync(WorkspaceFixture fx) + { + var before = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!.FramesVersion; + await fx.Archive.Service.Verifier.VerifyAsync(fx.Archive.Archive, null, default, (_, _, _) => ValueTask.CompletedTask, null, fx.Ct); + Assert.Equal(before, (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!.FramesVersion); + } + + /// The sequence's settings changed at the home by another path (an analysis): Main takes them as an external version, its photos named as the home's frames hold them. + private static async Task WriteAtHomeAsync(WorkspaceFixture fx, Func edit) + { + var row = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!; + await ProjectServiceTests.WriteSettingsAsync(fx.Archive, fx.Sequence.Id, edit(ProjectSettings.FromJson(row.SettingsJson!))); + } + + private static async Task SequenceSettingsAsync(WorkspaceFixture fx) => + ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!.SettingsJson!); + + [Test] + public async Task AnEditSurvivesAPullOnceTheHomeHashedAPhotoTheCopyHadNot() + { + var fx = await FixtureAsync(); + await UnhashAsync(fx, 2); + var (_, sync) = fx.Workspace(); + var project = await fx.CheckoutAsync(sync); + var id = project.ProjectId; + var copied = project.ReadMedia()!; + Assert.Null(copied.Frames[2].Sha256); + await EditAsync(sync, id, s => s with { ExcludedFrames = [2] }, fx.Ct); + await sync.CommitAsync(id, cancellationToken: fx.Ct); + await sync.PushAsync(id, fx.Home, fx.Ct); + + // The home hashes the photo, then an analysis writes the settings: Main's new version names the photo by its hash. + await VerifyAsync(fx); + var hashed = (await fx.Db.Frames.ListBySequenceAsync(fx.Sequence.Id))[2].Sha256!; + await WriteAtHomeAsync(fx, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.7 } }); + Assert.Contains(hashed, (await fx.Home.HeadAsync(id, fx.Ct)).SettingsJson); + + // Photo 2 brought back here: the pull takes the analysis's value and keeps the photo in, with nothing to settle. + await EditAsync(sync, id, s => s with { ExcludedFrames = [] }, fx.Ct); + var pulled = await sync.PullAsync(id, fx.Home, fx.Ct); + Assert.Null(pulled.Status.Conflict); + Assert.Equal(WorkspaceState.Modified, pulled.Status.State); + var working = (await sync.OpenAsync(id, fx.Ct)).Working; + Assert.Equal(0.7, working.Exposure.ExposureOffsetEv); + Assert.Empty(working.ExcludedFrames); + // The media learnt the hash: the frames and their files are as they were copied. + var media = project.ReadMedia()!; + Assert.Equal(hashed, media.Frames[2].Sha256); + Assert.Equal(copied.FramesVersion, media.FramesVersion); + Assert.SequenceEqual(copied.Frames.Select(f => f.FileSha256), media.Frames.Select(f => f.FileSha256)); + + Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct)); + Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State); + var settings = await SequenceSettingsAsync(fx); + Assert.Empty(settings.ExcludedFrames); + Assert.Equal(0.7, settings.Exposure.ExposureOffsetEv); + } + + [Test] + public async Task APullWithNothingEditedHereIsInSyncOnceTheHomeHashedAPhoto() + { + var fx = await FixtureAsync(); + await UnhashAsync(fx, 2); + var (_, sync) = fx.Workspace(); + var id = (await fx.CheckoutAsync(sync)).ProjectId; + await EditAsync(sync, id, s => s with { ExcludedFrames = [2] }, fx.Ct); + await sync.CommitAsync(id, cancellationToken: fx.Ct); + await sync.PushAsync(id, fx.Home, fx.Ct); + await VerifyAsync(fx); + await WriteAtHomeAsync(fx, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.7 } }); + + var pulled = await sync.PullAsync(id, fx.Home, fx.Ct); + Assert.Equal(WorkspaceState.InSync, pulled.Status.State); + Assert.SequenceEqual([2], (await sync.OpenAsync(id, fx.Ct)).Working.ExcludedFrames); + Assert.Null(await sync.CommitAsync(id, cancellationToken: fx.Ct)); + } + + [Test] + public async Task AVersionSavedBeforeAnotherWorkspaceNamedThePhotoByItsHashKeepsItsEdit() + { + var fx = await FixtureAsync(); + await UnhashAsync(fx, 2); + var (_, one) = fx.Workspace("one"); + var id = (await fx.CheckoutAsync(one)).ProjectId; + await EditAsync(one, id, s => s with { ExcludedFrames = [2] }, fx.Ct); + await one.CommitAsync(id, cancellationToken: fx.Ct); + await one.PushAsync(id, fx.Home, fx.Ct); + await VerifyAsync(fx); + + // Two copies the media once hashed and changes the stack. + var (_, two) = fx.Workspace("two"); + await fx.CheckoutAsync(two, id); + await EditAsync(two, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct); + await two.CommitAsync(id, cancellationToken: fx.Ct); + await two.PushAsync(id, fx.Home, fx.Ct); + + // One brings photo 2 back as a version of its own, then syncs: the version replayed still brings it back. + await EditAsync(one, id, s => s with { ExcludedFrames = [] }, fx.Ct); + await one.CommitAsync(id, "bring 2 back", cancellationToken: fx.Ct); + var pushed = await one.PushAsync(id, fx.Home, fx.Ct); + Assert.Equal(WorkspaceState.InSync, pushed.Status.State); + Assert.Equal("bring 2 back", pushed.Pushed.Single().Note); + var settings = await SequenceSettingsAsync(fx); + Assert.Empty(settings.ExcludedFrames); + Assert.Equal(StackSetting.Mean, settings.Stack.Mode); + } + + [Test] + public async Task APhotoConvertedAtTheHomeBeforeUpdateMediaMergesAsOnePhotoAndKeepMineKeepsMine() + { + var fx = await FixtureAsync(4); + var (_, sync) = fx.Workspace(); + var project = await fx.CheckoutAsync(sync); + var id = project.ProjectId; + await EditAsync(sync, id, s => s with { ExcludedFrames = [1], Camera = s.Camera with { Keyframes = [new CameraKey(1, 0.2, 0.5, 1.2, 0)] } }, fx.Ct); + await sync.CommitAsync(id, cancellationToken: fx.Ct); + await sync.PushAsync(id, fx.Home, fx.Ct); + + // Photo 1 converted at the home (the media still hold the encoding copied), then the home moves its keyframe. + await ProjectServiceTests.ConvertAsync(fx.Archive, fx.Sequence.Id, [1], 700); + await WriteAtHomeAsync(fx, s => s with + { + Exposure = s.Exposure with { ExposureOffsetEv = 0.7 }, + Camera = s.Camera with { Keyframes = [new CameraKey(1, 0.5, 0.5, 1.2, 0)] }, + }); + + // Here photo 1 is brought back and its keyframe moved elsewhere: the keyframe is the one thing both changed. + await EditAsync(sync, id, s => s with { ExcludedFrames = [], Camera = s.Camera with { Keyframes = [new CameraKey(1, 0.8, 0.5, 1.2, 0)] } }, fx.Ct); + var pulled = await sync.PullAsync(id, fx.Home, fx.Ct); + var difference = Assert.NotNull(pulled.Status.Conflict).Differences.Single(); + Assert.True(difference.Path.EndsWith(".keyframes", StringComparison.Ordinal), difference.Path); + Assert.Contains(project.ReadMedia()!.Frames[1].Sha256!, difference.Path); + + await sync.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct); + var working = (await sync.OpenAsync(id, fx.Ct)).Working; + Assert.Empty(working.ExcludedFrames); + Assert.Equal((1, 0.8), (working.Camera.Keyframes.Single().Frame, working.Camera.Keyframes.Single().CenterX)); + Assert.Equal(0.7, working.Exposure.ExposureOffsetEv); + await sync.CommitAsync(id, cancellationToken: fx.Ct); + Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State); + var settings = await SequenceSettingsAsync(fx); + Assert.Empty(settings.ExcludedFrames); + Assert.Equal((1, 0.8), (settings.Camera.Keyframes.Single().Frame, settings.Camera.Keyframes.Single().CenterX)); + } + + /// What a newer Chronos sharing the archive writes for a push: its version file after the head, then the record's head. + private static ProjectVersion NewerPush(WorkspaceFixture fx, ProjectVersion head) => NewerChronos.Push(fx.Archive, fx.Sequence, head); + + [Test] + public async Task LocalVersionsMeetingANewerChronosHeadStayAsTheyAreAndSaveBothSendsThem() + { + var fx = await FixtureAsync(3); + var (_, sync) = fx.Workspace(); + var named = await fx.Archive.Service.Projects.CreateAsync(fx.Sequence.Id, "Grade", null, null, fx.Ct); + var project = await fx.CheckoutAsync(sync, named.Id); + var id = project.ProjectId; + await EditAsync(sync, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.3 } }, fx.Ct); + var local = Assert.NotNull(await sync.CommitAsync(id, "offset", cancellationToken: fx.Ct)); + await EditAsync(sync, id, s => s with { ExcludedFrames = [1] }, fx.Ct); + var newer = NewerPush(fx, await fx.Home.HeadAsync(id, fx.Ct)); + Assert.Equal(newer.Id, (await fx.Home.HeadAsync(id, fx.Ct)).Id); + + // Nothing is merged into the newer settings: the local version, the working state and the base stay; a conflict of format waits. + var stopped = await sync.PushAsync(id, fx.Home, fx.Ct); + Assert.Empty(stopped.Pushed); + var conflict = Assert.NotNull(stopped.Status.Conflict); + Assert.True(conflict.NewerFormat); + Assert.Equal((WorkspaceState.Conflict, 1, false), (stopped.Status.State, stopped.Status.Ahead, stopped.Status.ReadOnly)); + Assert.Equal(local.Id, sync.Unpushed(id).Single().Id); + Assert.Equal(local.SettingsJson, sync.Unpushed(id).Single().SettingsJson); + var working = (await sync.OpenAsync(id, fx.Ct)).Working; + Assert.Equal((ProjectSettings.CurrentVersion, 0.3), (working.Version, working.Exposure.ExposureOffsetEv)); + Assert.SequenceEqual([1], working.ExcludedFrames); + Assert.Equal(WorkspaceState.Conflict, (await sync.PullAsync(id, fx.Home, fx.Ct)).Status.State); + + // Merging either way is refused and leaves everything as it was. + var refused = await Assert.ThrowsAsync(() => sync.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct)); + Assert.Equal(PushOutcome.NewerFormat, refused.Outcome); + await Assert.ThrowsAsync(() => sync.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, cancellationToken: fx.Ct)); + Assert.True((await sync.StatusAsync(id, fx.Home, fx.Ct)).Conflict!.NewerFormat); + Assert.Equal(local.Id, sync.Unpushed(id).Single().Id); + + // Save both: this workspace's work (the change not saved yet too) goes to a new project in this Chronos's format. + var both = await sync.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct); + var copy = Assert.NotNull(both.SavedAs); + Assert.Equal(2, both.Pushed.Count); + var kept = ProjectSettings.FromJson(ProjectContent.Place((await fx.Home.HeadAsync(copy.Id, fx.Ct)).SettingsJson, ProjectServiceTests.Photos(fx.Frames))); + Assert.Equal(0.3, kept.Exposure.ExposureOffsetEv); + Assert.SequenceEqual([1], kept.ExcludedFrames); + // This project shows the newer Chronos's head, read-only. + Assert.True(both.Status.ReadOnly); + Assert.Equal(newer.Id, both.Status.Base!.VersionId); + Assert.Equal(1.5, (await sync.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv); + } + + [Test] + public async Task AChangeNotSavedYetMeetingANewerChronosHeadWaitsOnAConflict() + { + var fx = await FixtureAsync(3); + var (_, sync) = fx.Workspace(); + var named = await fx.Archive.Service.Projects.CreateAsync(fx.Sequence.Id, "Grade", null, null, fx.Ct); + var id = (await fx.CheckoutAsync(sync, named.Id)).ProjectId; + await EditAsync(sync, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.7 } }, fx.Ct); + NewerPush(fx, await fx.Home.HeadAsync(id, fx.Ct)); + + var pulled = await sync.PullAsync(id, fx.Home, fx.Ct); + Assert.True(Assert.NotNull(pulled.Status.Conflict).NewerFormat); + Assert.Equal((WorkspaceState.Conflict, true, false), (pulled.Status.State, pulled.Status.Modified, pulled.Status.ReadOnly)); + Assert.Equal(0.7, (await sync.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv); + + var both = await sync.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct); + Assert.Equal(0.7, ProjectSettings.FromJson(Assert.NotNull(both.Pushed.SingleOrDefault()).SettingsJson).Exposure.ExposureOffsetEv); + Assert.True(both.Status.ReadOnly); + } + + [Test] + public async Task ANewerChronosHeadOverNothingOfThisWorkspacesIsTakenReadOnly() + { + var fx = await FixtureAsync(3); + var (_, sync) = fx.Workspace(); + var named = await fx.Archive.Service.Projects.CreateAsync(fx.Sequence.Id, "Grade", null, null, fx.Ct); + var id = (await fx.CheckoutAsync(sync, named.Id)).ProjectId; + var newer = NewerPush(fx, await fx.Home.HeadAsync(id, fx.Ct)); + + var pulled = await sync.PullAsync(id, fx.Home, fx.Ct); + Assert.Null(pulled.Status.Conflict); + Assert.Equal((WorkspaceState.InSync, true), (pulled.Status.State, pulled.Status.ReadOnly)); + Assert.Equal(newer.Id, pulled.Status.Base!.VersionId); + var session = await sync.OpenAsync(id, fx.Ct); + Assert.True(session.IsReadOnly); + Assert.Equal("film", session.Working.Extra!["grade"].GetProperty("look").GetString()); + } +} + +public sealed class SqliteWorkspaceMergeTests(TestContext context) : WorkspaceMergeTests(context) +{ + protected override Backend Backend => Backend.SqliteFile; +} + +[RequiresEnvironment(PgTestServer.Variable)] +public sealed class PostgresWorkspaceMergeTests(TestContext context) : WorkspaceMergeTests(context) +{ + protected override Backend Backend => Backend.Postgres; +}