feat(matching): move BotRoster from hardcoded fixture to DB-backed seed

Phase 3 shipped the AI rank battle bot pool as a hardcoded 8-entry list
inlined in SVSim.EmulatedEntrypoint/Matching/BotRoster.cs — editing meant
recompiling. Per PLAN.md 2026-06-02 item (d), move it to a Bootstrap
importer so the roster lives in seeds/bot-roster.json and the DB.

Shape mirrors PracticeOpponent end-to-end:
- BotRosterEntry (SVSim.Database/Models) — PK = AiId via the Id passthrough
  pattern. DbSet<BotRosterEntry> BotRoster on SVSimDbContext.
- AddBotRoster migration (DDL only, per migrations-are-DDL-only rule).
- seeds/bot-roster.json — 8 rows preserving the current prod-verified
  cosmetic ids (sleeve 704141010 / emblem 400001100 / degree 120027 /
  field 5) and series-1 ai_ids from rm_ai_setting.csv (1111..1181).
- BotRosterSeed POCO + BotRosterImporter (idempotent upsert keyed by AiId,
  leaves seed-missing rows intact). Wired into SVSim.Bootstrap/Program.cs
  next to PracticeOpponentImporter.
- IGlobalsRepository.GetBotRoster() + impl.

IBotRoster.Pick → PickAsync because BotRoster now depends on the transient
IGlobalsRepository. RankBattleController awaits the new signature. The
deterministic hash-on-ctx invariant (same ctx → same bot, so /ai_<fmt>/start
retries pick the same opponent) is preserved.

DI: AddSingleton<IBotRoster> → AddTransient (matches IGlobalsRepository's
lifetime). Test fixture's SeedGlobalsAsync also runs the importer so
RankBattleControllerTests + the rewritten BotRosterTests both see seeded
rows.

Tests: 931 → 936 passing. Existing 3 BotRosterTests reshaped for the DB
backing + 1 new "throws on empty roster" guard; 4 new
BotRosterImporterTests mirror PracticeOpponentImporterTests
(round-trip / idempotent / seed-missing-row-intact / ai_id=0 skip).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-02 11:58:19 -04:00
parent 8aead62116
commit 24f9b2240e
18 changed files with 4653 additions and 100 deletions

View File

@@ -0,0 +1,94 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using SVSim.Bootstrap.Importers;
using SVSim.Database;
using SVSim.UnitTests.Infrastructure;
namespace SVSim.UnitTests.Importers;
public class BotRosterImporterTests
{
private static string SeedDir => Path.Combine(AppContext.BaseDirectory, "Data", "seeds");
[Test]
public async Task Imports_bots_from_seed_file()
{
using var factory = new SVSimTestFactory();
using var scope = factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
await new BotRosterImporter().ImportAsync(db, SeedDir);
var bots = await db.BotRoster.OrderBy(b => b.Id).ToListAsync();
Assert.That(bots.Count, Is.GreaterThan(0), "seed file must contain bots");
Assert.That(bots.All(b => b.ClassId is >= 1 and <= 8), Is.True);
Assert.That(bots.All(b => !string.IsNullOrEmpty(b.UserName)), Is.True);
}
[Test]
public async Task Is_idempotent_on_rerun()
{
using var factory = new SVSimTestFactory();
using var scope = factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
await new BotRosterImporter().ImportAsync(db, SeedDir);
int before = await db.BotRoster.CountAsync();
await new BotRosterImporter().ImportAsync(db, SeedDir);
int after = await db.BotRoster.CountAsync();
Assert.That(after, Is.EqualTo(before));
}
[Test]
public async Task Leaves_existing_rows_untouched_when_missing_from_seed()
{
using var factory = new SVSimTestFactory();
using var scope = factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
const int legacyAiId = 99999;
db.BotRoster.Add(new SVSim.Database.Models.BotRosterEntry
{
Id = legacyAiId,
CountryCode = "ZZ",
UserName = "legacy",
SleeveId = 1,
EmblemId = 1,
DegreeId = 1,
FieldId = 1,
ClassId = 1,
CharaId = 1,
Rank = 1,
});
await db.SaveChangesAsync();
await new BotRosterImporter().ImportAsync(db, SeedDir);
var legacy = await db.BotRoster.FindAsync(legacyAiId);
Assert.That(legacy, Is.Not.Null, "seed-missing row must be left intact");
Assert.That(legacy!.UserName, Is.EqualTo("legacy"), "pre-existing values must not be wiped");
}
[Test]
public async Task Skips_rows_with_zero_ai_id()
{
using var factory = new SVSimTestFactory();
using var scope = factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
string tmp = Path.Combine(Path.GetTempPath(), $"seed-{Guid.NewGuid()}");
Directory.CreateDirectory(tmp);
try
{
File.WriteAllText(Path.Combine(tmp, "bot-roster.json"),
"[{\"ai_id\":0,\"user_name\":\"junk\",\"class_id\":1}]");
await new BotRosterImporter().ImportAsync(db, tmp);
int count = await db.BotRoster.CountAsync();
Assert.That(count, Is.EqualTo(0), "rows with ai_id=0 must not be inserted");
}
finally { Directory.Delete(tmp, true); }
}
}

