Files
Chronos/tests/Chronos.Data.Tests/Workspaces/WorkspaceFixture.cs
T
Alby96 03d32f18d3 feat(projects): workspace projects with linear versions, the local archive as their home
Owner review 2, phase 5a (R8): editing moves out of the library into a workspace on a fast disk, and a
sequence gets several projects, each with a linear version history pushed and pulled like git.

- Core: ProjectSettings keeps members a newer Chronos wrote (Extra) and reads a newer settings format
  read-only; ProjectContent is the portable form of a version (every frame position parked by photo,
  canonical text, SHA-256 content hash); ProjectBundle format 2 adds the workspace section, and a file
  without it is still written as format 1. JobKind.WorkspaceCopy = 20 (19 kept for Convert).
- Data: migration 0009 (projects, project_versions, the render's project columns); ProjectService on
  ArchiveService.Projects keeps projects as sidecars first (.chronos/projects/<id>/), then the index,
  under the sequence lease: create, rename, delete into the projects' trash, history, push with
  fast-forward, idempotent repeat, divergence and hash checks. Main is made from the sequence's settings
  and mirrored both ways, so every older code path keeps working; outside writes become "external"
  versions, a mirror a crash cut short is completed or merged. Full scans and rebuilds take the projects
  back from their sidecars.
- ProjectMerge moves from the App to Data/Projects, merges pending frames photo by photo and replays
  unpushed commits on a moved head.
- Workspaces: WorkspaceStore, WorkspaceSync (checkout, save, commit, push, pull with replay and merge,
  conflicts settled by keep mine / take server / save both, restore, status) and the WorkspaceCopy job
  (proxies copied hash-verified and resumable; originals refused until phase 5c).
- Renders of a project version place its positions on the frames and record the project and version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit c4c1802884e403db8759ed0501f770a67a047f20)
2026-10-27 22:51:32 +01:00

109 lines
5.5 KiB
C#

using Chronos.Core.Model;
using Chronos.Data.Archiving;
using Chronos.Data.Jobs;
using Chronos.Data.Projects;
using Chronos.Data.Tests.Archiving;
using Chronos.Data.Tests.Projects;
using Chronos.Data.Workspaces;
using Chronos.Testing;
namespace Chronos.Data.Tests.Workspaces;
/// <summary>An archive with an uploaded sequence as the projects' home, and workspaces that edit it (owner review 2, R8).</summary>
internal sealed class WorkspaceFixture : IAsyncDisposable
{
private readonly TestContext _context;
private JobEngine? _engine;
private WorkspaceFixture(TestContext context, ArchiveFixture archive, Sequence sequence, List<Frame> frames)
{
_context = context;
Archive = archive;
Sequence = sequence;
Frames = frames;
Home = new ArchiveProjectHome(archive.Db, archive.Service.Projects, sequence.Id);
}
public ArchiveFixture Archive { get; }
public Sequence Sequence { get; set; }
public List<Frame> Frames { get; }
public ArchiveProjectHome Home { get; }
public ChronosDatabase Db => Archive.Db;
public CancellationToken Ct => Archive.Ct;
public static async Task<WorkspaceFixture> CreateAsync(TestContext context, ArchiveFixture archive, int frames = 5)
{
var (sequence, list) = await ProjectServiceTests.UploadedAsync(archive, frames);
return new WorkspaceFixture(context, archive, sequence, list);
}
/// <summary>A workspace of its own (another PC, or another folder of this one) and its sync.</summary>
public (WorkspaceStore Store, WorkspaceSync Sync) Workspace(string name = "workspace")
{
var store = new WorkspaceStore(Path.Combine(_context.CreateTempDirectory(name), "workspace"));
return (store, new WorkspaceSync(store, "Chronos on " + name) { LockWait = TimeSpan.FromSeconds(TestTiming.Budget(10)) });
}
/// <summary>Main of the sequence checked out into <paramref name="sync"/>'s workspace, its media copied.</summary>
public async Task<WorkspaceProject> CheckoutAsync(WorkspaceSync sync, Guid? projectId = null)
{
var id = projectId ?? (await Archive.Service.Projects.MainAsync(Sequence.Id, Ct)).Id;
var project = await sync.CheckoutAsync(Home, id, cancellationToken: Ct);
await CopyAsync(sync.Store, id);
return project;
}
/// <summary>Runs a workspace copy job to its end.</summary>
public async Task<Job> CopyAsync(WorkspaceStore store, Guid projectId, JobState expected = JobState.Done, string? checkpoint = null, string media = "proxies")
{
var engine = await EngineAsync();
var request = WorkspaceJobs.Copy(Sequence, store.Root, projectId, media);
var job = checkpoint is null ? await engine.EnqueueAsync(request, cancellationToken: Ct) : await Archive.EnqueueWithCheckpointAsync(engine, request, checkpoint);
return await Archive.WaitForAsync(job.Id, expected, (int)TestTiming.Budget(60));
}
private async Task<JobEngine> EngineAsync()
{
if (_engine is not null) return _engine;
_engine = new JobEngine(Db, [.. Archive.Service.CreateJobHandlers(), .. WorkspaceJobs.CreateHandlers(Db, Archive.Service)],
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
await _engine.StartAsync(Ct);
return _engine;
}
public async ValueTask DisposeAsync()
{
if (_engine is not null) await _engine.DisposeAsync();
}
}
/// <summary>A home that cannot be reached, standing for a server out of reach or an archive offline.</summary>
internal sealed class UnreachableHome(IProjectHome home) : IProjectHome
{
public bool Reachable { get; set; }
private T Reach<T>(Func<T> call) => Reachable ? call() : throw new HomeUnavailableException("The home cannot be reached.");
public Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken) => Reach(() => home.SequenceAsync(cancellationToken));
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) => Reach(() => home.ListAsync(cancellationToken));
public Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
Reach(() => home.CreateAsync(name, fromVersionId, author, cancellationToken));
public Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken) => Reach(() => home.HeadAsync(projectId, cancellationToken));
public Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
Reach(() => home.GetVersionAsync(projectId, versionId, cancellationToken));
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
Reach(() => home.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken));
public Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
Reach(() => home.HistoryAsync(projectId, beforeNumber, limit, cancellationToken));
public Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) =>
Reach(() => home.PushAsync(projectId, version, cancellationToken));
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken) => Reach(() => home.FramesAsync(cancellationToken));
}