feat: owner review 2 phase 5c: originals, local render, clean-up and the workspace settings (#18)
CI / Build and test (push) Successful in 9m26s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 8s
Release / Build, test and publish (push) Successful in 14m31s
CI / PostgreSQL 16 integration (push) Successful in 9m53s
CI / PostgreSQL 17 integration (push) Successful in 9m22s
CI / Docker image (push) Successful in 44s
Release / Server image (push) Successful in 1m36s
CI / Build and test (push) Successful in 9m26s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 8s
Release / Build, test and publish (push) Successful in 14m31s
CI / PostgreSQL 16 integration (push) Successful in 9m53s
CI / PostgreSQL 17 integration (push) Successful in 9m22s
CI / Docker image (push) Successful in 44s
Release / Server image (push) Successful in 1m36s
Originals in the workspace, local render and send home with provenance, the workspace clean-up, size limit and trash, moving the workspace folder, and the complete Settings › Workspace page (owner review 2, R8, phase 5c). One adversarial review round (35 findings, 32 fixed), a copy-cancel race fixed, 184 strings in all 38 languages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #18.
This commit is contained in:
1 parent
81c303f484
commit
db389a54f2
364 files changed
+17417
-595
No files matched your search
@@ -0,0 +1,233 @@
|
||||
using System.Net;
|
||||
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 render made in a workspace and sent to its server (phase 5c): the server checks the project and version it names
|
||||
/// against the sequence (422 <c>project_mismatch</c>) and records them with the render; one naming neither is filed as
|
||||
/// before. The desktop's <see cref="JobKind.WorkspaceSend"/> uploads it with them, and waits while a metered connection holds
|
||||
/// transfers with the server.
|
||||
/// </summary>
|
||||
[Category("server")]
|
||||
[Category("workspace")]
|
||||
public sealed class WorkspaceSendServerTests(TestContext context)
|
||||
{
|
||||
private const string Title = "Rooftop";
|
||||
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(TestTiming.Budget(60));
|
||||
|
||||
[Test]
|
||||
public async Task TheServerFilesAWorkspaceRenderWithItsProjectAndVersion()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (http, _, token) = await server.UserAsync("alberto", "editor");
|
||||
var sid = (await Uploads.UploadAsync(http, Photo(1), "IMG_0001.JPG", sessionKey: "roof")).SequenceId;
|
||||
using var client = new ChronosClient(server.HttpBase, new CertificatePinning([]), "PC-A") { Token = token };
|
||||
var home = new ServerProjectHome(client, sid, "Test server");
|
||||
var main = (await home.ListAsync(context.Cancellation)).Single();
|
||||
var head = await home.HeadAsync(main.Id, context.Cancellation);
|
||||
|
||||
var (path, request) = Output(1);
|
||||
var dto = Assert.NotNull(await client.UploadRenderAsync(sid, path, request with { ProjectId = main.Id, ProjectVersionId = head.Id }, ct: context.Cancellation));
|
||||
Assert.Equal(((Guid?)main.Id, (Guid?)head.Id), (dto.ProjectId, dto.ProjectVersionId));
|
||||
var row = Assert.NotNull(await server.Database.Renders.GetAsync(request.RenderId));
|
||||
Assert.Equal((sid, (Guid?)main.Id, (Guid?)head.Id), (row.SequenceId, row.ProjectId, row.ProjectVersionId));
|
||||
Assert.Contains((await client.GetManifestAsync(sid, context.Cancellation)).Renders, r => r.Id == request.RenderId && r.ProjectVersionId == head.Id);
|
||||
|
||||
// A version alone names its project.
|
||||
var (alone, versionOnly) = Output(2);
|
||||
var named = Assert.NotNull(await client.UploadRenderAsync(sid, alone, versionOnly with { ProjectVersionId = head.Id }, ct: context.Cancellation));
|
||||
Assert.Equal(((Guid?)main.Id, (Guid?)head.Id), (named.ProjectId, named.ProjectVersionId));
|
||||
|
||||
// Sent again: the server holds it already, and nothing is filed twice.
|
||||
Assert.Null(await client.UploadRenderAsync(sid, path, request with { ProjectId = main.Id, ProjectVersionId = head.Id }, ct: context.Cancellation));
|
||||
Assert.Equal(2, (await server.Database.Renders.ListBySequenceAsync(sid)).Count);
|
||||
|
||||
// Without them, as from an earlier client: filed naming neither.
|
||||
var (plain, earlier) = Output(3);
|
||||
var before = Assert.NotNull(await client.UploadRenderAsync(sid, plain, earlier, ct: context.Cancellation));
|
||||
Assert.Equal(((Guid?)null, (Guid?)null), (before.ProjectId, before.ProjectVersionId));
|
||||
Assert.Null((await server.Database.Renders.GetAsync(earlier.RenderId))!.ProjectVersionId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProjectOrVersionOfAnotherSequenceIsRefused()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (http, _, token) = await server.UserAsync("alberto", "editor");
|
||||
var sid = (await Uploads.UploadAsync(http, Photo(1), "IMG_0001.JPG", sessionKey: "roof")).SequenceId;
|
||||
var other = (await Uploads.UploadAsync(http, Photo(2), "IMG_0002.JPG", sessionKey: "lake", capturedAt: new DateTime(2026, 10, 3, 9, 0, 0))).SequenceId;
|
||||
Assert.NotEqual(sid, other);
|
||||
using var client = new ChronosClient(server.HttpBase, new CertificatePinning([]), "PC-A") { Token = token };
|
||||
var main = (await new ServerProjectHome(client, sid).ListAsync(context.Cancellation)).Single();
|
||||
var foreignHome = new ServerProjectHome(client, other);
|
||||
var foreign = (await foreignHome.ListAsync(context.Cancellation)).Single();
|
||||
var foreignHead = await foreignHome.HeadAsync(foreign.Id, context.Cancellation);
|
||||
|
||||
var (path, request) = Output(4);
|
||||
foreach (var named in new[]
|
||||
{
|
||||
request with { ProjectId = main.Id, ProjectVersionId = foreignHead.Id },
|
||||
request with { ProjectId = foreign.Id },
|
||||
request with { ProjectVersionId = foreignHead.Id },
|
||||
request with { ProjectId = Guid.CreateVersion7() },
|
||||
})
|
||||
{
|
||||
var refused = await Assert.ThrowsAsync<ChronosApiException>(() => client.UploadRenderAsync(sid, path, named, ct: context.Cancellation));
|
||||
Assert.Equal((HttpStatusCode.UnprocessableEntity, "project_mismatch"), (refused.Status, refused.Error.Code));
|
||||
}
|
||||
Assert.Null(await server.Database.Renders.GetAsync(request.RenderId));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheSendUploadsTheRenderWithItsVersionOnceTheConnectionIsNoLongerMetered()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (http, _, token) = await server.UserAsync("alberto", "editor");
|
||||
var sid = (await Uploads.UploadAsync(http, Photo(1), "IMG_0001.JPG", sessionKey: "roof")).SequenceId;
|
||||
using var client = new ChronosClient(server.HttpBase, new CertificatePinning([]), "PC-A") { Token = token };
|
||||
var serverKey = SyncService.ServerKey(client.BaseAddress);
|
||||
await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("pc-db"), "chronos.db")), context.Cancellation);
|
||||
var archives = new ArchiveService(db, new ArchiveServiceOptions { Owner = "PC-A" });
|
||||
var metered = true;
|
||||
// Signalled once the send asks: from then on it waits, and nothing it holds went out.
|
||||
var asked = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var options = WorkspaceJobOptions.For(archives) with
|
||||
{
|
||||
HoldTransfers = () =>
|
||||
{
|
||||
var held = Volatile.Read(ref metered);
|
||||
if (held) asked.TrySetResult();
|
||||
return held;
|
||||
},
|
||||
};
|
||||
await using var engine = new JobEngine(db, WorkspaceJobs.CreateHandlers(db, archives, new Connector(client), options),
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "PC-A", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
|
||||
await engine.StartAsync(context.Cancellation);
|
||||
var store = new WorkspaceStore(Path.Combine(context.CreateTempDirectory("pc-ws"), "workspace"));
|
||||
var sync = new WorkspaceSync(store, "Chronos on PC-A") { LockWait = TimeSpan.FromSeconds(TestTiming.Budget(10)) };
|
||||
var home = new ServerProjectHome(client, sid, "Test server");
|
||||
var main = (await home.ListAsync(context.Cancellation)).Single();
|
||||
var project = await sync.CheckoutAsync(home, main.Id, cancellationToken: context.Cancellation);
|
||||
var head = project.ReadBundle().Workspace!.Base!.VersionId;
|
||||
|
||||
Directory.CreateDirectory(project.ExportsDirectory);
|
||||
var bytes = RandomNumberGenerator.GetBytes(120_000);
|
||||
File.WriteAllBytes(Path.Combine(project.ExportsDirectory, "Roof 1080p.mp4"), bytes);
|
||||
var entry = new WorkspaceRenderEntry(Guid.CreateVersion7(), Guid.CreateVersion7(), "Roof 1080p.mp4", Convert.ToHexStringLower(SHA256.HashData(bytes)),
|
||||
bytes.Length, "1080p H.264", "h264", 1920, 1080, 24, 2.5, main.Id, head, null, DateTimeOffset.UtcNow);
|
||||
project.WriteRenders(WorkspaceRenders.Empty.With(entry));
|
||||
|
||||
// Held while the connection is metered: nothing goes out.
|
||||
var job = await engine.EnqueueAsync(WorkspaceJobs.Send(sid, Title, store.Root, main.Id, [entry.RenderId], serverKey), cancellationToken: context.Cancellation);
|
||||
await asked.Task.WaitAsync(Wait, context.Cancellation);
|
||||
Assert.Equal(JobState.Running, (await db.Jobs.GetAsync(job.Id, context.Cancellation))!.State);
|
||||
Assert.Null(await server.Database.Renders.GetAsync(entry.RenderId));
|
||||
Assert.True(project.ReadRenders().Find(entry.RenderId)!.IsUnsent);
|
||||
|
||||
Volatile.Write(ref metered, false);
|
||||
var done = await WaitAsync(db, job.Id, JobState.Done);
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceSendHeld"), done.LogTail!);
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceSent", main.Name, 1, 0), done.LogTail!);
|
||||
var row = Assert.NotNull(await server.Database.Renders.GetAsync(entry.RenderId));
|
||||
Assert.Equal((sid, (Guid?)main.Id, (Guid?)head, entry.Size), (row.SequenceId, row.ProjectId, row.ProjectVersionId, row.Bytes));
|
||||
Assert.True(project.ReadRenders().Find(entry.RenderId)!.IsSent);
|
||||
Assert.FileExists(project.ExportFile(entry.File));
|
||||
// This PC's library is never written: the server is the home.
|
||||
Assert.Empty(await db.Renders.ListBySequenceAsync(sid, context.Cancellation));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnImageSequenceRenderedHereStaysInTheWorkspaceForAServerHome()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (http, _, token) = await server.UserAsync("alberto", "editor");
|
||||
var sid = (await Uploads.UploadAsync(http, Photo(1), "IMG_0001.JPG", sessionKey: "roof")).SequenceId;
|
||||
using var client = new ChronosClient(server.HttpBase, new CertificatePinning([]), "PC-A") { Token = token };
|
||||
await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("pc-db"), "chronos.db")), context.Cancellation);
|
||||
var archives = new ArchiveService(db, new ArchiveServiceOptions { Owner = "PC-A" });
|
||||
await using var engine = new JobEngine(db, WorkspaceJobs.CreateHandlers(db, archives, new Connector(client), WorkspaceJobOptions.For(archives)),
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "PC-A", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
|
||||
await engine.StartAsync(context.Cancellation);
|
||||
var store = new WorkspaceStore(Path.Combine(context.CreateTempDirectory("pc-ws"), "workspace"));
|
||||
var sync = new WorkspaceSync(store, "Chronos on PC-A") { LockWait = TimeSpan.FromSeconds(TestTiming.Budget(10)) };
|
||||
var home = new ServerProjectHome(client, sid, "Test server");
|
||||
var main = (await home.ListAsync(context.Cancellation)).Single();
|
||||
var project = await sync.CheckoutAsync(home, main.Id, cancellationToken: context.Cancellation);
|
||||
|
||||
// A TIFF sequence: a folder of frames, which a server's render (one file each) cannot take.
|
||||
var folder = Path.Combine(project.ExportsDirectory, "Roof frames");
|
||||
Directory.CreateDirectory(folder);
|
||||
for (var i = 1; i <= 3; i++) File.WriteAllBytes(Path.Combine(folder, $"{i:0000}.tif"), RandomNumberGenerator.GetBytes(4000));
|
||||
var entry = new WorkspaceRenderEntry(Guid.CreateVersion7(), Guid.CreateVersion7(), "Roof frames", new string('a', 64), 12_000, "TIFF sequence", "tiff",
|
||||
1920, 1080, 24, 0.125, main.Id, null, null, DateTimeOffset.UtcNow);
|
||||
project.WriteRenders(WorkspaceRenders.Empty.With(entry));
|
||||
|
||||
var job = await engine.EnqueueAsync(WorkspaceJobs.Send(sid, Title, store.Root, main.Id, [], SyncService.ServerKey(client.BaseAddress)),
|
||||
cancellationToken: context.Cancellation);
|
||||
var failed = await WaitAsync(db, job.Id, JobState.Failed);
|
||||
|
||||
Assert.Equal(Loc.L("Errors.Workspace.SendFailed", 1, 1), failed.Error);
|
||||
var kept = Assert.NotNull(project.ReadRenders().Find(entry.RenderId));
|
||||
Assert.Equal((WorkspaceRenderEntry.Failed, Loc.L("Errors.Workspace.SendSequence", "Roof frames")), (kept.State, kept.Error));
|
||||
Assert.Null(await server.Database.Renders.GetAsync(entry.RenderId));
|
||||
Assert.Equal(3, Directory.GetFiles(folder).Length);
|
||||
}
|
||||
|
||||
/// <summary>A render's file on this PC and the offer of it, naming no project.</summary>
|
||||
private (string Path, RenderUploadRequest Request) Output(int seed)
|
||||
{
|
||||
var data = Uploads.Photo(4000 + seed, seed);
|
||||
var path = System.IO.Path.Combine(context.CreateTempDirectory("render"), $"Roof {seed}.mp4");
|
||||
File.WriteAllBytes(path, data);
|
||||
return (path, new RenderUploadRequest(Guid.CreateVersion7(), System.IO.Path.GetFileName(path), data.Length, Uploads.Sha(data), "1080p H.264", "h264", 1920, 1080,
|
||||
24, 2.5, "PC-A"));
|
||||
}
|
||||
|
||||
private static async Task<Job> WaitAsync(ChronosDatabase db, Guid jobId, JobState state)
|
||||
{
|
||||
var until = DateTime.UtcNow + Wait;
|
||||
while (true)
|
||||
{
|
||||
var job = await db.Jobs.GetAsync(jobId);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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>The desktop's session with the test server: a remote and the project home over the signed-in client.</summary>
|
||||
private sealed class Connector(ChronosClient client) : ISyncConnector
|
||||
{
|
||||
public ValueTask<ISyncRemote?> ConnectAsync(string server, CancellationToken cancellationToken)
|
||||
{
|
||||
ISyncRemote remote = new ChronosSyncRemote(client, ownsClient: false);
|
||||
return ValueTask.FromResult<ISyncRemote?>(remote.Server == server ? remote : null);
|
||||
}
|
||||
|
||||
public IProjectHome? ProjectHome(BundleHome home) => new ServerProjectHome(client, home.SequenceId, "Test server");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user