Two client-crash bugs in the do_matching response when no partner is waiting: 1. matching_state was 3001 (RC_BATTLE_MATCHING_ILLEGAL); the client's Matching.OnFinishedDoMatching switch maps that to an error dialog, not a retry. The retry state is 3002 (RC_BATTLE_MATCHING_RETRY). 2. node_server_url was omitted entirely. The client's DoMatchingBase.SettingDoMatchingData reads it via data["node_server_url"].ToString() with no Keys.Contains guard, so absence throws KeyNotFoundException out of NetworkManager.Connect before the matching_state switch is even reached. Prod RETRY captures send "" while waiting and the real URL only on SUCCEEDED; match that. battle_id stays absent; its accessor IS guarded. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
176 lines
8.5 KiB
C#
176 lines
8.5 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_3002_RETRY_with_no_BattleId_but_empty_NodeServerUrl()
|
|
{
|
|
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;
|
|
|
|
// 3002 = RC_BATTLE_MATCHING_RETRY (client polls again). 3001 is ILLEGAL and
|
|
// pops an error dialog on the client.
|
|
Assert.That(root.GetProperty("matching_state").GetInt32(), Is.EqualTo(3002));
|
|
// battle_id must be ABSENT from the JSON; the client's accessor IS guarded with
|
|
// Keys.Contains so absence is the safe shape (matches prod RETRY captures).
|
|
Assert.That(root.TryGetProperty("battle_id", out _), Is.False,
|
|
"battle_id must be absent from the wire when matching_state==3002 RETRY.");
|
|
// node_server_url MUST be present (empty string while waiting, the real URL on
|
|
// SUCCEEDED). Client's DoMatchingBase.SettingDoMatchingData calls .ToString() on
|
|
// it without a Keys.Contains guard, so absence throws KeyNotFoundException.
|
|
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_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(3002),
|
|
"A's first poll parks (3002 = RETRY).");
|
|
|
|
// 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();
|
|
}
|
|
}
|