refactor(battle-node): remove ScriptedBotParticipant and dev-affordance wiring

Deletes the scripted opponent and every entry point that created a
BattleType.Scripted session (the ?scripted=1 query opt-in, the
SoloDefaultsToScripted toggle, the resolver short-circuit, the WS handler case,
the bridge validation arm). Real two-client PvP and the Bot matchmaking-timeout
fallback are untouched. ResolveAsync drops its scriptedOptIn parameter.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-03 20:15:48 -04:00
parent 8085119439
commit f21ab7a38c
21 changed files with 49 additions and 395 deletions

View File

@@ -8,19 +8,19 @@ namespace SVSim.UnitTests.BattleNode.Bridge;
public class MatchingBridgeTests
{
[Test]
public void RegisterBattle_Scripted_stores_pending_and_returns_node_url()
public void RegisterBattle_Bot_stores_pending_and_returns_node_url()
{
var store = new InMemoryBattleSessionStore();
var bridge = new MatchingBridge(store, new BattleNodeOptions { NodeServerUrl = "localhost:5148/socket.io/" });
var p1 = new BattlePlayer(906243102, FixtureCtx());
var match = bridge.RegisterBattle(p1, p2: null, BattleType.Scripted);
var match = bridge.RegisterBattle(p1, p2: null, BattleType.Bot);
Assert.That(match.NodeServerUrl, Is.EqualTo("localhost:5148/socket.io/"));
Assert.That(match.BattleId, Is.Not.Empty);
var pending = store.TryGetPending(match.BattleId);
Assert.That(pending, Is.Not.Null);
Assert.That(pending!.Type, Is.EqualTo(BattleType.Scripted));
Assert.That(pending!.Type, Is.EqualTo(BattleType.Bot));
Assert.That(pending.P1.ViewerId, Is.EqualTo(906243102));
Assert.That(pending.P2, Is.Null);
}
@@ -30,8 +30,8 @@ public class MatchingBridgeTests
{
var bridge = new MatchingBridge(new InMemoryBattleSessionStore(), new BattleNodeOptions());
var a = bridge.RegisterBattle(new BattlePlayer(1, FixtureCtx()), null, BattleType.Scripted);
var b = bridge.RegisterBattle(new BattlePlayer(2, FixtureCtx()), null, BattleType.Scripted);
var a = bridge.RegisterBattle(new BattlePlayer(1, FixtureCtx()), null, BattleType.Bot);
var b = bridge.RegisterBattle(new BattlePlayer(2, FixtureCtx()), null, BattleType.Bot);
Assert.That(a.BattleId, Is.Not.EqualTo(b.BattleId));
}
@@ -41,7 +41,7 @@ public class MatchingBridgeTests
{
var bridge = new MatchingBridge(new InMemoryBattleSessionStore(), new BattleNodeOptions());
var match = bridge.RegisterBattle(new BattlePlayer(1, FixtureCtx()), null, BattleType.Scripted);
var match = bridge.RegisterBattle(new BattlePlayer(1, FixtureCtx()), null, BattleType.Bot);
Assert.That(match.BattleId, Has.Length.EqualTo(12));
Assert.That(match.BattleId, Does.Match("^[0-9]{12}$"));

View File

@@ -173,8 +173,8 @@ public class ScriptedLifecycleTests
EmblemId: "701441011", DegreeId: "300003", FieldId: 43, IsOfficial: 0,
BattleType: 11);
// Mirrors ScriptedBotParticipant.Context — the scripted opponent's MatchContext fixture
// that the new BuildMatched/BuildBattleStart helpers read from for the oppo half.
// A prod-captured opponent MatchContext fixture that the BuildMatched/BuildBattleStart
// helpers read from for the oppo half.
private static MatchContext ScriptedBotCtx() => new(
SelfDeckCardIds: Enumerable.Range(1, 30).Select(_ => 0L).ToList(),
ClassId: "8", CharaId: "8", CardMasterName: "card_master_node_10015",

View File

@@ -159,7 +159,7 @@ public class TypedBodyWireShapeTests
EmblemId: "701441011", DegreeId: "300003", FieldId: 43, IsOfficial: 0,
BattleType: 11);
// Mirrors ScriptedBotParticipant.Context — 30-card deck and the prod-captured opponent
// Prod-captured opponent fixture — 30-card deck and the prod-captured opponent
// cosmetics (ClassId/CharaId "8") so the wire bytes asserted below (oppoInfo classId/charaId,
// oppoDeckCount=30, etc.) remain byte-identical after the BuildMatched/BuildBattleStart
// signature change.

View File

@@ -12,9 +12,8 @@ namespace SVSim.UnitTests.BattleNode.Sessions;
/// <summary>
/// Audit Md11 — confirms <see cref="BattleSession.RunAsync"/> drops the per-RealParticipant
/// <see cref="SVSim.BattleNode.Reliability.OutboundSequencer"/> archive when the session
/// terminates. The Scripted bot has no outbound archive of its own, so the test uses a
/// Scripted session (one Real, one ScriptedBot) and asserts only the Real side's archive
/// is cleared.
/// terminates. The NoOp bot has no outbound archive of its own, so the test uses a Bot
/// session (one Real, one NoOpBot) and asserts only the Real side's archive is cleared.
/// </summary>
[TestFixture]
public class BattleSessionTerminateCascadeTests
@@ -25,7 +24,7 @@ public class BattleSessionTerminateCascadeTests
var ws = new TestWebSocket();
var real = new RealParticipant(
ws, viewerId: 1, MakeFakeContext(), NullLogger<RealParticipant>.Instance);
var bot = new ScriptedBotParticipant();
var bot = new NoOpBotParticipant();
// Pre-load the archive so we can prove it was cleared (not just empty).
real.Outbound.AssignAndArchive(MakeEnvelope(NetworkBattleUri.Matched));
@@ -33,7 +32,7 @@ public class BattleSessionTerminateCascadeTests
Assume.That(real.Outbound.Archive.Count, Is.EqualTo(2), "Precondition: archive populated.");
var session = new BattleSession(
battleId: "test-bid", type: BattleType.Scripted,
battleId: "test-bid", type: BattleType.Bot,
a: real, b: bot, log: NullLogger<BattleSession>.Instance);
// Drive RunAsync to completion: closing the incoming side causes

View File

@@ -1,4 +1,4 @@
using NUnit.Framework;
using NUnit.Framework;
using SVSim.BattleNode.Bridge;
using SVSim.BattleNode.Sessions;
@@ -14,7 +14,7 @@ public class InMemoryBattleSessionStoreTests
[Test]
public void RegisterThenGet_ReturnsRegisteredBattle()
{
var battle = new PendingBattle("bid-1", BattleType.Scripted, new BattlePlayer(906243102, FixtureCtx()), null);
var battle = new PendingBattle("bid-1", BattleType.Bot, new BattlePlayer(906243102, FixtureCtx()), null);
_store.RegisterPending(battle);
Assert.That(_store.TryGetPending("bid-1"), Is.EqualTo(battle));
@@ -29,7 +29,7 @@ public class InMemoryBattleSessionStoreTests
[Test]
public void Remove_ReturnsTrueWhenPresent_FalseWhenAbsent()
{
_store.RegisterPending(new PendingBattle("bid", BattleType.Scripted, new BattlePlayer(1, FixtureCtx()), null));
_store.RegisterPending(new PendingBattle("bid", BattleType.Bot, new BattlePlayer(1, FixtureCtx()), null));
Assert.That(_store.RemovePending("bid"), Is.True);
Assert.That(_store.RemovePending("bid"), Is.False);
}
@@ -37,8 +37,8 @@ public class InMemoryBattleSessionStoreTests
[Test]
public void Register_DuplicateBattleId_OverwritesPrior()
{
_store.RegisterPending(new PendingBattle("bid", BattleType.Scripted, new BattlePlayer(1, FixtureCtx()), null));
_store.RegisterPending(new PendingBattle("bid", BattleType.Scripted, new BattlePlayer(2, FixtureCtx()), null));
_store.RegisterPending(new PendingBattle("bid", BattleType.Bot, new BattlePlayer(1, FixtureCtx()), null));
_store.RegisterPending(new PendingBattle("bid", BattleType.Bot, new BattlePlayer(2, FixtureCtx()), null));
Assert.That(_store.TryGetPending("bid")!.P1.ViewerId, Is.EqualTo(2));
}
@@ -49,3 +49,4 @@ public class InMemoryBattleSessionStoreTests
EmblemId: "701441011", DegreeId: "300003", FieldId: 43, IsOfficial: 0,
BattleType: 11);
}

