fix(archive): keep project frame positions on their frames after inserts

The project names frames by position (exclusions, hero, virtual-camera
keyframes), and an upload, import, scan or sync download of earlier photos
merges them by capture time and renumbers the frames after them; nothing
moved the project, so a render excluded the wrong photo and the project's
hero no longer matched the Library's.

project.json keeps its format: when the index takes a sidecar in which frames
were inserted before indexed ones, the index's copy of the project has its
positions moved from the indexed order to the new one in the same transaction
as the frame rows, and project.json mirrors it unless another writer changed
it meanwhile. The JSON is edited in place, so members a newer Titano wrote
survive; only an insertion is followed (if an indexed frame is gone, the
positions stay). The settings version stays, so a synced copy receiving the
same frames makes the same move (CD-2).

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 09:40:40 +00:00
1 parent 94b13cc064
commit 01e17c9961
7 files changed
+252 -3

No files matched your search

@@ -121,6 +121,52 @@ public sealed class ProjectSettingsTests
public void ProjectMustBeAnObject() =>
Assert.Throws<System.Text.Json.JsonException>(() => ProjectSettings.FromJson("[1, 2]"));
[Test]
public void FramePositionsFollowInsertedFramesAndOtherMembersStay()
{
var project = new ProjectSettings
{
ExcludedFrames = [3, 7, 40],
HeroFrame = 2,
Camera = new CameraSettings { Keyframes = [new CameraKey(0, 0.5, 0.5, 1, 0), new CameraKey(7, 0.4, 0.5, 1.5, 0)] },
Exposure = new ExposureSettings { DeflickerWindow = new Auto<int>().WithManual(41) },
};
// Two frames inserted before position 3 and one before position 7; 40 is no frame of the old list and stays.
var moves = new Dictionary<int, int> { [0] = 0, [1] = 1, [2] = 4, [3] = 5, [4] = 6, [5] = 7, [6] = 8, [7] = 10 };
var moved = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.MoveFramePositions(project.ToJson(), moves)));
Assert.SequenceEqual([5, 10, 40], moved.ExcludedFrames);
Assert.Equal(4, moved.HeroFrame);
Assert.SequenceEqual([0, 10], moved.Camera.Keyframes.Select(k => k.Frame));
Assert.Equal((0.4, 1.5), (moved.Camera.Keyframes[1].CenterX, moved.Camera.Keyframes[1].Zoom));
Assert.Equal(41, moved.Exposure.DeflickerWindow.Manual);
}
[Test]
public void MovingPositionsEditsTheJsonInPlace()
{
const string project = """{ "ExcludedFrames": [1, 2], "heroFrame": null, "futureOption": { "keep": [1, 2] }, "camera": { "keyframes": [ { "frame": 1.5 } ] } }""";
var moved = Assert.NotNull(ProjectSettings.MoveFramePositions(project, new Dictionary<int, int> { [1] = 3 }));
var json = System.Text.Json.Nodes.JsonNode.Parse(moved)!;
Assert.SequenceEqual([3, 2], json["ExcludedFrames"]!.AsArray().Select(n => n!.GetValue<int>()));
Assert.Equal("""{"keep":[1,2]}""", json["futureOption"]!.ToJsonString());
Assert.Null(json["heroFrame"]);
Assert.Equal(1.5, json["camera"]!["keyframes"]![0]!["frame"]!.GetValue<double>());
}
[Test]
public void NothingToMoveGivesNoNewProject()
{
Assert.Null(ProjectSettings.MoveFramePositions(new ProjectSettings { ExcludedFrames = [1] }.ToJson(), new Dictionary<int, int>()));
Assert.Null(ProjectSettings.MoveFramePositions(new ProjectSettings { ExcludedFrames = [1] }.ToJson(), new Dictionary<int, int> { [2] = 3, [1] = 1 }));
Assert.Null(ProjectSettings.MoveFramePositions("not json", new Dictionary<int, int> { [1] = 2 }));
Assert.Null(ProjectSettings.MoveFramePositions("[1, 2]", new Dictionary<int, int> { [1] = 2 }));
Assert.Null(ProjectSettings.MoveFramePositions("""{ "heroFrame": 1, "HeroFrame": 1 }""", new Dictionary<int, int> { [1] = 2 }));
}
[Test]
public void BuiltInPresetIdsAreUnique() =>
Assert.Equal(ExportPresets.BuiltIn.Count, ExportPresets.BuiltIn.Select(p => p.Id).Distinct().Count());
@@ -1,6 +1,10 @@
using System.Text.Json.Nodes;
using Titano.Core.Archiving;
using Titano.Core.IO;
using Titano.Core.Model;
using Titano.Core.Project;
using Titano.Data.Archiving;
using Titano.Data.Repositories;
using Titano.Testing;
using static Titano.Data.Tests.Archiving.ArchiveFixture;
@@ -215,4 +219,61 @@ public sealed class ArchiveIngestTests(TestContext context)
// The index can be rebuilt from the sidecar it now has.
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
}
[Test]
public async Task FramesInsertedBeforeOthersTakeTheProjectPositionsAlong()
{
await using var fx = await CreateAsync(context);
var results = new List<IngestResult>();
for (var i = 0; i < 5; i++) results.Add(await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(1 + i), 10 + i)), fx.Ct));
var sequenceId = results[0].SequenceId;
var folder = fx.Paths.Sequence((await fx.Db.Sequences.GetAsync(sequenceId))!.Folder);
var project = new ProjectSettings
{
ExcludedFrames = [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)] },
}.ToJson();
AtomicFile.WriteAllText(folder.ProjectPath, project);
var edited = await fx.Db.Sequences.EditAsync(sequenceId, new SequenceEdit { SettingsJson = project, HeroFrameId = results[1].FrameId }, null, fx.Ct);
var version = edited.Sequence!.SettingsVersion;
// Two photos captured before every other one, and one between the third and the fourth, arrive later.
await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start, 20)), fx.Ct);
await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddSeconds(30), 21)), fx.Ct);
await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(3.5), 22)), fx.Ct);
var row = (await fx.Db.Sequences.GetAsync(sequenceId))!;
var frames = await fx.Db.Frames.ListBySequenceAsync(sequenceId);
var moved = ProjectSettings.FromJson(row.SettingsJson!);
Assert.SequenceEqual([results[3].FrameId], moved.ExcludedFrames.Select(i => frames[i].Id));
Assert.Equal(results[1].FrameId, frames[moved.HeroFrame!.Value].Id);
Assert.SequenceEqual([results[0].FrameId, results[4].FrameId], moved.Camera.Keyframes.Select(k => frames[k.Frame].Id));
Assert.Equal(version, row.SettingsVersion);
Assert.Equal(results[1].FrameId, row.HeroFrameId);
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(row.SettingsJson!), JsonNode.Parse(File.ReadAllText(folder.ProjectPath))), "project.json mirrors the index");
}
[Test]
public async Task PhotosAFolderScanFindsMoveTheProjectPositionsButUnknownMembersStay()
{
await using var fx = await CreateAsync(context);
var late = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(1), 1)), fx.Ct);
await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(2), 2)), fx.Ct);
var row = (await fx.Db.Sequences.GetAsync(late.SequenceId))!;
var folder = fx.Paths.Sequence(row.Folder);
// A project with a member from a newer Titano; only the index copy holds it (the API edited it).
const string project = "{ \"excludedFrames\": [1], \"heroFrame\": 0, \"futureOption\": { \"keep\": true } }";
await fx.Db.Sequences.EditAsync(late.SequenceId, new SequenceEdit { SettingsJson = project }, null, fx.Ct);
File.WriteAllBytes(folder.File("originals/early.JPG"), Photos.Jpeg(Start, 3));
await fx.Service.Indexer.ReconcileAsync(fx.Archive, (await fx.Db.Sequences.GetAsync(late.SequenceId))!, fx.Ct);
var settings = (await fx.Db.Sequences.GetAsync(late.SequenceId))!.SettingsJson!;
var moved = ProjectSettings.FromJson(settings);
Assert.SequenceEqual([2], moved.ExcludedFrames);
Assert.Equal(1, moved.HeroFrame);
Assert.Contains("futureOption", settings);
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(settings), JsonNode.Parse(File.ReadAllText(folder.ProjectPath))), "project.json mirrors the index");
}
}