Files
SVSimServer/SVSim.UnitTests/Controllers/ArenaTwoPickBattleControllerTests.cs
gamer147 225c20daeb feat(arena-tk2): PvP pair-up trigger via /do_matching, ?scripted=1 opt-in
Solo pollers park (3001 RETRY); two concurrent pollers pair and both
receive 3004 + same BattleId. Cache hits on the first arriver's next
poll. ?scripted=1 retains today's solo Scripted path for dev work.
Response DTO's BattleId/NodeServerUrl become nullable so 3001 omits
them on the wire (WhenWritingNull policy drops them).

ASP.NET's default bool binder rejects "1" as a value, so the scripted
opt-in is bound as string? and parsed permissively (accepts "1" and
"true"/"True"/etc.) rather than relying on built-in bool binding.
2026-06-01 22:14:04 -04:00

170 lines
8.0 KiB
C#

using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using SVSim.Database;
using SVSim.Database.Models;
using SVSim.UnitTests.Infrastructure;
namespace SVSim.UnitTests.Controllers;
public class ArenaTwoPickBattleControllerTests
{
[Test]
public async Task DoMatching_AuthenticatedViewer_Returns3004WithBattleIdAndNodeUrl()
{
using var factory = new SVSimTestFactory();
var viewerId = await factory.SeedViewerAsync();
await SeedCompleteTwoPickRunAsync(factory, viewerId);
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));
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));
var battleId = root.GetProperty("battle_id").GetString();
Assert.That(battleId, Is.Not.Null.And.Not.Empty);
var nodeUrl = root.GetProperty("node_server_url").GetString();
Assert.That(nodeUrl, Does.Contain("/socket.io/"));
Assert.That(nodeUrl, Does.Not.StartWith("ws://"));
Assert.That(nodeUrl, Does.Not.StartWith("http://"));
Assert.That(root.GetProperty("card_master_id").GetInt32(), Is.EqualTo(1));
}
[Test]
public async Task DoMatching_solo_poller_returns_3001_RETRY_with_no_BattleId()
{
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", 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(3001));
// BattleId should be ABSENT from the JSON (WhenWritingNull) — TryGetProperty
// returns false when the key isn't present.
Assert.That(root.TryGetProperty("battle_id", out _), Is.False,
"battle_id must be absent from the wire when matching_state==3001 RETRY.");
}
[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_concurrent_pollers_both_return_3004_with_same_BattleId()
{
using var factory = new SVSimTestFactory();
var vidA = await factory.SeedViewerAsync(steamId: 76_561_198_000_000_011UL);
var vidB = await factory.SeedViewerAsync(steamId: 76_561_198_000_000_012UL);
await SeedCompleteTwoPickRunAsync(factory, vidA);
await SeedCompleteTwoPickRunAsync(factory, vidB);
using var clientA = factory.CreateAuthenticatedClient(vidA);
using var clientB = factory.CreateAuthenticatedClient(vidB);
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 = "",
};
// A polls first (parks).
var respA1 = await clientA.PostAsync("/arena_two_pick_battle/do_matching", JsonContent.Create(req));
using var docA1 = JsonDocument.Parse(await respA1.Content.ReadAsStringAsync());
Assert.That(docA1.RootElement.GetProperty("matching_state").GetInt32(), Is.EqualTo(3001),
"A's first poll parks.");
// B polls (pairs).
var respB = await clientB.PostAsync("/arena_two_pick_battle/do_matching", JsonContent.Create(req));
using var docB = JsonDocument.Parse(await respB.Content.ReadAsStringAsync());
Assert.That(docB.RootElement.GetProperty("matching_state").GetInt32(), Is.EqualTo(3004),
"B's poll pairs with A.");
var bBattleId = docB.RootElement.GetProperty("battle_id").GetString();
Assert.That(bBattleId, Is.Not.Null.And.Not.Empty);
// A polls again, picks up the cached result.
var respA2 = await clientA.PostAsync("/arena_two_pick_battle/do_matching", JsonContent.Create(req));
using var docA2 = JsonDocument.Parse(await respA2.Content.ReadAsStringAsync());
Assert.That(docA2.RootElement.GetProperty("matching_state").GetInt32(), Is.EqualTo(3004),
"A's second poll picks up the cached match.");
Assert.That(docA2.RootElement.GetProperty("battle_id").GetString(), Is.EqualTo(bBattleId));
}
[Test]
public async Task DoMatching_NoActiveRun_Returns400WithErrorCode()
{
using var factory = new SVSimTestFactory();
var viewerId = await factory.SeedViewerAsync();
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", JsonContent.Create(req));
Assert.That(resp.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
var body = await resp.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(body);
Assert.That(doc.RootElement.GetProperty("error_code").GetString(),
Is.EqualTo("arena_two_pick_no_active_run"));
}
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();
}
}