Files
Radicade/ServerTests/MatchmakingRuntimeStateTests.cs
2026-10-16 23:33:13 -07:00

57 lines
2.2 KiB
C#

using Microsoft.EntityFrameworkCore;
using RadicadeServer.DB;
using RadicadeServer.RecNet.Accounts;
using RadicadeServer.RecNet.Events;
using RadicadeServer.RecNet.Rooms;
using RadicadeServer.Utils;
using Xunit;
namespace RadicadeServer.Tests;
public class MatchmakingRuntimeStateTests
{
[Fact]
public async Task RestartClearsBroadcastSourcesIncludingPreviouslyDeletedInstances()
{
var options = TestDbOptions.Create()
.UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
await using var db = new RecDBContext(options);
db.RoomInstances.Add(new RoomInstance { roomInstanceId = 30895, roomId = 102, eventId = 9102 });
db.Presences.Add(new RealPresence { playerId = 2, isOnline = true, roomInstance = 30895 });
foreach (var (id, source) in new (long, long?)[] { (9102, 30895), (9103, 99999), (9104, null) })
db.PlayerEvents.Add(new PlayerEventDB
{
PlayerEventId = id,
CreatorPlayerId = 2,
RoomId = 102,
Name = "Broadcast test",
Description = "Restart recovery",
IsMultiInstance = true,
DefaultBroadcastPermissions = 3,
BroadcastingRoomInstanceId = source,
StartTime = DateTime.UtcNow.AddMinutes(-5),
EndTime = DateTime.UtcNow.AddHours(1)
});
await db.SaveChangesAsync();
await MatchmakingRuntimeState.ResetAsync(db);
// A second startup must also be safe when there is no remaining runtime state.
await MatchmakingRuntimeState.ResetAsync(db);
await using var verify = new RecDBContext(options);
Assert.Empty(await verify.RoomInstances.ToListAsync());
var presence = await verify.Presences.SingleAsync();
Assert.False(presence.isOnline);
Assert.Null(presence.roomInstance);
var events = await verify.PlayerEvents.ToListAsync();
Assert.Equal(3, events.Count);
Assert.All(events, ev =>
{
Assert.Null(ev.BroadcastingRoomInstanceId);
Assert.True(ev.IsMultiInstance);
Assert.Equal(3, ev.DefaultBroadcastPermissions);
Assert.Equal("Broadcast test", ev.Name);
});
}
}