|
- using System.Net;
- using System.Net.Http.Headers;
- using System.Net.Http.Json;
-
- namespace Campaign_Tracker.Server.Tests;
-
- public sealed class MunicipalityPriorCycleDefaultsControllerTests
- {
- [Fact]
- public async Task GetDefaults_WithPriorCycles_ReturnsMostRecentSelectedAndReadOnlyServices_AC1_AC2()
- {
- await using var factory = new AuthIntegrationTestFactory();
- using var client = CreateClient(factory);
- var profile = await CreateProfile(client, "LAKE02");
-
- var response = await client.GetAsync(
- $"/api/municipalities/{profile.ProfileId}/prior-cycle-defaults");
-
- Assert.Equal(HttpStatusCode.OK, response.StatusCode);
- var body = await response.Content.ReadFromJsonAsync<PriorCycleDefaultsDto>();
- Assert.NotNull(body);
- Assert.True(body.HasPriorCycles);
- Assert.Equal(2, body.Cycles.Length);
- Assert.Equal(body.Cycles[0].CycleId, body.SelectedCycleId);
- Assert.True(body.Cycles[0].CompletedAt.CompareTo(body.Cycles[1].CompletedAt) > 0);
- Assert.NotEmpty(body.Cycles[0].Services);
- Assert.All(body.Cycles[0].Services, service => Assert.NotEmpty(service.Values));
- }
-
- [Fact]
- public async Task GetDefaults_NoPriorCycles_ReturnsClearEmptyState_AC3()
- {
- await using var factory = new AuthIntegrationTestFactory();
- using var client = CreateClient(factory);
- var profile = await CreateProfile(client, "PINE03");
-
- var response = await client.GetAsync(
- $"/api/municipalities/{profile.ProfileId}/prior-cycle-defaults");
-
- Assert.Equal(HttpStatusCode.OK, response.StatusCode);
- var body = await response.Content.ReadFromJsonAsync<PriorCycleDefaultsDto>();
- Assert.NotNull(body);
- Assert.False(body.HasPriorCycles);
- Assert.Equal("No prior cycle defaults available.", body.EmptyStateMessage);
- Assert.Empty(body.Cycles);
- Assert.Null(body.SelectedCycleId);
- }
-
- [Fact]
- public async Task GetDefaults_UnknownProfile_Returns404()
- {
- await using var factory = new AuthIntegrationTestFactory();
- using var client = CreateClient(factory);
-
- var response = await client.GetAsync(
- "/api/municipalities/does-not-exist/prior-cycle-defaults");
-
- Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
- }
-
- private static HttpClient CreateClient(AuthIntegrationTestFactory factory)
- {
- var client = factory.CreateClient();
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
- "Bearer", AuthIntegrationTestFactory.CreateToken("cs@example.test", "client-services"));
- return client;
- }
-
- private static async Task<MunicipalityProfileDto> CreateProfile(HttpClient client, string jCode)
- {
- var created = await (await client.PostAsJsonAsync("/api/municipalities/profiles", new
- {
- jCode,
- displayName = (string?)null,
- })).Content.ReadFromJsonAsync<MunicipalityProfileDto>();
-
- Assert.NotNull(created);
- return created;
- }
-
- private sealed record MunicipalityProfileDto(string ProfileId);
-
- private sealed record PriorCycleDefaultsDto(
- string ProfileId,
- bool HasPriorCycles,
- string? SelectedCycleId,
- string EmptyStateMessage,
- PriorCycleDto[] Cycles);
-
- private sealed record PriorCycleDto(
- string CycleId,
- string CycleName,
- string CompletedAt,
- PriorCycleServiceDto[] Services);
-
- private sealed record PriorCycleServiceDto(
- string ServiceType,
- string Summary,
- Dictionary<string, string> Values);
- }
|