using System.Net.Http.Json; using System.Text.Json; using GameList.Tests.Support; namespace GameList.Tests; public class ResultsTests { [Fact] public async Task Results_available_after_admin_unlocks() { await using var factory = new TestWebApplicationFactory(); var admin = factory.CreateClientWithCookies(); await admin.RegisterAsync("admin", admin: true); var player = factory.CreateClientWithCookies(); await player.RegisterAsync("player"); var suggestionId = await player.CreateSuggestionAsync("ResultGame"); await player.PostAsJsonAsync("/api/me/phase/next", new { }); await player.PostAsJsonAsync("/api/votes", new { SuggestionId = suggestionId, Score = 8 }); await player.PostAsJsonAsync("/api/votes/finalize", new { Final = true }); await admin.PostAsJsonAsync("/api/admin/results", new { resultsOpen = true }); await player.PostAsJsonAsync("/api/me/phase/next", new { }); var results = await player.GetFromJsonAsync("/api/results"); Assert.True(results.GetArrayLength() >= 1); var first = results[0]; Assert.Equal("ResultGame", first.GetProperty("name").GetString()); Assert.Equal(8, (int)first.GetProperty("average").GetDouble()); } [Fact] public async Task Results_locked_returns_error() { await using var factory = new TestWebApplicationFactory(); var client = factory.CreateClientWithCookies(); await client.RegisterAsync("user"); await client.AdvanceToVoteAsync("Results locked seed"); var resp = await client.GetAsync("/api/results"); Assert.Equal(System.Net.HttpStatusCode.BadRequest, resp.StatusCode); } [Fact] public async Task Results_require_results_phase_and_auth() { await using var factory = new TestWebApplicationFactory(); var admin = factory.CreateClientWithCookies(); await admin.RegisterAsync("admin", admin: true); var player = factory.CreateClientWithCookies(); await player.RegisterAsync("player"); await admin.PostAsJsonAsync("/api/admin/results", new { resultsOpen = true }); var anon = factory.CreateClient(); var unauthorized = await anon.GetAsync("/api/results"); Assert.Equal(System.Net.HttpStatusCode.Unauthorized, unauthorized.StatusCode); var ok = await player.GetAsync("/api/results"); Assert.Equal(System.Net.HttpStatusCode.OK, ok.StatusCode); } [Fact] public async Task Results_payload_contains_fields_and_ordering() { await using var factory = new TestWebApplicationFactory(); var admin = factory.CreateClientWithCookies(); await admin.RegisterAsync("admin", admin: true); var player = factory.CreateClientWithCookies(); await player.RegisterAsync("player"); var s1 = await player.CreateSuggestionAsync("High"); _ = await player.CreateSuggestionAsync("NoVotes"); await player.PostAsJsonAsync("/api/me/phase/next", new { }); await player.PostAsJsonAsync("/api/votes", new { SuggestionId = s1, Score = 9 }); await player.PostAsJsonAsync("/api/votes/finalize", new { Final = true }); await admin.PostAsJsonAsync("/api/admin/results", new { resultsOpen = true }); await player.PostAsJsonAsync("/api/me/phase/next", new { }); var results = await player.GetFromJsonAsync>("/api/results"); Assert.NotNull(results); Assert.Equal(2, results.Count); Assert.Equal("High", results[0].GetProperty("name").GetString()); Assert.Equal(9, (int)results[0].GetProperty("average").GetDouble()); Assert.Equal(1, results[0].GetProperty("count").GetInt32()); Assert.Equal("player-name", results[0].GetProperty("voterNames")[0].GetString()); Assert.Equal(0, results[1].GetProperty("average").GetDouble()); Assert.Equal(0, results[1].GetProperty("voterNames").GetArrayLength()); } [Fact] public async Task Results_payload_contains_alphabetically_sorted_voter_names() { await using var factory = new TestWebApplicationFactory(); var admin = factory.CreateClientWithCookies(); await admin.RegisterAsync("admin", admin: true); var author = factory.CreateClientWithCookies(); await author.RegisterAsync("author"); var targetSuggestionId = await author.CreateSuggestionAsync("Target"); var zeta = factory.CreateClientWithCookies(); await zeta.RegisterAsync("zeta"); await zeta.AdvanceToVoteAsync("zeta-seed"); await zeta.PostAsJsonAsync("/api/votes", new { SuggestionId = targetSuggestionId, Score = 7 }); var alpha = factory.CreateClientWithCookies(); await alpha.RegisterAsync("alpha"); await alpha.AdvanceToVoteAsync("alpha-seed"); await alpha.PostAsJsonAsync("/api/votes", new { SuggestionId = targetSuggestionId, Score = 8 }); await admin.PostAsJsonAsync("/api/admin/results", new { resultsOpen = true }); var results = await alpha.GetFromJsonAsync>("/api/results"); Assert.NotNull(results); var target = results.Single(r => r.GetProperty("name").GetString() == "Target"); var voterNames = target .GetProperty("voterNames") .EnumerateArray() .Select(n => n.GetString()) .ToList(); Assert.Equal(new[] { "alpha-name", "zeta-name" }, voterNames); } }