-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerEventRelayTests.cs
More file actions
87 lines (73 loc) · 3.66 KB
/
Copy pathPlayerEventRelayTests.cs
File metadata and controls
87 lines (73 loc) · 3.66 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
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using RustPlusBot.Abstractions.Connections;
using RustPlusBot.Abstractions.Events;
using RustPlusBot.Features.Connections.Listening;
using RustPlusBot.Features.Players.Posting;
using RustPlusBot.Features.Players.Relaying;
using RustPlusBot.Features.Players.Rendering;
using RustPlusBot.Features.Workspace.Locating;
using RustPlusBot.Localization;
using RustPlusBot.Persistence.Map;
using RustPlusBot.Persistence.Workspace;
namespace RustPlusBot.Features.Players.Tests;
public sealed class PlayerEventRelayTests
{
private readonly IEventChannelLocator _locator = Substitute.For<IEventChannelLocator>();
private readonly IPlayerChannelPoster _poster = Substitute.For<IPlayerChannelPoster>();
private readonly IBotTeamChatSender _sender = Substitute.For<IBotTeamChatSender>();
private readonly IWorkspaceStore _workspace = Substitute.For<IWorkspaceStore>();
private PlayerEventRelay BuildRelay()
{
var scopeFactory = BuildScopeFactory(_workspace);
_workspace.GetCultureAsync(Arg.Any<ulong>(), Arg.Any<CancellationToken>()).Returns("en");
return new PlayerEventRelay(
new PlayerEventRenderer(new ResxLocalizer()),
_locator, _poster, _sender, scopeFactory);
}
private static IServiceScopeFactory BuildScopeFactory(IWorkspaceStore workspace)
{
var mapSettings = Substitute.For<IMapSettingsStore>();
mapSettings.GetAsync(Arg.Any<ulong>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(MapLayerSettings.AllOn);
var services = new ServiceCollection();
services.AddScoped(_ => workspace);
services.AddScoped(_ => mapSettings);
return services.BuildServiceProvider().GetRequiredService<IServiceScopeFactory>();
}
private static PlayerStateChangedEvent Evt(params PlayerTransition[] ts)
=> new(1UL, Guid.NewGuid(), new MapDimensions(3000, 3000, 0, WorldSize: 3000), ts);
[Fact]
public async Task Sends_ingame_line_for_each_transition()
{
var relay = BuildRelay();
_locator.GetChannelIdAsync(Arg.Any<ulong>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns((ulong?)null);
await relay.RelayAsync(
Evt(new PlayerTransition(PlayerTransitionKind.Connect, 1, "Bob", null)), CancellationToken.None);
await _sender.Received(1).SendAsync(
Arg.Any<ulong>(), Arg.Any<Guid>(), Arg.Is<string>(s => s!.Contains("Bob")), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Skips_discord_post_when_no_channel_but_still_sends_ingame()
{
var relay = BuildRelay();
_locator.GetChannelIdAsync(Arg.Any<ulong>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns((ulong?)null);
await relay.RelayAsync(
Evt(new PlayerTransition(PlayerTransitionKind.Disconnect, 1, "Bob", null)), CancellationToken.None);
await _poster.DidNotReceive()
.PostAsync(Arg.Any<ulong>(), Arg.Any<global::Discord.Embed>(), Arg.Any<CancellationToken>());
await _sender.ReceivedWithAnyArgs(1).SendAsync(default, Guid.Empty, default!, default);
}
[Fact]
public async Task Posts_embed_when_channel_resolves()
{
var relay = BuildRelay();
_locator.GetChannelIdAsync(Arg.Any<ulong>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns((ulong?)555);
await relay.RelayAsync(
Evt(new PlayerTransition(PlayerTransitionKind.Death, 1, "Bob", (10f, 10f))), CancellationToken.None);
await _poster.Received(1).PostAsync(555UL, Arg.Any<global::Discord.Embed>(), Arg.Any<CancellationToken>());
}
}