diff --git a/src/Titano.App/Import/ServerImportSink.cs b/src/Titano.App/Import/ServerImportSink.cs
index b0feb616..bd9b5952 100644
--- a/src/Titano.App/Import/ServerImportSink.cs
+++ b/src/Titano.App/Import/ServerImportSink.cs
@@ -26,6 +26,27 @@ public sealed class ServerImportSink(TitanoClient client, string stagingDirector
/// Every file committed or refused so far, by card path; pass it to .
public ImportSinkReport Report { get; } = new();
+ ///
+ /// Runs a confirmed plan into the server through this sink () and tells the server's
+ /// other clients about it as (BRIEF §14.3 cardEvent): started, its progress, and
+ /// finished, also when it stopped early.
+ ///
+ public async Task RunAsync(ImportPlan plan, string card, bool verify, IProgress? progress, CancellationToken cancellationToken)
+ {
+ var events = new CardImportReporter(client, plan.Id, card, plan.FileCount);
+ events.Started();
+ ImportOutcome? outcome = null;
+ try
+ {
+ outcome = await LocalImport.RunAsync(plan, this, verify, events.Forward(progress), cancellationToken, Report).ConfigureAwait(false);
+ return outcome;
+ }
+ finally
+ {
+ await events.FinishedAsync(outcome).ConfigureAwait(false);
+ }
+ }
+
public async Task> CommitAsync(PlannedSession session, IReadOnlyList files, CancellationToken cancellationToken)
{
var results = new List(files.Count);
@@ -81,3 +102,123 @@ public sealed class ServerImportSink(TitanoClient client, string stagingDirector
private static bool IsRefusal(TitanoApiException ex) =>
!ex.IsTransient && ex.Status is not (HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden) && (int)ex.Status is >= 400 and < 500;
}
+
+///
+/// The reports of one card import into the server (BRIEF §14.3 cardEvent, §14.4), sent one at a time and in order:
+/// progress at most once a second, and a progress report still waiting is replaced by the next report, so a slow or
+/// unreachable server never piles them up. A report that fails is logged and the import goes on; a server without the
+/// endpoint (404) gets no more reports.
+///
+/// Photos of the plan, skipped ones included (like ).
+public sealed class CardImportReporter(TitanoClient client, Guid importId, string card, int total, TimeProvider? time = null)
+{
+ private static readonly TimeSpan ProgressInterval = TimeSpan.FromSeconds(1);
+ private static readonly TimeSpan SendTimeout = TimeSpan.FromSeconds(10);
+
+ private readonly TimeProvider _time = time ?? TimeProvider.System;
+ private readonly Lock _gate = new();
+ private readonly List _queue = [];
+ private Task _sending = Task.CompletedTask;
+ private bool _draining;
+ private bool _finished;
+ private bool _unsupported;
+ private long? _lastProgress;
+ private ImportProgress? _latest;
+
+ public void Started() => Post(CardImportStates.Started, 0, 0);
+
+ /// Progress that reaches (the screen's own) and these reports.
+ public IProgress Forward(IProgress? inner) => new Forwarding(this, inner);
+
+ public void Report(ImportProgress progress)
+ {
+ lock (_gate)
+ {
+ _latest = progress;
+ var now = _time.GetTimestamp();
+ if (_lastProgress is { } last && _time.GetElapsedTime(last, now) < ProgressInterval) return;
+ _lastProgress = now;
+ }
+ Post(CardImportStates.Progress, progress.ItemsDone, progress.Failed);
+ }
+
+ ///
+ /// Reports the end: every photo handled when the import gave an , else as far as its progress
+ /// got. Completes when the reports still queued are sent (each gives up after 10 s).
+ ///
+ public Task FinishedAsync(ImportOutcome? outcome)
+ {
+ int done, failed;
+ lock (_gate)
+ {
+ done = outcome is null ? _latest?.ItemsDone ?? 0 : total;
+ failed = outcome?.Failures.Count ?? _latest?.Failed ?? 0;
+ }
+ Post(CardImportStates.Finished, done, failed);
+ lock (_gate) return _sending;
+ }
+
+ private void Post(string state, int done, int failed)
+ {
+ var report = new CardImportReport(importId, card, state, Math.Clamp(done, 0, total), total, Math.Clamp(failed, 0, total));
+ lock (_gate)
+ {
+ if (_finished || _unsupported) return;
+ _finished = state == CardImportStates.Finished;
+ if (_queue.Count > 0 && _queue[^1].State == CardImportStates.Progress) _queue[^1] = report;
+ else _queue.Add(report);
+ if (_draining) return;
+ _draining = true;
+ _sending = Task.Run(DrainAsync);
+ }
+ }
+
+ private async Task DrainAsync()
+ {
+ while (true)
+ {
+ CardImportReport report;
+ lock (_gate)
+ {
+ if (_queue.Count == 0 || _unsupported)
+ {
+ _queue.Clear();
+ _draining = false;
+ return;
+ }
+ report = _queue[0];
+ _queue.RemoveAt(0);
+ }
+ await SendAsync(report).ConfigureAwait(false);
+ }
+ }
+
+ private async Task SendAsync(CardImportReport report)
+ {
+ try
+ {
+ using var timeout = new CancellationTokenSource(SendTimeout);
+ await client.ReportCardAsync(report, timeout.Token).ConfigureAwait(false);
+ }
+ catch (TitanoApiException ex) when (ex.Status is HttpStatusCode.NotFound or HttpStatusCode.MethodNotAllowed)
+ {
+ // A server from before card events.
+ lock (_gate) _unsupported = true;
+ Log.Info("import", $"The server takes no card reports ({ex.Error.Code})");
+ }
+ catch (Exception ex)
+ {
+ // The reports only inform the other clients: nothing about them may stop or fail the import.
+ Log.Warning("import", $"The {report.State} report of the import from {card} did not reach the server", ex);
+ }
+ }
+
+ private sealed class Forwarding(CardImportReporter reporter, IProgress? inner) : IProgress
+ {
+ public void Report(ImportProgress value)
+ {
+ inner?.Report(value);
+ reporter.Report(value);
+ }
+ }
+}
diff --git a/src/Titano.App/Sections/ImportViewModel.cs b/src/Titano.App/Sections/ImportViewModel.cs
index 763091c5..e2afcae4 100644
--- a/src/Titano.App/Sections/ImportViewModel.cs
+++ b/src/Titano.App/Sections/ImportViewModel.cs
@@ -1165,7 +1165,7 @@ public sealed class ImportViewModel : SectionViewModel
if (choices.Destination == ImportDestinationKind.Folder)
outcome = await Task.Run(() => LocalImport.RunAsync(plan, new FolderImportSink(choices.FolderPath!), choices.VerifyCopies, progress, cts.Token), cts.Token);
else if (Services.Server.Flow is { Address: { } address, Token: { } token })
- outcome = await RunServerAsync(plan, choices, address, token, progress, cts.Token);
+ outcome = await RunServerAsync(plan, choices, source.Title, address, token, progress, cts.Token);
else
{
Fail(Loc.L("Import.Blocked.NoServer"));
@@ -1185,15 +1185,15 @@ public sealed class ImportViewModel : SectionViewModel
}
}
- private async Task RunServerAsync(ImportPlan plan, ImportChoices choices, Uri address, string token, IProgress progress,
- CancellationToken ct)
+ private async Task RunServerAsync(ImportPlan plan, ImportChoices choices, string card, Uri address, string token,
+ IProgress progress, CancellationToken ct)
{
var staging = Path.Combine(ServerStagingRoot, plan.Id.ToString("N"));
using var client = new TitanoClient(address, new CertificatePinning(Services.Server.Fingerprints), $"Titano ({Environment.MachineName})") { Token = token };
try
{
var sink = new ServerImportSink(client, staging);
- return await Task.Run(() => LocalImport.RunAsync(plan, sink, choices.VerifyCopies, progress, ct, sink.Report), ct);
+ return await Task.Run(() => sink.RunAsync(plan, card, choices.VerifyCopies, progress, ct), ct);
}
finally
{
diff --git a/src/Titano.Protocol/Client/TitanoClient.cs b/src/Titano.Protocol/Client/TitanoClient.cs
index 704ab62a..21a64e1e 100644
--- a/src/Titano.Protocol/Client/TitanoClient.cs
+++ b/src/Titano.Protocol/Client/TitanoClient.cs
@@ -173,6 +173,13 @@ public sealed class TitanoClient : IDisposable
return await RetryAsync(() => SendAsync(HttpMethod.Post, $"api/v1/uploads/{uploadId}/complete", ProtocolJson.Default.CompleteUploadResponse, ct), ct).ConfigureAwait(false);
}
+ ///
+ /// Reports a card import this client streams into the server (started, progress, finished); the server tells every
+ /// connected client with a (BRIEF §14.3).
+ ///
+ public Task ReportCardAsync(CardImportReport report, CancellationToken ct = default) =>
+ SendAsync(HttpMethod.Post, "api/v1/uploads/cards", report, ProtocolJson.Default.CardImportReport, ct);
+
///
/// Render node: uploads the output of a claimed job in resumable chunks, then completes the job; the server verifies
/// the SHA-256 in and files the render under the sequence's exports (BRIEF §14.7).
diff --git a/src/Titano.Protocol/Events.cs b/src/Titano.Protocol/Events.cs
index e149000c..c760510c 100644
--- a/src/Titano.Protocol/Events.cs
+++ b/src/Titano.Protocol/Events.cs
@@ -10,6 +10,7 @@ namespace Titano.Protocol;
[JsonDerivedType(typeof(JobProgressEvent), "jobProgress")]
[JsonDerivedType(typeof(JobStateEvent), "jobState")]
[JsonDerivedType(typeof(SequenceChangedEvent), "sequenceChanged")]
+[JsonDerivedType(typeof(CardEvent), "cardEvent")]
[JsonDerivedType(typeof(NodeStateEvent), "nodeState")]
public abstract record ServerEvent;
@@ -19,4 +20,24 @@ public sealed record JobStateEvent(Guid JobId, string State, string? Error) : Se
public sealed record SequenceChangedEvent(Guid SequenceId, string Change, int FramesVersion, int SettingsVersion) : ServerEvent;
+///
+/// A card import that a connected client streams into the server (BRIEF §14.4), as that client reported it
+/// (): is one of ,
+/// counts the photos of its plan handled so far (failures included), and
+/// say who imports it.
+///
+public sealed record CardEvent(Guid ImportId, string Card, string State, int Done, int Total, int Failed, string User, string? ClientName) : ServerEvent;
+
+/// States of a card import report and of its .
+public static class CardImportStates
+{
+ public const string Started = "started";
+ public const string Progress = "progress";
+
+ /// The import ended: every photo handled, or fewer when it stopped early.
+ public const string Finished = "finished";
+
+ public static bool IsKnown(string? state) => state is Started or Progress or Finished;
+}
+
public sealed record NodeStateEvent(Guid NodeId, string ClientName, string State) : ServerEvent;
diff --git a/src/Titano.Protocol/ProtocolJson.cs b/src/Titano.Protocol/ProtocolJson.cs
index 7af9a1b2..ab3ffa80 100644
--- a/src/Titano.Protocol/ProtocolJson.cs
+++ b/src/Titano.Protocol/ProtocolJson.cs
@@ -35,6 +35,7 @@ namespace Titano.Protocol;
[JsonSerializable(typeof(ChunkReceived))]
[JsonSerializable(typeof(CompleteUploadResponse))]
[JsonSerializable(typeof(UploadState))]
+[JsonSerializable(typeof(CardImportReport))]
[JsonSerializable(typeof(JobDto))]
[JsonSerializable(typeof(Page))]
[JsonSerializable(typeof(CreateJobRequest))]
diff --git a/src/Titano.Protocol/Uploads.cs b/src/Titano.Protocol/Uploads.cs
index 3be2f2e7..6977ef12 100644
--- a/src/Titano.Protocol/Uploads.cs
+++ b/src/Titano.Protocol/Uploads.cs
@@ -17,6 +17,13 @@ public sealed record CompleteUploadResponse(Guid FrameId, Guid SequenceId, int I
/// GET /api/v1/uploads/{id}: resume state.
public sealed record UploadState(Guid UploadId, string FileName, long Size, string Sha256, int ChunkSize, IReadOnlyList Received, string State);
+///
+/// POST /api/v1/uploads/cards: a client importing a card into the server through these uploads reports its start,
+/// progress and end (BRIEF §14.4); the server tells every connected client with a (BRIEF §14.3).
+/// is the client's import plan, the card's name as the client shows it.
+///
+public sealed record CardImportReport(Guid ImportId, string Card, string State, int Done, int Total, int Failed);
+
///
/// POST /api/v1/sequences/{id}/renders: a client offers one of its renders of the sequence (BRIEF §14.5,
/// renders are uploaded on request). The render keeps its id on the server, so both sides recognise it; when the
diff --git a/src/Titano.Server/Api/UploadEndpoints.cs b/src/Titano.Server/Api/UploadEndpoints.cs
index 2bf2e615..5f0c4f45 100644
--- a/src/Titano.Server/Api/UploadEndpoints.cs
+++ b/src/Titano.Server/Api/UploadEndpoints.cs
@@ -1,5 +1,6 @@
using Titano.Core.Model;
using Titano.Protocol;
+using Titano.Server.Events;
using Titano.Server.Security;
using Titano.Server.Storage;
@@ -7,10 +8,13 @@ namespace Titano.Server.Api;
///
/// /api/v1/uploads (BRIEF §14.4). Chunk, state and cancel calls also serve render nodes uploading their output,
-/// which start and complete through the node endpoints.
+/// which start and complete through the node endpoints. POST /uploads/cards takes the reports of a card import a
+/// client streams through these uploads and tells every connected client (BRIEF §14.3 cardEvent).
///
public static class UploadEndpoints
{
+ private const int MaxCardName = 200;
+
public static void Map(RouteGroupBuilder api)
{
var uploads = api.MapGroup("/uploads").Requires(Permissions.Upload | Permissions.RenderNode);
@@ -23,6 +27,7 @@ public static class UploadEndpoints
uploads.MapPost("/{id:guid}/complete", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
(await service.CompleteAsync(caller, id)).ToResult(context, r => Results.Json(r, ProtocolJson.Default.CompleteUploadResponse)))
.Requires(Permissions.Upload);
+ uploads.MapPost("/cards", ReportCard).Requires(Permissions.Upload);
}
private static async Task StartAsync(HttpContext context, Caller caller, StartUploadRequest request, UploadService service)
@@ -32,6 +37,19 @@ public static class UploadEndpoints
statusCode: r.AlreadyExists ? StatusCodes.Status200OK : StatusCodes.Status201Created));
}
+ /// A card import's start, progress or end, published as a naming who imports it; 204.
+ private static IResult ReportCard(HttpContext context, Caller caller, CardImportReport report, EventHub events)
+ {
+ if (report.ImportId == Guid.Empty) return ApiErrors.BadRequest(context, "bad_request", "importId");
+ var card = report.Card?.Trim();
+ if (string.IsNullOrEmpty(card) || card.Length > MaxCardName || card.Any(char.IsControl)) return ApiErrors.BadRequest(context, "bad_request", "card");
+ if (!CardImportStates.IsKnown(report.State)) return ApiErrors.BadRequest(context, "bad_request", "state");
+ if (report.Total < 0 || report.Done < 0 || report.Done > report.Total || report.Failed < 0 || report.Failed > report.Total)
+ return ApiErrors.BadRequest(context, "bad_request", "done");
+ events.Publish(new CardEvent(report.ImportId, card, report.State, report.Done, report.Total, report.Failed, caller.User.Username, caller.ClientName));
+ return Results.NoContent();
+ }
+
///
/// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. The service
/// raises it to the chunk size of this upload (fixed when it started) before it reads the body.
diff --git a/tests/Titano.App.Tests/Import/ServerImportSinkTests.cs b/tests/Titano.App.Tests/Import/ServerImportSinkTests.cs
index 49d8f170..bfdb6380 100644
--- a/tests/Titano.App.Tests/Import/ServerImportSinkTests.cs
+++ b/tests/Titano.App.Tests/Import/ServerImportSinkTests.cs
@@ -1,3 +1,4 @@
+using System.Collections.Concurrent;
using System.IO;
using System.Net;
using System.Net.Http;
@@ -113,7 +114,88 @@ public sealed class ServerImportSinkTests(TestContext context)
Assert.True(ImportPlans.Totals(unlimited, verify: true).HasEnoughSpace);
}
- /// Start, chunk and complete of BRIEF §14.4, plus the title update; the hash is checked at completion.
+ [Test]
+ public async Task AnImportIntoTheServerIsReportedAsCardEventsFromStartToFinish()
+ {
+ var card = Card(context.CreateTempDirectory("card"), 8, Start);
+ var staging = context.CreateTempDirectory("staging");
+ var server = new FakeUploadServer(chunkSize: 4096);
+ using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
+ var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
+ var progress = new List();
+
+ var outcome = await new ServerImportSink(client, staging).RunAsync(plan, "EOS R5 · card A", true, new SyncProgress(progress.Add),
+ context.Cancellation);
+
+ Assert.True(outcome.IsComplete);
+ Assert.NotEmpty(progress);
+ var reports = server.Cards.ToList();
+ Assert.Equal(CardImportStates.Started, reports[0].State);
+ Assert.Equal((CardImportStates.Finished, plan.FileCount, plan.FileCount, 0), (reports[^1].State, reports[^1].Done, reports[^1].Total, reports[^1].Failed));
+ Assert.True(reports.All(r => r.ImportId == plan.Id && r.Card == "EOS R5 · card A"), "every report names the import");
+ Assert.True(reports.Skip(1).SkipLast(1).All(r => r.State == CardImportStates.Progress), "progress in between");
+ }
+
+ [Test]
+ public async Task AServerWithoutCardEventsStillTakesTheImport()
+ {
+ var card = Card(context.CreateTempDirectory("card"), 8, Start);
+ var staging = context.CreateTempDirectory("staging");
+ var server = new FakeUploadServer(chunkSize: 4096) { CardsUnsupported = true };
+ using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
+ var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
+
+ var outcome = await new ServerImportSink(client, staging).RunAsync(plan, "EOS R5 · card A", true, null, context.Cancellation);
+
+ Assert.Equal(8, outcome.Copied);
+ Assert.Equal(1, server.CardAttempts, "a server that answers 404 gets no further reports");
+ }
+
+ [Test]
+ public async Task ReportsWaitingForASlowServerAreReplacedByNewerOnes()
+ {
+ var gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var server = new FakeUploadServer(chunkSize: 4096) { CardGate = gate.Task };
+ using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
+ var time = new ManualTime();
+ var reporter = new CardImportReporter(client, Guid.CreateVersion7(), "EOS R5 · card A", 100, time);
+
+ reporter.Started();
+ await server.CardArrived.Task.WaitAsync(TimeSpan.FromSeconds(10));
+ reporter.Report(Progress(10));
+ reporter.Report(Progress(20));
+ time.Advance(TimeSpan.FromSeconds(1));
+ reporter.Report(Progress(30));
+ var finished = reporter.FinishedAsync(null);
+ gate.SetResult();
+ await finished.WaitAsync(TimeSpan.FromSeconds(10));
+
+ // The start was on its way; progress 10 waited and was replaced by 30 (20 came within the second), then by the end.
+ Assert.SequenceEqual([(CardImportStates.Started, 0), (CardImportStates.Finished, 30)], server.Cards.Select(r => (r.State, r.Done)));
+ reporter.Report(Progress(40));
+ Assert.Count(2, server.Cards);
+ }
+
+ private static ImportProgress Progress(int done) => new(done, 100, done * 10L, 1000, done, 0, 0, null);
+
+ /// Reports synchronously (Progress<T> posts and may arrive late).
+ private sealed class SyncProgress(Action report) : IProgress
+ {
+ public void Report(T value) => report(value);
+ }
+
+ private sealed class ManualTime : TimeProvider
+ {
+ private long _ticks;
+
+ public override long TimestampFrequency => TimeSpan.TicksPerSecond;
+
+ public override long GetTimestamp() => Interlocked.Read(ref _ticks);
+
+ public void Advance(TimeSpan by) => Interlocked.Add(ref _ticks, by.Ticks);
+ }
+
+ /// Start, chunk and complete of BRIEF §14.4, plus the title update and card reports; the hash is checked at completion.
private sealed class FakeUploadServer(int chunkSize) : HttpMessageHandler
{
private readonly Dictionary _open = [];
@@ -130,9 +212,34 @@ public sealed class ServerImportSinkTests(TestContext context)
/// Answer 401 to new uploads once this many were committed (the sign-in expired).
public int? SignedOutAfter { get; init; }
+ /// Card reports taken, in order (they arrive on another thread than the uploads).
+ public ConcurrentQueue Cards { get; } = new();
+
+ public int CardAttempts => _cardAttempts;
+
+ /// A server from before card events: 404.
+ public bool CardsUnsupported { get; init; }
+
+ /// Card reports wait for this (a slow server); says when the first one is waiting.
+ public Task? CardGate { get; init; }
+
+ public TaskCompletionSource CardArrived { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
+
+ private int _cardAttempts;
+
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var path = request.RequestUri!.AbsolutePath.Trim('/').Split('/');
+ if (request.Method == HttpMethod.Post && path is ["api", "v1", "uploads", "cards"])
+ {
+ Interlocked.Increment(ref _cardAttempts);
+ if (CardsUnsupported) return new HttpResponseMessage(HttpStatusCode.NotFound);
+ var report = (await request.Content!.ReadFromJsonAsync(ProtocolJson.Default.CardImportReport, cancellationToken))!;
+ CardArrived.TrySetResult();
+ if (CardGate is { } gate) await gate.WaitAsync(cancellationToken);
+ Cards.Enqueue(report);
+ return new HttpResponseMessage(HttpStatusCode.NoContent);
+ }
if (request.Method == HttpMethod.Post && path is ["api", "v1", "uploads"])
{
var start = (await request.Content!.ReadFromJsonAsync(ProtocolJson.Default.StartUploadRequest, cancellationToken))!;
diff --git a/tests/Titano.Server.Tests/Events/EventTests.cs b/tests/Titano.Server.Tests/Events/EventTests.cs
index 820c0678..59201709 100644
--- a/tests/Titano.Server.Tests/Events/EventTests.cs
+++ b/tests/Titano.Server.Tests/Events/EventTests.cs
@@ -1,4 +1,5 @@
using System.Net;
+using System.Net.Http.Json;
using System.Net.WebSockets;
using Microsoft.Extensions.DependencyInjection;
using Titano.Core.Model;
@@ -74,6 +75,73 @@ public sealed class EventTests(TestContext context)
await plain.ExpectErrorAsync(HttpStatusCode.BadRequest, "websocket_required");
}
+ [Test]
+ public async Task ACardImportReportedByOneClientReachesTheOthersAsCardEvents()
+ {
+ await using var server = await TestServer.StartAsync(context);
+ var (uploaderHttp, _, uploaderToken) = await server.UserAsync("studio-pc", "uploader");
+ var (_, _, editorToken) = await server.UserAsync("alberto", "editor");
+
+ using var listener = new TitanoClient(server.HttpBase, new CertificatePinning([]), "laptop") { Token = editorToken };
+ using var listening = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation);
+ listening.CancelAfter(TimeSpan.FromSeconds(20));
+ var received = new List();
+ var connected = new TaskCompletionSource();
+ var reader = Task.Run(async () =>
+ {
+ var events = listener.EventsAsync(listening.Token).GetAsyncEnumerator(listening.Token);
+ var first = events.MoveNextAsync();
+ connected.TrySetResult();
+ try
+ {
+ if (!await first) return;
+ do
+ {
+ if (events.Current is CardEvent card) received.Add(card);
+ if (events.Current is CardEvent { State: CardImportStates.Finished }) return;
+ }
+ while (await events.MoveNextAsync());
+ }
+ finally
+ {
+ await events.DisposeAsync();
+ }
+ });
+ await connected.Task;
+ await WaitForSubscriberAsync(server);
+
+ // The importing client (an upload-only account is enough) reports through the protocol client.
+ using var importer = new TitanoClient(server.HttpBase, new CertificatePinning([]), "Titano (STUDIO)") { Token = uploaderToken };
+ var importId = Guid.CreateVersion7();
+ await importer.ReportCardAsync(new CardImportReport(importId, "EOS R5 · card A", CardImportStates.Started, 0, 120, 0), context.Cancellation);
+ await importer.ReportCardAsync(new CardImportReport(importId, "EOS R5 · card A", CardImportStates.Progress, 60, 120, 1), context.Cancellation);
+ await importer.ReportCardAsync(new CardImportReport(importId, "EOS R5 · card A", CardImportStates.Finished, 120, 120, 2), context.Cancellation);
+ await reader.WaitAsync(TimeSpan.FromSeconds(20));
+
+ Assert.SequenceEqual([CardImportStates.Started, CardImportStates.Progress, CardImportStates.Finished], received.Select(e => e.State));
+ Assert.True(received.All(e => e.ImportId == importId && e.Card == "EOS R5 · card A" && e.Total == 120), "every event names the import");
+ Assert.Equal((60, 1), (received[1].Done, received[1].Failed));
+ // Who imports: the account and the client name it signed in with.
+ Assert.Equal(("studio-pc", "tests"), (received[2].User, received[2].ClientName));
+
+ // Reports are checked like any request.
+ foreach (var (report, field) in new[]
+ {
+ (new CardImportReport(Guid.Empty, "card", CardImportStates.Started, 0, 1, 0), "importId"),
+ (new CardImportReport(importId, " ", CardImportStates.Started, 0, 1, 0), "card"),
+ (new CardImportReport(importId, "card", "paused", 0, 1, 0), "state"),
+ (new CardImportReport(importId, "card", CardImportStates.Progress, 2, 1, 0), "done"),
+ })
+ {
+ using var refused = await uploaderHttp.PostAsJsonAsync("/api/v1/uploads/cards", report, ProtocolJson.Default.CardImportReport);
+ Assert.Equal(field, (await refused.ExpectErrorAsync(HttpStatusCode.BadRequest, "bad_request")).Details);
+ }
+ var (viewer, _, _) = await server.UserAsync("guest", "viewer");
+ using var forbidden = await viewer.PostAsJsonAsync("/api/v1/uploads/cards", new CardImportReport(importId, "card", CardImportStates.Started, 0, 1, 0),
+ ProtocolJson.Default.CardImportReport);
+ await forbidden.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
+ }
+
private static async Task WaitForSubscriberAsync(TestServer server)
{
var hub = server.Services.GetRequiredService();