CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
The owner renamed the product and moved the repository to the self-hosted Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control, and asked for Actions and a first automatic release with the server package and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md). - Chronos everywhere: a byte-exact rename of contents and paths, then a per-language review of the forms the old name's ending had dictated. - Gitea instead of GitHub: the update check, licence, issue and support links read ChronosReleasesUrl; the update check takes the highest stable version, offers a pre-release build its final release and reports a repository it cannot see as a failed check. EULA, security policy and Gitea issue forms live at the repository root. - Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release, nightly image and performance gates in the .NET SDK container with git checkout and the system SQLite. The release cross-compiles the Windows programs, checks their icon, version and DPI manifest, packs installers and zips, publishes the Gitea release and pushes the server image (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows runner (WINDOWS_RUNNER). - New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the docker-exec script, which Gitea's service containers do not allow; the throw-away server runs without durability) and pe-check. - Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR, 3 on the shared runner; the weekly performance gates keep theirs). BREAKING CHANGE: programs, settings, data folders, file types, environment variables and the Docker image use the new name; nothing is migrated from Titano, which never had a release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
198 lines
10 KiB
C#
198 lines
10 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Net.WebSockets;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Data.Jobs;
|
|
using Chronos.Protocol;
|
|
using Chronos.Protocol.Client;
|
|
using Chronos.Server.Tests.Jobs;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.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<IJobHandler>(handler));
|
|
var (editor, _, token) = await server.UserAsync("alberto", "editor");
|
|
var sequence = await JobApiTests.CreateSequenceAsync(editor);
|
|
|
|
using var client = new ChronosClient(server.HttpBase, new CertificatePinning([]), "tests") { Token = token };
|
|
using var listening = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation);
|
|
listening.CancelAfter(TimeSpan.FromSeconds(20));
|
|
var received = new List<ServerEvent>();
|
|
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<WebSocketException>(() => 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 ChronosClient(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 ChronosClient(server.HttpBase, new CertificatePinning([]), "Chronos (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<TimeProvider>(clock));
|
|
var (uploader, _, _) = await server.UserAsync("studio-pc", "uploader");
|
|
var (other, _, _) = await server.UserAsync("roof-pc", "uploader");
|
|
using var subscription = server.Services.GetRequiredService<Chronos.Server.Events.EventHub>().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);
|
|
}
|
|
|
|
/// <summary>The card events published since the last call.</summary>
|
|
private static List<CardEvent> CardEvents(Chronos.Server.Events.EventHub.Subscription subscription)
|
|
{
|
|
var events = new List<CardEvent>();
|
|
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<Chronos.Server.Events.EventHub>();
|
|
for (var i = 0; i < 200 && hub.SubscriberCount == 0; i++) await Task.Delay(25);
|
|
Assert.Equal(1, hub.SubscriberCount);
|
|
}
|
|
}
|