122 lines
3.9 KiB
C#
122 lines
3.9 KiB
C#
using NeonSprawl.Server.Game.Factions;
|
|
|
|
namespace NeonSprawl.Server.Tests.Game.Factions;
|
|
|
|
public class FactionDefinitionRegistryTests
|
|
{
|
|
private static FactionDefinitionRegistry CreateRegistryFromRows(IReadOnlyDictionary<string, FactionDefRow> byId)
|
|
{
|
|
var catalog = new FactionDefinitionCatalog("/tmp/catalog", byId, catalogJsonFileCount: 1);
|
|
return new FactionDefinitionRegistry(catalog);
|
|
}
|
|
|
|
[Fact]
|
|
public void TryGetDefinition_ShouldReturnTrue_WhenIdExists()
|
|
{
|
|
// Arrange
|
|
var rows = new Dictionary<string, FactionDefRow>(StringComparer.Ordinal)
|
|
{
|
|
["prototype_faction_grid_operators"] = new(
|
|
"prototype_faction_grid_operators",
|
|
"Grid Operators",
|
|
-100,
|
|
100,
|
|
0),
|
|
};
|
|
var registry = CreateRegistryFromRows(rows);
|
|
// Act
|
|
var found = registry.TryGetDefinition("prototype_faction_grid_operators", out var def);
|
|
// Assert
|
|
Assert.True(found);
|
|
Assert.NotNull(def);
|
|
Assert.Equal("Grid Operators", def!.DisplayName);
|
|
Assert.Equal(-100, def.MinStanding);
|
|
}
|
|
|
|
[Fact]
|
|
public void TryGetDefinition_ShouldReturnFalse_WhenFactionIdIsNull()
|
|
{
|
|
// Arrange
|
|
var rows = new Dictionary<string, FactionDefRow>(StringComparer.Ordinal)
|
|
{
|
|
["prototype_faction_grid_operators"] = new(
|
|
"prototype_faction_grid_operators",
|
|
"Grid Operators",
|
|
-100,
|
|
100,
|
|
0),
|
|
};
|
|
var registry = CreateRegistryFromRows(rows);
|
|
// Act
|
|
var found = registry.TryGetDefinition(null, out var def);
|
|
// Assert
|
|
Assert.False(found);
|
|
Assert.Null(def);
|
|
}
|
|
|
|
[Fact]
|
|
public void TryGetDefinition_ShouldReturnFalse_WhenIdUnknown()
|
|
{
|
|
// Arrange
|
|
var rows = new Dictionary<string, FactionDefRow>(StringComparer.Ordinal)
|
|
{
|
|
["prototype_faction_grid_operators"] = new(
|
|
"prototype_faction_grid_operators",
|
|
"Grid Operators",
|
|
-100,
|
|
100,
|
|
0),
|
|
};
|
|
var registry = CreateRegistryFromRows(rows);
|
|
// Act
|
|
var found = registry.TryGetDefinition("not_a_real_faction", out var def);
|
|
// Assert
|
|
Assert.False(found);
|
|
Assert.Null(def);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetDefinitionsInIdOrder_ShouldListAllRowsOrderedById_WhenMultipleFactions()
|
|
{
|
|
// Arrange
|
|
var rows = new Dictionary<string, FactionDefRow>(StringComparer.Ordinal)
|
|
{
|
|
["prototype_faction_rust_collective"] = new(
|
|
"prototype_faction_rust_collective",
|
|
"Rust Collective",
|
|
-100,
|
|
100,
|
|
0),
|
|
["prototype_faction_grid_operators"] = new(
|
|
"prototype_faction_grid_operators",
|
|
"Grid Operators",
|
|
-100,
|
|
100,
|
|
0),
|
|
};
|
|
var registry = CreateRegistryFromRows(rows);
|
|
// Act
|
|
var list = registry.GetDefinitionsInIdOrder();
|
|
// Assert
|
|
Assert.Equal(2, list.Count);
|
|
Assert.Equal("prototype_faction_grid_operators", list[0].Id);
|
|
Assert.Equal("prototype_faction_rust_collective", list[1].Id);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Host_ShouldResolveRegistryFromDi_WhenStartupSucceeds()
|
|
{
|
|
// Arrange
|
|
await using var factory = new InMemoryWebApplicationFactory();
|
|
using var client = factory.CreateClient();
|
|
_ = await client.GetAsync("/health");
|
|
// Act
|
|
var registry = factory.Services.GetRequiredService<IFactionDefinitionRegistry>();
|
|
var found = registry.TryGetDefinition("prototype_faction_grid_operators", out var grid);
|
|
// Assert
|
|
Assert.True(found);
|
|
Assert.NotNull(grid);
|
|
Assert.Equal("Grid Operators", grid!.DisplayName);
|
|
}
|
|
}
|