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

73 lines
2.9 KiB
C#

using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RadicadeServer.API.GameRewards.V1;
using RadicadeServer.DB;
using RadicadeServer.Extensions;
using RadicadeServer.RecNet.Avatar;
using RadicadeServer.RecNet.Progression;
using RadicadeServer.RecNet.Storefront;
using Xunit;
namespace RadicadeServer.Tests;
public class GameRewardPersistenceTests
{
[Fact]
public async Task CorrectedGiftIdsSurviveReloadAndRepeatedPendingRequests()
{
var options = TestDbOptions.Create()
.UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
const long selectionId = 5425026455704055785;
await using (var seed = new RecDBContext(options))
{
seed.Progression.Add(new ProgressionData
{
PlayerId = 2,
Progress = new ProgressionJson
{
XpEarnedToday = 75,
PendingGameRewards = [new GameReward
{
RewardSelectionId = selectionId,
GiftDrop1 = new Gift { GiftDropId = long.MaxValue },
GiftDrop2 = new Gift { GiftDropId = long.MinValue },
GiftDrop3 = new Gift { GiftDropId = 123 }
}]
}.ToJson()
});
await seed.SaveChangesAsync();
}
static GameRewardsAPI CreateApi(RecDBContext db) => new(db)
{
ControllerContext = new ControllerContext
{
HttpContext = new DefaultHttpContext
{
User = new ClaimsPrincipal(new ClaimsIdentity(
[new Claim(ClaimTypes.NameIdentifier, "2")], "test"))
}
}
};
await using var firstDb = new RecDBContext(options);
var first = Assert.Single(await CreateApi(firstDb).GetPendingGameRewards());
Assert.InRange(first.GiftDrop1!.GiftDropId, 1L, int.MaxValue);
Assert.InRange(first.GiftDrop2!.GiftDropId, 1L, int.MaxValue);
Assert.Equal(123L, first.GiftDrop3!.GiftDropId);
Assert.Equal(selectionId, first.RewardSelectionId);
await using var nextDb = new RecDBContext(options);
var persisted = (await nextDb.Progression.SingleAsync()).GetProgressionJson()!;
Assert.Equal(75, persisted.XpEarnedToday);
var saved = Assert.Single(persisted.PendingGameRewards);
Assert.Equal(first.GiftDrop1.GiftDropId, saved.GiftDrop1!.GiftDropId);
Assert.Equal(first.GiftDrop2.GiftDropId, saved.GiftDrop2!.GiftDropId);
var second = Assert.Single(await CreateApi(nextDb).GetPendingGameRewards());
Assert.Equal(first.GiftDrop1.GiftDropId, second.GiftDrop1!.GiftDropId);
Assert.Equal(first.GiftDrop2.GiftDropId, second.GiftDrop2!.GiftDropId);
}
}