feat(server): publish card imports as cardEvent
BRIEF §14.3 lists cardEvent among the WebSocket events, but the server had none. A client importing a card into the server now reports its start, progress (at most once a second, a report still waiting replaced by the next) and end to the new POST /api/v1/uploads/cards (Upload permission; import id, card name, state, done, total, failed), and the server publishes a CardEvent naming the account and client to every connected client; the protocol client parses it and offers ReportCardAsync. A report that fails is logged and never stops the import; a server without the endpoint gets no more reports (SRS-10). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
9 files changed
+376
-6
No files matched your search
@@ -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<CardEvent>();
|
||||
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<Titano.Server.Events.EventHub>();
|
||||
|
||||
Reference in new issue
Block a user