fix(sync): keep project positions on their photos when settings cross copies

Settings synced between a PC and its server sequence were stored as they
came, in the other copy's frame order. When the inserted photos arrived
later (a download, an upload), the index moved those already-correct
positions a second time, so exclusions, the hero and camera keyframes
landed on other photos (V-21, CD-2).

- Settings are translated by photo (SHA-256, else size and capture time)
  to the frames of the copy they go to: the server's before they are
  compared or applied here, this PC's to the server's frames as its
  manifest lists them before a push (SyncPositions).
- A position on a photo the receiving copy lacks waits in project.json as
  a pending frame and takes its place when the photo arrives, on an
  insertion or an append in step with the index; the Library's hero
  frame follows. A hero chosen meanwhile wins, and advice does not fill
  a hero while one waits. Old projects without the member are unchanged.
- Pushes send expectedFramesVersion; PATCH /api/v1/sequences/{id} answers
  409 frames_conflict when the frames changed since, and the next
  comparison translates again.
- A pulled payload equal to the local copy is settled without the
  archive: an offline or read-only archive no longer fails the
  comparison (V-10).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 16:20:11 +00:00
1 parent 32a0e9bbc1
commit ea6e1a69e0
25 files changed
+1091 -51

No files matched your search

@@ -167,6 +167,119 @@ public sealed class ProjectSettingsTests
Assert.Null(ProjectSettings.MoveFramePositions("""{ "heroFrame": 1, "HeroFrame": 1 }""", new Dictionary<int, int> { [1] = 2 }));
}
[Test]
public void PositionsCrossToAnotherCopyByPhotoAndWaitForPhotosItLacks()
{
// The server holds six photos; this copy only the last four, and one of its own the server lacks.
var photos = Enumerable.Range(0, 7).Select(Photo).ToList();
var server = Frames(photos[..6]);
var here = Frames([.. photos[2..6], photos[6]]);
var project = new ProjectSettings
{
ExcludedFrames = [0, 3],
HeroFrame = 1,
Camera = new CameraSettings { Keyframes = [new CameraKey(0, 0.5, 0.5, 1, 0), new CameraKey(4, 0.4, 0.5, 1.5, 0)] },
Exposure = new ExposureSettings { DeflickerWindow = new Auto<int>().WithManual(41) },
};
var taken = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(project.ToJson(), server, here)));
Assert.SequenceEqual([1], taken.ExcludedFrames);
Assert.Null(taken.HeroFrame);
Assert.SequenceEqual([2], taken.Camera.Keyframes.Select(k => k.Frame));
Assert.Equal(1.5, taken.Camera.Keyframes[0].Zoom);
Assert.Equal(41, taken.Exposure.DeflickerWindow.Manual);
var pending = Assert.NotNull(taken.PendingFrames);
Assert.Equal(2, pending.Count);
var first = Assert.Contains(pending, p => p.Sha256 == photos[0].Sha256);
Assert.True(first.Excluded && !first.Hero);
Assert.Count(1, first.Keyframes);
Assert.Equal(0.5, first.Keyframes[0].CenterX);
Assert.True(Assert.Contains(pending, p => p.Sha256 == photos[1].Sha256).Hero);
Assert.True(taken.HeroPending);
// The two photos arrive (inserted before the others): what waited takes its place, and the rest moved with its frames.
var arrived = Frames(photos);
var moved = ProjectSettings.MoveFramePositions(taken.ToJson(), new Dictionary<int, int> { [0] = 2, [1] = 3, [2] = 4, [3] = 5, [4] = 6 })!;
var placed = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(moved, null, arrived)));
Assert.SequenceEqual([0, 3], placed.ExcludedFrames);
Assert.Equal(1, placed.HeroFrame);
Assert.SequenceEqual([0, 4], placed.Camera.Keyframes.Select(k => k.Frame));
Assert.Null(placed.PendingFrames);
Assert.Null(ProjectSettings.TranslateFramePositions(placed.ToJson(), null, arrived));
}
[Test]
public void SettingsSentBackLandOnTheSamePhotos()
{
var photos = Enumerable.Range(0, 5).Select(Photo).ToList();
var server = Frames(photos[1..]);
var here = Frames(photos);
// Photo 0 is only here: its exclusion waits on the server, and comes back as a position.
var project = new ProjectSettings { ExcludedFrames = [0, 2], HeroFrame = 4 };
var sent = Assert.NotNull(ProjectSettings.TranslateFramePositions(project.ToJson(), here, server));
var back = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(sent, server, here)));
Assert.SequenceEqual([1], ProjectSettings.FromJson(sent).ExcludedFrames);
Assert.Equal(3, ProjectSettings.FromJson(sent).HeroFrame);
Assert.Equal(project.ToJson(), back.ToJson());
}
[Test]
public void AHeroOrKeyframeChosenSinceIsNewerThanTheOneThatWaited()
{
var photos = Enumerable.Range(0, 3).Select(Photo).ToList();
var waiting = new ProjectSettings
{
HeroFrame = 2,
Camera = new CameraSettings { Keyframes = [new CameraKey(0, 0.2, 0.2, 2, 0)] },
PendingFrames = [new PendingFrame { Sha256 = photos[0].Sha256, Size = photos[0].Size, Hero = true, Keyframes = [new CameraKey(9, 0.7, 0.7, 1, 0)] }],
};
var placed = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(waiting.ToJson(), null, Frames(photos))));
Assert.Equal(2, placed.HeroFrame);
Assert.Count(1, placed.Camera.Keyframes);
Assert.Equal(0.2, placed.Camera.Keyframes[0].CenterX);
Assert.Null(placed.PendingFrames);
}
[Test]
public void AProjectNamingNoFrameCrossesAsItIs()
{
var json = new ProjectSettings { Exposure = new ExposureSettings { DeflickerWindow = new Auto<int>().WithManual(41) } }.ToJson();
Assert.False(ProjectSettings.NamesFrames(json));
Assert.False(json.Contains("pendingFrames", StringComparison.OrdinalIgnoreCase), "an old project reads and writes as it did");
Assert.False(ProjectSettings.NamesFrames("not json"));
Assert.True(ProjectSettings.NamesFrames("""{ "HeroFrame": 0 }"""));
Assert.Null(ProjectSettings.TranslateFramePositions(json, Frames([Photo(1)]), Frames([Photo(2)])));
Assert.Null(ProjectSettings.TranslateFramePositions("[1]", null, Frames([])));
}
[Test]
public void PhotosMatchByHashElseBySizeAndCaptureTime()
{
var time = new DateTime(2026, 9, 28, 18, 0, 0, 123);
var hashed = new FramePhoto(new string('a', 64), 100, time);
Assert.True(hashed.Matches(new FramePhoto(new string('A', 64), 1, null)));
Assert.False(hashed.Matches(new FramePhoto(new string('b', 64), 100, time)));
Assert.True(hashed.Matches(new FramePhoto("", 100, time.AddTicks(9_999))));
Assert.False(hashed.Matches(new FramePhoto(null, 100, time.AddMilliseconds(1))));
Assert.False(new FramePhoto(null, 100, null).Matches(new FramePhoto(null, 100, null)));
var frames = Frames([new FramePhoto(null, 100, time), hashed]);
Assert.Equal(1, frames.IndexOf(new FramePhoto(new string('a', 64), 100, null)));
Assert.Equal(0, frames.IndexOf(new FramePhoto(null, 100, time)));
Assert.Null(frames.IndexOf(new FramePhoto(null, 101, time)));
}
private static FramePhoto Photo(int seed) =>
new(Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData([(byte)seed])), 1000 + seed, new DateTime(2026, 9, 28, 18, 0, 0).AddSeconds(10 * seed));
private static FramePhotos Frames(IEnumerable<FramePhoto> photos) => new(photos.Select((p, i) => (i, p)));
[Test]
public void BuiltInPresetIdsAreUnique() =>
Assert.Equal(ExportPresets.BuiltIn.Count, ExportPresets.BuiltIn.Select(p => p.Id).Distinct().Count());