Owner review 2, R8, phase 5b (client part), ported onto the final phase 5a.
A Chronos Server with the "projects" capability is now the home of
workspace projects. A server sequence is edited in the workspace like a
local one, not inside the library, and its versions sync with the server
"like git".
- Data: ServerProjectHome is an IProjectHome over ChronosClient.
- These are HomeUnavailableException, so the workspace shows Offline: a
server out of reach, an expired sign-in, a 503 (lease, offline archive,
projects_unreadable), a 409 folder_in_use, and a server without projects.
- 404 sequence/project/version and refused names are ProjectException;
other refusals keep their meaning.
- A deleted project's head is 404 project, so final 5a's HeadAsync NotFound
rule reads it as Gone. The original 5b's IsLiveAsync and
WorkspaceStatus.Gone are dropped. IProjectHome gains rename and delete.
- Data: WorkspaceJobs.CopyFromServer downloads into the workspace, through
the desktop's session, the proxies of one frames version (or the
thumbnails standing in for them), with the aliases of the server's
conversions. It is rebuilt on final 5a's copy handler:
- the copy:frame:fv:folder checkpoint, EnsureSpace (from the server's proxy
sizes), WritingMedia, and the cancelled-copy clean-up;
- the editor's current file is never overwritten;
- each file is hashed as it arrives and checked on disk, four at a time.
It never writes this PC's archive. Originals are still refused until 5c.
- Data: SyncAgent passes on the server's projectChanged events, and after a
lost event stream it reports that changes may have been missed.
- App: SyncHost gives the server home over one session client. The Server
screen offers Open in workspace, disabled with its reason on older
servers, whose sequences keep opening with proxies.
- App: Edit opens or checks out server projects with final 5a's refusals:
unreadable copies, a disabled damaged or newer Main, a refused folder. The
"sequence left the index" refusal now applies only to projects of this
PC's archive.
- App: the picker and the chip follow projectChanged (Server newer, Not on
server). Every state, conflict, delete, remove and failure text uses its
".Server" form when one exists (WorkspaceText.ForHome). That includes 5a's
newer-format conflict, the save conflict, the taken-values toast, Gone and
version-too-large.
- App: a render of a server project first offers to send what is not on the
server, then renders the version the server holds. Offline, Gone, Damaged
and missing media hold the render. Analyses and renders run as server jobs
(ServerJobs, shared with RemoteSequenceSource).
- Pruning keeps final 5a's rule: a version is pruned only after HoldsAsync
reads it back.
- Strings in en and it; ARCHITECTURE, DECISIONS and CHANGELOG.
Tests:
- ServerProjectHome error mapping, using a fake handler.
- Against the in-process server:
- two workspaces on one project: fast-forward, replay and merge, keep mine,
take server, save both, restore;
- the copy job verifying, resuming and following new frames;
- a project deleted on the server (Gone from its head; a push sends
nothing);
- an offline server.
- SyncAgent project events.
- App tests for the host, the project menu and renders of a server home.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
451 lines
27 KiB
C#
451 lines
27 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// A and B change one setting from the same version, A first: B's push stops at a conflict, which B settles as
|
|
/// <paramref name="choice"/>; returns Main's head at the server as placed on its frames.
|
|
/// </summary>
|
|
private async Task<ProjectSettings> 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);
|
|
var originals = await a.EnqueueAsync(WorkspaceJobs.CopyFromServer(sid, Title, a.Server, a.Store.Root, main.Id, BundleMedia.Originals));
|
|
Assert.Equal(Loc.L("Errors.Workspace.OriginalsLater"), (await a.WaitAsync(originals.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 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<ProjectException>(() => 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 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<HomeUnavailableException>(() => a.Sync.PushAsync(main.Id, home, context.Cancellation));
|
|
await Assert.ThrowsAsync<HomeUnavailableException>(() => 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)));
|
|
|
|
/// <summary>Every file and folder under <paramref name="root"/> with its size, in order.</summary>
|
|
private static List<string> Listing(string root) =>
|
|
[.. new DirectoryInfo(root).EnumerateFileSystemInfos("*", SearchOption.AllDirectories).Select(e => $"{Path.GetRelativePath(root, e.FullName)} {(e as FileInfo)?.Length}")
|
|
.Order(StringComparer.Ordinal)];
|
|
|
|
/// <summary>A sequence of <paramref name="photos"/> JPEG photos a minute apart, filed by the archive engine, once its jobs are done.</summary>
|
|
private static async Task<Guid> 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;
|
|
}
|
|
|
|
/// <summary>Waits until the server's jobs (thumbnails, proxies, analyses) are done.</summary>
|
|
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 });
|
|
}
|
|
|
|
/// <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
|
|
{
|
|
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);
|
|
|
|
/// <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)
|
|
{
|
|
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 };
|
|
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();
|
|
|
|
/// <summary>The project checked out and its media downloaded.</summary>
|
|
public async Task CheckoutAsync(ServerProjectHome home, Guid projectId)
|
|
{
|
|
await Sync.CheckoutAsync(home, projectId, cancellationToken: _context.Cancellation);
|
|
await CopyAsync(home.SequenceId, projectId);
|
|
}
|
|
|
|
/// <summary>A copy job of the project's media from the server to its end (from <paramref name="checkpoint"/> when given).</summary>
|
|
public async Task<Job> 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<Job> 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<Job> 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);
|
|
}
|
|
}
|
|
|
|
/// <summary>The working state edited as the editor does: read, changed, stored.</summary>
|
|
public async Task EditAsync(Guid projectId, Func<ProjectSettings, ProjectSettings> 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);
|
|
}
|
|
|
|
/// <summary>The project's head at the server, its positions placed on the server's frames.</summary>
|
|
public async Task<ProjectSettings> 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();
|
|
}
|
|
|
|
/// <summary>The copy job's connection: a remote over the signed-in client while <c>connected</c> says so.</summary>
|
|
private sealed class Connector(ChronosClient client, Func<bool> connected) : ISyncConnector
|
|
{
|
|
public ValueTask<ISyncRemote?> ConnectAsync(string server, CancellationToken cancellationToken)
|
|
{
|
|
ISyncRemote remote = new ChronosSyncRemote(client, ownsClient: false);
|
|
return ValueTask.FromResult<ISyncRemote?>(connected() && remote.Server == server ? remote : null);
|
|
}
|
|
}
|
|
}
|
|
}
|