Files
SVSimServer/SVSim.UnitTests/Matching/MatchingResolverTests.cs
gamer147 672a89ed46 refactor(matching): IMatchingResolver shared by every do_matching family
SoloDefaultsToScripted was only consulted by ArenaTwoPickBattleController;
RankBattleController did its own inline pair-up + state-code mapping and
ignored the flag entirely. Result: turning on the flag globally only
short-circuited TK2 polls, while rank-battle polls still parked for the
PvpFirstThenAiFallback threshold (15s) before resolving — surfaced today
when the user set the flag and saw rank-battle still queue, then bot-
battle via the client-side AI (not the server-side Scripted lifecycle we
need to test WS traffic against).

New IMatchingResolver owns the cross-cutting decisions:
- honor scriptedOptIn (per-request) OR options.SoloDefaultsToScripted
  (process-wide) — bypass pair-up, register Scripted, return 3004
- otherwise call IMatchingPairUpService.TryPairAsync and translate the
  PairUpResult to the 3002/3004/3007/3011 vocabulary

Family controllers shed the duplicated logic:
- ArenaTwoPickBattleController: ~50 LOC → ~25; preserves ?scripted=1
  query opt-in (parsed permissively for "1"/"true") and the
  ArenaTwoPickException catch
- RankBattleController: ~30 LOC → ~12; preserves the 3001 mapping for
  InvalidOperationException (no deck for format) and card_master_id
  emission

DoMatchingContractTests is the durable enforcement: parametrized over
TK2 + rotation + unlimited rank, asserts SoloDefaultsToScripted=true
makes every family's first poll skip 3002 and return SUCCEEDED with a
battle_id + node_server_url. Adding a fourth family that forgets to
route through IMatchingResolver fails this test — that's the point.

MatchingResolverTests covers the six resolver paths in isolation with
mocks; per-test Harness locals (not fixture-level fields) because the
assembly is [Parallelizable(ParallelScope.All)] and shared mocks race.

957 tests passing (was 948; +9: 6 resolver + 3 contract parametrizations).
No regressions in the existing TK2 / rank-battle controller suites.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-02 15:18:48 -04:00

130 lines
5.6 KiB
C#

using Moq;
using NUnit.Framework;
using SVSim.BattleNode.Bridge;
using SVSim.BattleNode.Sessions;
using SVSim.EmulatedEntrypoint.Matching;
namespace SVSim.UnitTests.Matching;
/// <summary>
/// Per-test locals (no fixture-level fields) because the assembly runs with
/// <c>[Parallelizable(ParallelScope.All)]</c> — shared <c>_resolver</c>/<c>_bridge</c>
/// fields would race across concurrent tests in this fixture.
/// </summary>
[TestFixture]
public class MatchingResolverTests
{
private sealed record Harness(
Mock<IMatchingBridge> Bridge,
Mock<IMatchingPairUpService> PairUp,
BattleNodeOptions Options,
MatchingResolver Resolver);
private static Harness BuildHarness()
{
var bridge = new Mock<IMatchingBridge>(MockBehavior.Strict);
var pairUp = new Mock<IMatchingPairUpService>(MockBehavior.Strict);
var options = new BattleNodeOptions { NodeServerUrl = "localhost:5148/socket.io/" };
return new Harness(bridge, pairUp, options, new MatchingResolver(bridge.Object, pairUp.Object, options));
}
private static BattlePlayer Player(long vid = 1) =>
new(vid, new MatchContext(
SelfDeckCardIds: Array.Empty<long>(), ClassId: "0", CharaId: "0",
CardMasterName: "card_master_node_10015",
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()
{
var h = BuildHarness();
var player = Player();
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);
Assert.That(r.MatchingState, Is.EqualTo(3002));
Assert.That(r.BattleId, Is.Null);
Assert.That(r.NodeServerUrl, Is.EqualTo(""), "Empty string (not null) — client unguarded-.ToString()s it.");
h.Bridge.Verify(b => b.RegisterBattle(It.IsAny<BattlePlayer>(), It.IsAny<BattlePlayer?>(), It.IsAny<BattleType>()), Times.Never);
}
[Test]
public async Task Pair_owner_role_returns_3007()
{
var h = BuildHarness();
var player = Player();
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);
Assert.That(r.MatchingState, Is.EqualTo(3007));
Assert.That(r.BattleId, Is.EqualTo("bid-x"));
}
[Test]
public async Task Pair_joiner_role_returns_3004()
{
var h = BuildHarness();
var player = Player();
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);
Assert.That(r.MatchingState, Is.EqualTo(3004));
}
[Test]
public async Task AI_fallback_returns_3011_regardless_of_owner_flag()
{
// IsAiFallback wins the switch even if IsOwner is also true (the resolver's first arm).
var h = BuildHarness();
var player = Player();
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);
Assert.That(r.MatchingState, Is.EqualTo(3011));
}
}