The remote source loads, saves with the settings version, merges a 409, posts one server job per preset and follows the server's job events; Export "Sync" gating and the send it queues; "Save both" copies listed and restored through undo; Set hero reaches the index. A smoke test opens the sample server's proxied sequence in the real window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
143 lines
5.4 KiB
C#
143 lines
5.4 KiB
C#
using System.Net;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading.Channels;
|
|
using Titano.App.Editor;
|
|
using Titano.Core.Model;
|
|
using Titano.Protocol;
|
|
using Titano.Protocol.Client;
|
|
|
|
namespace Titano.App.Tests.Editor;
|
|
|
|
/// <summary>
|
|
/// One server sequence as the API serves it to an editor: settings with a version that refuses stale edits (409),
|
|
/// recorded edits and posted jobs, jobs other clients run, an analysis report and an event stream the test feeds.
|
|
/// </summary>
|
|
internal sealed class FakeRemoteSequenceApi(Sequence sequence) : IRemoteSequenceApi
|
|
{
|
|
private readonly Lock _gate = new();
|
|
private readonly Channel<ServerEvent> _events = Channel.CreateUnbounded<ServerEvent>();
|
|
|
|
public string? SettingsJson { get; set; }
|
|
|
|
public int SettingsVersion { get; set; } = 3;
|
|
|
|
public string? Report { get; set; }
|
|
|
|
public bool Gone { get; set; }
|
|
|
|
/// <summary>Every edit the server accepted or refused, in order.</summary>
|
|
public List<(UpdateSequenceRequest Request, bool Accepted)> Edits { get; } = [];
|
|
|
|
public List<CreateJobRequest> Posted { get; } = [];
|
|
|
|
public Dictionary<Guid, JobDto> Jobs { get; } = [];
|
|
|
|
/// <summary>Someone else edits the settings before each of the next edits reaches the server (it is refused with 409).</summary>
|
|
public Queue<Func<string?, string>> EditsBefore { get; } = new();
|
|
|
|
public bool Disposed { get; private set; }
|
|
|
|
/// <summary>A job of the server, as <c>GET /jobs/{id}</c> returns it.</summary>
|
|
public JobDto Job(Guid id, string kind, string state, Guid? target = null, long done = 0, long total = 0) =>
|
|
new(id, kind, [target ?? sequence.Id], sequence.DisplayTitle, state, "user", done, total, null, null, null, null, 0, null, DateTimeOffset.UtcNow,
|
|
state == "queued" ? null : DateTimeOffset.UtcNow, state is "done" or "failed" ? DateTimeOffset.UtcNow : null);
|
|
|
|
public void Publish(ServerEvent e) => _events.Writer.TryWrite(e);
|
|
|
|
public Task<SequenceDto> GetSequenceAsync(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
lock (_gate) return Task.FromResult(Dto(id));
|
|
}
|
|
|
|
public Task<SequenceManifest> GetManifestAsync(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
Check(id);
|
|
return Task.FromResult(new SequenceManifest(id, 1, SettingsVersion, SettingsJson, [], [], [
|
|
new RenderDto(Guid.CreateVersion7(), "4K archive", "hevc", 3840, 2160, 24, 5, 2_000_000_000, DateTimeOffset.UtcNow.AddDays(-1), "studio-pc"),
|
|
]));
|
|
}
|
|
}
|
|
|
|
public Task<SequenceDto> UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
Check(id);
|
|
if (EditsBefore.TryDequeue(out var theirs))
|
|
{
|
|
SettingsJson = theirs(SettingsJson);
|
|
SettingsVersion++;
|
|
}
|
|
if (request.ExpectedSettingsVersion is { } expected && expected != SettingsVersion)
|
|
{
|
|
Edits.Add((request, false));
|
|
throw new TitanoApiException(HttpStatusCode.Conflict, new ApiError("settings_conflict", "changed", SettingsVersion.ToString()));
|
|
}
|
|
Edits.Add((request, true));
|
|
SettingsJson = request.SettingsJson ?? SettingsJson;
|
|
SettingsVersion++;
|
|
return Task.FromResult(Dto(id));
|
|
}
|
|
}
|
|
|
|
public Task<string?> GetAnalysisReportAsync(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
Check(id);
|
|
return Task.FromResult(Report);
|
|
}
|
|
}
|
|
|
|
public Task<JobDto> CreateJobAsync(CreateJobRequest request, CancellationToken cancellationToken)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
Posted.Add(request);
|
|
var job = Job(Guid.CreateVersion7(), request.Kind, "queued", request.Target[0]);
|
|
Jobs[job.Id] = job;
|
|
return Task.FromResult(job);
|
|
}
|
|
}
|
|
|
|
public Task<JobDto> GetJobAsync(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return Jobs.TryGetValue(id, out var job)
|
|
? Task.FromResult(job)
|
|
: throw new TitanoApiException(HttpStatusCode.NotFound, new ApiError("not_found", "job", "job"));
|
|
}
|
|
}
|
|
|
|
public Task<IReadOnlyList<JobDto>> ListActiveJobsAsync(CancellationToken cancellationToken)
|
|
{
|
|
lock (_gate) return Task.FromResult<IReadOnlyList<JobDto>>([.. Jobs.Values.Where(j => j.State is "queued" or "running" or "paused")]);
|
|
}
|
|
|
|
public async IAsyncEnumerable<ServerEvent> EventsAsync([EnumeratorCancellation] CancellationToken cancellationToken)
|
|
{
|
|
while (await _events.Reader.WaitToReadAsync(cancellationToken))
|
|
{
|
|
while (_events.Reader.TryRead(out var e)) yield return e;
|
|
}
|
|
}
|
|
|
|
public void Dispose() => Disposed = true;
|
|
|
|
private SequenceDto Dto(Guid id)
|
|
{
|
|
Check(id);
|
|
return new SequenceDto(id, sequence.Title, sequence.DisplayTitle, sequence.Folder, sequence.CameraMake, sequence.CameraModel, null,
|
|
sequence.FirstCapture, sequence.LastCapture, sequence.FrameCount, sequence.MedianInterval, 0, (long)SequenceStatus.ProxiesBuilt, 1, null, [], 0,
|
|
null, SettingsVersion, 1, 0, null, DateTimeOffset.UtcNow);
|
|
}
|
|
|
|
private void Check(Guid id)
|
|
{
|
|
if (Gone || id != sequence.Id) throw new TitanoApiException(HttpStatusCode.NotFound, new ApiError("not_found", "gone", "sequence"));
|
|
}
|
|
}
|