-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandRegistrationTests.cs
More file actions
105 lines (96 loc) · 5.27 KB
/
Copy pathCommandRegistrationTests.cs
File metadata and controls
105 lines (96 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NSubstitute;
using RustPlusBot.Abstractions.Connections;
using RustPlusBot.Abstractions.Events;
using RustPlusBot.Abstractions.Time;
using RustPlusBot.Discord;
using RustPlusBot.Features.Commands.Dispatching;
using RustPlusBot.Features.Commands.Hosting;
using RustPlusBot.Features.Commands.Servers;
using RustPlusBot.Features.Connections.Listening;
using RustPlusBot.Features.Events.State;
using RustPlusBot.Features.ItemData;
using RustPlusBot.Localization;
using RustPlusBot.Persistence.Commands;
using RustPlusBot.Persistence.Servers;
using RustPlusBot.Persistence.Workspace;
namespace RustPlusBot.Features.Commands.Tests;
public sealed class CommandRegistrationTests
{
[Fact]
public void Dispatcher_and_handlers_resolve()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IClock>(Substitute.For<IClock>());
services.AddSingleton<IEventBus>(Substitute.For<IEventBus>());
services.AddSingleton<IBotTeamChatSender>(Substitute.For<IBotTeamChatSender>());
services.AddSingleton<IRustServerQuery>(Substitute.For<IRustServerQuery>());
services.AddSingleton<IEventState>(_ => Substitute.For<IEventState>());
services.AddSingleton<IRigState>(_ => Substitute.For<IRigState>());
services.AddSingleton<IAfkState>(_ => Substitute.For<IAfkState>());
services.AddScoped<IMuteStore>(_ => Substitute.For<IMuteStore>());
services.AddScoped<IWorkspaceStore>(_ => Substitute.For<IWorkspaceStore>());
services.AddScoped<IServerService>(_ => Substitute.For<IServerService>());
services.AddOptions<CommandOptions>();
services.AddItemData();
services.AddCommands();
using var provider = services.BuildServiceProvider(validateScopes: true);
Assert.Contains(provider.GetServices<IHostedService>(), h => h is CommandsHostedService);
// The singletons AddCommands wires must each resolve (guards against an accidentally dropped registration).
Assert.NotNull(provider.GetRequiredService<CommandCooldown>());
Assert.NotNull(provider.GetRequiredService<BotUptime>());
Assert.NotNull(provider.GetRequiredService<ILocalizer>());
using var scope = provider.CreateScope();
Assert.NotNull(scope.ServiceProvider.GetRequiredService<CommandDispatcher>());
var handlers = scope.ServiceProvider.GetServices<ICommandHandler>().ToList();
Assert.Equal(28, handlers.Count);
Assert.Contains(handlers, h => h.Name == "mute");
Assert.Contains(handlers, h => h.Name == "pop");
Assert.Contains(handlers, h => h.Name == "time");
Assert.Contains(handlers, h => h.Name == "wipe");
Assert.Contains(handlers, h => h.Name == "online");
Assert.Contains(handlers, h => h.Name == "offline");
Assert.Contains(handlers, h => h.Name == "team");
Assert.Contains(handlers, h => h.Name == "steamid");
Assert.Contains(handlers, h => h.Name == "alive");
Assert.Contains(handlers, h => h.Name == "prox");
Assert.Contains(handlers, h => h.Name == "cargo");
Assert.Contains(handlers, h => h.Name == "heli");
Assert.Contains(handlers, h => h.Name == "chinook");
Assert.Contains(handlers, h => h.Name == "events");
Assert.Contains(handlers, h => h.Name == "small");
Assert.Contains(handlers, h => h.Name == "large");
Assert.Contains(handlers, h => h.Name == "afk");
Assert.Contains(handlers, h => h.Name == "decay");
Assert.Contains(handlers, h => h.Name == "upkeep");
Assert.Contains(handlers, h => h.Name == "durability");
Assert.Contains(handlers, h => h.Name == "smelt");
Assert.Contains(handlers, h => h.Name == "cctv");
Assert.NotNull(scope.ServiceProvider.GetRequiredService<ServerResolver>());
Assert.NotNull(scope.ServiceProvider.GetRequiredService<ServerQueryService>());
}
[Fact]
public void Commands_contribute_an_interaction_module_assembly()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IClock>(Substitute.For<IClock>());
services.AddSingleton<IEventBus>(Substitute.For<IEventBus>());
services.AddSingleton<IBotTeamChatSender>(Substitute.For<IBotTeamChatSender>());
services.AddSingleton<IRustServerQuery>(Substitute.For<IRustServerQuery>());
services.AddSingleton<IEventState>(_ => Substitute.For<IEventState>());
services.AddSingleton<IRigState>(_ => Substitute.For<IRigState>());
services.AddSingleton<IAfkState>(_ => Substitute.For<IAfkState>());
services.AddScoped<IMuteStore>(_ => Substitute.For<IMuteStore>());
services.AddScoped<IWorkspaceStore>(_ => Substitute.For<IWorkspaceStore>());
services.AddScoped<IServerService>(_ => Substitute.For<IServerService>());
services.AddOptions<CommandOptions>();
services.AddItemData();
services.AddCommands();
using var provider = services.BuildServiceProvider(validateScopes: true);
var assemblies = provider.GetServices<InteractionModuleAssembly>();
Assert.Contains(assemblies, a => a.Assembly == typeof(CommandServiceCollectionExtensions).Assembly);
}
}