|
| 1 | +using NSubstitute; |
| 2 | +using RustPlusBot.Abstractions.Events; |
| 3 | +using RustPlusBot.Abstractions.Time; |
| 4 | +using RustPlusBot.Features.Connections.Listening; |
| 5 | +using RustPlusBot.Features.Events.Classifying; |
| 6 | +using RustPlusBot.Features.Events.State; |
| 7 | + |
| 8 | +namespace RustPlusBot.Features.Events.Tests.State; |
| 9 | + |
| 10 | +public sealed class EventStateStoreTests |
| 11 | +{ |
| 12 | + private const ulong Guild = 1UL; |
| 13 | + private static readonly Guid Server = Guid.NewGuid(); |
| 14 | + private static readonly DateTimeOffset Now = new(2026, 6, 17, 12, 0, 0, TimeSpan.Zero); |
| 15 | + |
| 16 | + private static EventStateStore Build() |
| 17 | + { |
| 18 | + var clock = Substitute.For<IClock>(); |
| 19 | + clock.UtcNow.Returns(Now); |
| 20 | + return new EventStateStore(clock); |
| 21 | + } |
| 22 | + |
| 23 | + private static MapMarkersChangedEvent Delta( |
| 24 | + IReadOnlyList<MapMarkerSnapshot> added, |
| 25 | + IReadOnlyList<MapMarkerSnapshot> removed) => |
| 26 | + new(Guild, Server, null, added, removed); |
| 27 | + |
| 28 | + private static MapMarkersChangedEvent DeltaWithDims( |
| 29 | + IReadOnlyList<MapMarkerSnapshot> added, |
| 30 | + IReadOnlyList<MapMarkerSnapshot> removed, |
| 31 | + MapDimensions? dims) => |
| 32 | + new(Guild, Server, dims, added, removed); |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void Added_marker_becomes_active_and_carries_dimensions() |
| 36 | + { |
| 37 | + var store = Build(); |
| 38 | + var dims = new MapDimensions(4000u, 4000u, 500); |
| 39 | + store.Apply( |
| 40 | + DeltaWithDims([new MapMarkerSnapshot(1, MarkerKind.CargoShip, 10f, 20f, null)], [], dims), |
| 41 | + [new RustMapEvent(MapEventKind.CargoEntered, 10f, 20f, dims, Now)]); |
| 42 | + |
| 43 | + var active = store.GetActiveMarkers(Guild, Server, MarkerKind.CargoShip); |
| 44 | + Assert.Single(active); |
| 45 | + Assert.Equal(10f, active[0].X); |
| 46 | + Assert.Equal(dims, active[0].Dimensions); |
| 47 | + Assert.Equal(Now, active[0].SeenAtUtc); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void Removed_marker_leaves_active_even_when_unalerted() |
| 52 | + { |
| 53 | + var store = Build(); |
| 54 | + // A Crate marker produces NO classified event (core-3), but is still tracked in the active set... |
| 55 | + store.Apply(Delta([new MapMarkerSnapshot(4, MarkerKind.Crate, 0f, 0f, null)], []), []); |
| 56 | + Assert.Single(store.GetActiveMarkers(Guild, Server, MarkerKind.Crate)); |
| 57 | + // ...and its (un-alerted) removal must still clear it. |
| 58 | + store.Apply(Delta([], [new MapMarkerSnapshot(4, MarkerKind.Crate, 0f, 0f, null)]), []); |
| 59 | + |
| 60 | + Assert.Empty(store.GetActiveMarkers(Guild, Server, MarkerKind.Crate)); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void Recent_events_are_newest_first_and_bounded_to_ten() |
| 65 | + { |
| 66 | + var store = Build(); |
| 67 | + for (var i = 0; i < 12; i++) |
| 68 | + { |
| 69 | + store.Apply( |
| 70 | + Delta([new MapMarkerSnapshot((ulong)i, MarkerKind.CargoShip, i, 0f, null)], []), |
| 71 | + [new RustMapEvent(MapEventKind.CargoEntered, i, 0f, null, Now.AddMinutes(i))]); |
| 72 | + } |
| 73 | + |
| 74 | + var recent = store.GetRecentEvents(Guild, Server); |
| 75 | + Assert.Equal(10, recent.Count); |
| 76 | + Assert.Equal(Now.AddMinutes(11), recent[0].AtUtc); // newest first |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public void Clear_drops_active_and_recent_for_that_server() |
| 81 | + { |
| 82 | + var store = Build(); |
| 83 | + store.Apply(Delta([new MapMarkerSnapshot(1, MarkerKind.CargoShip, 0f, 0f, null)], []), |
| 84 | + [new RustMapEvent(MapEventKind.CargoEntered, 0f, 0f, null, Now)]); |
| 85 | + |
| 86 | + store.Clear(Guild, Server); |
| 87 | + |
| 88 | + Assert.Empty(store.GetActiveMarkers(Guild, Server, MarkerKind.CargoShip)); |
| 89 | + Assert.Empty(store.GetRecentEvents(Guild, Server)); |
| 90 | + } |
| 91 | +} |
0 commit comments