The translation middleware decrypts + msgpack-decodes the request body into the action's first-parameter type, then re-serializes that DTO to JSON for the auth handler to read. Phase 3's DoMatchingRequestDto and RankBattleFinishRequestDto didn't inherit BaseRequest, so viewer_id / steam_id / steam_session_ticket were dropped during the msgpack → DTO → JSON pivot — the auth handler then saw a body with no auth fields and 401'd every request. Fixed by making both DTOs extend BaseRequest, mirroring the Phase 2 TK2 DoMatchingRequest pattern. Also added [FromBody] BaseRequest parameters to the previously body-less actions (AiStart × 2, ForceFinish, AddClientLog, GetLatestMasterPoint). The translation middleware explicitly requires at least one parameter to bind the decrypted msgpack body (see L130-136 of the middleware); without it the request would throw InvalidOperationException at runtime. Tests updated to post viewer_id / steam_id / steam_session_ticket placeholder values in the request body, matching the existing TK2 test pattern. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
199 lines
9.1 KiB
C#
199 lines
9.1 KiB
C#
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using NUnit.Framework;
|
|
using SVSim.Database.Enums;
|
|
using SVSim.UnitTests.Infrastructure;
|
|
|
|
namespace SVSim.UnitTests.Controllers;
|
|
|
|
[TestFixture]
|
|
public class RankBattleControllerTests
|
|
{
|
|
// BaseRequest fields (viewer_id / steam_id / steam_session_ticket) are required by the
|
|
// request DTOs — the ApiController's auto-validation rejects bodies missing them. We
|
|
// post placeholder values here; the TestAuthHandler injects the real viewer-id via the
|
|
// X-Test-Viewer-Id header set by CreateAuthenticatedClient, so these body values are
|
|
// ignored by auth.
|
|
private static readonly object DoMatchingBody = new
|
|
{
|
|
deck_no = 1,
|
|
need_init = 1,
|
|
log = 0,
|
|
viewer_id = "0",
|
|
steam_id = 0,
|
|
steam_session_ticket = "",
|
|
};
|
|
|
|
private static object FinishBody(int battleResult, int classId = 3) => new
|
|
{
|
|
battle_result = battleResult,
|
|
is_retire = 0,
|
|
recovery_data = "{}",
|
|
class_id = classId,
|
|
total_turn = 5,
|
|
viewer_id = "0",
|
|
steam_id = 0,
|
|
steam_session_ticket = "",
|
|
};
|
|
|
|
private static readonly object EmptyAuthedBody = new
|
|
{
|
|
viewer_id = "0",
|
|
steam_id = 0,
|
|
steam_session_ticket = "",
|
|
};
|
|
|
|
[Test]
|
|
public async Task DoMatching_rotation_first_poll_returns_3002_RETRY_with_empty_node_server_url()
|
|
{
|
|
await using var factory = new SVSimTestFactory();
|
|
var viewerId = await factory.SeedViewerAsync();
|
|
await factory.SeedGlobalsAsync();
|
|
await factory.SeedDeckAsync(viewerId, Format.Rotation, 1);
|
|
var client = factory.CreateAuthenticatedClient(viewerId);
|
|
|
|
var resp = await client.PostAsJsonAsync("/rotation_rank_battle/do_matching", DoMatchingBody);
|
|
|
|
Assert.That(resp.IsSuccessStatusCode, Is.True, $"Expected 2xx, got {resp.StatusCode}");
|
|
var raw = await resp.Content.ReadAsStringAsync();
|
|
using var doc = JsonDocument.Parse(raw);
|
|
var data = doc.RootElement;
|
|
Assert.That(data.GetProperty("matching_state").GetInt32(), Is.EqualTo(3002));
|
|
Assert.That(data.GetProperty("node_server_url").GetString(), Is.EqualTo(""),
|
|
"Empty string, not absent — Phase 2 fix pattern.");
|
|
}
|
|
|
|
[Test]
|
|
public async Task DoMatching_rotation_two_viewers_pair_PvP()
|
|
{
|
|
await using var factory = new SVSimTestFactory();
|
|
var v1 = await factory.SeedViewerAsync(steamId: 76_561_198_000_000_001UL, displayName: "Alice");
|
|
var v2 = await factory.SeedViewerAsync(steamId: 76_561_198_000_000_002UL, displayName: "Bob");
|
|
await factory.SeedGlobalsAsync();
|
|
await factory.SeedDeckAsync(v1, Format.Rotation, 1);
|
|
await factory.SeedDeckAsync(v2, Format.Rotation, 1);
|
|
|
|
// Alice polls first → parks.
|
|
var c1 = factory.CreateAuthenticatedClient(v1);
|
|
var r1 = await c1.PostAsJsonAsync("/rotation_rank_battle/do_matching", DoMatchingBody);
|
|
var j1 = JsonDocument.Parse(await r1.Content.ReadAsStringAsync()).RootElement;
|
|
Assert.That(j1.GetProperty("matching_state").GetInt32(), Is.EqualTo(3002));
|
|
|
|
// Bob polls — pairs, returns joiner (3004).
|
|
var c2 = factory.CreateAuthenticatedClient(v2);
|
|
var r2 = await c2.PostAsJsonAsync("/rotation_rank_battle/do_matching", DoMatchingBody);
|
|
var j2 = JsonDocument.Parse(await r2.Content.ReadAsStringAsync()).RootElement;
|
|
Assert.That(j2.GetProperty("matching_state").GetInt32(), Is.EqualTo(3004), "Joiner = 3004.");
|
|
Assert.That(j2.GetProperty("battle_id").GetString(), Is.Not.Null.And.Not.Empty);
|
|
Assert.That(j2.GetProperty("node_server_url").GetString(), Is.Not.Empty);
|
|
|
|
// Alice polls again — gets cached match, owner role (3007).
|
|
var r3 = await c1.PostAsJsonAsync("/rotation_rank_battle/do_matching", DoMatchingBody);
|
|
var j3 = JsonDocument.Parse(await r3.Content.ReadAsStringAsync()).RootElement;
|
|
Assert.That(j3.GetProperty("matching_state").GetInt32(), Is.EqualTo(3007), "Owner = 3007.");
|
|
Assert.That(j3.GetProperty("battle_id").GetString(), Is.EqualTo(j2.GetProperty("battle_id").GetString()));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AiStart_rotation_returns_ai_id_plus_self_oppo_info_camelCase_keys()
|
|
{
|
|
await using var factory = new SVSimTestFactory();
|
|
var viewerId = await factory.SeedViewerAsync(displayName: "TestViewer");
|
|
await factory.SeedGlobalsAsync();
|
|
await factory.SeedDeckAsync(viewerId, Format.Rotation, 1);
|
|
var client = factory.CreateAuthenticatedClient(viewerId);
|
|
|
|
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", EmptyAuthedBody);
|
|
var raw = await resp.Content.ReadAsStringAsync();
|
|
|
|
using var doc = JsonDocument.Parse(raw);
|
|
var data = doc.RootElement;
|
|
Assert.That(data.GetProperty("ai_id").GetInt32(), Is.InRange(4001, 4008));
|
|
Assert.That(data.GetProperty("turnState").GetInt32(), Is.EqualTo(0));
|
|
|
|
// Literal camelCase wire-key checks — these MUST be present verbatim
|
|
// (client uses JsonData.Keys.Contains).
|
|
Assert.That(raw, Does.Contain("\"userName\""), "Wire key must be camelCase, not snake_case.");
|
|
Assert.That(raw, Does.Contain("\"sleeveId\""));
|
|
Assert.That(raw, Does.Contain("\"emblemId\""));
|
|
Assert.That(raw, Does.Contain("\"degreeId\""));
|
|
Assert.That(raw, Does.Contain("\"fieldId\""));
|
|
Assert.That(raw, Does.Contain("\"isOfficial\""));
|
|
Assert.That(raw, Does.Contain("\"classId\""));
|
|
Assert.That(raw, Does.Contain("\"charaId\""));
|
|
Assert.That(raw, Does.Contain("\"isMasterRank\""));
|
|
Assert.That(raw, Does.Contain("\"battlePoint\""));
|
|
Assert.That(raw, Does.Contain("\"masterPoint\""));
|
|
// self_info / oppo_info / country_code stay snake_case (the outliers per ai-start.md).
|
|
Assert.That(raw, Does.Contain("\"self_info\""));
|
|
Assert.That(raw, Does.Contain("\"oppo_info\""));
|
|
Assert.That(raw, Does.Contain("\"country_code\""));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AiStart_self_info_reflects_caller_user_name()
|
|
{
|
|
await using var factory = new SVSimTestFactory();
|
|
var viewerId = await factory.SeedViewerAsync(displayName: "Alice");
|
|
await factory.SeedGlobalsAsync();
|
|
await factory.SeedDeckAsync(viewerId, Format.Rotation, 1);
|
|
var client = factory.CreateAuthenticatedClient(viewerId);
|
|
|
|
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", EmptyAuthedBody);
|
|
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
|
|
var selfInfo = doc.RootElement.GetProperty("self_info");
|
|
|
|
Assert.That(selfInfo.GetProperty("userName").GetString(), Is.EqualTo("Alice"));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AiStart_oppo_info_reflects_roster_pick()
|
|
{
|
|
await using var factory = new SVSimTestFactory();
|
|
var viewerId = await factory.SeedViewerAsync(displayName: "PlayerA");
|
|
await factory.SeedGlobalsAsync();
|
|
await factory.SeedDeckAsync(viewerId, Format.Rotation, 1);
|
|
var client = factory.CreateAuthenticatedClient(viewerId);
|
|
|
|
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", EmptyAuthedBody);
|
|
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
|
|
var oppoInfo = doc.RootElement.GetProperty("oppo_info");
|
|
|
|
// BotRoster's stub names contain "AI" — verify the roster was consulted.
|
|
Assert.That(oppoInfo.GetProperty("userName").GetString(), Does.Contain("AI"));
|
|
Assert.That(oppoInfo.GetProperty("classId").GetInt32(), Is.InRange(1, 8));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Finish_emits_stubbed_zeros_with_battle_result_echo()
|
|
{
|
|
await using var factory = new SVSimTestFactory();
|
|
var viewerId = await factory.SeedViewerAsync();
|
|
var client = factory.CreateAuthenticatedClient(viewerId);
|
|
|
|
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/finish", FinishBody(battleResult: 1));
|
|
|
|
Assert.That(resp.IsSuccessStatusCode, Is.True);
|
|
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
|
|
var data = doc.RootElement;
|
|
Assert.That(data.GetProperty("battle_result").GetInt32(), Is.EqualTo(1));
|
|
Assert.That(data.GetProperty("rank").GetInt32(), Is.EqualTo(0));
|
|
Assert.That(data.GetProperty("after_battle_point").GetInt32(), Is.EqualTo(0));
|
|
Assert.That(data.GetProperty("class_level").GetInt32(), Is.EqualTo(1));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Finish_with_consistency_result_echoes_2()
|
|
{
|
|
await using var factory = new SVSimTestFactory();
|
|
var viewerId = await factory.SeedViewerAsync();
|
|
var client = factory.CreateAuthenticatedClient(viewerId);
|
|
|
|
var resp = await client.PostAsJsonAsync("/rotation_rank_battle/finish", FinishBody(battleResult: 2));
|
|
|
|
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
|
|
Assert.That(doc.RootElement.GetProperty("battle_result").GetInt32(), Is.EqualTo(2));
|
|
}
|
|
}
|