fix(workspace): follow server renames, never keep a thumbnail as a proxy on resume
A pull and the server's renamed event write the home's name into the copy, so a rename made on the server reaches the workspace. A copy from a server marks a thumbnail taken for a missing proxy before moving it into place; a resumed run reads the marker and fetches the proxy the server has now, instead of recording the thumbnail as one. Tests also cover the frames stamp (frames read only when it moves), an older server's copy refusal and a viewer's 403 push. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
44f0d30147
commit
cdd29f8e84
4 files changed
+227
-7
No files matched your search
@@ -235,6 +235,132 @@ public sealed class ServerWorkspaceTests(TestContext context)
|
||||
Assert.SequenceEqual([1], (await home.HistoryAsync(vertical.Id, null, 10, context.Cancellation)).Select(v => v.Number));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AResumedCopyNeverRecordsAThumbnailAsAProxy()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (_, _, token) = await server.UserAsync("alberto", "editor");
|
||||
var sid = await SequenceAsync(server, token, 3);
|
||||
await using var a = await WorkspaceClient.StartAsync(context, server, token, "PC-A");
|
||||
var home = a.Home(sid);
|
||||
var main = (await home.ListAsync(context.Cancellation)).Single();
|
||||
await a.CheckoutAsync(home, main.Id);
|
||||
var project = a.Store.Find(main.Id)!;
|
||||
var media = Assert.NotNull(project.ReadMedia());
|
||||
var manifest = await a.Client.GetManifestAsync(sid, context.Cancellation);
|
||||
var frame = manifest.Frames.OrderBy(f => f.Index).First(f => f.HasProxy);
|
||||
var entry = media.Frames.Single(f => f.Id == frame.Id);
|
||||
Assert.Equal("proxy", entry.FileKind);
|
||||
Assert.Empty(Directory.EnumerateFiles(project.ProxiesDirectory, "*.thumb"));
|
||||
|
||||
// A run stopped after it took the thumbnail for a proxy the server had not made yet, which it has now: the resumed
|
||||
// run (every frame done by its checkpoint) downloads the proxy instead of keeping the thumbnail as one.
|
||||
var file = project.MediaFile(entry.File!);
|
||||
await using (var thumb = File.Create(file))
|
||||
await a.Client.DownloadFrameAsync(frame.Id, "thumb", thumb, null, context.Cancellation);
|
||||
File.WriteAllBytes(file + ".thumb", []);
|
||||
File.Delete(project.MediaIndexPath);
|
||||
await a.CopyAsync(sid, main.Id, $"copy:{manifest.Frames.Count}:{manifest.FramesVersion}");
|
||||
var resumed = Assert.NotNull(project.ReadMedia()).Frames.Single(f => f.Id == frame.Id);
|
||||
Assert.Equal(("proxy", entry.FileSha256), (resumed.FileKind, resumed.FileSha256));
|
||||
Assert.Equal(entry.FileSha256, Sha(project.MediaFile(resumed.File!)));
|
||||
Assert.Empty(Directory.EnumerateFiles(project.ProxiesDirectory, "*.thumb"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AViewersPushIsRefusedForGoodAndItsVersionWaits()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (_, _, token) = await server.UserAsync("alberto", "editor");
|
||||
var (_, _, viewing) = await server.UserAsync("vera", "viewer");
|
||||
var sid = await SequenceAsync(server, token, 2);
|
||||
await using var v = await WorkspaceClient.StartAsync(context, server, viewing, "PC-V");
|
||||
var home = v.Home(sid);
|
||||
var main = (await home.ListAsync(context.Cancellation)).Single();
|
||||
await v.CheckoutAsync(home, main.Id);
|
||||
await v.EditAsync(main.Id, s => s with { HeroFrame = 1 });
|
||||
Assert.NotNull(await v.Sync.CommitAsync(main.Id, cancellationToken: context.Cancellation));
|
||||
|
||||
// 403: a refusal, never "out of reach" (nothing to wait out); the version stays here, the server's head as it was.
|
||||
var refused = await Assert.ThrowsAsync<ChronosApiException>(() => v.Sync.PushAsync(main.Id, home, context.Cancellation));
|
||||
Assert.Equal(System.Net.HttpStatusCode.Forbidden, refused.Status);
|
||||
Assert.False(ServerProjectHome.IsUnavailable(refused));
|
||||
Assert.Equal(1, v.Sync.Unpushed(main.Id).Count);
|
||||
Assert.Equal(1, (await home.HeadAsync(main.Id, context.Cancellation)).Number);
|
||||
Assert.Equal(WorkspaceState.Ahead, (await v.Sync.StatusAsync(main.Id, home, context.Cancellation)).State);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheStatusReadsTheServersFramesAgainOnlyWhenTheirStampMoved()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (_, _, token) = await server.UserAsync("alberto", "editor");
|
||||
var sid = await SequenceAsync(server, token, 2);
|
||||
var tap = new Tap();
|
||||
await using var a = await WorkspaceClient.StartAsync(context, server, token, "PC-A", tap);
|
||||
var home = a.Home(sid);
|
||||
var main = (await home.ListAsync(context.Cancellation)).Single();
|
||||
await a.CheckoutAsync(home, main.Id);
|
||||
|
||||
// The frames are downloaded once; asked again with nothing changed, only their stamp is.
|
||||
await a.Sync.StatusAsync(main.Id, home, context.Cancellation);
|
||||
var read = tap.Frames;
|
||||
Assert.True(read > 0, "the frames were read");
|
||||
await a.Sync.StatusAsync(main.Id, home, context.Cancellation);
|
||||
await a.Sync.StatusAsync(main.Id, home, context.Cancellation);
|
||||
Assert.Equal(read, tap.Frames);
|
||||
Assert.True(tap.Stamps >= 3, "the stamp was asked each time");
|
||||
|
||||
// A photo more on the server moves the stamp: the frames are read again, and the copy reads as outdated.
|
||||
await Uploads.UploadAsync(server.NewClient(token), Photo(42), "IMG_0042.JPG", sid, capturedAt: new DateTime(2026, 9, 28, 18, 30, 11));
|
||||
await JobsDoneAsync(server);
|
||||
var status = await a.Sync.StatusAsync(main.Id, home, context.Cancellation);
|
||||
Assert.Equal(read + 1, tap.Frames);
|
||||
Assert.True(status.MediaOutdated, "the server's frames moved");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACopyFromAServerThatKeepsNoProjectsIsRefusedSayingSo()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (_, _, token) = await server.UserAsync("alberto", "editor");
|
||||
var sid = await SequenceAsync(server, token, 2);
|
||||
var tap = new Tap();
|
||||
await using var a = await WorkspaceClient.StartAsync(context, server, token, "PC-A", tap);
|
||||
var home = a.Home(sid);
|
||||
var main = (await home.ListAsync(context.Cancellation)).Single();
|
||||
await a.Sync.CheckoutAsync(home, main.Id, cancellationToken: context.Cancellation);
|
||||
|
||||
// An older server's API knows no such call (404 naming nothing): the copy fails saying the server keeps no projects.
|
||||
tap.OlderServer = true;
|
||||
var job = await a.EnqueueAsync(WorkspaceJobs.CopyFromServer(sid, Title, a.Server, a.Store.Root, main.Id));
|
||||
Assert.Equal(Loc.L("Errors.Workspace.NoProjects"), (await a.WaitAsync(job.Id, JobState.Failed)).Error);
|
||||
Assert.Null(a.Store.Find(main.Id)!.ReadMedia());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ARenameOnTheServerReachesTheCopyAtItsNextPull()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (_, _, token) = await server.UserAsync("alberto", "editor");
|
||||
var sid = await SequenceAsync(server, token, 2);
|
||||
await using var a = await WorkspaceClient.StartAsync(context, server, token, "PC-A");
|
||||
var home = a.Home(sid);
|
||||
var vertical = await home.CreateAsync("Vertical", null, null, context.Cancellation);
|
||||
await a.CheckoutAsync(home, vertical.Id);
|
||||
|
||||
// Renamed from another program: the copy here takes the name at its next pull, and says so when asked alone.
|
||||
using var other = new ChronosClient(server.HttpBase, new CertificatePinning([]), "PC-B") { Token = token };
|
||||
var otherHome = new ServerProjectHome(other, sid);
|
||||
await otherHome.RenameAsync(vertical.Id, "Square", context.Cancellation);
|
||||
await a.Sync.PullAsync(vertical.Id, home, context.Cancellation);
|
||||
Assert.Equal("Square", a.Bundle(vertical.Id).Workspace!.Name);
|
||||
await otherHome.RenameAsync(vertical.Id, "Wide", context.Cancellation);
|
||||
Assert.True(await a.Sync.FollowNameAsync(vertical.Id, home, context.Cancellation));
|
||||
Assert.False(await a.Sync.FollowNameAsync(vertical.Id, home, context.Cancellation));
|
||||
Assert.Equal("Wide", a.Bundle(vertical.Id).Workspace!.Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AServerOutOfReachLeavesTheProjectOfflineAndItsWorkWaits()
|
||||
{
|
||||
@@ -315,6 +441,36 @@ public sealed class ServerWorkspaceTests(TestContext context)
|
||||
return JpegEncoder.Encode(pixels, width, height, 3, new JpegEncodeOptions { Quality = 90 });
|
||||
}
|
||||
|
||||
/// <summary>The desktop's requests as they go: the frames and stamps read, and an older server's answer to the frames.</summary>
|
||||
private sealed class Tap() : DelegatingHandler(new HttpClientHandler())
|
||||
{
|
||||
private int _frames;
|
||||
private int _stamps;
|
||||
|
||||
public int Frames => Volatile.Read(ref _frames);
|
||||
|
||||
public int Stamps => Volatile.Read(ref _stamps);
|
||||
|
||||
/// <summary>The frames answer 404 naming nothing, as the API's fallback of a server without the projects does.</summary>
|
||||
public bool OlderServer { get; set; }
|
||||
|
||||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
var path = request.RequestUri!.AbsolutePath;
|
||||
if (path.EndsWith("/projects/frames/stamp", StringComparison.Ordinal)) Interlocked.Increment(ref _stamps);
|
||||
if (path.EndsWith("/projects/frames", StringComparison.Ordinal))
|
||||
{
|
||||
Interlocked.Increment(ref _frames);
|
||||
if (OlderServer)
|
||||
return Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.NotFound)
|
||||
{
|
||||
Content = new StringContent("""{"code":"not_found","message":"Not found"}""", System.Text.Encoding.UTF8, "application/json"),
|
||||
});
|
||||
}
|
||||
return base.SendAsync(request, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A desktop's side: its own index (for the job engine), the workspace copy job connected to the test server, a workspace and its sync.</summary>
|
||||
private sealed class WorkspaceClient : IAsyncDisposable
|
||||
{
|
||||
@@ -342,13 +498,13 @@ public sealed class ServerWorkspaceTests(TestContext context)
|
||||
/// <summary>Whether the copy job finds this PC signed in to the server.</summary>
|
||||
public bool Connected { get; set; } = true;
|
||||
|
||||
public static async Task<WorkspaceClient> StartAsync(TestContext context, TestServer server, string token, string name)
|
||||
public static async Task<WorkspaceClient> StartAsync(TestContext context, TestServer server, string token, string name, HttpMessageHandler? handler = null)
|
||||
{
|
||||
var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory(name + "-db"), "chronos.db")), context.Cancellation);
|
||||
var archives = new ArchiveService(db, new ArchiveServiceOptions { Owner = name });
|
||||
// This PC's own library: the copy of a server project never writes into it.
|
||||
var archive = await archives.CreateAsync(context.CreateTempDirectory(name + "-archive"), "Local", cancellationToken: context.Cancellation);
|
||||
var client = new ChronosClient(server.HttpBase, new CertificatePinning([]), name) { Token = token };
|
||||
var client = new ChronosClient(server.HttpBase, new CertificatePinning([]), name, handler) { Token = token };
|
||||
WorkspaceClient? created = null;
|
||||
var engine = new JobEngine(db, WorkspaceJobs.CreateHandlers(db, archives, new Connector(client, () => created?.Connected ?? true)),
|
||||
new JobEngineOptions { Workers = 1, WorkerId = name, PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
|
||||
|
||||
Reference in new issue
Block a user