97 lines
4.2 KiB
C#
97 lines
4.2 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using NeonSprawl.Server.Game.Gathering;
|
|
using NeonSprawl.Server.Tests;
|
|
using Xunit;
|
|
|
|
namespace NeonSprawl.Server.Tests.Game.Gathering;
|
|
|
|
public sealed class ResourceNodeInstanceOperationsTests
|
|
{
|
|
private const string AlphaId = "prototype_resource_node_alpha";
|
|
private const string DeltaId = "prototype_urban_bulk_delta";
|
|
|
|
[Fact]
|
|
public void TryCommitSuccessfulGatherDecrement_OnFreshNode_ShouldLazyInitAndDecrementToNine()
|
|
{
|
|
// Arrange
|
|
using var factory = new InMemoryWebApplicationFactory();
|
|
var registry = factory.Services.GetRequiredService<IResourceNodeDefinitionRegistry>();
|
|
var store = factory.Services.GetRequiredService<IResourceNodeInstanceStore>();
|
|
|
|
// Act
|
|
var outcome = ResourceNodeInstanceOperations.TryCommitSuccessfulGatherDecrement(AlphaId, registry, store);
|
|
|
|
// Assert
|
|
Assert.Equal(ResourceNodeInstanceMutationKind.Applied, outcome.Kind);
|
|
Assert.Null(outcome.ReasonCode);
|
|
Assert.NotNull(outcome.Snapshot);
|
|
Assert.Equal(AlphaId, outcome.Snapshot!.Value.InteractableId);
|
|
Assert.Equal(9, outcome.Snapshot.Value.RemainingGathers);
|
|
}
|
|
|
|
[Fact]
|
|
public void TryCommitSuccessfulGatherDecrement_RepeatedUntilCapacity_ShouldReturnNodeDepletedWithoutGoingNegative()
|
|
{
|
|
// Arrange
|
|
using var factory = new InMemoryWebApplicationFactory();
|
|
var registry = factory.Services.GetRequiredService<IResourceNodeDefinitionRegistry>();
|
|
var store = factory.Services.GetRequiredService<IResourceNodeInstanceStore>();
|
|
|
|
// Act — ten successful gathers (10 → 0)
|
|
ResourceNodeInstanceMutationOutcome lastApplied = default;
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
lastApplied = ResourceNodeInstanceOperations.TryCommitSuccessfulGatherDecrement(AlphaId, registry, store);
|
|
Assert.Equal(ResourceNodeInstanceMutationKind.Applied, lastApplied.Kind);
|
|
Assert.True(lastApplied.Snapshot!.Value.RemainingGathers >= 0);
|
|
}
|
|
|
|
var depleted = ResourceNodeInstanceOperations.TryCommitSuccessfulGatherDecrement(AlphaId, registry, store);
|
|
|
|
// Assert
|
|
Assert.Equal(0, lastApplied.Snapshot!.Value.RemainingGathers);
|
|
Assert.Equal(ResourceNodeInstanceMutationKind.Denied, depleted.Kind);
|
|
Assert.Equal(ResourceNodeInstanceReasonCodes.NodeDepleted, depleted.ReasonCode);
|
|
Assert.NotNull(depleted.Snapshot);
|
|
Assert.Equal(0, depleted.Snapshot!.Value.RemainingGathers);
|
|
store.TryGetRemainingGathers(AlphaId, out var persisted);
|
|
Assert.Equal(0, persisted.RemainingGathers);
|
|
}
|
|
|
|
[Fact]
|
|
public void TryCommitSuccessfulGatherDecrement_ForUnknownNode_ShouldDenyWithoutCreatingRow()
|
|
{
|
|
// Arrange
|
|
using var factory = new InMemoryWebApplicationFactory();
|
|
var registry = factory.Services.GetRequiredService<IResourceNodeDefinitionRegistry>();
|
|
var store = factory.Services.GetRequiredService<IResourceNodeInstanceStore>();
|
|
const string unknownId = "not_a_real_node_def";
|
|
|
|
// Act
|
|
var outcome = ResourceNodeInstanceOperations.TryCommitSuccessfulGatherDecrement(unknownId, registry, store);
|
|
var rowExists = store.TryGetRemainingGathers(unknownId, out _);
|
|
|
|
// Assert
|
|
Assert.Equal(ResourceNodeInstanceMutationKind.Denied, outcome.Kind);
|
|
Assert.Equal(ResourceNodeInstanceReasonCodes.UnknownNode, outcome.ReasonCode);
|
|
Assert.Null(outcome.Snapshot);
|
|
Assert.False(rowExists);
|
|
}
|
|
|
|
[Fact]
|
|
public void TryCommitSuccessfulGatherDecrement_ForDeltaNode_ShouldLazyInitAtTen()
|
|
{
|
|
// Arrange
|
|
using var factory = new InMemoryWebApplicationFactory();
|
|
var registry = factory.Services.GetRequiredService<IResourceNodeDefinitionRegistry>();
|
|
var store = factory.Services.GetRequiredService<IResourceNodeInstanceStore>();
|
|
|
|
// Act
|
|
var outcome = ResourceNodeInstanceOperations.TryCommitSuccessfulGatherDecrement(DeltaId, registry, store);
|
|
|
|
// Assert
|
|
Assert.Equal(ResourceNodeInstanceMutationKind.Applied, outcome.Kind);
|
|
Assert.Equal(9, outcome.Snapshot!.Value.RemainingGathers);
|
|
}
|
|
}
|