feat(workspace): send local renders home with their project and version
Phase 5c P3. A render made in a workspace must reach its home with the project and version it was rendered from, and transfers with a server must respect a metered connection, downloads into the workspace included. - WorkspaceSendJobHandler: per unsent ledger entry, hash the file again (missing or changed: marked failed, file left in place), push the named version first when the home lacks it (failure fails the job, entry pending), then file it in this PC's archive (staged copy read back against its SHA-256, project and version checked against the sequence) or upload it to the server with ProjectId/ProjectVersionId; mark it sent. Idempotent by render id. A send to a server waits while HoldTransfers says so. - SyncTransfers: extract FileRenderAsync, the lease-held filing of a staged render into a sequence's exports, shared by GetExportsAsync and the send. - ISyncConnector.ProjectHome (default null; SyncHost already implements it) so the send can push a version to a server home. - Server: RenderUploads checks the project fields at start and completion (422 project_mismatch) and records them; RenderDto carries them. - UploadHold also pauses WorkspaceCopy and WorkspaceSend jobs naming a server; Server screen checkbox reads "Pause metered transfers". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2e1c67f531
commit
00b8dcd249
12 files changed
+894
-53
No files matched your search
@@ -0,0 +1,62 @@
|
||||
using Chronos.App.Server;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Archiving.Jobs;
|
||||
using Chronos.Data.Workspaces;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.App.Tests.Server;
|
||||
|
||||
/// <summary>
|
||||
/// Which jobs "Pause metered transfers" holds (Server.html, phase 5c): uploads, and the workspace's copies from and renders
|
||||
/// sent to a server; never a copy from or a send to this PC's archive.
|
||||
/// </summary>
|
||||
public sealed class UploadHoldTests
|
||||
{
|
||||
private const string Server = "https://nas.local:8443";
|
||||
private static readonly Guid SequenceId = Guid.CreateVersion7();
|
||||
private static readonly Guid ProjectId = Guid.CreateVersion7();
|
||||
private const string Root = @"C:\Users\me\Chronos\workspace";
|
||||
|
||||
private static Job Of(ArchiveJobRequest request) => new()
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
Kind = request.Kind,
|
||||
TargetIds = request.Targets,
|
||||
TargetLabel = request.Label,
|
||||
ParametersJson = request.ParametersJson,
|
||||
State = JobState.Queued,
|
||||
Created = DateTimeOffset.UtcNow,
|
||||
};
|
||||
|
||||
private static Job Of(JobKind kind, string? parameters = null) => new()
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
Kind = kind,
|
||||
TargetIds = [SequenceId],
|
||||
ParametersJson = parameters,
|
||||
State = JobState.Queued,
|
||||
Created = DateTimeOffset.UtcNow,
|
||||
};
|
||||
|
||||
[Test]
|
||||
public void TransfersWithAServerAreHeld()
|
||||
{
|
||||
Assert.True(UploadHold.Holds(Of(JobKind.UploadCommit)));
|
||||
Assert.True(UploadHold.Holds(Of(WorkspaceJobs.CopyFromServer(SequenceId, "Harbour", Server, Root, ProjectId))));
|
||||
Assert.True(UploadHold.Holds(Of(WorkspaceJobs.Send(SequenceId, "Harbour", Root, ProjectId, [], Server))));
|
||||
Assert.SequenceEqual([JobKind.UploadCommit, JobKind.WorkspaceCopy, JobKind.WorkspaceSend], UploadHold.Kinds);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TransfersWithThisPcsLibraryAndOtherJobsAreNot()
|
||||
{
|
||||
var sequence = new Sequence { Id = SequenceId, ArchiveId = Guid.CreateVersion7(), Title = "Harbour", Folder = "2026/harbour" };
|
||||
Assert.False(UploadHold.Holds(Of(WorkspaceJobs.Copy(sequence, Root, ProjectId))));
|
||||
Assert.False(UploadHold.Holds(Of(WorkspaceJobs.Send(SequenceId, "Harbour", Root, ProjectId, []))));
|
||||
Assert.False(UploadHold.Holds(Of(JobKind.Download)));
|
||||
Assert.False(UploadHold.Holds(Of(JobKind.Render)));
|
||||
// Parameters that cannot be read are not held: the job fails with its reason.
|
||||
Assert.False(UploadHold.Holds(Of(JobKind.WorkspaceSend, "{not json")));
|
||||
Assert.False(UploadHold.Holds(Of(JobKind.WorkspaceCopy)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
using System.Security.Cryptography;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Tests.Archiving;
|
||||
using Chronos.Data.Tests.Infrastructure;
|
||||
using Chronos.Data.Workspaces;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Data.Tests.Workspaces;
|
||||
|
||||
/// <summary>
|
||||
/// A workspace's local renders sent home (phase 5c, <see cref="JobKind.WorkspaceSend"/>) to this PC's archive: filed with
|
||||
/// their project and version, the version pushed first when the home lacks it, a changed or missing file refused and left in
|
||||
/// the workspace, a send repeated after a cut harmless, and never held on a metered connection.
|
||||
/// </summary>
|
||||
[Category("workspace")]
|
||||
public abstract class WorkspaceSendTests(TestContext context) : DatabaseTest(context)
|
||||
{
|
||||
private static readonly DateTimeOffset Rendered = new(2026, 11, 4, 9, 0, 0, TimeSpan.Zero);
|
||||
|
||||
private async Task<WorkspaceFixture> FixtureAsync()
|
||||
{
|
||||
var archive = Own(await ArchiveFixture.CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true)));
|
||||
return Own(await WorkspaceFixture.CreateAsync(Context, archive, 3));
|
||||
}
|
||||
|
||||
/// <summary>An engine with the workspace's handlers; <paramref name="hold"/> stands for a metered connection.</summary>
|
||||
private async Task<JobEngine> EngineAsync(WorkspaceFixture fx, Func<bool>? hold = null)
|
||||
{
|
||||
var options = WorkspaceJobOptions.For(fx.Archive.Service) with { HoldTransfers = hold ?? (() => false) };
|
||||
var engine = new JobEngine(fx.Db, WorkspaceJobs.CreateHandlers(fx.Db, fx.Archive.Service, null, options),
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
|
||||
await engine.StartAsync(Ct);
|
||||
Own(engine);
|
||||
return engine;
|
||||
}
|
||||
|
||||
/// <summary>Main checked out into a workspace of its own (its media are not needed to send).</summary>
|
||||
private static async Task<(WorkspaceStore Store, WorkspaceSync Sync, WorkspaceProject Project)> CheckoutAsync(WorkspaceFixture fx)
|
||||
{
|
||||
var (store, sync) = fx.Workspace();
|
||||
var main = await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct);
|
||||
return (store, sync, await sync.CheckoutAsync(fx.Home, main.Id, cancellationToken: fx.Ct));
|
||||
}
|
||||
|
||||
/// <summary>An output in the project's exports with its ledger entry, as a local render leaves them.</summary>
|
||||
private static WorkspaceRenderEntry Render(WorkspaceProject project, Guid? versionId, string name, int seed)
|
||||
{
|
||||
Directory.CreateDirectory(project.ExportsDirectory);
|
||||
var bytes = new byte[150_000 + seed];
|
||||
new Random(seed).NextBytes(bytes);
|
||||
File.WriteAllBytes(Path.Combine(project.ExportsDirectory, name), bytes);
|
||||
var entry = new WorkspaceRenderEntry(Guid.CreateVersion7(), Guid.CreateVersion7(), name, Convert.ToHexStringLower(SHA256.HashData(bytes)), bytes.Length,
|
||||
"1080p H.264", "h264", 1920, 1080, 24, 2.5, project.ProjectId, versionId, null, Rendered);
|
||||
project.WriteRenders(project.ReadRenders().With(entry));
|
||||
return entry;
|
||||
}
|
||||
|
||||
private static string Sha(string path) => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(path)));
|
||||
|
||||
private static async Task<Job> SendAsync(WorkspaceFixture fx, JobEngine engine, WorkspaceStore store, Guid projectId, JobState expected,
|
||||
params Guid[] renderIds)
|
||||
{
|
||||
var job = await engine.EnqueueAsync(WorkspaceJobs.Send(fx.Sequence.Id, fx.Sequence.DisplayTitle, store.Root, projectId, renderIds), cancellationToken: fx.Ct);
|
||||
return await fx.Archive.WaitForAsync(job.Id, expected, (int)TestTiming.Budget(60));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheSendFilesTheRenderInTheLibraryWithItsProjectAndVersion()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var engine = await EngineAsync(fx);
|
||||
var (store, _, project) = await CheckoutAsync(fx);
|
||||
var head = project.ReadBundle().Workspace!.Base!.VersionId;
|
||||
var entry = Render(project, head, "Main 1080p.mp4", 1);
|
||||
|
||||
await SendAsync(fx, engine, store, project.ProjectId, JobState.Done);
|
||||
|
||||
var row = Assert.NotNull(await fx.Db.Renders.GetAsync(entry.RenderId, fx.Ct));
|
||||
Assert.Equal((fx.Sequence.Id, (Guid?)project.ProjectId, (Guid?)head, "1080p H.264", entry.Size, 1920),
|
||||
(row.SequenceId, row.ProjectId, row.ProjectVersionId, row.PresetName, row.Bytes, row.Width));
|
||||
var filed = Path.Combine(fx.Archive.Archive.RootPath, row.OutputPath);
|
||||
Assert.Equal(entry.Sha256, Sha(filed));
|
||||
Assert.Equal("Main 1080p.mp4", Path.GetFileName(filed));
|
||||
// The workspace keeps its file until a clean-up; the ledger says it went home.
|
||||
Assert.Equal(entry.Sha256, Sha(project.ExportFile(entry.File)));
|
||||
var sent = Assert.NotNull(project.ReadRenders().Find(entry.RenderId));
|
||||
Assert.True(sent.IsSent);
|
||||
Assert.NotNull(sent.Sent);
|
||||
Assert.Empty(Directory.GetFiles(fx.Archive.Paths.StagingDirectory, "workspace-render-*"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASendRepeatedAfterACutFilesNothingTwice()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var engine = await EngineAsync(fx);
|
||||
var (store, _, project) = await CheckoutAsync(fx);
|
||||
var entry = Render(project, project.ReadBundle().Workspace!.Base!.VersionId, "Main.mp4", 2);
|
||||
await SendAsync(fx, engine, store, project.ProjectId, JobState.Done, entry.RenderId);
|
||||
|
||||
// Cut after the home filed it, before the ledger said so: the next send finds it there and only marks it sent.
|
||||
project.WriteRenders(project.ReadRenders().With(entry));
|
||||
Assert.True(project.ReadRenders().Find(entry.RenderId)!.IsUnsent);
|
||||
var again = await SendAsync(fx, engine, store, project.ProjectId, JobState.Done);
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceSent", project.ReadBundle().Workspace!.Name, 0, 1), again.LogTail);
|
||||
Assert.True(project.ReadRenders().Find(entry.RenderId)!.IsSent);
|
||||
Assert.Equal(1, (await fx.Db.Renders.ListBySequenceAsync(fx.Sequence.Id, fx.Ct)).Count);
|
||||
var exports = Path.GetDirectoryName(Path.Combine(fx.Archive.Archive.RootPath, (await fx.Db.Renders.GetAsync(entry.RenderId, fx.Ct))!.OutputPath))!;
|
||||
Assert.Equal(1, Directory.GetFiles(exports).Length);
|
||||
|
||||
// A send with nothing unsent is done at once.
|
||||
var nothing = await SendAsync(fx, engine, store, project.ProjectId, JobState.Done);
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceSent", project.ReadBundle().Workspace!.Name, 0, 0), nothing.LogTail);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AVersionTheHomeLacksIsSentFirst()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var engine = await EngineAsync(fx);
|
||||
// With its media: a version is saved only of frames the workspace holds.
|
||||
var (store, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var session = await sync.OpenAsync(project.ProjectId, fx.Ct);
|
||||
await sync.SaveAsync(project.ProjectId, session.Working with { HeroFrame = 2 }, fx.Ct);
|
||||
var local = Assert.NotNull(await sync.CommitAsync(project.ProjectId, "Render", cancellationToken: fx.Ct));
|
||||
Assert.Null(await fx.Home.GetVersionAsync(project.ProjectId, local.Id, fx.Ct));
|
||||
var entry = Render(project, local.Id, "Hero.mp4", 3);
|
||||
|
||||
var job = await SendAsync(fx, engine, store, project.ProjectId, JobState.Done);
|
||||
|
||||
var pushed = Assert.NotNull(await fx.Home.GetVersionAsync(project.ProjectId, local.Id, fx.Ct));
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceVersionPushed", project.ReadBundle().Workspace!.Name, pushed.Number), job.LogTail);
|
||||
Assert.Equal((Guid?)local.Id, (await fx.Db.Renders.GetAsync(entry.RenderId, fx.Ct))!.ProjectVersionId);
|
||||
Assert.Equal(WorkspaceState.InSync, (await sync.StatusAsync(project.ProjectId, fx.Home, fx.Ct)).State);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AChangedOrMissingRenderIsRefusedAndTheOthersGoOn()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var engine = await EngineAsync(fx);
|
||||
var (store, _, project) = await CheckoutAsync(fx);
|
||||
var head = project.ReadBundle().Workspace!.Base!.VersionId;
|
||||
var changed = Render(project, head, "Changed.mp4", 4);
|
||||
var gone = Render(project, head, "Gone.mp4", 5);
|
||||
var good = Render(project, head, "Good.mp4", 6);
|
||||
var original = File.ReadAllBytes(project.ExportFile(changed.File));
|
||||
File.AppendAllText(project.ExportFile(changed.File), "edited");
|
||||
File.Delete(project.ExportFile(gone.File));
|
||||
|
||||
var failed = await SendAsync(fx, engine, store, project.ProjectId, JobState.Failed);
|
||||
|
||||
Assert.Equal(Loc.L("Errors.Workspace.SendFailed", 2, 3), failed.Error);
|
||||
var ledger = project.ReadRenders();
|
||||
Assert.True(ledger.Find(good.RenderId)!.IsSent);
|
||||
Assert.Equal((WorkspaceRenderEntry.Failed, Loc.L("Errors.Workspace.SendHashMismatch", "Changed.mp4")),
|
||||
(ledger.Find(changed.RenderId)!.State, ledger.Find(changed.RenderId)!.Error));
|
||||
Assert.Equal((WorkspaceRenderEntry.Failed, Loc.L("Errors.Workspace.SendMissing", "Gone.mp4")),
|
||||
(ledger.Find(gone.RenderId)!.State, ledger.Find(gone.RenderId)!.Error));
|
||||
Assert.Null(await fx.Db.Renders.GetAsync(changed.RenderId, fx.Ct));
|
||||
Assert.Null(await fx.Db.Renders.GetAsync(gone.RenderId, fx.Ct));
|
||||
// The changed file stays in the workspace as it is.
|
||||
Assert.FileExists(project.ExportFile(changed.File));
|
||||
|
||||
// Put back as rendered (and the missing one discarded), the next send takes it.
|
||||
File.WriteAllBytes(project.ExportFile(changed.File), original);
|
||||
project.WriteRenders(project.ReadRenders().Without(gone.RenderId));
|
||||
await SendAsync(fx, engine, store, project.ProjectId, JobState.Done);
|
||||
Assert.True(project.ReadRenders().Find(changed.RenderId)!.IsSent);
|
||||
Assert.Equal(2, (await fx.Db.Renders.ListBySequenceAsync(fx.Sequence.Id, fx.Ct)).Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AVersionOfAnotherProjectIsNeverFiledWithTheRender()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var engine = await EngineAsync(fx);
|
||||
var (store, _, project) = await CheckoutAsync(fx);
|
||||
var other = await fx.Home.CreateAsync("Vertical", null, null, fx.Ct);
|
||||
var foreign = (await fx.Home.HeadAsync(other.Id, fx.Ct)).Id;
|
||||
var entry = Render(project, foreign, "Foreign.mp4", 7);
|
||||
|
||||
var failed = await SendAsync(fx, engine, store, project.ProjectId, JobState.Failed);
|
||||
|
||||
Assert.Equal(Loc.L("Errors.Workspace.SendVersion"), failed.Error);
|
||||
Assert.Null(await fx.Db.Renders.GetAsync(entry.RenderId, fx.Ct));
|
||||
// Not the render's fault: it waits, unsent, for the next send.
|
||||
Assert.Equal(WorkspaceRenderEntry.Pending, project.ReadRenders().Find(entry.RenderId)!.State);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASendToThisPcsLibraryIsNeverHeldOnAMeteredConnection()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var engine = await EngineAsync(fx, hold: () => true);
|
||||
var (store, _, project) = await CheckoutAsync(fx);
|
||||
var entry = Render(project, null, "Working.mp4", 8);
|
||||
|
||||
var job = await SendAsync(fx, engine, store, project.ProjectId, JobState.Done);
|
||||
|
||||
Assert.False(job.LogTail?.Contains(Loc.L("Archive.Log.WorkspaceSendHeld"), StringComparison.Ordinal) ?? false);
|
||||
var row = Assert.NotNull(await fx.Db.Renders.GetAsync(entry.RenderId, fx.Ct));
|
||||
Assert.Equal(((Guid?)project.ProjectId, (Guid?)null), (row.ProjectId, row.ProjectVersionId));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SqliteWorkspaceSendTests(TestContext context) : WorkspaceSendTests(context)
|
||||
{
|
||||
protected override Backend Backend => Backend.SqliteFile;
|
||||
}
|
||||
|
||||
[RequiresEnvironment(PgTestServer.Variable)]
|
||||
public sealed class PostgresWorkspaceSendTests(TestContext context) : WorkspaceSendTests(context)
|
||||
{
|
||||
protected override Backend Backend => Backend.Postgres;
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
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;
|
||||
var options = WorkspaceJobOptions.For(archives) with { HoldTransfers = () => Volatile.Read(ref metered) };
|
||||
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 Task.Delay(TimeSpan.FromSeconds(1), 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));
|
||||
}
|
||||
|
||||
/// <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