using System.Security.Cryptography; using Chronos.Core.Codecs.Jpeg; using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Core.Project; using Chronos.Data; using Chronos.Data.Archiving; using Chronos.Data.Jobs; using Chronos.Data.Projects; using Chronos.Data.Sync; using Chronos.Data.Workspaces; using Chronos.Protocol; using Chronos.Protocol.Client; using Chronos.Server.Storage; using Chronos.Testing; namespace Chronos.Server.Tests.Workspaces; /// /// A server as the home of desktop workspaces (owner review 2, R8, phase 5b), end to end through the protocol client: /// two workspaces of two accounts on one server project (checkout, the copy of its media, commit, push, the other's /// fast-forward pull, replays and merges, a real conflict settled by keep mine, take server and save both, restore), the /// copy job resuming after an interruption and keeping what it holds, and a server out of reach. /// [Category("server")] [Category("workspace")] public sealed class ServerWorkspaceTests(TestContext context) { private static readonly TimeSpan Wait = TimeSpan.FromSeconds(TestTiming.Budget(60)); [Test] public async Task TwoWorkspacesShareAProjectOfTheServerLikeGit() { await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); var (_, _, albertoToken) = await server.UserAsync("alberto", "editor"); var (_, _, giuliaToken) = await server.UserAsync("giulia", "editor"); var sid = await SequenceAsync(server, albertoToken, 3); await using var a = await WorkspaceClient.StartAsync(context, server, albertoToken, "PC-A"); await using var b = await WorkspaceClient.StartAsync(context, server, giuliaToken, "PC-B"); var homeA = a.Home(sid); var homeB = b.Home(sid); var main = (await homeA.ListAsync(context.Cancellation)).Single(p => p.IsMain); // Each workspace takes the server's Main and downloads its media. await a.CheckoutAsync(homeA, main.Id); await b.CheckoutAsync(homeB, main.Id); Assert.Equal((BundleHome.ServerKind, a.Server, sid), (a.Bundle(main.Id).Workspace!.Home.Kind, a.Bundle(main.Id).Workspace!.Home.Server, a.Bundle(main.Id).Workspace!.Home.SequenceId)); Assert.Equal(WorkspaceState.InSync, (await a.Sync.StatusAsync(main.Id, homeA, context.Cancellation)).State); // A saves a version and sends it: a fast-forward, numbered and signed by the server. await a.EditAsync(main.Id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.3 } }); Assert.NotNull(await a.Sync.CommitAsync(main.Id, "warmer", cancellationToken: context.Cancellation)); var pushed = await a.Sync.PushAsync(main.Id, homeA, context.Cancellation); var v2 = pushed.Pushed.Single(); Assert.Equal((2, "alberto", "warmer", WorkspaceState.InSync), (v2.Number, v2.Author, v2.Note, pushed.Status.State)); // B is behind and takes it. Assert.Equal((WorkspaceState.Behind, 1), ((await b.Sync.StatusAsync(main.Id, homeB, context.Cancellation)).State, (await b.Sync.StatusAsync(main.Id, homeB, context.Cancellation)).Behind)); var pulled = await b.Sync.PullAsync(main.Id, homeB, context.Cancellation); Assert.Equal((v2.Id, WorkspaceState.InSync), (pulled.Pulled.Single().Id, pulled.Status.State)); Assert.Equal(0.3, (await b.Sync.OpenAsync(main.Id, context.Cancellation)).Working.Exposure.ExposureOffsetEv); // Both change different settings: B's push meets A's, replays on it and merges, no question asked. await a.EditAsync(main.Id, s => s with { ExcludedFrames = [1] }); await a.CommitAndPushAsync(homeA, main.Id); await b.EditAsync(main.Id, s => s with { HeroFrame = 2 }); Assert.NotNull(await b.Sync.CommitAsync(main.Id, cancellationToken: context.Cancellation)); var replayed = await b.Sync.PushAsync(main.Id, homeB, context.Cancellation); Assert.Equal((1, 1, WorkspaceState.InSync), (replayed.Pulled.Count, replayed.Pushed.Count, replayed.Status.State)); var both = await a.PlacedHeadAsync(homeA, main.Id); Assert.SequenceEqual([1], both.ExcludedFrames); Assert.Equal((2, 0.3), (both.HeroFrame, both.Exposure.ExposureOffsetEv)); await a.Sync.PullAsync(main.Id, homeA, context.Cancellation); Assert.Equal(2, (await a.Sync.OpenAsync(main.Id, context.Cancellation)).Working.HeroFrame); // The same setting changed on both sides: a real conflict, settled by keeping mine. Assert.Equal(0.9, (await ConflictAsync(a, b, homeA, homeB, main.Id, 0.5, 0.9, ConflictChoice.KeepMine)).Exposure.ExposureOffsetEv); // Taking the server's. Assert.Equal(0.1, (await ConflictAsync(a, b, homeA, homeB, main.Id, 0.1, 0.2, ConflictChoice.TakeServer)).Exposure.ExposureOffsetEv); // Saving both: B's versions go to a new project of the server, its Main takes A's. var savedBoth = await ConflictAsync(a, b, homeA, homeB, main.Id, 0.6, 0.7, ConflictChoice.SaveBoth, "Main (PC-B)"); Assert.Equal(0.6, savedBoth.Exposure.ExposureOffsetEv); var sibling = Assert.NotNull((await homeA.ListAsync(context.Cancellation)).SingleOrDefault(p => p.Name == "Main (PC-B)")); Assert.Equal(0.7, (await a.PlacedHeadAsync(homeA, sibling.Id)).Exposure.ExposureOffsetEv); Assert.Equal("giulia", sibling.CreatedBy); // A restores the first version it sent: the next version says so, and the server keeps it as a restore. await a.Sync.PullAsync(main.Id, homeA, context.Cancellation); var restored = await a.Sync.RestoreAsync(main.Id, v2.Id, homeA, context.Cancellation); Assert.Equal(0.3, restored.Exposure.ExposureOffsetEv); var restore = Assert.NotNull(await a.Sync.CommitAsync(main.Id, cancellationToken: context.Cancellation)); Assert.Equal((ProjectVersionKind.Restore, (Guid?)v2.Id), (restore.Kind, restore.RestoredFrom)); await a.Sync.PushAsync(main.Id, homeA, context.Cancellation); var head = await homeB.HeadAsync(main.Id, context.Cancellation); Assert.Equal((ProjectVersionKind.Restore, (Guid?)v2.Id, "alberto"), (head.Kind, head.RestoredFrom, head.Author)); // Main's head is the sequence's own settings, for older clients and the panel. var sequence = ProjectSettings.FromJson(Assert.NotNull((await server.Database.Sequences.GetAsync(sid))!.SettingsJson)); Assert.Equal(0.3, sequence.Exposure.ExposureOffsetEv); } /// /// A and B change one setting from the same version, A first: B's push stops at a conflict, which B settles as /// ; returns Main's head at the server as placed on its frames. /// private async Task ConflictAsync(WorkspaceClient a, WorkspaceClient b, ServerProjectHome homeA, ServerProjectHome homeB, Guid projectId, double theirs, double mine, ConflictChoice choice, string? saveBothName = null) { await a.Sync.PullAsync(projectId, homeA, context.Cancellation); await b.Sync.PullAsync(projectId, homeB, context.Cancellation); await a.EditAsync(projectId, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = theirs } }); await a.CommitAndPushAsync(homeA, projectId); await b.EditAsync(projectId, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = mine } }); Assert.NotNull(await b.Sync.CommitAsync(projectId, cancellationToken: context.Cancellation)); var met = await b.Sync.PushAsync(projectId, homeB, context.Cancellation); var conflict = Assert.NotNull(met.Status.Conflict, "both changed the exposure offset"); Assert.Equal(WorkspaceState.Conflict, met.Status.State); Assert.Contains(conflict.Differences, d => d.Path.StartsWith("exposure.exposureOffsetEv", StringComparison.Ordinal)); var settled = await b.Sync.ResolveAsync(projectId, homeB, choice, saveBothName: saveBothName, cancellationToken: context.Cancellation); Assert.Null(settled.Status.Conflict); if (choice == ConflictChoice.SaveBoth) Assert.Equal(saveBothName, settled.SavedAs?.Name); var sent = await b.Sync.PushAsync(projectId, homeB, context.Cancellation); Assert.Equal(WorkspaceState.InSync, sent.Status.State, $"after {choice}"); return await b.PlacedHeadAsync(homeB, projectId); } [Test] public async Task TheCopyDownloadsVerifiedProxiesResumesAndKeepsWhatItHolds() { await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); var (_, _, token) = await server.UserAsync("alberto", "editor"); var sid = await SequenceAsync(server, token, 4); 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(); var project = await a.Sync.CheckoutAsync(home, main.Id, cancellationToken: context.Cancellation); Assert.Equal(WorkspaceState.MediaRemoved, (await a.Sync.StatusAsync(main.Id, home, context.Cancellation)).State); var library = Listing(a.Archive.RootPath); // The frames as the server holds them, each file hashed as it arrived and checked on the disk; this PC's library never written. var job = await a.CopyAsync(sid, main.Id); var media = Assert.NotNull(project.ReadMedia()); var manifest = await a.Client.GetManifestAsync(sid, context.Cancellation); Assert.Equal((BundleMedia.Proxies, sid, manifest.FramesVersion, 4), (media.Kind, media.SequenceId, media.FramesVersion, media.Frames.Count)); foreach (var (frame, entry) in manifest.Frames.OrderBy(f => f.Index).Zip(media.Frames)) { Assert.Equal((frame.Id, frame.Sha256), (entry.Id, entry.Sha256)); Assert.Equal(entry.FileSha256, Sha(project.MediaFile(Assert.NotNull(entry.File)))); Assert.Equal(frame.HasProxy ? "proxy" : "thumb", entry.FileKind); } var downloaded = media.Frames.Count(f => f.FileKind == "proxy"); Assert.Contains(Loc.L("Archive.Log.WorkspaceDownloaded", Title, downloaded, 4 - downloaded, 0, 0), job.LogTail!); Assert.SequenceEqual(library, Listing(a.Archive.RootPath), "this PC's library is as it was"); var session = await a.Sync.OpenAsync(main.Id, context.Cancellation); Assert.Equal(4, Assert.NotNull(session.Sequence).Files.Count(f => f is not null)); // A run stopped after two frames, a later file left damaged and the media index gone: the two are kept as they are, // the others downloaded again. File.WriteAllText(project.MediaFile(media.Frames[3].File!), "not a photo"); File.Delete(project.MediaIndexPath); job = await a.CopyAsync(sid, main.Id, $"copy:2:{manifest.FramesVersion}"); Assert.Contains(Loc.L("Archive.Log.WorkspaceDownloaded", Title, Downloaded(media, 2..), media.Frames.Count - 2 - Downloaded(media, 2..), 2, 0), job.LogTail!); var again = Assert.NotNull(project.ReadMedia()); Assert.SequenceEqual(media.Frames.Select(f => f.FileSha256), again.Frames.Select(f => f.FileSha256)); // Update media while nothing changed keeps every file it holds of the same photo, hashed again; one damaged since is fetched anew. job = await a.CopyAsync(sid, main.Id); Assert.Contains(Loc.L("Archive.Log.WorkspaceDownloaded", Title, 0, 0, 4, 0), job.LogTail!); File.WriteAllText(project.MediaFile(media.Frames[0].File!), "damaged"); job = await a.CopyAsync(sid, main.Id); Assert.Contains(Loc.L("Archive.Log.WorkspaceDownloaded", Title, Downloaded(media, ..1), 1 - Downloaded(media, ..1), 3, 0), job.LogTail!); Assert.Equal(media.Frames[0].FileSha256, Sha(project.MediaFile(media.Frames[0].File!))); // Positions follow their photos when the server's frames change and the copy runs again. await a.EditAsync(main.Id, s => s with { ExcludedFrames = [2] }); var uploader = server.NewClient(token); await Uploads.UploadAsync(uploader, Photo(99), "IMG_0000.JPG", sid, capturedAt: new DateTime(2026, 9, 28, 17, 59, 11)); await JobsDoneAsync(server); await a.CopyAsync(sid, main.Id); var moved = await a.Sync.OpenAsync(main.Id, context.Cancellation); Assert.Equal(5, moved.Media!.Frames.Count); Assert.SequenceEqual([3], moved.Working.ExcludedFrames); Assert.Equal(media.Frames[2].Id, moved.Media.Frames[3].Id); static int Downloaded(WorkspaceMedia media, Range frames) => media.Frames.Take(frames).Count(f => f.FileKind == "proxy"); } [Test] public async Task ACopyIsRefusedForAnotherHomeOrWithoutTheServer() { await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); var (_, _, token) = await server.UserAsync("alberto", "editor"); var sid = await SequenceAsync(server, token, 1); await using var a = await WorkspaceClient.StartAsync(context, server, token, "PC-A"); var main = (await a.Home(sid).ListAsync(context.Cancellation)).Single(); await a.Sync.CheckoutAsync(a.Home(sid), main.Id, cancellationToken: context.Cancellation); // Another server's job for this project, or this server's for another sequence: refused, nothing downloaded. var other = await a.EnqueueAsync(WorkspaceJobs.CopyFromServer(sid, Title, "https://elsewhere:8443", a.Store.Root, main.Id)); Assert.Equal(Loc.L("Errors.Workspace.OtherHome"), (await a.WaitAsync(other.Id, JobState.Failed)).Error); a.Connected = false; var signedOut = await a.EnqueueAsync(WorkspaceJobs.CopyFromServer(sid, Title, a.Server, a.Store.Root, main.Id)); Assert.Equal(Loc.L("Errors.Sync.NotConnected"), (await a.WaitAsync(signedOut.Id, JobState.Failed)).Error); Assert.Null(a.Store.Find(main.Id)!.ReadMedia()); } [Test] public async Task TheOriginalsAreDownloadedCheckedAndKeptBesideTheProxies() { 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(); var project = await a.Sync.CheckoutAsync(home, main.Id, BundleMedia.Originals, context.Cancellation); var library = Listing(a.Archive.RootPath); var job = await a.EnqueueAsync(WorkspaceJobs.CopyFromServer(sid, Title, a.Server, a.Store.Root, main.Id, BundleMedia.Originals)); job = await a.WaitAsync(job.Id, JobState.Done); Assert.Contains(Loc.L("Archive.Log.WorkspaceOriginalsDownloaded", Title, 3, 0, 0), job.LogTail!); var media = Assert.NotNull(project.ReadMedia()); Assert.True(media.HoldsOriginals); Assert.Equal(BundleMedia.Originals, project.ReadBundle().Workspace!.Media.Kind); var manifest = await a.Client.GetManifestAsync(sid, context.Cancellation); foreach (var (frame, entry) in manifest.Frames.OrderBy(f => f.Index).Zip(media.Frames)) { Assert.Equal(frame.Sha256.ToLowerInvariant(), entry.OriginalSha256); Assert.Equal(entry.OriginalSha256, Sha(project.MediaFile(Assert.NotNull(entry.Original)))); Assert.NotNull(entry.File); } Assert.False(Directory.Exists(Path.Combine(project.OriginalsDirectory, ".staging"))); Assert.SequenceEqual(library, Listing(a.Archive.RootPath), "this PC's library is as it was"); // Again: every original held is kept, hashed again. job = await a.WaitAsync((await a.EnqueueAsync(WorkspaceJobs.CopyFromServer(sid, Title, a.Server, a.Store.Root, main.Id, BundleMedia.Originals))).Id, JobState.Done); Assert.Contains(Loc.L("Archive.Log.WorkspaceOriginalsDownloaded", Title, 0, 3, 0), job.LogTail!); } [Test] public async Task AProjectDeletedOnTheServerIsGoneAndItsCopyStays() { 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); Assert.Equal("alberto", vertical.CreatedBy); await a.CheckoutAsync(home, vertical.Id); await a.EditAsync(vertical.Id, s => s with { HeroFrame = 1 }); Assert.NotNull(await a.Sync.CommitAsync(vertical.Id, cancellationToken: context.Cancellation)); // Deleted on the server (by its maker, from another program): the copy here reads as gone, with its unsent version. using (var deleted = await server.NewClient(token).DeleteAsync($"/api/v1/sequences/{sid}/projects/{vertical.Id}")) Assert.Equal(System.Net.HttpStatusCode.NoContent, deleted.StatusCode); // The server has no head for it any more (404 project): the copy here reads as gone, never as in step. Assert.Equal(ProjectError.NotFound, (await Assert.ThrowsAsync(() => home.HeadAsync(vertical.Id, context.Cancellation))).Reason); var status = await a.Sync.StatusAsync(vertical.Id, home, context.Cancellation); Assert.Equal((WorkspaceState.Gone, 0, 1), (status.State, status.Behind, status.Ahead)); // A push sends nothing to it: the copy keeps reading as gone, its version kept here. var pushed = await a.Sync.PushAsync(vertical.Id, home, context.Cancellation); Assert.Equal((WorkspaceState.Gone, 0), (pushed.Status.State, pushed.Pushed.Count)); Assert.Equal(1, a.Sync.Unpushed(vertical.Id).Count); Assert.Equal(1, (await a.Sync.OpenAsync(vertical.Id, context.Cancellation)).Working.HeroFrame); // Its versions stay readable on the server, as in its project trash. 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(() => 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() { var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); var running = true; WorkspaceClient? a = null; try { var (_, _, token) = await server.UserAsync("alberto", "editor"); var sid = await SequenceAsync(server, token, 2); 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); await a.EditAsync(main.Id, s => s with { HeroFrame = 1 }); Assert.NotNull(await a.Sync.CommitAsync(main.Id, cancellationToken: context.Cancellation)); running = false; await server.DisposeAsync(); var status = await a.Sync.StatusAsync(main.Id, home, context.Cancellation); Assert.Equal((WorkspaceState.Offline, true, 1), (status.State, status.Offline, status.Ahead)); await Assert.ThrowsAsync(() => a.Sync.PushAsync(main.Id, home, context.Cancellation)); await Assert.ThrowsAsync(() => a.Sync.PullAsync(main.Id, home, context.Cancellation)); // Nothing was lost: the version still waits, and the project still opens from the workspace. Assert.Equal(1, a.Sync.Unpushed(main.Id).Count); Assert.Equal(1, (await a.Sync.OpenAsync(main.Id, context.Cancellation)).Working.HeroFrame); } finally { if (a is not null) await a.DisposeAsync(); if (running) await server.DisposeAsync(); } } private const string Title = "harbour"; private static string Sha(string path) => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(path))); /// Every file and folder under with its size, in order. private static List Listing(string root) => [.. new DirectoryInfo(root).EnumerateFileSystemInfos("*", SearchOption.AllDirectories).Select(e => $"{Path.GetRelativePath(root, e.FullName)} {(e as FileInfo)?.Length}") .Order(StringComparer.Ordinal)]; /// A sequence of JPEG photos a minute apart, filed by the archive engine, once its jobs are done. private static async Task SequenceAsync(TestServer server, string token, int photos) { var uploader = server.NewClient(token); Guid id = default; for (var i = 0; i < photos; i++) id = (await Uploads.UploadAsync(uploader, Photo(i), $"IMG_{i + 1:0000}.JPG", sessionKey: Title, capturedAt: new DateTime(2026, 9, 28, 18, i, 11))).SequenceId; await JobsDoneAsync(server); var admin = await server.AdminAsync(); using var titled = await admin.PatchAsync($"/api/v1/sequences/{id}", System.Net.Http.Json.JsonContent.Create(new UpdateSequenceRequest(Title, null, null, null, null, null), ProtocolJson.Default.UpdateSequenceRequest)); titled.EnsureSuccessStatusCode(); return id; } /// Waits until the server's jobs (thumbnails, proxies, analyses) are done. private static async Task JobsDoneAsync(TestServer server) { var until = DateTime.UtcNow + Wait; foreach (var state in new[] { JobState.Queued, JobState.Running, JobState.Queued, JobState.Running }) { while ((await server.Database.Jobs.ListAsync(state)).Items.Any(j => j.Kind != JobKind.Render)) { Assert.True(DateTime.UtcNow < until, $"jobs still {state}"); await Task.Delay(50); } } } private static byte[] Photo(int seed) { const int width = 96, height = 64; var pixels = new byte[width * height * 3]; for (var i = 0; i < pixels.Length; i++) pixels[i] = (byte)((i * 7 + seed * 31) % 251); return JpegEncoder.Encode(pixels, width, height, 3, new JpegEncodeOptions { Quality = 90 }); } /// The desktop's requests as they go: the frames and stamps read, and an older server's answer to the frames. 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); /// The frames answer 404 naming nothing, as the API's fallback of a server without the projects does. public bool OlderServer { get; set; } protected override Task 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); } } /// 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. private sealed class WorkspaceClient : IAsyncDisposable { private readonly TestContext _context; private WorkspaceClient(TestContext context, ChronosDatabase db, Archive archive, ChronosClient client, JobEngine engine, WorkspaceStore store, string name) { _context = context; Db = db; Archive = archive; Client = client; Engine = engine; Store = store; Sync = new WorkspaceSync(store, "Chronos on " + name) { LockWait = TimeSpan.FromSeconds(TestTiming.Budget(10)) }; } public ChronosDatabase Db { get; } public Archive Archive { get; } public ChronosClient Client { get; } public JobEngine Engine { get; } public WorkspaceStore Store { get; } public WorkspaceSync Sync { get; } public string Server => SyncService.ServerKey(Client.BaseAddress); /// Whether the copy job finds this PC signed in to the server. public bool Connected { get; set; } = true; public static async Task 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, 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) }); await engine.StartAsync(context.Cancellation); var store = new WorkspaceStore(Path.Combine(context.CreateTempDirectory(name + "-ws"), "workspace")); created = new WorkspaceClient(context, db, archive, client, engine, store, name); return created; } public ServerProjectHome Home(Guid sequenceId) => new(Client, sequenceId, "Test server"); public ProjectBundle Bundle(Guid projectId) => Store.Find(projectId)!.ReadBundle(); /// The project checked out and its media downloaded. public async Task CheckoutAsync(ServerProjectHome home, Guid projectId) { await Sync.CheckoutAsync(home, projectId, cancellationToken: _context.Cancellation); await CopyAsync(home.SequenceId, projectId); } /// A copy job of the project's media from the server to its end (from when given). public async Task CopyAsync(Guid sequenceId, Guid projectId, string? checkpoint = null) { var request = WorkspaceJobs.CopyFromServer(sequenceId, Title, Server, Store.Root, projectId); var job = await EnqueueAsync(request, checkpoint); return await WaitAsync(job.Id, JobState.Done); } public async Task EnqueueAsync(Data.Archiving.Jobs.ArchiveJobRequest request, string? checkpoint = null) { if (checkpoint is null) return await Engine.EnqueueAsync(request, cancellationToken: _context.Cancellation); var job = new Job { Id = Guid.CreateVersion7(), Kind = request.Kind, TargetIds = request.Targets, TargetLabel = request.Label, ParametersJson = request.ParametersJson, State = JobState.Queued, Checkpoint = checkpoint, Created = DateTimeOffset.UtcNow, }; await Db.Jobs.EnqueueAsync(job, _context.Cancellation); Engine.Wake(); return job; } public async Task WaitAsync(Guid jobId, JobState state) { var until = DateTime.UtcNow + Wait; while (true) { var job = await Db.Jobs.GetAsync(jobId, _context.Cancellation); if (job?.State == state) return job; if (job?.State is JobState.Failed or JobState.Done or JobState.Cancelled) Assert.Fail($"Job ended {job.State}: {job.Error}\n{job.LogTail}"); if (DateTime.UtcNow > until) Assert.Fail($"Job {jobId} did not reach {state} (now {job?.State})"); await Task.Delay(25, _context.Cancellation); } } /// The working state edited as the editor does: read, changed, stored. public async Task EditAsync(Guid projectId, Func edit) { var session = await Sync.OpenAsync(projectId, _context.Cancellation); await Sync.SaveAsync(projectId, edit(session.Working), _context.Cancellation); } public async Task CommitAndPushAsync(ServerProjectHome home, Guid projectId) { Assert.NotNull(await Sync.CommitAsync(projectId, cancellationToken: _context.Cancellation)); Assert.Equal(1, (await Sync.PushAsync(projectId, home, _context.Cancellation)).Pushed.Count); } /// The project's head at the server, its positions placed on the server's frames. public async Task PlacedHeadAsync(ServerProjectHome home, Guid projectId) { var head = await home.HeadAsync(projectId, _context.Cancellation); var frames = await home.FramesAsync(_context.Cancellation); return ProjectSettings.FromJson(ProjectContent.Place(head.SettingsJson, frames.Photos)); } public async ValueTask DisposeAsync() { await Engine.DisposeAsync(); Client.Dispose(); await Db.DisposeAsync(); } /// The copy job's connection: a remote over the signed-in client while connected says so. private sealed class Connector(ChronosClient client, Func connected) : ISyncConnector { public ValueTask ConnectAsync(string server, CancellationToken cancellationToken) { ISyncRemote remote = new ChronosSyncRemote(client, ownsClient: false); return ValueTask.FromResult(connected() && remote.Server == server ? remote : null); } } } }