188 lines
6.3 KiB
C#
188 lines
6.3 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using TheLivingWorld.Api.Simulation;
|
|
using TheLivingWorld.Api.Storage;
|
|
using TheLivingWorld.Core.Contracts;
|
|
using TheLivingWorld.Core.Simulation;
|
|
|
|
namespace TheLivingWorld.Tests;
|
|
|
|
public sealed class WorldSimulationHostTests : IDisposable
|
|
{
|
|
private readonly string _root = Path.Combine(Path.GetTempPath(), $"tlw-sim-{Guid.NewGuid():n}");
|
|
private readonly WorldStore _store;
|
|
private readonly WorldSimulationHost _host;
|
|
|
|
public WorldSimulationHostTests()
|
|
{
|
|
_store = new WorldStore(
|
|
Options.Create(new WorldStorageOptions { RootDirectory = _root }),
|
|
NullLogger<WorldStore>.Instance);
|
|
_host = new WorldSimulationHost(_store, NullLogger<WorldSimulationHost>.Instance);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Startup_seeds_legacy_worlds_without_catching_up_from_created_at()
|
|
{
|
|
var legacy = Summary("legacy-11111111", "Legacy") with
|
|
{
|
|
CreatedAt = DateTimeOffset.UtcNow - TimeSpan.FromDays(30),
|
|
Clock = null,
|
|
LastTickedAt = null,
|
|
};
|
|
await _store.SaveSummaryAsync(legacy);
|
|
|
|
await _host.StartAsync(CancellationToken.None);
|
|
try
|
|
{
|
|
var clock = await WaitForClockAsync(legacy.Id);
|
|
|
|
// Thirty days of catch-up would be centuries of game time; the seed must start at the default
|
|
// morning and only drift by the handful of real seconds this test takes.
|
|
Assert.InRange(clock.GameTime, GameTime.DefaultStart, GameTime.DefaultStart.AddHours(1));
|
|
|
|
var stored = await _store.GetSummaryAsync(legacy.Id);
|
|
Assert.NotNull(stored?.Clock);
|
|
Assert.NotNull(stored.LastTickedAt);
|
|
}
|
|
finally
|
|
{
|
|
await _host.StopAsync(CancellationToken.None);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Concurrent_attach_registers_exactly_one_simulation()
|
|
{
|
|
var summary = Summary("racy-44444444", "Racy");
|
|
|
|
// Every loser of the GetOrAdd race owns an Arch world that must be disposed, not dropped.
|
|
Parallel.For(0, 32, _ => _host.Attach(summary));
|
|
|
|
Assert.True(_host.IsAttached(summary.Id));
|
|
|
|
var clock = _host.TryGetClock(summary.Id);
|
|
Assert.NotNull(clock);
|
|
Assert.InRange(clock.GameTime, GameTime.DefaultStart, GameTime.DefaultStart.AddMinutes(5));
|
|
}
|
|
|
|
[Fact]
|
|
public void Attach_keeps_the_running_clock_when_called_again_with_a_stale_summary()
|
|
{
|
|
var summary = Summary("stale-55555555", "Stale");
|
|
_host.Attach(summary);
|
|
_host.UpdateClock(summary.Id, new UpdateClockRequest { TimeScale = 4, Paused = true });
|
|
|
|
_host.Attach(summary);
|
|
|
|
var clock = _host.TryGetClock(summary.Id);
|
|
Assert.NotNull(clock);
|
|
Assert.Equal(4, clock.TimeScale);
|
|
Assert.True(clock.Paused);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateClock_changes_pause_and_Detach_stops_simulation()
|
|
{
|
|
var summary = Summary("ready-22222222", "Ready");
|
|
await _store.SaveSummaryAsync(summary);
|
|
_host.Attach(summary);
|
|
|
|
var paused = _host.UpdateClock(summary.Id, new UpdateClockRequest { Paused = true });
|
|
Assert.True(paused.Paused);
|
|
|
|
var scaled = _host.UpdateClock(summary.Id, new UpdateClockRequest { TimeScale = 3 });
|
|
Assert.Equal(3, scaled.TimeScale);
|
|
Assert.True(scaled.Paused);
|
|
|
|
_host.Detach(summary.Id);
|
|
Assert.False(_host.IsAttached(summary.Id));
|
|
Assert.Null(_host.TryGetClock(summary.Id));
|
|
}
|
|
|
|
[Fact]
|
|
public void Overlay_strips_last_ticked_at_for_api()
|
|
{
|
|
var summary = Summary("ready-33333333", "Ready");
|
|
_host.Attach(summary);
|
|
|
|
var overlaid = _host.Overlay(summary);
|
|
Assert.NotNull(overlaid.Clock);
|
|
Assert.Null(overlaid.LastTickedAt);
|
|
}
|
|
|
|
[Fact]
|
|
public void Overlay_strips_last_ticked_at_for_worlds_that_are_not_running()
|
|
{
|
|
var overlaid = _host.Overlay(Summary("pending-66666666", "Pending") with
|
|
{
|
|
Status = WorldStatus.Pending,
|
|
});
|
|
|
|
Assert.Null(overlaid.LastTickedAt);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The tick loop must not recreate the folder of a world deleted underneath it - that would leave a
|
|
/// state-only husk that lists as Ready but has no map behind it.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Persisting_a_deleted_world_does_not_resurrect_it()
|
|
{
|
|
var summary = Summary("doomed-77777777", "Doomed");
|
|
await _store.SaveSummaryAsync(summary);
|
|
_host.Attach(summary);
|
|
|
|
// Dirty the clock, then delete the world the way the endpoint does not: storage only, still attached.
|
|
_host.UpdateClock(summary.Id, new UpdateClockRequest { TimeScale = 2 });
|
|
Assert.True(_store.Delete(summary.Id));
|
|
|
|
await _host.StartAsync(CancellationToken.None);
|
|
await _host.StopAsync(CancellationToken.None);
|
|
|
|
Assert.Null(await _store.GetSummaryAsync(summary.Id));
|
|
Assert.False(Directory.Exists(Path.Combine(_root, summary.Id)));
|
|
Assert.False(_host.IsAttached(summary.Id));
|
|
}
|
|
|
|
private async Task<WorldClockDto> WaitForClockAsync(string id)
|
|
{
|
|
for (var attempt = 0; attempt < 100; attempt++)
|
|
{
|
|
if (_host.TryGetClock(id) is { } clock) return clock;
|
|
await Task.Delay(50);
|
|
}
|
|
|
|
throw new TimeoutException($"Simulation for world '{id}' never attached.");
|
|
}
|
|
|
|
private static WorldSummaryDto Summary(string id, string name) => new()
|
|
{
|
|
Id = id,
|
|
Name = name,
|
|
Latitude = 31.9,
|
|
Longitude = -100.5,
|
|
SizeMeters = 10_000,
|
|
Status = WorldStatus.Ready,
|
|
CreatedAt = DateTimeOffset.UtcNow,
|
|
Clock = WorldSimulation.DefaultClock(),
|
|
LastTickedAt = DateTimeOffset.UtcNow,
|
|
};
|
|
|
|
public void Dispose()
|
|
{
|
|
// Detach is a no-op for ids this test never attached, so one list covers every case.
|
|
string[] ids =
|
|
[
|
|
"legacy-11111111", "ready-22222222", "ready-33333333",
|
|
"racy-44444444", "stale-55555555", "pending-66666666", "doomed-77777777",
|
|
];
|
|
|
|
foreach (var id in ids)
|
|
_host.Detach(id);
|
|
|
|
if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|