View File

@@ -12,18 +12,24 @@ namespace SVSim.UnitTests.Controllers;
public class ArenaTwoPickBattleControllerTests
{
[Test]
public async Task DoMatching_AuthenticatedViewer_Returns3004WithBattleIdAndNodeUrl()
public async Task DoMatching_joiner_Returns3004WithBattleIdAndNodeUrlAndCardMaster()
{
using var factory = new SVSimTestFactory();
var viewerId = await factory.SeedViewerAsync();
await SeedCompleteTwoPickRunAsync(factory, viewerId);
var vidA = await factory.SeedViewerAsync(steamId: 76_561_198_000_000_021UL);
var vidB = await factory.SeedViewerAsync(steamId: 76_561_198_000_000_022UL);
await SeedCompleteTwoPickRunAsync(factory, vidA);
await SeedCompleteTwoPickRunAsync(factory, vidB);
using var clientA = factory.CreateAuthenticatedClient(vidA);
using var clientB = factory.CreateAuthenticatedClient(vidB);
using var client = factory.CreateAuthenticatedClient(viewerId);
var req = new {
deck_no = 1L, need_init = 1, log = 1, excluded_field_id_list = new long[] { }, use_stage_select = 1, is_default_skin = 0,
viewer_id = "0", steam_id = 0, steam_session_ticket = "",
};
var resp = await client.PostAsync("/arena_two_pick_battle/do_matching?scripted=1", JsonContent.Create(req));
// A parks first; B triggers the pair and gets the 3004 joiner response.
await clientA.PostAsync("/arena_two_pick_battle/do_matching", JsonContent.Create(req));
var resp = await clientB.PostAsync("/arena_two_pick_battle/do_matching", JsonContent.Create(req));
Assert.That(resp.StatusCode, Is.EqualTo(HttpStatusCode.OK));
var body = await resp.Content.ReadAsStringAsync();
@@ -72,29 +78,6 @@ public class ArenaTwoPickBattleControllerTests
Assert.That(root.GetProperty("node_server_url").GetString(), Is.EqualTo(""));
}
[Test]
public async Task DoMatching_with_scripted_flag_returns_3004_Scripted_match_immediately()
{
using var factory = new SVSimTestFactory();
var vid = await factory.SeedViewerAsync();
await SeedCompleteTwoPickRunAsync(factory, vid);
using var client = factory.CreateAuthenticatedClient(vid);
var req = new {
deck_no = 1L, need_init = 1, log = 1, excluded_field_id_list = new long[] { }, use_stage_select = 1, is_default_skin = 0,
viewer_id = "0", steam_id = 0, steam_session_ticket = "",
};
var resp = await client.PostAsync("/arena_two_pick_battle/do_matching?scripted=1", JsonContent.Create(req));
Assert.That(resp.StatusCode, Is.EqualTo(HttpStatusCode.OK));
var body = await resp.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(body);
var root = doc.RootElement;
Assert.That(root.GetProperty("matching_state").GetInt32(), Is.EqualTo(3004));
Assert.That(root.GetProperty("battle_id").GetString(), Is.Not.Null.And.Not.Empty);
}
[Test]
public async Task DoMatching_two_pollers_get_3004_joiner_and_3007_owner_with_same_BattleId()
{
@@ -137,35 +120,6 @@ public class ArenaTwoPickBattleControllerTests
"Owner and joiner must see the same node_server_url.");
}
[Test]
public async Task DoMatching_SoloDefaultsToScripted_flag_makes_solo_poll_return_3004_without_query_param()
{
using var factory = new SVSimTestFactory();
// BattleNodeOptions is a singleton in DI; flipping it before the request takes
// effect immediately for this factory. Real deployments toggle it via the
// "BattleNode:SoloDefaultsToScripted" key in appsettings*.json.
factory.Services.GetRequiredService<BattleNodeOptions>().SoloDefaultsToScripted = true;
var vid = await factory.SeedViewerAsync();
await SeedCompleteTwoPickRunAsync(factory, vid);
using var client = factory.CreateAuthenticatedClient(vid);
var req = new {
deck_no = 1L, need_init = 1, log = 1, excluded_field_id_list = new long[] { }, use_stage_select = 1, is_default_skin = 0,
viewer_id = "0", steam_id = 0, steam_session_ticket = "",
};
// No ?scripted=1 — the flag alone should drive the Scripted branch.
var resp = await client.PostAsync("/arena_two_pick_battle/do_matching", JsonContent.Create(req));
Assert.That(resp.StatusCode, Is.EqualTo(HttpStatusCode.OK));
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
var root = doc.RootElement;
Assert.That(root.GetProperty("matching_state").GetInt32(), Is.EqualTo(3004),
"SoloDefaultsToScripted=true should bypass pair-up and return a Scripted 3004 SUCCEEDED.");
Assert.That(root.GetProperty("battle_id").GetString(), Is.Not.Null.And.Not.Empty);
Assert.That(root.GetProperty("node_server_url").GetString(), Does.Contain("/socket.io/"));
}
[Test]
public async Task DoMatching_NoActiveRun_Returns400WithErrorCode()

