merge: sync and project position fixes (V-9, V-10, V-21, V-29, CD-2)
Merge branch fix2/sync-positions into the verification branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
32 files changed
+1843
-153
No files matched your search
@@ -1,7 +1,9 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using Titano.Data;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Api;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests.Api;
|
||||
@@ -60,6 +62,32 @@ public sealed class SequenceTests(TestContext context)
|
||||
await invalid.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_settings");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SettingsWrittenAgainstFramesThatChangedAreRefused()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var first = await Uploads.UploadAsync(editor, Uploads.Photo(2000, 41), "a.CR3", sessionKey: "k");
|
||||
using var read = await editor.GetAsync($"/api/v1/sequences/{first.SequenceId}");
|
||||
var sequence = await read.ReadAsync(ProtocolJson.Default.SequenceDto);
|
||||
// Another photo arrives after this client read the frames its positions name.
|
||||
await Uploads.UploadAsync(editor, Uploads.Photo(2000, 42), "b.CR3", sessionKey: "k");
|
||||
|
||||
using (var stale = await editor.PatchAsJsonAsync($"/api/v1/sequences/{sequence.Id}",
|
||||
new UpdateSequenceRequest(null, null, null, null, """{"excludedFrames":[0]}""", sequence.SettingsVersion, ExpectedFramesVersion: sequence.FramesVersion),
|
||||
ProtocolJson.Default.UpdateSequenceRequest))
|
||||
{
|
||||
var error = await stale.ExpectErrorAsync(HttpStatusCode.Conflict, "frames_conflict");
|
||||
Assert.Equal((sequence.FramesVersion + 1).ToString(System.Globalization.CultureInfo.InvariantCulture), error.Details);
|
||||
}
|
||||
|
||||
using var fresh = await editor.PatchAsJsonAsync($"/api/v1/sequences/{sequence.Id}",
|
||||
new UpdateSequenceRequest(null, null, null, null, """{"excludedFrames":[1]}""", sequence.SettingsVersion, ExpectedFramesVersion: sequence.FramesVersion + 1),
|
||||
ProtocolJson.Default.UpdateSequenceRequest);
|
||||
await fresh.ExpectAsync(HttpStatusCode.OK);
|
||||
Assert.Equal(sequence.SettingsVersion + 1, (await fresh.ReadAsync(ProtocolJson.Default.SequenceDto)).SettingsVersion);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheManifestCarriesHashesAndAnEtag()
|
||||
{
|
||||
@@ -88,6 +116,59 @@ public sealed class SequenceTests(TestContext context)
|
||||
Assert.Count(3, (await changed.ReadAsync(ProtocolJson.Default.SequenceManifest)).Frames);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheManifestListsTheFramesOfOneFramesVersion()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
await FramesOfOneVersionAsync(server);
|
||||
}
|
||||
|
||||
/// <summary>The manifest's frames are read again while frames are committed, then under the row lock (also run on PostgreSQL).</summary>
|
||||
internal static async Task FramesOfOneVersionAsync(TestServer server)
|
||||
{
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var first = await Uploads.UploadAsync(editor, Uploads.Photo(2000, 51), "a.CR3", sessionKey: "k");
|
||||
var db = server.Database;
|
||||
var id = first.SequenceId;
|
||||
var start = (await db.Sequences.GetAsync(id))!.FramesVersion;
|
||||
|
||||
// Frames are committed while the first listing is read: it is read again.
|
||||
var listings = new List<int>();
|
||||
var snapshot = await SequenceEndpoints.SnapshotAsync(db, id, async (_, sequence, ct) =>
|
||||
{
|
||||
listings.Add(sequence.FramesVersion);
|
||||
if (listings.Count == 1) await db.Sequences.RefreshStatsAsync(id, ct);
|
||||
return [];
|
||||
}, CancellationToken.None);
|
||||
Assert.SequenceEqual([start, start + 1], listings);
|
||||
Assert.Equal(start + 1, Assert.NotNull(snapshot).Sequence.FramesVersion);
|
||||
|
||||
// Commits keep landing: the last listing is read while none can.
|
||||
Task? waiting = null;
|
||||
listings.Clear();
|
||||
var locked = await SequenceEndpoints.SnapshotAsync(db, id, async (on, sequence, ct) =>
|
||||
{
|
||||
listings.Add(sequence.FramesVersion);
|
||||
if (listings.Count <= 3)
|
||||
{
|
||||
await db.Sequences.RefreshStatsAsync(id, ct);
|
||||
return [];
|
||||
}
|
||||
// Listed on the transaction that holds the row (on SQLite, the only connection that may while it writes).
|
||||
Assert.True(on is TitanoTransaction, "the frames are listed inside the locking transaction");
|
||||
Assert.Count(1, await on.Frames.ListBySequenceAsync(id, cancellationToken: ct));
|
||||
waiting = db.Sequences.RefreshStatsAsync(id, ct);
|
||||
await Task.WhenAny(waiting, Task.Delay(300, ct));
|
||||
Assert.False(waiting.IsCompleted, "a commit of frames waits until the frames are listed");
|
||||
return [];
|
||||
}, CancellationToken.None);
|
||||
await Assert.NotNull(waiting);
|
||||
Assert.SequenceEqual([start + 1, start + 2, start + 3, start + 4], listings);
|
||||
Assert.Equal(start + 4, Assert.NotNull(locked).Sequence.FramesVersion);
|
||||
Assert.Equal(start + 5, (await db.Sequences.GetAsync(id))!.FramesVersion);
|
||||
Assert.Null(await SequenceEndpoints.SnapshotAsync(db, Guid.NewGuid(), (_, _, _) => Task.FromResult(new List<ManifestFrame>()), CancellationToken.None));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ListingFiltersAndPagesWithACursor()
|
||||
{
|
||||
|
||||
@@ -54,6 +54,19 @@ public sealed class PostgresServerTests(TestContext context)
|
||||
Assert.Equal("ok", (await health.ReadAsync(ProtocolJson.Default.HealthStatus)).Checks["database"]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheManifestListsTheFramesOfOneFramesVersionOnPostgres()
|
||||
{
|
||||
await using var schema = await Schema.CreateAsync(context);
|
||||
await using var server = await TestServer.StartAsync(context, o =>
|
||||
{
|
||||
o.Database = DatabaseKind.Postgres;
|
||||
o.DbConnectionString = schema.ConnectionString;
|
||||
});
|
||||
Assert.Equal(Dialect.Postgres, server.Database.Dialect);
|
||||
await Api.SequenceTests.FramesOfOneVersionAsync(server);
|
||||
}
|
||||
|
||||
/// <summary>A throw-away schema on the test server, selected through <c>Search Path</c>.</summary>
|
||||
private sealed class Schema : IAsyncDisposable
|
||||
{
|
||||
|
||||
@@ -85,6 +85,26 @@ internal sealed class SyncClient : IAsyncDisposable
|
||||
return edited.Sequence!;
|
||||
}
|
||||
|
||||
/// <summary>One more photo imported into a local sequence, captured at <paramref name="captured"/> (before others: it is inserted).</summary>
|
||||
public async Task<IngestResult> AddPhotoAsync(Guid sequenceId, byte[] photo, DateTime captured)
|
||||
{
|
||||
var staging = Path.Combine(Archive.RootPath, ".titano", "staging", "test");
|
||||
Directory.CreateDirectory(staging);
|
||||
var path = Path.Combine(staging, $"{Guid.NewGuid():N}.part");
|
||||
await File.WriteAllBytesAsync(path, photo, Ct);
|
||||
return await Archives.Ingest.CommitAsync(new IngestRequest(Archive.Id, sequenceId, null, null, path, $"IMG_{captured:HHmmss}.JPG", photo.Length, Sha(photo),
|
||||
captured, null), Ct);
|
||||
}
|
||||
|
||||
/// <summary>An edit on this PC, as the editor makes it (project file and index, settings version + 1).</summary>
|
||||
public async Task<Sequence> EditSettingsAsync(Guid sequenceId, string settingsJson)
|
||||
{
|
||||
var sequence = (await Db.Sequences.GetAsync(sequenceId, Ct))!;
|
||||
await File.WriteAllTextAsync(new Titano.Core.Archiving.ArchivePaths(Archive.RootPath).Sequence(sequence.Folder).ProjectPath, settingsJson, Ct);
|
||||
return (await Db.Sequences.EditAsync(sequenceId, new Titano.Data.Repositories.SequenceEdit { SettingsJson = settingsJson }, sequence.SettingsVersion, Ct))
|
||||
.Sequence!;
|
||||
}
|
||||
|
||||
/// <summary>A render of <paramref name="sequence"/> made on this PC: its file in the sequence's exports and its row.</summary>
|
||||
public async Task<Render> RenderAsync(Sequence sequence, string fileName, byte[] bytes)
|
||||
{
|
||||
|
||||
@@ -249,6 +249,117 @@ public sealed class SyncServerTests(TestContext context)
|
||||
Assert.Equal(0, (await laptop.EntryAsync("Star trails")).Link!.ExportsToGet);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PositionsEditedOnTheServerStayOnTheirPhotosWhenAnotherPcDownloadsTheInsertedFrames()
|
||||
{
|
||||
await using var server = await StartServerAsync();
|
||||
var (http, _, token) = await server.UserAsync("alberto", "editor");
|
||||
await using var studio = await SyncClient.StartAsync(context, server, token, "studio");
|
||||
await using var laptop = await SyncClient.StartAsync(context, server, token, "laptop");
|
||||
var photos = Enumerable.Range(60, 5).Select(SyncClient.Photo).ToList();
|
||||
// Both PCs hold the last four photos, the third of them excluded.
|
||||
var local = await studio.LocalAsync("Harbour at dawn", photos[1..], new ProjectSettings { ExcludedFrames = [2] }.ToJson());
|
||||
await studio.WaitAsync((await studio.Sync.EnqueueUploadAsync(studio.Engine, studio.Server, local)).Id);
|
||||
var remoteId = (await studio.EntryAsync("Harbour at dawn")).Link!.RemoteId;
|
||||
await ServerIdleAsync(server);
|
||||
await laptop.Sync.RefreshAsync(laptop.Remote, new SyncPolicy(), context.Cancellation);
|
||||
await laptop.WaitAsync((await laptop.Sync.EnqueueDownloadAsync(laptop.Engine, laptop.Server, remoteId, "Harbour at dawn", SyncDownloadMode.Originals,
|
||||
laptop.Archive.Id)).Id);
|
||||
var copy = (await laptop.EntryAsync("Harbour at dawn")).Local!;
|
||||
|
||||
// The studio files an earlier photo and uploads it: the server inserts it first and moves its own positions.
|
||||
await studio.AddPhotoAsync(local.Id, photos[0], new DateTime(2026, 9, 28, 17, 59, 50));
|
||||
await studio.WaitAsync((await studio.Sync.EnqueueUploadAsync(studio.Engine, studio.Server, local)).Id);
|
||||
await ServerIdleAsync(server);
|
||||
Assert.SequenceEqual([SyncClient.Sha(photos[3])], (await ServerNamedAsync(server, remoteId)).Excluded);
|
||||
|
||||
// An editor on the server excludes the new photo too, by the server's frames, and changes the deflicker window.
|
||||
var row = (await server.Database.Sequences.GetAsync(remoteId))!;
|
||||
var current = ProjectSettings.FromJson(row.SettingsJson!);
|
||||
var edit = current with
|
||||
{
|
||||
ExcludedFrames = [0, .. current.ExcludedFrames],
|
||||
Exposure = current.Exposure with { DeflickerWindow = new Auto<int>().WithManual(33) },
|
||||
};
|
||||
using (var patch = await http.PatchAsJsonAsync($"/api/v1/sequences/{remoteId}",
|
||||
new UpdateSequenceRequest(null, null, null, null, edit.ToJson(), row.SettingsVersion), ProtocolJson.Default.UpdateSequenceRequest))
|
||||
await patch.ExpectAsync(HttpStatusCode.OK);
|
||||
|
||||
// The laptop takes the edit while it still lacks the new photo, then downloads it.
|
||||
await laptop.Sync.RefreshAsync(laptop.Remote, new SyncPolicy(), context.Cancellation);
|
||||
var taken = await LocalNamedAsync(laptop, copy.Id);
|
||||
Assert.Equal(33, taken.Project.Exposure.DeflickerWindow.Manual);
|
||||
Assert.SequenceEqual([SyncClient.Sha(photos[3])], taken.Excluded);
|
||||
Assert.True(taken.Project.PendingFrames is [{ Excluded: true } waiting] && waiting.Sha256 == SyncClient.Sha(photos[0]), "the new photo's exclusion waits for it");
|
||||
await laptop.WaitAsync((await laptop.Sync.EnqueueDownloadAsync(laptop.Engine, laptop.Server, remoteId, "Harbour at dawn", SyncDownloadMode.Originals,
|
||||
null)).Id);
|
||||
|
||||
var onServer = await ServerNamedAsync(server, remoteId);
|
||||
var here = await LocalNamedAsync(laptop, copy.Id);
|
||||
Assert.SequenceEqual([SyncClient.Sha(photos[0]), SyncClient.Sha(photos[3])], here.Excluded);
|
||||
Assert.SequenceEqual(onServer.Excluded, here.Excluded);
|
||||
Assert.Equal(onServer.Hero, here.Hero);
|
||||
Assert.Null(here.Project.PendingFrames);
|
||||
await laptop.Sync.RefreshAsync(laptop.Remote, new SyncPolicy(), context.Cancellation);
|
||||
Assert.Equal(SyncStatus.InSync, (await laptop.EntryAsync("Harbour at dawn")).Status);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PositionsSentBeforeTheirPhotosStayOnThemWhenTheUploadArrives()
|
||||
{
|
||||
await using var server = await StartServerAsync();
|
||||
var (_, _, token) = await server.UserAsync("alberto", "editor");
|
||||
await using var studio = await SyncClient.StartAsync(context, server, token, "studio");
|
||||
var photos = Enumerable.Range(70, 5).Select(SyncClient.Photo).ToList();
|
||||
var local = await studio.LocalAsync("Pier", photos[1..]);
|
||||
await studio.WaitAsync((await studio.Sync.EnqueueUploadAsync(studio.Engine, studio.Server, local)).Id);
|
||||
var remoteId = (await studio.EntryAsync("Pier")).Link!.RemoteId;
|
||||
await ServerIdleAsync(server);
|
||||
await studio.Sync.RefreshAsync(studio.Remote, new SyncPolicy(), context.Cancellation);
|
||||
|
||||
// This PC files an earlier photo and, before uploading it, excludes it and a later one and picks the hero.
|
||||
await studio.AddPhotoAsync(local.Id, photos[0], new DateTime(2026, 9, 28, 17, 59, 50));
|
||||
var project = ProjectSettings.FromJson((await studio.Db.Sequences.GetAsync(local.Id))!.SettingsJson ?? "{}");
|
||||
await studio.EditSettingsAsync(local.Id, (project with { ExcludedFrames = [0, 3], HeroFrame = 1 }).ToJson());
|
||||
await studio.Sync.RefreshAsync(studio.Remote, new SyncPolicy(), context.Cancellation);
|
||||
|
||||
var sent = await ServerNamedAsync(server, remoteId);
|
||||
Assert.SequenceEqual([SyncClient.Sha(photos[3])], sent.Excluded);
|
||||
Assert.Equal(SyncClient.Sha(photos[1]), sent.Hero);
|
||||
Assert.True(sent.Project.PendingFrames is [{ Excluded: true } waiting] && waiting.Sha256 == SyncClient.Sha(photos[0]), "the exclusion waits for its photo");
|
||||
|
||||
await studio.WaitAsync((await studio.Sync.EnqueueUploadAsync(studio.Engine, studio.Server, local)).Id);
|
||||
await ServerIdleAsync(server);
|
||||
|
||||
var arrived = await ServerNamedAsync(server, remoteId);
|
||||
Assert.SequenceEqual([SyncClient.Sha(photos[0]), SyncClient.Sha(photos[3])], arrived.Excluded);
|
||||
Assert.Equal(SyncClient.Sha(photos[1]), arrived.Hero);
|
||||
Assert.Null(arrived.Project.PendingFrames);
|
||||
var heroFrame = (await server.Database.Sequences.GetAsync(remoteId))!.HeroFrameId;
|
||||
Assert.Equal(SyncClient.Sha(photos[1]), (await server.Database.Frames.ListBySequenceAsync(remoteId)).Single(f => f.Id == heroFrame).Sha256);
|
||||
var here = await LocalNamedAsync(studio, local.Id);
|
||||
Assert.SequenceEqual(arrived.Excluded, here.Excluded);
|
||||
Assert.Equal(arrived.Hero, here.Hero);
|
||||
await studio.Sync.RefreshAsync(studio.Remote, new SyncPolicy(), context.Cancellation);
|
||||
Assert.Equal(SyncStatus.InSync, (await studio.EntryAsync("Pier")).Status);
|
||||
}
|
||||
|
||||
/// <summary>The server's project and the photos (SHA-256) it excludes and shows as hero.</summary>
|
||||
private static async Task<(ProjectSettings Project, string?[] Excluded, string? Hero)> ServerNamedAsync(TestServer server, Guid remoteId)
|
||||
{
|
||||
var project = ProjectSettings.FromJson((await server.Database.Sequences.GetAsync(remoteId))!.SettingsJson ?? "{}");
|
||||
var frames = await server.Database.Frames.ListBySequenceAsync(remoteId);
|
||||
return (project, [.. project.ExcludedFrames.Select(i => frames[i].Sha256)], project.HeroFrame is { } hero ? frames[hero].Sha256 : null);
|
||||
}
|
||||
|
||||
/// <summary>A PC's project and the photos (SHA-256) it excludes and shows as hero.</summary>
|
||||
private static async Task<(ProjectSettings Project, string?[] Excluded, string? Hero)> LocalNamedAsync(SyncClient client, Guid localId)
|
||||
{
|
||||
var project = ProjectSettings.FromJson((await client.Db.Sequences.GetAsync(localId))!.SettingsJson ?? "{}");
|
||||
var frames = await client.Db.Frames.ListBySequenceAsync(localId);
|
||||
return (project, [.. project.ExcludedFrames.Select(i => frames[i].Sha256)], project.HeroFrame is { } hero ? frames[hero].Sha256 : null);
|
||||
}
|
||||
|
||||
private Task<TestServer> StartServerAsync() => TestServer.StartAsync(context, o => o.ChunkSizeBytes = 64 * 1024, ArchiveModule.Register);
|
||||
|
||||
/// <summary>Waits until the server's engines finished what the commits queued (thumbnails, proxies, analysis, which may edit settings).</summary>
|
||||
|
||||
Reference in new issue
Block a user