Files
SVSimServer/SVSim.UnitTests/Controllers/PracticeControllerTests.cs
2026-05-23 18:14:42 -04:00

126 lines
5.9 KiB
C#

using System.Net;
using System.Text;
using System.Text.Json;
using SVSim.Database.Enums;
using SVSim.EmulatedEntrypoint.Extensions;
using SVSim.UnitTests.Infrastructure;
namespace SVSim.UnitTests.Controllers;
/// <summary>
/// Coverage for <c>/practice/*</c>. The solo-battle subsystem is mostly stubbed (no XP,
/// no missions, no rewards) but the endpoints must still round-trip successfully or the
/// solo-play UI breaks before reaching the battle screen.
/// </summary>
public class PracticeControllerTests
{
private const string BaseRequestJson =
"""{"viewer_id":"0","steam_id":0,"steam_session_ticket":""}""";
// ToApi() converts internal Format -> wire deck_format int (Format.All -> 0, etc.).
private static string DeckFormatRequestJson(Format f) =>
$$"""{"viewer_id":"0","steam_id":0,"steam_session_ticket":"","deck_format":{{f.ToApi()}}}""";
[Test]
public async Task Info_returns_non_empty_opponent_array()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
using var client = factory.CreateAuthenticatedClient(viewerId);
var response = await client.PostAsync("/practice/info",
new StringContent(BaseRequestJson, Encoding.UTF8, "application/json"));
var body = await response.Content.ReadAsStringAsync();
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK), body);
using var doc = JsonDocument.Parse(body);
Assert.That(doc.RootElement.ValueKind, Is.EqualTo(JsonValueKind.Array),
"/practice/info returns a bare array (no wrapper object) per spec.");
Assert.That(doc.RootElement.GetArrayLength(), Is.GreaterThan(0));
Assert.That(doc.RootElement[0].GetProperty("practice_id").GetInt32(), Is.GreaterThan(0));
}
[Test]
public async Task DeckList_returns_viewer_decks()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
await factory.SeedDeckAsync(viewerId, Format.Rotation, number: 1, name: "Rotation Deck");
await factory.SeedDeckAsync(viewerId, Format.Unlimited, number: 1, name: "Unlimited Deck");
using var client = factory.CreateAuthenticatedClient(viewerId);
var response = await client.PostAsync("/practice/deck_list",
new StringContent(DeckFormatRequestJson(Format.All), Encoding.UTF8, "application/json"));
var body = await response.Content.ReadAsStringAsync();
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK), body);
using var doc = JsonDocument.Parse(body);
var rotation = doc.RootElement.GetProperty("user_deck_rotation");
var unlimited = doc.RootElement.GetProperty("user_deck_unlimited");
Assert.That(rotation.GetArrayLength(), Is.EqualTo(1));
Assert.That(rotation[0].GetProperty("deck_name").GetString(), Is.EqualTo("Rotation Deck"));
Assert.That(unlimited.GetArrayLength(), Is.EqualTo(1));
Assert.That(unlimited[0].GetProperty("deck_name").GetString(), Is.EqualTo("Unlimited Deck"));
}
[Test]
public async Task DeckList_empty_when_viewer_has_none()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
using var client = factory.CreateAuthenticatedClient(viewerId);
var response = await client.PostAsync("/practice/deck_list",
new StringContent(DeckFormatRequestJson(Format.All), Encoding.UTF8, "application/json"));
var body = await response.Content.ReadAsStringAsync();
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK), body);
using var doc = JsonDocument.Parse(body);
Assert.That(doc.RootElement.GetProperty("user_deck_rotation").GetArrayLength(), Is.EqualTo(0));
Assert.That(doc.RootElement.GetProperty("user_deck_unlimited").GetArrayLength(), Is.EqualTo(0));
}
[Test]
public async Task Start_returns_200()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
using var client = factory.CreateAuthenticatedClient(viewerId);
var response = await client.PostAsync("/practice/start",
new StringContent(BaseRequestJson, Encoding.UTF8, "application/json"));
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
}
[Test]
public async Task Finish_accepts_any_recovery_data_returns_zero_xp()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
using var client = factory.CreateAuthenticatedClient(viewerId);
// recoveryData is an opaque JSON blob serialized to string by the client; the server
// is supposed to accept it without validation. Anything goes.
// deck_format:1 = Format.Rotation on the wire. The controller ignores the field today
// (practice is per-format upstream), but sending a coherent wire code keeps the test
// intent clean if Finish ever starts validating it.
var finishJson =
"""{"viewer_id":"0","steam_id":0,"steam_session_ticket":"","deck_no":1,"is_win":1,"evolve_count":2,"total_turn":5,"enemy_class_id":3,"difficulty":1,"deck_format":1,"class_id":1,"recovery_data":"{\"opaque\":\"blob\"}"}""";
var response = await client.PostAsync("/practice/finish",
new StringContent(finishJson, Encoding.UTF8, "application/json"));
var body = await response.Content.ReadAsStringAsync();
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK), body);
using var doc = JsonDocument.Parse(body);
Assert.That(doc.RootElement.GetProperty("get_class_experience").GetInt32(), Is.EqualTo(0));
Assert.That(doc.RootElement.GetProperty("class_experience").GetInt32(), Is.EqualTo(0));
Assert.That(doc.RootElement.GetProperty("reward_list").GetArrayLength(), Is.EqualTo(0));
}
}