|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using Microsoft.Extensions.Logging.Abstractions; |
| 3 | +using Microsoft.Extensions.Options; |
| 4 | +using NSubstitute; |
| 5 | +using RustPlusBot.Abstractions.Events; |
| 6 | +using RustPlusBot.Abstractions.Time; |
| 7 | +using RustPlusBot.Features.Commands.Dispatching; |
| 8 | +using RustPlusBot.Features.Commands.Hosting; |
| 9 | +using RustPlusBot.Features.Connections.Listening; |
| 10 | +using RustPlusBot.Persistence.Commands; |
| 11 | +using RustPlusBot.Persistence.Workspace; |
| 12 | + |
| 13 | +namespace RustPlusBot.Features.Commands.Tests.Hosting; |
| 14 | + |
| 15 | +public sealed class CommandsHostedServiceTests |
| 16 | +{ |
| 17 | + private static Harness Create(bool prefixThrows = false) |
| 18 | + { |
| 19 | + var sender = Substitute.For<ITeamChatSender>(); |
| 20 | + sender.SendAsync(Arg.Any<ulong>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>()) |
| 21 | + .Returns(TeamChatSendResult.Sent); |
| 22 | + |
| 23 | + var muteStore = Substitute.For<IMuteStore>(); |
| 24 | + muteStore.GetMutedAsync(Arg.Any<ulong>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>()).Returns(false); |
| 25 | + if (prefixThrows) |
| 26 | + { |
| 27 | + muteStore.When(m => m.GetPrefixAsync(Arg.Any<ulong>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>())) |
| 28 | + .Do(_ => throw new InvalidOperationException("dispatch boom")); |
| 29 | + } |
| 30 | + else |
| 31 | + { |
| 32 | + muteStore.GetPrefixAsync(Arg.Any<ulong>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>()).Returns("!"); |
| 33 | + } |
| 34 | + |
| 35 | + var workspace = Substitute.For<IWorkspaceStore>(); |
| 36 | + workspace.GetCultureAsync(Arg.Any<ulong>(), Arg.Any<CancellationToken>()).Returns("en"); |
| 37 | + |
| 38 | + var handler = new StubHandler("pop"); |
| 39 | + var clock = Substitute.For<IClock>(); |
| 40 | + clock.UtcNow.Returns(DateTimeOffset.UnixEpoch); |
| 41 | + var cooldown = new CommandCooldown(clock, Options.Create(new CommandOptions())); |
| 42 | + var dispatcher = new CommandDispatcher( |
| 43 | + [handler], cooldown, muteStore, workspace, sender, NullLogger<CommandDispatcher>.Instance); |
| 44 | + |
| 45 | + var services = new ServiceCollection(); |
| 46 | + services.AddScoped(_ => dispatcher); |
| 47 | + var provider = services.BuildServiceProvider(); |
| 48 | + var scopeFactory = provider.GetRequiredService<IServiceScopeFactory>(); |
| 49 | + |
| 50 | + var bus = new InMemoryEventBus(); |
| 51 | + var service = new CommandsHostedService(bus, scopeFactory, NullLogger<CommandsHostedService>.Instance); |
| 52 | + |
| 53 | + return new Harness(service, bus, sender, handler, muteStore); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public async Task TeamMessageReceivedEvent_dispatches_the_command_and_relays_the_reply() |
| 58 | + { |
| 59 | + var h = Create(); |
| 60 | + await h.Service.StartAsync(default); |
| 61 | + |
| 62 | + var serverId = Guid.NewGuid(); |
| 63 | + var deadline = DateTimeOffset.UtcNow.AddSeconds(20); |
| 64 | + while (DateTimeOffset.UtcNow < deadline |
| 65 | + && !h.Sender.ReceivedCalls().Any(c => |
| 66 | + c.GetMethodInfo().Name == nameof(ITeamChatSender.SendAsync))) |
| 67 | + { |
| 68 | + await h.Bus.PublishAsync( |
| 69 | + new TeamMessageReceivedEvent(10UL, serverId, 7UL, "alice", "!pop", FromActivePlayer: false)); |
| 70 | + await Task.Delay(20); |
| 71 | + } |
| 72 | + |
| 73 | + Assert.True(h.Handler.Calls >= 1); |
| 74 | + await h.Sender.Received().SendAsync(10UL, serverId, "reply", Arg.Any<CancellationToken>()); |
| 75 | + |
| 76 | + await h.Service.StopAsync(default); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public async Task StartAsync_then_StopAsync_completes_cleanly() |
| 81 | + { |
| 82 | + var h = Create(); |
| 83 | + await h.Service.StartAsync(default); |
| 84 | + |
| 85 | + var stop = h.Service.StopAsync(default); |
| 86 | + await stop; |
| 87 | + |
| 88 | + Assert.True(stop.IsCompletedSuccessfully); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public async Task DispatchLoop_survives_a_faulting_dispatch_and_StopAsync_completes_cleanly() |
| 93 | + { |
| 94 | + var h = Create(prefixThrows: true); |
| 95 | + await h.Service.StartAsync(default); |
| 96 | + |
| 97 | + var serverId = Guid.NewGuid(); |
| 98 | + var deadline = DateTimeOffset.UtcNow.AddSeconds(20); |
| 99 | + while (DateTimeOffset.UtcNow < deadline |
| 100 | + && !h.MuteStore.ReceivedCalls().Any(c => |
| 101 | + c.GetMethodInfo().Name == nameof(IMuteStore.GetPrefixAsync))) |
| 102 | + { |
| 103 | + await h.Bus.PublishAsync( |
| 104 | + new TeamMessageReceivedEvent(10UL, serverId, 7UL, "alice", "!pop", FromActivePlayer: false)); |
| 105 | + await Task.Delay(20); |
| 106 | + } |
| 107 | + |
| 108 | + // The dispatch threw; the per-event catch swallowed it (LogDispatchFaulted) so the loop keeps running. |
| 109 | + await h.MuteStore.Received().GetPrefixAsync(10UL, serverId, Arg.Any<CancellationToken>()); |
| 110 | + await h.Sender.DidNotReceive().SendAsync( |
| 111 | + Arg.Any<ulong>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>()); |
| 112 | + await h.Service.StopAsync(default); |
| 113 | + } |
| 114 | + |
| 115 | + private sealed record Harness( |
| 116 | + CommandsHostedService Service, |
| 117 | + InMemoryEventBus Bus, |
| 118 | + ITeamChatSender Sender, |
| 119 | + StubHandler Handler, |
| 120 | + IMuteStore MuteStore); |
| 121 | + |
| 122 | + private sealed class StubHandler(string name) : ICommandHandler |
| 123 | + { |
| 124 | + public int Calls { get; private set; } |
| 125 | + |
| 126 | + public string Name => name; |
| 127 | + |
| 128 | + public Task<string?> ExecuteAsync(CommandContext context, CancellationToken cancellationToken) |
| 129 | + { |
| 130 | + Calls++; |
| 131 | + return Task.FromResult<string?>("reply"); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments