From 6606f8e6e6cf0cfde36406de067777aaa84d4993 Mon Sep 17 00:00:00 2001 From: Heimdall Date: Sat, 14 Nov 2026 10:42:57 +0000 Subject: [PATCH] Test: merge notice feed and its bearer (red; the feed is a skeleton and the routes do not exist) Co-Authored-By: Claude Sonnet 5.5 --- .../Configuration/ChatNoticesOptions.cs | 10 + .../Features/Membership/MergeNoticeFeed.cs | 27 ++ .../AccountMergeSagaTests.cs | 4 +- .../Bifrost.Web.Tests/MergeNoticeAuthTests.cs | 129 +++++++ .../Bifrost.Web.Tests/MergeNoticeFeedTests.cs | 341 ++++++++++++++++++ .../Support/HeimdallTestKit.cs | 10 +- 6 files changed, 518 insertions(+), 3 deletions(-) create mode 100644 src/Bifrost.Web/Configuration/ChatNoticesOptions.cs create mode 100644 src/Bifrost.Web/Features/Membership/MergeNoticeFeed.cs create mode 100644 tests/Bifrost.Web.Tests/MergeNoticeAuthTests.cs create mode 100644 tests/Bifrost.Web.Tests/MergeNoticeFeedTests.cs diff --git a/src/Bifrost.Web/Configuration/ChatNoticesOptions.cs b/src/Bifrost.Web/Configuration/ChatNoticesOptions.cs new file mode 100644 index 0000000..35f865e --- /dev/null +++ b/src/Bifrost.Web/Configuration/ChatNoticesOptions.cs @@ -0,0 +1,10 @@ +namespace Bifrost.Web.Configuration; + +/// The chat host's pull of merge notices (MergeNoticeFeed). An empty token turns both routes off (404); startup does not require it. +public sealed class ChatNoticesOptions +{ + public const string SectionName = "ChatNotices"; + + /// The bearer the chat host presents (environment ChatNotices__Token). Its own secret: it grants nothing else. + public string Token { get; init; } = string.Empty; +} diff --git a/src/Bifrost.Web/Features/Membership/MergeNoticeFeed.cs b/src/Bifrost.Web/Features/Membership/MergeNoticeFeed.cs new file mode 100644 index 0000000..0620027 --- /dev/null +++ b/src/Bifrost.Web/Features/Membership/MergeNoticeFeed.cs @@ -0,0 +1,27 @@ +using Bifrost.Web.Data; +using Bifrost.Web.Domain; + +namespace Bifrost.Web.Features.Membership; + +/// What the chat host is told about one merge. Derived from the merge row on every read; never stored. +public sealed record MergeNotice( + AccountMergeNoticeKind Kind, + Guid MergeId, + DateTimeOffset OccurredAt, + DateTimeOffset? GraceEndsAt, + Guid OldSubject, + Guid NewSubject, + string OldPreferredUsername, + string NewPreferredUsername); + +public sealed class MergeNoticeFeed(BifrostDbContext dbContext, TimeProvider timeProvider) +{ + public const int DefaultLimit = 50; + public const int MaxLimit = 200; + + public Task> ReadAsync(AccountMergeNoticeTarget target, DateTimeOffset? after, int? limit, CancellationToken cancellationToken) => + throw new NotImplementedException(); + + public Task AckAsync(AccountMergeNoticeTarget target, Guid mergeId, AccountMergeNoticeKind kind, CancellationToken cancellationToken) => + throw new NotImplementedException(); +} diff --git a/tests/Bifrost.Web.Tests/AccountMergeSagaTests.cs b/tests/Bifrost.Web.Tests/AccountMergeSagaTests.cs index 1ab4fcd..0e73216 100644 --- a/tests/Bifrost.Web.Tests/AccountMergeSagaTests.cs +++ b/tests/Bifrost.Web.Tests/AccountMergeSagaTests.cs @@ -23,9 +23,9 @@ public sealed class AccountMergeSagaTests internal sealed record Party(Guid Id, string HeimdallId, IReadOnlyDictionary Logins); /// A host plus the helpers every test needs: accounts with logins at the fake Heimdall, proofs, a scope per operation. - internal sealed class Rig(bool realSessions = false, bool accountMerge = false) : IDisposable + internal sealed class Rig(bool realSessions = false, bool accountMerge = false, string? chatNoticesToken = null) : IDisposable { - public HeimdallHost Host { get; } = new(realSessions: realSessions, accountMerge: accountMerge); + public HeimdallHost Host { get; } = new(realSessions: realSessions, accountMerge: accountMerge, chatNoticesToken: chatNoticesToken); public FakeMoves Moves => Host.Heimdall.Moves; diff --git a/tests/Bifrost.Web.Tests/MergeNoticeAuthTests.cs b/tests/Bifrost.Web.Tests/MergeNoticeAuthTests.cs new file mode 100644 index 0000000..5e7403b --- /dev/null +++ b/tests/Bifrost.Web.Tests/MergeNoticeAuthTests.cs @@ -0,0 +1,129 @@ +using System.Net; +using System.Text; +using Bifrost.Web.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using static Bifrost.Web.Tests.AccountMergeSagaTests; + +namespace Bifrost.Web.Tests; + +/// Who may pull merge notices: the chat host's own bearer and nobody else, whatever else the request carries (the test host signs every request in as an admin). +public sealed class MergeNoticeAuthTests +{ + private const string Token = "chat-notices-test-token-0123456789"; + private const string Get = "/api/merge-notices/chat"; + private const string Ack = "/api/merge-notices/chat/ack"; + + private static HttpRequestMessage Request(HttpMethod method, string url, string? authorization, string? body = null) + { + var request = new HttpRequestMessage(method, url); + if (authorization is not null) + { + request.Headers.TryAddWithoutValidation("Authorization", authorization); + } + + if (body is not null) + { + request.Content = new StringContent(body, Encoding.UTF8, "application/json"); + } + + return request; + } + + private static string AckBody(Guid id) => $"{{\"merge_id\":\"{id}\",\"type\":\"merged\"}}"; + + [Theory] + [InlineData(null)] + [InlineData("")] + public async Task With_no_token_configured_both_routes_are_404_even_for_a_bearer_that_looks_right(string? token) + { + using var rig = new Rig(chatNoticesToken: token); + using var client = rig.Host.NewClient(); + + foreach (var authorization in new[] { null, "Bearer " + Token, "Bearer " }) + { + using var read = await client.SendAsync(Request(HttpMethod.Get, Get, authorization)); + using var ack = await client.SendAsync(Request(HttpMethod.Post, Ack, authorization, AckBody(Guid.NewGuid()))); + Assert.Equal(HttpStatusCode.NotFound, read.StatusCode); + Assert.Equal(HttpStatusCode.NotFound, ack.StatusCode); + } + } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData("Bearer")] + [InlineData("Bearer ")] + [InlineData("Bearer wrong-wrong-wrong-wrong-wrong-wrong")] + [InlineData("Bearer chat-notices-test-token-012345678")] // one short + [InlineData("Bearer chat-notices-test-token-01234567890")] // one long + [InlineData("Bearer c")] + [InlineData("Bearer chat-notices-test-token-0123456789 ")] + [InlineData("chat-notices-test-token-0123456789")] + [InlineData("Basic chat-notices-test-token-0123456789")] + [InlineData("Bearer chat-notices-test-token-0123456789, Bearer other")] + public async Task A_missing_wrong_short_or_long_token_is_401_with_no_body_on_both_routes_and_acks_nothing(string? authorization) + { + using var rig = new Rig(chatNoticesToken: Token); + var (merge, _, _) = await Committed(rig); + using var client = rig.Host.NewClient(); + + using var read = await client.SendAsync(Request(HttpMethod.Get, Get, authorization)); + using var ack = await client.SendAsync(Request(HttpMethod.Post, Ack, authorization, AckBody(merge))); + + foreach (var response in new[] { read, ack }) + { + Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + Assert.Empty(await response.Content.ReadAsStringAsync()); + Assert.Contains("no-store", response.Headers.CacheControl?.ToString()); + } + + Assert.Equal(0, await rig.InScopeAsync(async sp => await sp.GetRequiredService().AccountMergeNoticeAcks.CountAsync())); + } + + [Fact] + public async Task A_token_in_the_query_or_a_cookie_is_not_a_bearer() + { + using var rig = new Rig(chatNoticesToken: Token); + using var client = rig.Host.NewClient(); + + using var query = await client.SendAsync(Request(HttpMethod.Get, $"{Get}?token={Token}&access_token={Token}", null)); + using var cookie = Request(HttpMethod.Get, Get, null); + cookie.Headers.Add("Cookie", $"token={Token}"); + using var withCookie = await client.SendAsync(cookie); + + Assert.Equal(HttpStatusCode.Unauthorized, query.StatusCode); + Assert.Equal(HttpStatusCode.Unauthorized, withCookie.StatusCode); + } + + [Fact] + public async Task The_right_token_reads_and_acks_with_no_store_and_no_cors_or_cookie() + { + using var rig = new Rig(chatNoticesToken: Token); + var (merge, _, _) = await Committed(rig); + using var client = rig.Host.NewClient(); + + using var read = await client.SendAsync(Request(HttpMethod.Get, Get, "Bearer " + Token)); + using var ack = await client.SendAsync(Request(HttpMethod.Post, Ack, "Bearer " + Token, AckBody(merge))); + + Assert.Equal(HttpStatusCode.OK, read.StatusCode); + Assert.Equal(HttpStatusCode.NoContent, ack.StatusCode); + foreach (var response in new[] { read, ack }) + { + Assert.Contains("no-store", response.Headers.CacheControl?.ToString()); + Assert.False(response.Headers.Contains("Access-Control-Allow-Origin")); + Assert.False(response.Headers.Contains("Set-Cookie")); + } + + Assert.Contains("application/json", read.Content.Headers.ContentType?.ToString()); + } + + internal static async Task<(Guid Merge, Party Survivor, Party Folded)> Committed(Rig rig) + { + var survivor = await rig.AccountAsync("survivor", "twitch"); + var folded = await rig.AccountAsync("folded", "discord"); + var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord")); + Assert.Equal(Bifrost.Web.Domain.AccountMergeState.Committed, (await rig.AdvanceAsync(id)).State); + return (id, survivor, folded); + } +} diff --git a/tests/Bifrost.Web.Tests/MergeNoticeFeedTests.cs b/tests/Bifrost.Web.Tests/MergeNoticeFeedTests.cs new file mode 100644 index 0000000..4577583 --- /dev/null +++ b/tests/Bifrost.Web.Tests/MergeNoticeFeedTests.cs @@ -0,0 +1,341 @@ +using System.Net; +using System.Text; +using System.Text.Json; +using Bifrost.Web.Data; +using Bifrost.Web.Domain; +using Bifrost.Web.Features.Membership; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using static Bifrost.Web.Tests.AccountMergeSagaTests; + +namespace Bifrost.Web.Tests; + +/// +/// The merge notice feed from the chat host's side: pulled over HTTP with the bearer, acked over HTTP, against merges the saga rig made; +/// bounds and the unique ack key from the feed itself over seeded rows. Notices are derived from the merge row: no stored status. +/// +public sealed class MergeNoticeFeedTests(MergePostgres postgres) : IClassFixture +{ + private const string Token = "chat-notices-test-token-0123456789"; + + private static readonly string[] PayloadKeys = + ["type", "merge_id", "occurred_at", "grace_ends_at", "old_subject", "new_subject", "old_preferred_username", "new_preferred_username"]; + + private static async Task PullAsync(Rig rig, string query = "") + { + using var client = rig.Host.NewClient(); + using var request = new HttpRequestMessage(HttpMethod.Get, "/api/merge-notices/chat" + query); + request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + Token); + using var response = await client.SendAsync(request); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + return JsonDocument.Parse(await response.Content.ReadAsStringAsync()).RootElement.EnumerateArray().Select(x => x.Clone()).ToArray(); + } + + private static async Task AckAsync(Rig rig, Guid merge, string type) + { + using var client = rig.Host.NewClient(); + using var request = new HttpRequestMessage(HttpMethod.Post, "/api/merge-notices/chat/ack") + { + Content = new StringContent($"{{\"merge_id\":\"{merge}\",\"type\":\"{type}\"}}", Encoding.UTF8, "application/json") + }; + request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + Token); + using var response = await client.SendAsync(request); + return response.StatusCode; + } + + private static string[] Types(JsonElement[] notices) => [.. notices.Select(x => x.GetProperty("type").GetString()!)]; + + private static Task<(Guid Merge, Party Survivor, Party Folded)> CommittedAsync(Rig rig) => MergeNoticeAuthTests.Committed(rig); + + private static async Task FinalizeAsync(Rig rig) + { + rig.Clock.Advance(AccountMergeLedger.Grace + TimeSpan.FromMinutes(1)); + await rig.InScopeAsync(async sp => await sp.GetRequiredService().FinalizeDueAsync(rig.Clock.Now, default)); + } + + // ---- what a notice says ---- + + [Fact] + public async Task A_committed_merge_yields_one_merged_notice_with_exactly_the_documented_keys_and_nothing_that_names_a_person() + { + using var rig = new Rig(chatNoticesToken: Token); + var (merge, survivor, folded) = await CommittedAsync(rig); + var committed = await rig.MergeAsync(merge); + + var notice = Assert.Single(await PullAsync(rig)); + + Assert.Equal(PayloadKeys.Order(), notice.EnumerateObject().Select(x => x.Name).Order()); + Assert.Equal("merged", notice.GetProperty("type").GetString()); + Assert.Equal(merge.ToString("D"), notice.GetProperty("merge_id").GetString()); + Assert.Equal(committed.CommittedUtc, notice.GetProperty("occurred_at").GetDateTimeOffset()); + Assert.Equal(committed.GraceEndsUtc, notice.GetProperty("grace_ends_at").GetDateTimeOffset()); + Assert.Equal(folded.Id.ToString("D"), notice.GetProperty("old_subject").GetString()); + Assert.Equal(survivor.Id.ToString("D"), notice.GetProperty("new_subject").GetString()); + Assert.Equal("folded", notice.GetProperty("old_preferred_username").GetString()); + Assert.Equal("survivor", notice.GetProperty("new_preferred_username").GetString()); + Assert.DoesNotContain(folded.HeimdallId, notice.GetRawText()); + Assert.DoesNotContain(survivor.HeimdallId, notice.GetRawText()); + } + + [Fact] + public async Task An_undone_merge_yields_merged_then_undone_and_each_is_acked_alone() + { + using var rig = new Rig(chatNoticesToken: Token); + var (merge, survivor, folded) = await CommittedAsync(rig); + rig.Clock.Advance(TimeSpan.FromHours(1)); + Assert.IsType(await rig.InScopeAsync(sp => + sp.GetRequiredService().RequestUndoAsync(merge, survivor.Id, [rig.Moved(survivor, folded, "discord")], default))); + Assert.Equal(AccountMergeState.Undone, (await rig.AdvanceAsync(merge)).State); + var stored = await rig.MergeAsync(merge); + + var notices = await PullAsync(rig); + + Assert.Equal(["merged", "undone"], Types(notices)); + Assert.Equal(stored.UndoneUtc, notices[1].GetProperty("occurred_at").GetDateTimeOffset()); + Assert.Equal(stored.GraceEndsUtc, notices[1].GetProperty("grace_ends_at").GetDateTimeOffset()); + Assert.Equal(HttpStatusCode.NoContent, await AckAsync(rig, merge, "merged")); + Assert.Equal(["undone"], Types(await PullAsync(rig))); + } + + [Fact] + public async Task A_finalized_merge_yields_merged_then_final() + { + using var rig = new Rig(chatNoticesToken: Token); + var (merge, _, _) = await CommittedAsync(rig); + await FinalizeAsync(rig); + var stored = await rig.MergeAsync(merge); + Assert.Equal(AccountMergeState.Final, stored.State); + + var notices = await PullAsync(rig); + + Assert.Equal(["merged", "final"], Types(notices)); + Assert.Equal(stored.FinalizedUtc, notices[1].GetProperty("occurred_at").GetDateTimeOffset()); + } + + [Fact] + public async Task A_confirmed_a_frozen_and_a_failed_merge_yield_nothing() + { + using var rig = new Rig(chatNoticesToken: Token); + var survivor = await rig.AccountAsync("survivor", "twitch"); + var frozen = await rig.AccountAsync("frozen", "discord"); + var stale = await rig.AccountAsync("stale", "patreon"); + var confirmed = await rig.ConfirmedAsync(survivor, frozen, rig.Weak(survivor, "twitch"), rig.Weak(frozen, "discord")); + Assert.Equal(AccountMergeState.Confirmed, (await rig.MergeAsync(confirmed)).State); + Assert.IsType(await rig.StandingAsync(frozen)); + Assert.Empty(await PullAsync(rig)); + + var other = await rig.AccountAsync("other", "twitch2"); + var failing = await rig.ConfirmedAsync(other, stale, rig.Weak(other, "twitch2"), rig.Weak(stale, "patreon")); + rig.Clock.Advance(TimeSpan.FromSeconds(400)); + Assert.Equal(AccountMergeState.Failed, (await rig.AdvanceAsync(failing)).State); + + // The first merge is still Confirmed and the second Failed: neither committed, neither is told. + Assert.Empty(await PullAsync(rig)); + } + + // ---- ack ---- + + [Fact] + public async Task An_ack_is_idempotent_and_the_notice_is_gone_from_the_next_read() + { + using var rig = new Rig(chatNoticesToken: Token); + var (merge, _, _) = await CommittedAsync(rig); + Assert.Single(await PullAsync(rig)); + + Assert.Equal(HttpStatusCode.NoContent, await AckAsync(rig, merge, "merged")); + Assert.Equal(HttpStatusCode.NoContent, await AckAsync(rig, merge, "merged")); + + Assert.Empty(await PullAsync(rig)); + Assert.Equal(1, await rig.InScopeAsync(async sp => await sp.GetRequiredService().AccountMergeNoticeAcks.CountAsync())); + } + + [Fact] + public async Task An_ack_of_a_notice_the_feed_would_not_emit_is_refused_and_stores_nothing() + { + using var rig = new Rig(chatNoticesToken: Token); + var (merge, _, _) = await CommittedAsync(rig); // merged only: not undone, not final + + Assert.Equal(HttpStatusCode.NotFound, await AckAsync(rig, merge, "undone")); + Assert.Equal(HttpStatusCode.NotFound, await AckAsync(rig, merge, "final")); + Assert.Equal(HttpStatusCode.NotFound, await AckAsync(rig, Guid.NewGuid(), "merged")); + Assert.Equal(HttpStatusCode.BadRequest, await AckAsync(rig, merge, "created")); + Assert.Equal(HttpStatusCode.BadRequest, await AckAsync(rig, merge, "Merged")); + + Assert.Equal(0, await rig.InScopeAsync(async sp => await sp.GetRequiredService().AccountMergeNoticeAcks.CountAsync())); + Assert.Single(await PullAsync(rig)); + } + + [Fact] + public async Task A_malformed_ack_body_is_400_and_stores_nothing() + { + using var rig = new Rig(chatNoticesToken: Token); + await CommittedAsync(rig); + using var client = rig.Host.NewClient(); + foreach (var body in new[] { "", "not json", "{}", "{\"merge_id\":\"nope\",\"type\":\"merged\"}", "{\"type\":\"merged\"}", "[]" }) + { + using var request = new HttpRequestMessage(HttpMethod.Post, "/api/merge-notices/chat/ack") { Content = new StringContent(body, Encoding.UTF8, "application/json") }; + request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + Token); + using var response = await client.SendAsync(request); + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + } + + Assert.Equal(0, await rig.InScopeAsync(async sp => await sp.GetRequiredService().AccountMergeNoticeAcks.CountAsync())); + } + + // ---- order, bounds, after: the feed over seeded rows ---- + + private static readonly DateTimeOffset T0 = AccountMergeLedgerTests.T0; + + private static async Task<(AccountMergeLedgerTests.World World, UserAccount Survivor, UserAccount Folded)> SeededAsync(Func> merges, string? postgres = null) + { + var world = postgres is null ? new AccountMergeLedgerTests.World() : AccountMergeLedgerTests.World.OnPostgres(postgres); + await using (var migrate = world.Db()) + { + if (postgres is not null) + { + await migrate.Database.MigrateAsync(); + } + } + + var survivor = await world.AccountAsync("survivor"); + var folded = await world.AccountAsync("folded"); + await using var db = world.Db(); + db.AccountMerges.AddRange(merges(survivor.Id, folded.Id)); + await db.SaveChangesAsync(); + return (world, survivor, folded); + } + + private static AccountMerge Committed(Guid survivor, Guid folded, DateTimeOffset at, Guid? id = null) => new() + { + Id = id ?? Guid.NewGuid(), State = AccountMergeState.Committed, SurvivorUserAccountId = survivor, FoldedUserAccountId = folded, + CreatedUtc = at, CommittedUtc = at, GraceEndsUtc = at + AccountMergeLedger.Grace + }; + + private static MergeNoticeFeed Feed(BifrostDbContext db) => new(db, TimeProvider.System); + + [Fact] + public async Task Notices_come_ordered_by_occurred_at_then_merge_id_then_kind_and_after_keeps_only_later_ones() + { + var low = new Guid("00000000-0000-0000-0000-000000000001"); + var high = new Guid("00000000-0000-0000-0000-000000000002"); + var (world, _, _) = await SeededAsync((s, f) => + [ + Committed(s, f, T0.AddHours(3), high), + Committed(s, f, T0.AddHours(3), low), + Committed(s, f, T0.AddHours(1), Guid.NewGuid()) + ]); + await using var db = world.Db(); + + var all = await Feed(db).ReadAsync(AccountMergeNoticeTarget.Chat, null, null, default); + var later = await Feed(db).ReadAsync(AccountMergeNoticeTarget.Chat, T0.AddHours(1), null, default); + + Assert.Equal([T0.AddHours(1), T0.AddHours(3), T0.AddHours(3)], all.Select(x => x.OccurredAt)); + Assert.Equal([low, high], all.Skip(1).Select(x => x.MergeId)); + Assert.Equal([low, high], later.Select(x => x.MergeId)); + } + + [Fact] + public async Task A_merge_that_is_undone_and_one_that_is_final_order_their_two_notices_by_their_own_times() + { + var (world, _, _) = await SeededAsync((s, f) => + { + var undone = Committed(s, f, T0); + undone.State = AccountMergeState.Undone; + undone.UndoneUtc = T0.AddHours(5); + var final = Committed(s, f, T0.AddHours(1)); + final.State = AccountMergeState.Final; + final.FinalizedUtc = T0.AddHours(2); + return [undone, final]; + }); + await using var db = world.Db(); + + var kinds = (await Feed(db).ReadAsync(AccountMergeNoticeTarget.Chat, null, null, default)).Select(x => x.Kind); + + Assert.Equal([AccountMergeNoticeKind.Merged, AccountMergeNoticeKind.Merged, AccountMergeNoticeKind.Final, AccountMergeNoticeKind.Undone], kinds); + } + + [Theory] + [InlineData(null, 50)] + [InlineData(0, 50)] + [InlineData(-5, 50)] + [InlineData(3, 3)] + [InlineData(200, 200)] + [InlineData(100000, 200)] + public async Task The_limit_is_bounded_default_fifty_maximum_two_hundred(int? asked, int expected) + { + var (world, _, _) = await SeededAsync((s, f) => Enumerable.Range(0, 230).Select(i => Committed(s, f, T0.AddMinutes(i)))); + await using var db = world.Db(); + + var read = await Feed(db).ReadAsync(AccountMergeNoticeTarget.Chat, null, asked, default); + + Assert.Equal(expected, read.Count); + Assert.Equal(T0.AddMinutes(expected - 1), read[^1].OccurredAt); // the oldest first, cut at the limit + } + + [Fact] + public async Task The_feed_writes_no_merge_row_only_acks() + { + var (world, _, _) = await SeededAsync((s, f) => [Committed(s, f, T0)]); + await using var db = world.Db(); + var before = await db.AccountMerges.AsNoTracking().SingleAsync(); + var feed = Feed(db); + + await feed.ReadAsync(AccountMergeNoticeTarget.Chat, null, null, default); + Assert.True(await feed.AckAsync(AccountMergeNoticeTarget.Chat, before.Id, AccountMergeNoticeKind.Merged, default)); + Assert.True(await feed.AckAsync(AccountMergeNoticeTarget.Chat, before.Id, AccountMergeNoticeKind.Merged, default)); + Assert.False(await feed.AckAsync(AccountMergeNoticeTarget.Chat, before.Id, AccountMergeNoticeKind.Final, default)); + + await using var fresh = world.Db(); + var after = await fresh.AccountMerges.AsNoTracking().SingleAsync(); + Assert.Equal(System.Text.Json.JsonSerializer.Serialize(before), System.Text.Json.JsonSerializer.Serialize(after)); + Assert.Equal(1, await fresh.AccountMergeNoticeAcks.CountAsync()); + } + + // ---- the unique key, on a real database ---- + + [PostgresFact] + public async Task Two_concurrent_acks_of_one_notice_both_succeed_and_leave_one_row() + { + var name = $"bifrost_notices_{Guid.NewGuid():N}"; + var admin = Environment.GetEnvironmentVariable(PostgresGate.Variable)!; + await using (var connection = new Npgsql.NpgsqlConnection(admin)) + { + await connection.OpenAsync(); + await using var create = new Npgsql.NpgsqlCommand($"CREATE DATABASE \"{name}\"", connection); + await create.ExecuteNonQueryAsync(); + } + + try + { + var (world, _, _) = await SeededAsync((s, f) => [Committed(s, f, T0)], new Npgsql.NpgsqlConnectionStringBuilder(admin) { Database = name }.ConnectionString); + Guid id; + await using (var read = world.Db()) + { + id = (await read.AccountMerges.SingleAsync()).Id; + } + + for (var round = 0; round < 5; round++) // a fresh race each round + { + const AccountMergeNoticeKind kind = AccountMergeNoticeKind.Merged; + var results = await Task.WhenAll(Enumerable.Range(0, 6).Select(async _ => + { + await using var db = world.Db(); + return await Feed(db).AckAsync(AccountMergeNoticeTarget.Chat, id, kind, default); + })); + Assert.All(results, Assert.True); + } + + await using var check = world.Db(); + Assert.Equal(1, await check.AccountMergeNoticeAcks.CountAsync()); + Assert.Empty(await Feed(check).ReadAsync(AccountMergeNoticeTarget.Chat, null, null, default)); + } + finally + { + Npgsql.NpgsqlConnection.ClearAllPools(); + await using var connection = new Npgsql.NpgsqlConnection(admin); + await connection.OpenAsync(); + await using var drop = new Npgsql.NpgsqlCommand($"DROP DATABASE IF EXISTS \"{name}\" WITH (FORCE)", connection); + await drop.ExecuteNonQueryAsync(); + } + } +} diff --git a/tests/Bifrost.Web.Tests/Support/HeimdallTestKit.cs b/tests/Bifrost.Web.Tests/Support/HeimdallTestKit.cs index 9074fcd..b916e60 100644 --- a/tests/Bifrost.Web.Tests/Support/HeimdallTestKit.cs +++ b/tests/Bifrost.Web.Tests/Support/HeimdallTestKit.cs @@ -741,6 +741,7 @@ public sealed class HeimdallHost : TestWebApplicationFactory private readonly bool _traceHttp; private readonly bool _realSessions; private readonly bool _accountMerge; + private readonly string? _chatNoticesToken; public TestClock Clock { get; } = new(); @@ -758,9 +759,11 @@ public sealed class HeimdallHost : TestWebApplicationFactory string appSlug = "bifrost", bool traceHttp = false, bool realSessions = false, - bool accountMerge = false) + bool accountMerge = false, + string? chatNoticesToken = null) { _accountMerge = accountMerge; + _chatNoticesToken = chatNoticesToken; _traceHttp = traceHttp; _realSessions = realSessions; _appSecret = appSecret; @@ -794,6 +797,11 @@ public sealed class HeimdallHost : TestWebApplicationFactory configuration.AddInMemoryCollection(new Dictionary { ["AccountMerge:Enabled"] = "true" }); } + if (_chatNoticesToken is not null) + { + configuration.AddInMemoryCollection(new Dictionary { ["ChatNotices:Token"] = _chatNoticesToken }); + } + if (_discordRoles) { configuration.AddInMemoryCollection(new Dictionary { ["Heimdall:DiscordAllowedRoleIds:0"] = "role-1" });