using System.Net; using System.Net.Http.Json; using System.Net.WebSockets; using Microsoft.Extensions.DependencyInjection; using Titano.Core.Model; using Titano.Data.Jobs; using Titano.Protocol; using Titano.Protocol.Client; using Titano.Server.Tests.Jobs; using Titano.Testing; namespace Titano.Server.Tests.Events; [Category("server")] public sealed class EventTests(TestContext context) { [Test] public async Task AWebSocketClientReceivesJobProgressAndStates() { var handler = new BlockingJobHandler(JobKind.DeepAnalyse); await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton(handler)); var (editor, _, token) = await server.UserAsync("alberto", "editor"); var sequence = await JobApiTests.CreateSequenceAsync(editor); using var client = new TitanoClient(server.HttpBase, new CertificatePinning([]), "tests") { Token = token }; 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 = client.EventsAsync(listening.Token).GetAsyncEnumerator(listening.Token); var first = events.MoveNextAsync(); connected.TrySetResult(); try { if (!await first) return; do { received.Add(events.Current); if (events.Current is JobStateEvent { State: "done" }) return; } while (await events.MoveNextAsync()); } finally { await events.DisposeAsync(); } }); await connected.Task; await WaitForSubscriberAsync(server); var job = await JobApiTests.CreateAsync(editor, "deep-analyse", sequence); await handler.Started.Task.WaitAsync(TimeSpan.FromSeconds(10)); handler.Release(); await reader.WaitAsync(TimeSpan.FromSeconds(20)); Assert.Contains(received, e => e is JobProgressEvent p && p.JobId == job.Id && p.Total == 4); Assert.Contains(received, e => e is JobStateEvent { State: "running" } s && s.JobId == job.Id); Assert.Contains(received, e => e is JobStateEvent { State: "done" } s && s.JobId == job.Id); } [Test] public async Task TheEventSocketNeedsAValidToken() { await using var server = await TestServer.StartAsync(context); var ws = new UriBuilder(new Uri(server.HttpBase, "/api/v1/events")) { Scheme = "ws" }.Uri; using (var anonymous = new ClientWebSocket()) { var error = await Assert.ThrowsAsync(() => anonymous.ConnectAsync(ws, context.Cancellation)); Assert.Contains("401", error.Message); } var admin = await server.AdminAsync(); using var plain = await admin.GetAsync("/api/v1/events"); 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"); } [Test] public async Task OneAccountsCardReportsCannotCrowdTheEventQueues() { var clock = new ManualTimeProvider(); await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton(clock)); var (uploader, _, _) = await server.UserAsync("studio-pc", "uploader"); var (other, _, _) = await server.UserAsync("roof-pc", "uploader"); using var subscription = server.Services.GetRequiredService().Subscribe(); var importId = Guid.CreateVersion7(); // Progress goes out at most once a second; the reports in between are taken and dropped. for (var done = 1; done <= 5; done++) await ReportCardAsync(uploader, new CardImportReport(importId, "card", CardImportStates.Progress, done, 100, 0)); clock.Advance(TimeSpan.FromSeconds(1)); await ReportCardAsync(uploader, new CardImportReport(importId, "card", CardImportStates.Progress, 6, 100, 0)); Assert.SequenceEqual([1, 6], CardEvents(subscription).Select(e => e.Done)); // Starts and ends always go out, up to 120 events of one account a minute. for (var i = 0; i < 118; i++) await ReportCardAsync(uploader, new CardImportReport(Guid.CreateVersion7(), "card", CardImportStates.Started, 0, 100, 0)); using (var refused = await uploader.PostAsJsonAsync("/api/v1/uploads/cards", new CardImportReport(importId, "card", CardImportStates.Finished, 100, 100, 0), ProtocolJson.Default.CardImportReport)) Assert.Equal("59", (await refused.ExpectErrorAsync(HttpStatusCode.TooManyRequests, "rate_limited")).Details); await ReportCardAsync(other, new CardImportReport(Guid.CreateVersion7(), "card", CardImportStates.Started, 0, 100, 0)); Assert.Count(119, CardEvents(subscription)); clock.Advance(TimeSpan.FromMinutes(1)); await ReportCardAsync(uploader, new CardImportReport(importId, "card", CardImportStates.Finished, 100, 100, 0)); Assert.SequenceEqual([CardImportStates.Finished], CardEvents(subscription).Select(e => e.State)); } private static async Task ReportCardAsync(HttpClient client, CardImportReport report) { using var response = await client.PostAsJsonAsync("/api/v1/uploads/cards", report, ProtocolJson.Default.CardImportReport); await response.ExpectAsync(HttpStatusCode.NoContent); } /// The card events published since the last call. private static List CardEvents(Titano.Server.Events.EventHub.Subscription subscription) { var events = new List(); while (subscription.Reader.TryRead(out var e)) { if (e is CardEvent card) events.Add(card); } return events; } private static async Task WaitForSubscriberAsync(TestServer server) { var hub = server.Services.GetRequiredService(); for (var i = 0; i < 200 && hub.SubscriberCount == 0; i++) await Task.Delay(25); Assert.Equal(1, hub.SubscriberCount); } }