View File

@@ -1,118 +0,0 @@
using System.Net.Http.Json;
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using SVSim.BattleNode.Bridge;
using SVSim.Database;
using SVSim.Database.Enums;
using SVSim.Database.Models;
using SVSim.UnitTests.Infrastructure;
namespace SVSim.UnitTests.Controllers;
/// <summary>
/// Cross-family contract for <c>/do_matching</c>. The single load-bearing assertion: when
/// <see cref="BattleNodeOptions.SoloDefaultsToScripted"/> is <c>true</c>, every family's
/// first poll must bypass pair-up and return a SUCCEEDED matching_state with a battle_id +
/// node_server_url — not the 3002 RETRY of the normal pair-up path.
/// <para>
/// Adding a new family is the failure trigger for this test: the new controller MUST route
/// through <see cref="SVSim.EmulatedEntrypoint.Matching.IMatchingResolver"/>, or this test
/// fails. That's the point — the test enforces "stay in line" across families.
/// </para>
/// </summary>
[TestFixture]
public class DoMatchingContractTests
{
private static readonly object DoMatchingBody = new
{
deck_no = 1L,
need_init = 1,
log = 1,
excluded_field_id_list = Array.Empty<long>(),
use_stage_select = 1,
is_default_skin = 0,
viewer_id = "0",
steam_id = 0,
steam_session_ticket = "",
};
[TestCase("/arena_two_pick_battle/do_matching", FamilyKind.TwoPick)]
[TestCase("/rotation_rank_battle/do_matching", FamilyKind.RankRotation)]
[TestCase("/unlimited_rank_battle/do_matching", FamilyKind.RankUnlimited)]
public async Task SoloDefaultsToScripted_short_circuits_every_family_to_immediate_SUCCEEDED(string url, FamilyKind family)
{
await using var factory = new SVSimTestFactory();
factory.Services.GetRequiredService<BattleNodeOptions>().SoloDefaultsToScripted = true;
var viewerId = await factory.SeedViewerAsync();
await SetupFamilyAsync(factory, viewerId, family);
using var client = factory.CreateAuthenticatedClient(viewerId);
var resp = await client.PostAsJsonAsync(url, DoMatchingBody);
Assert.That(resp.IsSuccessStatusCode, Is.True, $"Expected 2xx from {url}, got {resp.StatusCode}.");
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
var root = doc.RootElement;
var state = root.GetProperty("matching_state").GetInt32();
Assert.That(state, Is.Not.EqualTo(3002),
$"{url}: SoloDefaultsToScripted=true must bypass pair-up; saw matching_state=3002 RETRY which means the family didn't honor the flag (probably forgot to route through IMatchingResolver).");
Assert.That(state, Is.AnyOf(3004, 3007, 3011),
$"{url}: matching_state must be SUCCEEDED (3004), SUCCEEDED_OWNER (3007), or AI_SUCCEEDED (3011) — got {state}.");
Assert.That(root.GetProperty("battle_id").GetString(), Is.Not.Null.And.Not.Empty,
$"{url}: SUCCEEDED responses must carry battle_id.");
Assert.That(root.GetProperty("node_server_url").GetString(), Does.Contain("/socket.io/"),
$"{url}: node_server_url must point at the WS endpoint.");
}
// Each family has different prerequisites — TK2 needs an active draft run, rank needs
// a deck for the requested format. The factory's seeders are sufficient for both.
public enum FamilyKind { TwoPick, RankRotation, RankUnlimited }
private static async Task SetupFamilyAsync(SVSimTestFactory factory, long viewerId, FamilyKind family)
{
switch (family)
{
case FamilyKind.TwoPick:
await SeedCompleteTwoPickRunAsync(factory, viewerId);
break;
case FamilyKind.RankRotation:
await factory.SeedGlobalsAsync();
await factory.SeedDeckAsync(viewerId, Format.Rotation, number: 1);
break;
case FamilyKind.RankUnlimited:
await factory.SeedGlobalsAsync();
await factory.SeedDeckAsync(viewerId, Format.Unlimited, number: 1);
break;
default:
throw new ArgumentOutOfRangeException(nameof(family));
}
}
// Mirrors ArenaTwoPickBattleControllerTests.SeedCompleteTwoPickRunAsync. Duplicated
// rather than promoted because the original is a private static there and only this
// test class needs to share it cross-family today; promote if a third caller surfaces.
private static async Task SeedCompleteTwoPickRunAsync(SVSimTestFactory factory, long viewerId)
{
using var scope = factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
var deck = Enumerable.Range(1, 30).Select(i => 100_011_000L + i).ToList();
db.ViewerArenaTwoPickRuns.Add(new ViewerArenaTwoPickRun
{
ViewerId = viewerId,
EntryId = 1,
ClassId = 1,
LeaderSkinId = 1,
SelectedCardIdsJson = JsonSerializer.Serialize(deck),
IsSelectCompleted = true,
MaxBattleCount = 5,
CandidateClassIdsJson = "[1,2,3]",
PendingPickSetsJson = "[]",
ResultListJson = "[]",
NextCandidateId = 1,
});
await db.SaveChangesAsync();
}
}

