fix(rank-battle): route ai-start through the queue-time MatchContext

Live-smoke bug 2026-06-02: queued Bloodcraft (deck #5), wire showed
classId=2 (Swordcraft) for self_info on the /ai_unlimited_rank_battle/start
response — client rendered the wrong leader.

Two layers of the same bug:

1. MatchContextBuilder.BuildForRankBattleAsync hardcoded deckNo=1 instead
   of taking it from the do_matching request — verified against
   data_dumps/captures/traffic.ndjson L17 where deck_no=5 was on the wire.
   Signature changes to (viewerId, format, deckNo); DoMatchingInternal
   passes req.DeckNo.

2. AiStartInternal rebuilt MatchContext from scratch — but the /ai_*/start
   request body is BaseRequest only, no deck_no on the wire. The fix uses
   the MatchContext the bridge already stored at do_matching resolution time
   (in the Bot PendingBattle), so deck/cosmetic data is consistent end-to-end.
   New IBattleSessionStore.TryFindPendingForViewer(viewerId) finds the
   viewer's pending battle for lookup. The store entry persists across
   ai_start (idempotent reads are fine — the WS handler removes on connect).
   No-pending sentinel: ai_id=-1 surfaces the "no AI assigned" error in the
   client.

Tests: 936 → 939 passing.
- MatchContextBuilderTests.BuildForRankBattle_uses_the_caller_supplied_deck_number
  seeds deck #1 (class 1) and deck #5 (class 6) and asserts the deckNo
  argument picks the right one.
- RankBattleControllerTests.AiStart_self_info_class_matches_queued_deck_number
  is the end-to-end regression: register Bot battle with deck #5, hit
  /ai_unlimited_rank_battle/start, assert self_info.classId == 6.
- RankBattleControllerTests.AiStart_without_pending_battle_returns_neg1_sentinel
  locks the defensive ai_id=-1 path.
- Existing AiStart_* tests bypass do_matching, so adapted to call a new
  RegisterBotBattleAsync helper that mirrors what InProcessPairUp does on
  AI-fallback resolution.

SeedDeckAsync gains an optional classId so test cases can differentiate
decks by class (was always picking Classes.First()).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-02 12:28:42 -04:00
parent 24f9b2240e
commit 898b872edd
9 changed files with 150 additions and 34 deletions

View File

@@ -2,7 +2,10 @@ using System.Net.Http.Json;
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using SVSim.BattleNode.Bridge;
using SVSim.BattleNode.Sessions;
using SVSim.Database.Enums;
using SVSim.EmulatedEntrypoint.Services;
using SVSim.UnitTests.Infrastructure;
namespace SVSim.UnitTests.Controllers;
@@ -44,6 +47,21 @@ public class RankBattleControllerTests
steam_session_ticket = "",
};
/// <summary>
/// AiStart in the real client flow always follows a do_matching call that resolved
/// to 3011 (AI fallback) — that's when the PendingBattle is registered with the
/// viewer's queue-time MatchContext (deck/cosmetics). Tests bypass do_matching's
/// time-threshold to register a Bot PendingBattle directly via the bridge.
/// </summary>
private static async Task RegisterBotBattleAsync(SVSimTestFactory factory, long viewerId, Format format, int deckNo)
{
var bridge = factory.Services.GetRequiredService<IMatchingBridge>();
using var scope = factory.Services.CreateScope();
var builder = scope.ServiceProvider.GetRequiredService<IMatchContextBuilder>();
var ctx = await builder.BuildForRankBattleAsync(viewerId, format, deckNo);
bridge.RegisterBattle(new BattlePlayer(viewerId, ctx), p2: null, BattleType.Bot);
}
[Test]
public async Task DoMatching_rotation_first_poll_returns_3002_RETRY_with_empty_node_server_url()
{
@@ -102,6 +120,7 @@ public class RankBattleControllerTests
var viewerId = await factory.SeedViewerAsync(displayName: "TestViewer");
await factory.SeedGlobalsAsync();
await factory.SeedDeckAsync(viewerId, Format.Rotation, 1);
await RegisterBotBattleAsync(factory, viewerId, Format.Rotation, deckNo: 1);
var client = factory.CreateAuthenticatedClient(viewerId);
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", EmptyAuthedBody);
@@ -140,6 +159,7 @@ public class RankBattleControllerTests
var viewerId = await factory.SeedViewerAsync(displayName: "Alice");
await factory.SeedGlobalsAsync();
await factory.SeedDeckAsync(viewerId, Format.Rotation, 1);
await RegisterBotBattleAsync(factory, viewerId, Format.Rotation, deckNo: 1);
var client = factory.CreateAuthenticatedClient(viewerId);
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", EmptyAuthedBody);
@@ -156,6 +176,7 @@ public class RankBattleControllerTests
var viewerId = await factory.SeedViewerAsync(displayName: "PlayerA");
await factory.SeedGlobalsAsync();
await factory.SeedDeckAsync(viewerId, Format.Rotation, 1);
await RegisterBotBattleAsync(factory, viewerId, Format.Rotation, deckNo: 1);
var client = factory.CreateAuthenticatedClient(viewerId);
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", EmptyAuthedBody);
@@ -167,6 +188,48 @@ public class RankBattleControllerTests
Assert.That(oppoInfo.GetProperty("classId").GetInt32(), Is.InRange(1, 8));
}
[Test]
public async Task AiStart_self_info_class_matches_queued_deck_number()
{
// Regression for the 2026-06-02 "queued Bloodcraft, saw Swordcraft leader" wire bug.
// The original impl rebuilt MatchContext from deck #1 inside AiStart; the fix routes
// it through the PendingBattle the bridge stored at do_matching time (which carries
// the queue-time deck_no). Seed two distinct-class decks and confirm /ai_*/start
// returns the right class for a viewer registered with deck #5.
await using var factory = new SVSimTestFactory();
var viewerId = await factory.SeedViewerAsync(displayName: "Ranker");
await factory.SeedGlobalsAsync();
await factory.SeedDeckAsync(viewerId, Format.Unlimited, number: 1, classId: 1);
await factory.SeedDeckAsync(viewerId, Format.Unlimited, number: 5, classId: 6);
await RegisterBotBattleAsync(factory, viewerId, Format.Unlimited, deckNo: 5);
var client = factory.CreateAuthenticatedClient(viewerId);
var resp = await client.PostAsJsonAsync("/ai_unlimited_rank_battle/start", EmptyAuthedBody);
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
var selfInfo = doc.RootElement.GetProperty("self_info");
Assert.That(selfInfo.GetProperty("classId").GetInt32(), Is.EqualTo(6),
"Self class must reflect the deck the viewer queued with (deck #5 = Bloodcraft, class 6).");
}
[Test]
public async Task AiStart_without_pending_battle_returns_neg1_sentinel()
{
// Defensive: clients always do_matching before ai_start, but if /ai_*/start is hit
// without a registered PendingBattle (server restart, expired match, ...), the spec
// sentinel ai_id=-1 surfaces the "no AI assigned" error in the client.
await using var factory = new SVSimTestFactory();
var viewerId = await factory.SeedViewerAsync();
await factory.SeedGlobalsAsync();
await factory.SeedDeckAsync(viewerId, Format.Rotation, 1);
var client = factory.CreateAuthenticatedClient(viewerId);
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", EmptyAuthedBody);
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
Assert.That(doc.RootElement.GetProperty("ai_id").GetInt32(), Is.EqualTo(-1));
}
[Test]
public async Task Finish_emits_stubbed_zeros_with_battle_result_echo()
{