View File

@@ -258,6 +258,7 @@ internal sealed class SVSimTestFactory : WebApplicationFactory<Program>
await new RotationFlagUpdater().UpdateAsync(ctx);
await new PracticeOpponentImporter().ImportAsync(ctx, seedDir);
await new BotRosterImporter().ImportAsync(ctx, seedDir);
await new PaymentItemImporter().ImportAsync(ctx, seedDir);
await new ItemImporter().ImportAsync(ctx, seedDir);
await new SleeveShopImporter().ImportAsync(ctx, seedDir);

View File

@@ -1,6 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using SVSim.BattleNode.Bridge;
using SVSim.Database.Repositories.Globals;
using SVSim.EmulatedEntrypoint.Matching;
using SVSim.UnitTests.Infrastructure;
namespace SVSim.UnitTests.Matching;
@@ -13,11 +16,21 @@ public class BotRosterTests
CountryCode: "JP", UserName: userName, SleeveId: "0",
EmblemId: "0", DegreeId: "0", FieldId: 0, IsOfficial: 0, BattleType: 11);
[Test]
public void Pick_returns_a_bot_with_valid_ai_id_from_rm_ai_setting()
private static async Task<BotRoster> NewRosterAsync(SVSimTestFactory factory)
{
var roster = new BotRoster();
var bot = roster.Pick(Ctx("PlayerA", "1"));
await factory.SeedGlobalsAsync();
var scope = factory.Services.CreateScope();
var globals = scope.ServiceProvider.GetRequiredService<IGlobalsRepository>();
return new BotRoster(globals);
}
[Test]
public async Task PickAsync_returns_a_bot_with_valid_ai_id_from_rm_ai_setting()
{
using var factory = new SVSimTestFactory();
var roster = await NewRosterAsync(factory);
var bot = await roster.PickAsync(Ctx("PlayerA", "1"));
// Series-1 enemy_ai_id values from data_dumps/client-assets/rm_ai_setting.csv —
// one per class (1=Forest, 2=Sword, 3=Rune, 4=Dragon, 5=Shadow, 6=Blood, 7=Haven, 8=Portal).
@@ -26,10 +39,12 @@ public class BotRosterTests
}
[Test]
public void Pick_returns_bot_with_class_metadata_set()
public async Task PickAsync_returns_bot_with_class_metadata_set()
{
var roster = new BotRoster();
var bot = roster.Pick(Ctx("PlayerA", "1"));
using var factory = new SVSimTestFactory();
var roster = await NewRosterAsync(factory);
var bot = await roster.PickAsync(Ctx("PlayerA", "1"));
Assert.That(bot.ClassId, Is.InRange(1, 8));
Assert.That(bot.CharaId, Is.InRange(1, 8));
@@ -38,14 +53,28 @@ public class BotRosterTests
}
[Test]
public void Pick_is_deterministic_per_match_context()
public async Task PickAsync_is_deterministic_per_match_context()
{
var roster = new BotRoster();
using var factory = new SVSimTestFactory();
var roster = await NewRosterAsync(factory);
var ctx = Ctx("PlayerA", "3");
var a = roster.Pick(ctx);
var b = roster.Pick(ctx);
var a = await roster.PickAsync(ctx);
var b = await roster.PickAsync(ctx);
Assert.That(a, Is.EqualTo(b), "Same ctx → same bot, so mid-flight retries get the same opponent.");
}
[Test]
public async Task PickAsync_throws_when_roster_empty()
{
// Empty DB (no SeedGlobalsAsync call) → no rows → invariant violated.
using var factory = new SVSimTestFactory();
var scope = factory.Services.CreateScope();
var globals = scope.ServiceProvider.GetRequiredService<IGlobalsRepository>();
var roster = new BotRoster(globals);
Assert.That(async () => await roster.PickAsync(Ctx("PlayerA", "1")),
Throws.InvalidOperationException);
}
}