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;
/// An archive with an uploaded sequence as the projects' home, and workspaces that edit it (owner review 2, R8).
internal sealed class WorkspaceFixture : IAsyncDisposable
{
private readonly TestContext _context;
private JobEngine? _engine;
private WorkspaceFixture(TestContext context, ArchiveFixture archive, Sequence sequence, List 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 Frames { get; }
public ArchiveProjectHome Home { get; }
public ChronosDatabase Db => Archive.Db;
public CancellationToken Ct => Archive.Ct;
public static async Task CreateAsync(TestContext context, ArchiveFixture archive, int frames = 5)
{
var (sequence, list) = await ProjectServiceTests.UploadedAsync(archive, frames);
return new WorkspaceFixture(context, archive, sequence, list);
}
/// A workspace of its own (another PC, or another folder of this one) and its sync.
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)) });
}
/// Main of the sequence checked out into 's workspace, its media copied.
public async Task 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;
}
/// Runs a workspace copy job to its end.
public Task CopyAsync(WorkspaceStore store, Guid projectId, JobState expected = JobState.Done, string? checkpoint = null, string media = "proxies") =>
CopyAsync(store.Root, projectId, expected, checkpoint, media);
/// Runs a workspace copy job into the workspace at to its end.
public async Task CopyAsync(string root, Guid projectId, JobState expected = JobState.Done, string? checkpoint = null, string media = "proxies")
{
var engine = await EngineAsync();
var request = WorkspaceJobs.Copy(Sequence, 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 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();
}
}
/// A home that cannot be reached, standing for a server out of reach or an archive offline.
internal sealed class UnreachableHome(IProjectHome home) : IProjectHome
{
public bool Reachable { get; set; }
private T Reach(Func call) => Reachable ? call() : throw new HomeUnavailableException("The home cannot be reached.");
public Task SequenceAsync(CancellationToken cancellationToken) => Reach(() => home.SequenceAsync(cancellationToken));
public Task> ListAsync(CancellationToken cancellationToken) => Reach(() => home.ListAsync(cancellationToken));
public Task CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
Reach(() => home.CreateAsync(name, fromVersionId, author, cancellationToken));
public Task HeadAsync(Guid projectId, CancellationToken cancellationToken) => Reach(() => home.HeadAsync(projectId, cancellationToken));
public Task GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
Reach(() => home.GetVersionAsync(projectId, versionId, cancellationToken));
public Task> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
Reach(() => home.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken));
public Task> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
Reach(() => home.HistoryAsync(projectId, beforeNumber, limit, cancellationToken));
public Task PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) =>
Reach(() => home.PushAsync(projectId, version, cancellationToken));
public Task FramesAsync(CancellationToken cancellationToken) => Reach(() => home.FramesAsync(cancellationToken));
}