neon-sprawl/server/NeonSprawl.Server.Tests/Game/Gathering/ResourceNodeFixtureApiTests.cs

67 lines
2.3 KiB
C#

using System.Net;
using System.Net.Http.Json;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using NeonSprawl.Server.Game.Gathering;
using NeonSprawl.Server.Tests;
using Xunit;
namespace NeonSprawl.Server.Tests.Game.Gathering;
public sealed class ResourceNodeFixtureApiTests
{
private const string FixturePath = "/game/__dev/resource-node-fixture";
private const string AlphaId = "prototype_resource_node_alpha";
[Fact]
public async Task PostResourceNodeFixture_ShouldRestoreMaxGathers_WhenNodeDepleted()
{
// Arrange
await using var factory = new InMemoryWebApplicationFactory();
var client = factory.CreateClient();
var store = factory.Services.GetRequiredService<IResourceNodeInstanceStore>();
var registry = factory.Services.GetRequiredService<IResourceNodeDefinitionRegistry>();
registry.TryGetDefinition(AlphaId, out var definition);
store.TryEnsureInitialized(AlphaId, initialRemainingGathers: 0);
// Act
var response = await client.PostAsJsonAsync(
FixturePath,
new ResourceNodeFixtureRequest
{
SchemaVersion = ResourceNodeFixtureRequest.CurrentSchemaVersion,
});
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.Content.ReadFromJsonAsync<ResourceNodeFixtureResponse>();
Assert.NotNull(body);
Assert.True(body!.Applied);
store.TryGetRemainingGathers(AlphaId, out var snapshot);
Assert.Equal(definition!.MaxGathers, snapshot.RemainingGathers);
}
[Fact]
public async Task PostResourceNodeFixture_ShouldReturnNotFound_WhenRouteNotRegistered()
{
// Arrange
await using var factory = new InMemoryWebApplicationFactory();
var client = factory.WithWebHostBuilder(b =>
{
b.UseEnvironment("Production");
b.UseSetting("Game:EnableResourceNodeFixtureApi", "false");
}).CreateClient();
// Act
var response = await client.PostAsJsonAsync(
FixturePath,
new ResourceNodeFixtureRequest
{
SchemaVersion = ResourceNodeFixtureRequest.CurrentSchemaVersion,
});
// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
}