fix(guild): make GuildInvite unique index partial on Status=Pending to allow re-invite after reject/cancel
Partial index (filter: "Status" = 0) replaces the unconditional unique index on (GuildId, InviteeViewerId). Old Rejected/Canceled rows no longer conflict when a new Pending invite is inserted for the same pair. Adds regression tests: Reinvite_after_reject_succeeds and Reinvite_after_cancel_succeeds. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
5008
SVSim.Database/Migrations/20260627172620_MakeGuildInviteUniqueIndexPartial.Designer.cs
generated
Normal file
5008
SVSim.Database/Migrations/20260627172620_MakeGuildInviteUniqueIndexPartial.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,39 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SVSim.Database.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class MakeGuildInviteUniqueIndexPartial : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_GuildInvites_GuildId_InviteeViewerId",
|
||||
table: "GuildInvites");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_GuildInvites_GuildId_InviteeViewerId",
|
||||
table: "GuildInvites",
|
||||
columns: new[] { "GuildId", "InviteeViewerId" },
|
||||
unique: true,
|
||||
filter: "\"Status\" = 0");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_GuildInvites_GuildId_InviteeViewerId",
|
||||
table: "GuildInvites");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_GuildInvites_GuildId_InviteeViewerId",
|
||||
table: "GuildInvites",
|
||||
columns: new[] { "GuildId", "InviteeViewerId" },
|
||||
unique: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -536,7 +536,8 @@ public class SVSimDbContext : DbContext
|
||||
e.Property(i => i.Id).ValueGeneratedOnAdd();
|
||||
e.HasOne(i => i.Guild).WithMany().HasForeignKey(i => i.GuildId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
e.HasIndex(i => new { i.GuildId, i.InviteeViewerId }).IsUnique(); // one pending invite per pair
|
||||
e.HasIndex(i => new { i.GuildId, i.InviteeViewerId }).IsUnique()
|
||||
.HasFilter("\"Status\" = 0"); // partial: one PENDING invite per pair
|
||||
e.HasIndex(i => new { i.InviteeViewerId, i.Status }); // "my pending invites"
|
||||
});
|
||||
|
||||
|
||||
@@ -234,6 +234,105 @@ public class GuildInviteFlowTests
|
||||
Assert.That(rc.GetInt32(), Is.EqualTo(2), $"Expected error code 2, got: {attemptJson}");
|
||||
}
|
||||
|
||||
// ─── Re-invite after reject / cancel (partial-index regression) ─────────
|
||||
|
||||
[Test]
|
||||
public async Task Reinvite_after_reject_succeeds()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
|
||||
// A creates guild and becomes leader.
|
||||
long viewerA = await factory.SeedViewerAsync(steamId: 76_561_198_300_000_061UL, displayName: "ReinviteLeader1");
|
||||
using var clientA = factory.CreateAuthenticatedClient(viewerA);
|
||||
await clientA.PostAsync("/guild/create",
|
||||
JsonContent.Create(new { guild_name = "ReinviteRejectGuild", activity = 1, join_condition = 1,
|
||||
viewer_id = Vid, steam_id = Sid, steam_session_ticket = Stk }));
|
||||
|
||||
// B: not in any guild.
|
||||
long viewerB = await factory.SeedViewerAsync(steamId: 76_561_198_300_000_062UL, displayName: "ReinviteeB1");
|
||||
using var clientB = factory.CreateAuthenticatedClient(viewerB);
|
||||
|
||||
// First invite: A → B.
|
||||
await clientA.PostAsync("/guild/invite",
|
||||
JsonContent.Create(new { invited_viewer_id = (int)viewerB,
|
||||
invite_message = "", viewer_id = Vid, steam_id = Sid, steam_session_ticket = Stk }));
|
||||
|
||||
// B rejects.
|
||||
var list1 = await clientB.PostAsync("/guild/invited_guild_list",
|
||||
JsonContent.Create(new { page = 0, viewer_id = Vid, steam_id = Sid, steam_session_ticket = Stk }));
|
||||
using var l1Doc = JsonDocument.Parse(await list1.Content.ReadAsStringAsync());
|
||||
long inviteId1 = l1Doc.RootElement.GetProperty("list")[0].GetProperty("invite_id").GetInt64();
|
||||
|
||||
await clientB.PostAsync("/guild/reject_invite",
|
||||
JsonContent.Create(new { invite_id = inviteId1,
|
||||
viewer_id = Vid, steam_id = Sid, steam_session_ticket = Stk }));
|
||||
|
||||
// Second invite: A → B again (was crashing before partial index).
|
||||
var reinvite = await clientA.PostAsync("/guild/invite",
|
||||
JsonContent.Create(new { invited_viewer_id = (int)viewerB,
|
||||
invite_message = "", viewer_id = Vid, steam_id = Sid, steam_session_ticket = Stk }));
|
||||
var reinviteJson = await reinvite.Content.ReadAsStringAsync();
|
||||
Assert.That(reinvite.IsSuccessStatusCode, Is.True, $"Re-invite after reject HTTP failed: {reinviteJson}");
|
||||
using var riDoc = JsonDocument.Parse(reinviteJson);
|
||||
if (riDoc.RootElement.TryGetProperty("result_code", out var rc))
|
||||
Assert.That(rc.GetInt32(), Is.EqualTo(1), $"Re-invite after reject must return result_code=1, got: {reinviteJson}");
|
||||
|
||||
// B should see a new pending invite.
|
||||
var list2 = await clientB.PostAsync("/guild/invited_guild_list",
|
||||
JsonContent.Create(new { page = 0, viewer_id = Vid, steam_id = Sid, steam_session_ticket = Stk }));
|
||||
using var l2Doc = JsonDocument.Parse(await list2.Content.ReadAsStringAsync());
|
||||
Assert.That(l2Doc.RootElement.GetProperty("list").GetArrayLength(), Is.EqualTo(1),
|
||||
"After re-invite post-reject, invitee should see exactly 1 pending invite");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Reinvite_after_cancel_succeeds()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
|
||||
// A creates guild and becomes leader.
|
||||
long viewerA = await factory.SeedViewerAsync(steamId: 76_561_198_300_000_071UL, displayName: "ReinviteLeader2");
|
||||
using var clientA = factory.CreateAuthenticatedClient(viewerA);
|
||||
await clientA.PostAsync("/guild/create",
|
||||
JsonContent.Create(new { guild_name = "ReinviteCancelGuild", activity = 1, join_condition = 1,
|
||||
viewer_id = Vid, steam_id = Sid, steam_session_ticket = Stk }));
|
||||
|
||||
// B: not in any guild.
|
||||
long viewerB = await factory.SeedViewerAsync(steamId: 76_561_198_300_000_072UL, displayName: "ReinviteeB2");
|
||||
using var clientB = factory.CreateAuthenticatedClient(viewerB);
|
||||
|
||||
// First invite: A → B.
|
||||
await clientA.PostAsync("/guild/invite",
|
||||
JsonContent.Create(new { invited_viewer_id = (int)viewerB,
|
||||
invite_message = "", viewer_id = Vid, steam_id = Sid, steam_session_ticket = Stk }));
|
||||
|
||||
// A cancels.
|
||||
var ulResp = await clientA.PostAsync("/guild/invite_user_list", JsonContent.Create(BaseReq()));
|
||||
using var ulDoc = JsonDocument.Parse(await ulResp.Content.ReadAsStringAsync());
|
||||
long inviteId1 = ulDoc.RootElement.GetProperty("list")[0].GetProperty("invite_id").GetInt64();
|
||||
|
||||
await clientA.PostAsync("/guild/cancel_invite",
|
||||
JsonContent.Create(new { invite_id = inviteId1,
|
||||
viewer_id = Vid, steam_id = Sid, steam_session_ticket = Stk }));
|
||||
|
||||
// Second invite: A → B again (was crashing before partial index).
|
||||
var reinvite = await clientA.PostAsync("/guild/invite",
|
||||
JsonContent.Create(new { invited_viewer_id = (int)viewerB,
|
||||
invite_message = "", viewer_id = Vid, steam_id = Sid, steam_session_ticket = Stk }));
|
||||
var reinviteJson = await reinvite.Content.ReadAsStringAsync();
|
||||
Assert.That(reinvite.IsSuccessStatusCode, Is.True, $"Re-invite after cancel HTTP failed: {reinviteJson}");
|
||||
using var riDoc = JsonDocument.Parse(reinviteJson);
|
||||
if (riDoc.RootElement.TryGetProperty("result_code", out var rc))
|
||||
Assert.That(rc.GetInt32(), Is.EqualTo(1), $"Re-invite after cancel must return result_code=1, got: {reinviteJson}");
|
||||
|
||||
// B should see a new pending invite.
|
||||
var list2 = await clientB.PostAsync("/guild/invited_guild_list",
|
||||
JsonContent.Create(new { page = 0, viewer_id = Vid, steam_id = Sid, steam_session_ticket = Stk }));
|
||||
using var l2Doc = JsonDocument.Parse(await list2.Content.ReadAsStringAsync());
|
||||
Assert.That(l2Doc.RootElement.GetProperty("list").GetArrayLength(), Is.EqualTo(1),
|
||||
"After re-invite post-cancel, invitee should see exactly 1 pending invite");
|
||||
}
|
||||
|
||||
// ─── Wire-shape literal JSON test for invited_guild_list ─────────────────
|
||||
|
||||
[Test]
|
||||
|
||||
Reference in New Issue
Block a user