feat(matching): per-mode policy + AI-fallback branch in InProcessPairUp
InProcessPairUp now consults ModePolicyRegistry per call and reads the fallback threshold from MatchingConfig via IServiceScopeFactory (singleton service consuming a scoped IGameConfigService). New behavior for PvpFirstThenAiFallback modes: when the calling viewer IS the slot's waiter and Now - WaitingSince >= threshold, the waiter unparks and the bridge resolves a Bot match. PvpOnly modes (TK2) keep parking forever (modulo a 5-minute stale-waiter eviction backstop). TimeProvider is injected so tests can drive time forward with FakeTimeProvider — 7 new tests cover the four key transitions (stay-parked / pair-pvp / fall-back / stale-evict) plus per-mode isolation. Fixture uses [FixtureLifeCycle(InstancePerTestCase)] because the assembly is Parallelizable(ParallelScope.All). Program.cs registers ModePolicyRegistry with three rows: TK2 PvpOnly, rotation/unlimited rank PvpFirstThenAiFallback. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
156
SVSim.UnitTests/Matching/InProcessPairUpRankFallbackTests.cs
Normal file
156
SVSim.UnitTests/Matching/InProcessPairUpRankFallbackTests.cs
Normal file
@@ -0,0 +1,156 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Time.Testing;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using SVSim.BattleNode.Bridge;
|
||||
using SVSim.BattleNode.Sessions;
|
||||
using SVSim.Database.Models.Config;
|
||||
using SVSim.Database.Services;
|
||||
using SVSim.EmulatedEntrypoint.Matching;
|
||||
|
||||
namespace SVSim.UnitTests.Matching;
|
||||
|
||||
[TestFixture]
|
||||
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
|
||||
public class InProcessPairUpRankFallbackTests
|
||||
{
|
||||
private FakeTimeProvider _clock = null!;
|
||||
private Mock<IMatchingBridge> _bridge = null!;
|
||||
private Mock<IGameConfigService> _config = null!;
|
||||
private ModePolicyRegistry _policies = null!;
|
||||
private InProcessPairUp _pairUp = null!;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_clock = new FakeTimeProvider(startDateTime: new DateTimeOffset(2026, 6, 2, 0, 0, 0, TimeSpan.Zero));
|
||||
_bridge = new Mock<IMatchingBridge>();
|
||||
_config = new Mock<IGameConfigService>();
|
||||
_config.Setup(c => c.Get<MatchingConfig>())
|
||||
.Returns(new MatchingConfig { RankBattleAiFallbackThresholdSeconds = 15 });
|
||||
_policies = new ModePolicyRegistry(new[]
|
||||
{
|
||||
new ModePolicy("rotation_rank_battle", PolicyKind.PvpFirstThenAiFallback),
|
||||
new ModePolicy("unlimited_rank_battle", PolicyKind.PvpFirstThenAiFallback),
|
||||
new ModePolicy("arena_two_pick_battle", PolicyKind.PvpOnly),
|
||||
});
|
||||
|
||||
// Build a tiny service provider exposing the mock IGameConfigService as scoped,
|
||||
// and inject IServiceScopeFactory into InProcessPairUp the same way prod does.
|
||||
var services = new ServiceCollection();
|
||||
services.AddScoped<IGameConfigService>(_ => _config.Object);
|
||||
var sp = services.BuildServiceProvider();
|
||||
_pairUp = new InProcessPairUp(_bridge.Object, _policies, sp.GetRequiredService<IServiceScopeFactory>(), _clock);
|
||||
}
|
||||
|
||||
private static BattlePlayer Player(long id) =>
|
||||
new(id, new MatchContext(
|
||||
SelfDeckCardIds: Array.Empty<long>(), ClassId: "0", CharaId: "0",
|
||||
CardMasterName: "card_master_node_10015",
|
||||
CountryCode: "JP", UserName: $"P{id}", SleeveId: "0",
|
||||
EmblemId: "0", DegreeId: "0", FieldId: 0, IsOfficial: 0, BattleType: 11));
|
||||
|
||||
[Test]
|
||||
public async Task TK2_policy_is_PvpOnly_no_fallback_regression()
|
||||
{
|
||||
var p = Player(1);
|
||||
var first = await _pairUp.TryPairAsync("arena_two_pick_battle", p, default);
|
||||
Assert.That(first, Is.Null, "First poll should park.");
|
||||
|
||||
_clock.Advance(TimeSpan.FromSeconds(20)); // Past the rotation threshold.
|
||||
var second = await _pairUp.TryPairAsync("arena_two_pick_battle", p, default);
|
||||
|
||||
Assert.That(second, Is.Null, "TK2 must not fall back to AI even past threshold.");
|
||||
_bridge.Verify(b => b.RegisterBattle(It.IsAny<BattlePlayer>(), It.IsAny<BattlePlayer?>(), BattleType.Bot), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Rotation_first_poll_parks_no_fallback()
|
||||
{
|
||||
var p = Player(1);
|
||||
var result = await _pairUp.TryPairAsync("rotation_rank_battle", p, default);
|
||||
Assert.That(result, Is.Null, "First poll should park even on fallback-eligible modes.");
|
||||
_bridge.Verify(b => b.RegisterBattle(It.IsAny<BattlePlayer>(), It.IsAny<BattlePlayer?>(), It.IsAny<BattleType>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Rotation_second_poll_under_threshold_stays_parked()
|
||||
{
|
||||
var p = Player(1);
|
||||
await _pairUp.TryPairAsync("rotation_rank_battle", p, default);
|
||||
_clock.Advance(TimeSpan.FromSeconds(5));
|
||||
|
||||
var result = await _pairUp.TryPairAsync("rotation_rank_battle", p, default);
|
||||
|
||||
Assert.That(result, Is.Null, "Sub-threshold polls should keep the viewer parked.");
|
||||
_bridge.Verify(b => b.RegisterBattle(It.IsAny<BattlePlayer>(), It.IsAny<BattlePlayer?>(), It.IsAny<BattleType>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Rotation_poll_past_threshold_falls_back_to_Bot()
|
||||
{
|
||||
var p = Player(1);
|
||||
var bid = "bot-bid-1";
|
||||
var url = "http://node.local/socket.io/";
|
||||
_bridge.Setup(b => b.RegisterBattle(p, null, BattleType.Bot))
|
||||
.Returns(new PendingMatch(bid, url));
|
||||
|
||||
await _pairUp.TryPairAsync("rotation_rank_battle", p, default);
|
||||
_clock.Advance(TimeSpan.FromSeconds(16));
|
||||
|
||||
var result = await _pairUp.TryPairAsync("rotation_rank_battle", p, default);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result!.IsAiFallback, Is.True);
|
||||
Assert.That(result.IsOwner, Is.True);
|
||||
Assert.That(result.Match.BattleId, Is.EqualTo(bid));
|
||||
_bridge.Verify(b => b.RegisterBattle(p, null, BattleType.Bot), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Rotation_partner_arrives_before_threshold_pairs_PvP()
|
||||
{
|
||||
var pA = Player(1);
|
||||
var pB = Player(2);
|
||||
_bridge.Setup(b => b.RegisterBattle(pA, pB, BattleType.Pvp))
|
||||
.Returns(new PendingMatch("pvp-bid", "http://node.local/socket.io/"));
|
||||
|
||||
await _pairUp.TryPairAsync("rotation_rank_battle", pA, default);
|
||||
_clock.Advance(TimeSpan.FromSeconds(10)); // Sub-threshold.
|
||||
var joinerResult = await _pairUp.TryPairAsync("rotation_rank_battle", pB, default);
|
||||
|
||||
Assert.That(joinerResult, Is.Not.Null);
|
||||
Assert.That(joinerResult!.IsAiFallback, Is.False, "Pair-up wins over AI fallback when partner arrives in window.");
|
||||
Assert.That(joinerResult.IsOwner, Is.False, "Joiner role.");
|
||||
_bridge.Verify(b => b.RegisterBattle(pA, pB, BattleType.Pvp), Times.Once);
|
||||
_bridge.Verify(b => b.RegisterBattle(It.IsAny<BattlePlayer>(), null, BattleType.Bot), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Rotation_stale_waiter_evicted_on_next_arriver()
|
||||
{
|
||||
var pA = Player(1);
|
||||
var pB = Player(2);
|
||||
_bridge.Setup(b => b.RegisterBattle(It.IsAny<BattlePlayer>(), null, BattleType.Bot))
|
||||
.Returns<BattlePlayer, BattlePlayer?, BattleType>((p, _, _) => new PendingMatch("bot-" + p.ViewerId, "http://node.local/socket.io/"));
|
||||
|
||||
await _pairUp.TryPairAsync("rotation_rank_battle", pA, default);
|
||||
_clock.Advance(TimeSpan.FromMinutes(6)); // Past the 5-minute stale eviction.
|
||||
|
||||
var resultB = await _pairUp.TryPairAsync("rotation_rank_battle", pB, default);
|
||||
|
||||
// B sees an empty slot (A evicted as stale) and becomes the new waiter.
|
||||
Assert.That(resultB, Is.Null);
|
||||
_bridge.Verify(b => b.RegisterBattle(pA, pB, BattleType.Pvp), Times.Never, "Stale A should not have paired with B.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Unlimited_independent_from_Rotation()
|
||||
{
|
||||
var p = Player(1);
|
||||
await _pairUp.TryPairAsync("rotation_rank_battle", p, default);
|
||||
var unlimitedResult = await _pairUp.TryPairAsync("unlimited_rank_battle", p, default);
|
||||
|
||||
Assert.That(unlimitedResult, Is.Null, "Per-mode slots must be independent.");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Time.Testing;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using SVSim.BattleNode.Bridge;
|
||||
using SVSim.BattleNode.Sessions;
|
||||
using SVSim.Database.Models.Config;
|
||||
using SVSim.Database.Services;
|
||||
using SVSim.EmulatedEntrypoint.Matching;
|
||||
|
||||
namespace SVSim.UnitTests.Matching;
|
||||
@@ -11,8 +16,7 @@ public class InProcessPairUpTests
|
||||
[Test]
|
||||
public async Task TryPairAsync_on_empty_slot_returns_null_and_parks()
|
||||
{
|
||||
var bridge = new MatchingBridge(new InMemoryBattleSessionStore(), new BattleNodeOptions());
|
||||
var svc = new InProcessPairUp(bridge);
|
||||
var svc = BuildSvc();
|
||||
|
||||
var match = await svc.TryPairAsync("tk2", new BattlePlayer(1, Ctx()), CancellationToken.None);
|
||||
|
||||
@@ -22,8 +26,7 @@ public class InProcessPairUpTests
|
||||
[Test]
|
||||
public async Task TryPairAsync_with_waiting_partner_pairs_returns_match_as_joiner()
|
||||
{
|
||||
var bridge = new MatchingBridge(new InMemoryBattleSessionStore(), new BattleNodeOptions());
|
||||
var svc = new InProcessPairUp(bridge);
|
||||
var svc = BuildSvc();
|
||||
|
||||
await svc.TryPairAsync("tk2", new BattlePlayer(1, Ctx()), CancellationToken.None);
|
||||
var result = await svc.TryPairAsync("tk2", new BattlePlayer(2, Ctx()), CancellationToken.None);
|
||||
@@ -32,13 +35,14 @@ public class InProcessPairUpTests
|
||||
Assert.That(result!.Match.BattleId, Is.Not.Empty);
|
||||
Assert.That(result.IsOwner, Is.False,
|
||||
"The second arriver (who triggered the pair) is the joiner — wire matching_state 3004.");
|
||||
Assert.That(result.IsAiFallback, Is.False,
|
||||
"TK2 is PvpOnly — never falls back to AI.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task First_arrivers_next_poll_returns_cached_match_as_owner_then_evicts()
|
||||
{
|
||||
var bridge = new MatchingBridge(new InMemoryBattleSessionStore(), new BattleNodeOptions());
|
||||
var svc = new InProcessPairUp(bridge);
|
||||
var svc = BuildSvc();
|
||||
|
||||
await svc.TryPairAsync("tk2", new BattlePlayer(1, Ctx()), CancellationToken.None); // park
|
||||
var secondPaired = await svc.TryPairAsync("tk2", new BattlePlayer(2, Ctx()), CancellationToken.None); // pair
|
||||
@@ -57,8 +61,7 @@ public class InProcessPairUpTests
|
||||
[Test]
|
||||
public async Task Different_modes_do_not_pair_across_slots()
|
||||
{
|
||||
var bridge = new MatchingBridge(new InMemoryBattleSessionStore(), new BattleNodeOptions());
|
||||
var svc = new InProcessPairUp(bridge);
|
||||
var svc = BuildSvc();
|
||||
|
||||
await svc.TryPairAsync("tk2", new BattlePlayer(1, Ctx()), CancellationToken.None);
|
||||
var rankMatch = await svc.TryPairAsync("rank_rotation", new BattlePlayer(2, Ctx()), CancellationToken.None);
|
||||
@@ -66,6 +69,26 @@ public class InProcessPairUpTests
|
||||
Assert.That(rankMatch, Is.Null, "Different mode shouldn't pair with tk2's waiting viewer.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds an InProcessPairUp with a real MatchingBridge (so BattleIds are real)
|
||||
/// + a fake clock, default-threshold MatchingConfig, and an empty policy registry
|
||||
/// (so unknown modes default to PvpOnly — preserving Phase 2 behaviour for
|
||||
/// these legacy tests).
|
||||
/// </summary>
|
||||
private static InProcessPairUp BuildSvc()
|
||||
{
|
||||
var bridge = new MatchingBridge(new InMemoryBattleSessionStore(), new BattleNodeOptions());
|
||||
var clock = new FakeTimeProvider();
|
||||
var config = new Mock<IGameConfigService>();
|
||||
config.Setup(c => c.Get<MatchingConfig>()).Returns(new MatchingConfig());
|
||||
var policies = new ModePolicyRegistry(Array.Empty<ModePolicy>());
|
||||
|
||||
var services = new ServiceCollection();
|
||||
services.AddScoped<IGameConfigService>(_ => config.Object);
|
||||
var sp = services.BuildServiceProvider();
|
||||
return new InProcessPairUp(bridge, policies, sp.GetRequiredService<IServiceScopeFactory>(), clock);
|
||||
}
|
||||
|
||||
private static MatchContext Ctx() => new(
|
||||
SelfDeckCardIds: Enumerable.Range(1, 30).Select(_ => 100_011_010L).ToList(),
|
||||
ClassId: "1", CharaId: "1", CardMasterName: "card_master_node_10015",
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
|
||||
|
||||
Reference in New Issue
Block a user