feat(inventory): TrySpendAsync covers all 4 wallets + freeplay

Crystal/Rupy/RedEther freeplay no-op (returns configured amount,
balance unchanged); SpotPoint always real. Insufficient returns
current balance; success returns post-deduction balance.
SVSimTestFactory gains freeplayEnabled ctor overload that upserts
the Freeplay GameConfigSection row after EnsureSeedData.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-05-31 15:59:26 -04:00
parent a821b7f6b4
commit 301da9eeca
3 changed files with 127 additions and 3 deletions

View File

@@ -28,9 +28,11 @@ internal sealed class SVSimTestFactory : WebApplicationFactory<Program>
{
private readonly SqliteConnection _connection;
private long _nextSeededShortUdid = 400_000_001;
private readonly bool _freeplayEnabled;
public SVSimTestFactory()
public SVSimTestFactory(bool freeplayEnabled = false)
{
_freeplayEnabled = freeplayEnabled;
// SQLite :memory: lives only as long as a connection is open — keep ours open for the
// factory's lifetime so the DbContext can reattach to the same DB across scopes.
_connection = new SqliteConnection("DataSource=:memory:");
@@ -59,6 +61,19 @@ internal sealed class SVSimTestFactory : WebApplicationFactory<Program>
db.Database.EnsureCreated();
db.EnsureSeedDataAsync().GetAwaiter().GetResult();
if (_freeplayEnabled)
{
using var seedScope = host.Services.CreateScope();
var seedDb = seedScope.ServiceProvider.GetRequiredService<SVSimDbContext>();
const string freeplayJson = "{\"Enabled\":true,\"CurrencyAmount\":99999,\"CardCopies\":3}";
var existing = seedDb.GameConfigs.FirstOrDefault(s => s.SectionName == "Freeplay");
if (existing is null)
seedDb.GameConfigs.Add(new SVSim.Database.Models.GameConfigSection { SectionName = "Freeplay", ValueJson = freeplayJson });
else
existing.ValueJson = freeplayJson;
seedDb.SaveChanges();
}
// Reference data is no longer HasData-seeded; load the CSVs via the same importer
// production uses so tests exercise the same code path. CardCosmeticRewards skipped —
// FK to Cards would reject every row against the minimal 3-card test seed below.

View File

@@ -0,0 +1,70 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using SVSim.Database;
using SVSim.Database.Models.Config;
using SVSim.Database.Services;
using SVSim.Database.Services.Inventory;
using SVSim.UnitTests.Infrastructure;
namespace SVSim.UnitTests.Services.Inventory;
public class InventorySpendTests
{
[Test]
public async Task Spend_sufficient_returns_post_deduction_total()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
using var scope = factory.Services.CreateScope();
var ctx = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
var v = await ctx.Viewers.FirstAsync(x => x.Id == viewerId);
v.Currency.Crystals = 1000;
await ctx.SaveChangesAsync();
var inv = scope.ServiceProvider.GetRequiredService<IInventoryService>();
await using var tx = await inv.BeginAsync(viewerId);
var r = await tx.TrySpendAsync(SpendCurrency.Crystal, 300);
Assert.That(r.Success, Is.True);
Assert.That(r.PostStateTotal, Is.EqualTo(700));
Assert.That(tx.Viewer.Currency.Crystals, Is.EqualTo(700UL));
}
[Test]
public async Task Spend_insufficient_returns_insufficient_with_current_balance()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
using var scope = factory.Services.CreateScope();
var ctx = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
var v = await ctx.Viewers.FirstAsync(x => x.Id == viewerId);
v.Currency.Crystals = 100;
await ctx.SaveChangesAsync();
var inv = scope.ServiceProvider.GetRequiredService<IInventoryService>();
await using var tx = await inv.BeginAsync(viewerId);
var r = await tx.TrySpendAsync(SpendCurrency.Crystal, 300);
Assert.That(r.Outcome, Is.EqualTo(SpendOutcome.Insufficient));
Assert.That(r.PostStateTotal, Is.EqualTo(100));
Assert.That(tx.Viewer.Currency.Crystals, Is.EqualTo(100UL), "balance unchanged");
}
[Test]
public async Task Freeplay_returns_success_with_configured_amount_for_main_currencies()
{
using var factory = new SVSimTestFactory(freeplayEnabled: true);
long viewerId = await factory.SeedViewerAsync();
using var scope = factory.Services.CreateScope();
var inv = scope.ServiceProvider.GetRequiredService<IInventoryService>();
await using var tx = await inv.BeginAsync(viewerId);
ulong balanceBefore = tx.Viewer.Currency.Crystals;
var r = await tx.TrySpendAsync(SpendCurrency.Crystal, 99999);
Assert.That(r.Success, Is.True);
var freeCfg = scope.ServiceProvider.GetRequiredService<IGameConfigService>().Get<FreeplayConfig>();
Assert.That(r.PostStateTotal, Is.EqualTo(checked((long)freeCfg.CurrencyAmount)));
Assert.That(tx.Viewer.Currency.Crystals, Is.EqualTo(balanceBefore), "freeplay never deducts");
}
}