fix(editor): move a local editor's positions with frames inserted under it

An editor open on a sequence of this PC while earlier photos arrived (a
sync download, an import, a scan) kept its frames and positions in the old
order, while the index moved the stored positions with their frames. The
next edit of exclusions, the hero or keyframes was merged as the editor's
own value and stored in the old order, so other photos were excluded and
the Library's hero and the project's named different frames.

LocalSequenceSource records the frames version it read the frames at (row
and frames read as one state) and refuses a save under the sequence lease
when the row's frames version moved (FramesChangedException); the index
edit also names that version. EditorSession then reads the sequence again
and moves its project, its basis and every undo and redo step the way the
index moved the stored copy (ProjectSettings.FollowFrames: by frame id when
frames were only inserted, otherwise unchanged; waiting photos placed),
and saves in the new order. A read after a job follows the frames the same
way, and saves wait for a read under way.

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 19:13:55 +00:00
1 parent f2ce806d56
commit 31dea66270
9 files changed
+357 -33

No files matched your search

@@ -98,6 +98,77 @@ public sealed class EditorSessionTests
}
}
[Test]
public async Task AnEditMadeBeforeInsertedFramesWereReadIsSavedOnTheFramesItNamed()
{
var (session, source) = OpenShort();
await using (session)
{
Assert.True(session.SetExcluded([3], true));
Assert.True(await session.FlushAsync());
// Two earlier photos are inserted (a download, an import, a scan): the stored exclusion moved with its frame.
var frames = session.Content.Frames;
InsertEarlier(source, 2);
source.Stored = source.Stored with { ExcludedFrames = [5] };
source.FailNextSave = new FramesChangedException("frames changed");
// The editor still shows the frames it read when the user excludes another one.
Assert.True(session.SetExcluded([6], true));
Assert.True(await session.FlushAsync());
Assert.Equal(frames.Count + 2, session.Content.Count, "the frames were read again");
Assert.SequenceEqual([5, 8], source.Stored.ExcludedFrames);
Assert.SequenceEqual([frames[3].Id, frames[6].Id], session.Project.ExcludedFrames.Select(i => session.Content.Frames[i].Id));
Assert.True(session.Undo());
Assert.SequenceEqual([5], session.Project.ExcludedFrames, "undo steps name the same frames");
Assert.True(session.Undo());
Assert.Empty(session.Project.ExcludedFrames);
}
}
[Test]
public async Task AReadAfterFramesWereInsertedMovesTheOpenProjectWithThem()
{
var (session, source) = OpenShort();
await using (session)
{
Assert.True(session.SetExcluded([3], true));
Assert.True(session.SetHero(4));
Assert.True(await session.FlushAsync());
InsertEarlier(source, 2);
source.Stored = source.Stored with { ExcludedFrames = [5], HeroFrame = 6 };
await session.ReloadAsync();
Assert.SequenceEqual([5], session.Project.ExcludedFrames);
Assert.Equal(6, session.Project.HeroFrame);
Assert.True(session.Undo());
Assert.Null(session.Project.HeroFrame);
Assert.SequenceEqual([5], session.Project.ExcludedFrames, "the step before Set hero names the same frame");
Assert.True(await session.FlushAsync());
Assert.SequenceEqual([5], source.Stored.ExcludedFrames);
Assert.Null(source.Stored.HeroFrame);
}
}
/// <summary>A short sequence without its analysis, whose frame ids are all different (the synthetic ids repeat every 64 frames).</summary>
private static (EditorSession Session, FakeSequenceSource Source) OpenShort()
{
var sequence = EditorFixture.Sequence(12);
var content = EditorFixture.Content(sequence, deep: false);
var source = new FakeSequenceSource(sequence, content);
return (new EditorSession(source, content, [], TimeSpan.FromMilliseconds(20)), source);
}
/// <summary>Photos captured before the first frame join the source's sequence, as an insertion renumbers it.</summary>
private static void InsertEarlier(FakeSequenceSource source, int count)
{
var first = source.Content.Frames[0];
var early = Enumerable.Range(0, count).Select(i => first with { Id = Guid.CreateVersion7(), CapturedAt = first.CapturedAt!.Value.AddMinutes(i - count) });
IReadOnlyList<Frame> frames = [.. early.Concat(source.Content.Frames).Select((f, i) => f with { Index = i })];
source.Content = source.Content with { Frames = frames, Light = LightAnalyzer.Analyze(frames) };
}
[Test]
public async Task AFailedSaveStaysPendingUntilItIsStored()
{
@@ -138,6 +138,63 @@ public sealed class ProjectStoreTests(TestContext context)
}
}
[Test]
public async Task AnEditorOpenWhileEarlierPhotosArriveSavesOnTheFramesTheUserChose()
{
var (db, sequenceId, root) = await ArchiveAsync(10);
await using (db)
{
var options = new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) };
await using var source = new LocalSequenceSource(db, sequenceId, DecoderFactory.CreateClient(null), null, options);
var content = await source.LoadAsync(context.Cancellation);
var session = new EditorSession(source, content, [], TimeSpan.FromMilliseconds(20));
await using (session)
{
Assert.True(session.SetExcluded([5], true));
Assert.True(session.SetHero(6));
Assert.True(await session.FlushAsync());
// Three photos taken before the others are found in the folder (a sync download, an import or a scan
// inserts them the same way): the index moves the stored positions with their frames.
var sequence = EditorFixture.Sequence(3);
var first = content.Frames[0].CapturedAt!.Value;
for (var i = 0; i < 3; i++)
{
using var image = sequence.Render(i);
var exif = ExifWriter.BuildJpegApp1(sequence.Metadata[i] with { CapturedAt = first.AddMinutes(i - 3) });
File.WriteAllBytes(Path.Combine(root, "Synthetic", $"EARLY_{i + 1}.JPG"),
JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 90, App1Segments = [exif[4..]] }));
}
var archive = (await db.Archives.GetAsync(content.Sequence.ArchiveId, context.Cancellation))!;
await new ArchiveService(db, options).Indexer.ScanAsync(archive, ScanMode.Full, null, _ => ValueTask.CompletedTask, context.Cancellation);
var moved = ProjectSettings.FromJson((await db.Sequences.GetAsync(sequenceId))!.SettingsJson!);
Assert.SequenceEqual([8], moved.ExcludedFrames);
Assert.Equal(9, moved.HeroFrame);
// The editor still shows the ten frames it read; the user excludes the eighth of them.
Assert.Equal(10, session.Content.Count);
Assert.True(session.SetExcluded([7], true));
Assert.True(await session.FlushAsync());
Assert.Equal(13, session.Content.Count, "the frames were read again before the save");
Assert.SequenceEqual([8, 10], session.Project.ExcludedFrames);
Assert.Equal(9, session.Project.HeroFrame);
var row = (await db.Sequences.GetAsync(sequenceId))!;
var stored = ProjectSettings.FromJson(row.SettingsJson!);
var frames = session.Content.Frames;
Assert.SequenceEqual([content.Frames[5].Id, content.Frames[7].Id], stored.ExcludedFrames.Select(i => frames[i].Id), "the photos the user chose");
Assert.Equal(content.Frames[6].Id, frames[stored.HeroFrame!.Value].Id);
Assert.Equal(content.Frames[6].Id, row.HeroFrameId);
Assert.Equal(row.SettingsJson, File.ReadAllText(Path.Combine(root, "Synthetic", ".titano", "project.json")));
// Undo takes back the user's last step on the new frames too.
Assert.True(session.Undo());
Assert.True(await session.FlushAsync());
Assert.SequenceEqual([8], ProjectSettings.FromJson((await db.Sequences.GetAsync(sequenceId))!.SettingsJson!).ExcludedFrames);
}
}
}
[Test]
public async Task ASaveThatDoesNotReachTheIndexThrows()
{
@@ -167,6 +167,47 @@ public sealed class ProjectSettingsTests
Assert.Null(ProjectSettings.MoveFramePositions("""{ "heroFrame": 1, "HeroFrame": 1 }""", new Dictionary<int, int> { [1] = 2 }));
}
[Test]
public void AnEditorsPositionsFollowFramesInsertedUnderItAsTheIndexMovedTheStoredOnes()
{
// An editor read four frames; a download then inserted two earlier photos, one of which an exclusion waited for.
var photos = Enumerable.Range(0, 6).Select(Photo).ToList();
var ids = Enumerable.Range(0, 6).Select(_ => Guid.CreateVersion7()).ToList();
IReadOnlyList<Guid> before = ids[2..];
var after = ids.Select((id, i) => new Titano.Core.Model.Frame
{
Id = id,
SequenceId = Guid.Empty,
Index = i,
RelativePath = $"s/IMG_{i}.JPG",
FileName = $"IMG_{i}.JPG",
Sha256 = photos[i].Sha256,
Size = photos[i].Size,
CapturedAt = photos[i].CapturedAt,
}).ToList();
var project = new ProjectSettings
{
ExcludedFrames = [1, 3],
HeroFrame = 0,
Camera = new CameraSettings { Keyframes = [new CameraKey(2, 0.5, 0.5, 1, 0)] },
Exposure = new ExposureSettings { DeflickerWindow = new Auto<int>().WithManual(41) },
PendingFrames = [new PendingFrame { Sha256 = photos[1].Sha256, Size = photos[1].Size, Excluded = true }],
};
var followed = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.FollowFrames(project.ToJson(), before, after)));
Assert.SequenceEqual([1, 3, 5], followed.ExcludedFrames);
Assert.Equal(2, followed.HeroFrame);
Assert.SequenceEqual([4], followed.Camera.Keyframes.Select(k => k.Frame));
Assert.Equal(41, followed.Exposure.DeflickerWindow.Manual);
Assert.Null(followed.PendingFrames);
// The same frames: nothing moves. A frame the editor read that is gone: positions stay, as the index leaves them.
Assert.Null(ProjectSettings.FollowFrames(new ProjectSettings { ExcludedFrames = [1] }.ToJson(), ids, after));
var removed = after.Where(f => f.Id != ids[3]).Select((f, i) => f with { Index = i }).ToList();
Assert.Null(ProjectSettings.FollowFrames(new ProjectSettings { ExcludedFrames = [1, 3] }.ToJson(), before, removed));
}
[Test]
public void PositionsCrossToAnotherCopyByPhotoAndWaitForPhotosItLacks()
{