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:
gamer147
2026-06-02 01:09:42 -04:00
parent 3866c93065
commit b65cf81977
5 changed files with 281 additions and 30 deletions

View File

@@ -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",