View File

@@ -35,41 +35,6 @@ public class MatchingResolverTests
CountryCode: "JP", UserName: $"P{vid}", SleeveId: "0",
EmblemId: "0", DegreeId: "0", FieldId: 0, IsOfficial: 0, BattleType: 11));
[Test]
public async Task When_scriptedOptIn_is_true_registers_Scripted_and_returns_3004()
{
var h = BuildHarness();
var player = Player();
h.Bridge.Setup(b => b.RegisterBattle(player, null, BattleType.Scripted))
.Returns(new PendingMatch("bid-scripted", "node.local/socket.io/"));
var r = await h.Resolver.ResolveAsync("arena_two_pick_battle", player, scriptedOptIn: true, default);
Assert.That(r.MatchingState, Is.EqualTo(3004));
Assert.That(r.BattleId, Is.EqualTo("bid-scripted"));
Assert.That(r.NodeServerUrl, Is.EqualTo("node.local/socket.io/"));
h.Bridge.VerifyAll();
h.PairUp.Verify(p => p.TryPairAsync(It.IsAny<string>(), It.IsAny<BattlePlayer>(), It.IsAny<CancellationToken>()), Times.Never);
}
[Test]
public async Task When_options_SoloDefaultsToScripted_is_true_registers_Scripted_for_any_mode()
{
// Cross-family contract: the process-wide flag overrides pair-up for every mode,
// not just TK2.
var h = BuildHarness();
h.Options.SoloDefaultsToScripted = true;
var player = Player();
h.Bridge.Setup(b => b.RegisterBattle(player, null, BattleType.Scripted))
.Returns(new PendingMatch("bid-rank-scripted", "node.local/socket.io/"));
var r = await h.Resolver.ResolveAsync("rotation_rank_battle", player, scriptedOptIn: false, default);
Assert.That(r.MatchingState, Is.EqualTo(3004));
Assert.That(r.BattleId, Is.EqualTo("bid-rank-scripted"));
h.PairUp.Verify(p => p.TryPairAsync(It.IsAny<string>(), It.IsAny<BattlePlayer>(), It.IsAny<CancellationToken>()), Times.Never);
}
[Test]
public async Task When_neither_flag_set_calls_pairUp_and_parks_returns_3002_with_empty_url()
{
@@ -78,7 +43,7 @@ public class MatchingResolverTests
h.PairUp.Setup(p => p.TryPairAsync("arena_two_pick_battle", player, It.IsAny<CancellationToken>()))
.ReturnsAsync((PairUpResult?)null);
var r = await h.Resolver.ResolveAsync("arena_two_pick_battle", player, scriptedOptIn: false, default);
var r = await h.Resolver.ResolveAsync("arena_two_pick_battle", player, default);
Assert.That(r.MatchingState, Is.EqualTo(3002));
Assert.That(r.BattleId, Is.Null);
@@ -94,7 +59,7 @@ public class MatchingResolverTests
h.PairUp.Setup(p => p.TryPairAsync("rotation_rank_battle", player, It.IsAny<CancellationToken>()))
.ReturnsAsync(new PairUpResult(new PendingMatch("bid-x", "node.local/socket.io/"), IsOwner: true, IsAiFallback: false));
var r = await h.Resolver.ResolveAsync("rotation_rank_battle", player, scriptedOptIn: false, default);
var r = await h.Resolver.ResolveAsync("rotation_rank_battle", player, default);
Assert.That(r.MatchingState, Is.EqualTo(3007));
Assert.That(r.BattleId, Is.EqualTo("bid-x"));
@@ -108,7 +73,7 @@ public class MatchingResolverTests
h.PairUp.Setup(p => p.TryPairAsync("rotation_rank_battle", player, It.IsAny<CancellationToken>()))
.ReturnsAsync(new PairUpResult(new PendingMatch("bid-x", "node.local/socket.io/"), IsOwner: false, IsAiFallback: false));
var r = await h.Resolver.ResolveAsync("rotation_rank_battle", player, scriptedOptIn: false, default);
var r = await h.Resolver.ResolveAsync("rotation_rank_battle", player, default);
Assert.That(r.MatchingState, Is.EqualTo(3004));
}
@@ -122,7 +87,7 @@ public class MatchingResolverTests
h.PairUp.Setup(p => p.TryPairAsync("unlimited_rank_battle", player, It.IsAny<CancellationToken>()))
.ReturnsAsync(new PairUpResult(new PendingMatch("bid-ai", "node.local/socket.io/"), IsOwner: true, IsAiFallback: true));
var r = await h.Resolver.ResolveAsync("unlimited_rank_battle", player, scriptedOptIn: false, default);
var r = await h.Resolver.ResolveAsync("unlimited_rank_battle", player, default);
Assert.That(r.MatchingState, Is.EqualTo(3011));
}