using System.Net.Http.Json;
namespace HSchool.AppHost.Tests;
///
/// The people seed is a school field of its own, not the school id. These tests share the
/// AppHost, so each one starts by clearing the list.
///
[Collection(AppHostCollection.Name)]
public class SchoolSeedTests(AppHostFixture fixture)
{
private static readonly DateTime Start = new(2012, 4, 3, 6, 0, 0, DateTimeKind.Utc);
[Fact]
public async Task SameSeed_PopulatesTheSamePeople_DifferentSeedsDoNot()
{
using var client = fixture.App.CreateHttpClient("server");
await SchoolApiTests.ResetAsync(client);
var first = await SchoolApiTests.CreateAsync(client, "Сид одинаковый А", Start, seed: 42);
var second = await SchoolApiTests.CreateAsync(client, "Сид одинаковый Б", Start, seed: 42);
var firstYearFive = await YearFiveAsync(client, first.Id);
var secondYearFive = await YearFiveAsync(client, second.Id);
Assert.Equal(firstYearFive, secondYearFive);
using (var delete = await client.DeleteAsync($"/api/schools/{first.Id}", TestContext.Current.CancellationToken))
{
delete.EnsureSuccessStatusCode();
}
var other = await SchoolApiTests.CreateAsync(client, "Сид другой", Start, seed: 7);
var otherYearFive = await YearFiveAsync(client, other.Id);
Assert.NotEqual(secondYearFive, otherYearFive);
}
[Fact]
public async Task UnseededSchool_KeepsItsPeopleAfterReload()
{
using var client = fixture.App.CreateHttpClient("server");
await SchoolApiTests.ResetAsync(client);
var created = await SchoolApiTests.CreateAsync(client, "Сид брошен", Start);
var before = await YearFiveAsync(client, created.Id);
using var reload = await client.PostAsync("/api/dev/reload-schools", content: null, TestContext.Current.CancellationToken);
reload.EnsureSuccessStatusCode();
var restored = (await SchoolApiTests.GetSchoolsAsync(client)).Schools.Single(school => school.Id == created.Id);
Assert.Equal(created.Seed, restored.Seed);
Assert.Equal(before, await YearFiveAsync(client, created.Id));
}
[Fact]
public async Task PassedSeed_IsVisibleInTheSchoolList()
{
using var client = fixture.App.CreateHttpClient("server");
await SchoolApiTests.ResetAsync(client);
var created = await SchoolApiTests.CreateAsync(client, "Сид в списке", Start, seed: 20260819);
Assert.Equal(20260819, created.Seed);
var listed = (await SchoolApiTests.GetSchoolsAsync(client)).Schools.Single(school => school.Id == created.Id);
Assert.Equal(20260819, listed.Seed);
}
[Fact]
public async Task TwoUnseededSchools_ArePopulatedDifferently()
{
using var client = fixture.App.CreateHttpClient("server");
await SchoolApiTests.ResetAsync(client);
var first = await SchoolApiTests.CreateAsync(client, "Без сида А", Start);
var second = await SchoolApiTests.CreateAsync(client, "Без сида Б", Start);
Assert.NotEqual(first.Seed, second.Seed);
Assert.NotEqual(await YearFiveAsync(client, first.Id), await YearFiveAsync(client, second.Id));
}
private static async Task<(string Id, string FullName)[]> YearFiveAsync(HttpClient client, int schoolId)
{
var page = await client.GetFromJsonAsync(
$"/api/schools/{schoolId}/people?role=student&year=5&sort=surname&pageSize=100",
TestContext.Current.CancellationToken);
Assert.NotNull(page);
Assert.NotEmpty(page.People);
return [.. page.People.Select(person => (person.Id, person.FullName))];
}
private sealed record PeoplePage(IReadOnlyList People);
private sealed record PersonRow(string Id, string FullName);
}