tests: oh we love our tests
This commit is contained in:
@@ -138,6 +138,70 @@ public sealed class DiscordGatewayClientTests : IDisposable
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ConnectAsync_WhenConnectedAndHelloEventIsReceived_ItShouldStartSendingHeartbeatsAndIdentify()
|
||||||
|
{
|
||||||
|
// TODO: This does not work! You need
|
||||||
|
// to figure it out! When this runs
|
||||||
|
// concurrently with test above that
|
||||||
|
// test starts to fail
|
||||||
|
|
||||||
|
_mockDiscordRestClient
|
||||||
|
.Setup(static x => x.GetGatewayUrlAsync(It.IsAny<CancellationToken>()))
|
||||||
|
.ReturnsAsync("wss://gateway.discord.gg");
|
||||||
|
|
||||||
|
var mockWebSocket = new Mock<IWebSocket>();
|
||||||
|
|
||||||
|
mockWebSocket
|
||||||
|
.Setup(static x => x.ConnectAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Returns(Task.CompletedTask);
|
||||||
|
|
||||||
|
mockWebSocket
|
||||||
|
.SetupSequence(static x => x.State)
|
||||||
|
.Returns(WebSocketState.Closed)
|
||||||
|
.Returns(WebSocketState.Open);
|
||||||
|
|
||||||
|
var messagesToReceive = new Queue<(WebSocketReceiveResult, byte[])>();
|
||||||
|
var helloEvent = new HelloDiscordEvent();
|
||||||
|
var payload = CreateEventPayload(helloEvent);
|
||||||
|
var result = new WebSocketReceiveResult(payload.Bytes.Length, WebSocketMessageType.Text, true);
|
||||||
|
messagesToReceive.Enqueue((result, payload.Bytes));
|
||||||
|
|
||||||
|
SetupReceiveMessageSequence(mockWebSocket, messagesToReceive);
|
||||||
|
|
||||||
|
_mockWebSocketFactory
|
||||||
|
.Setup(static x => x.Create())
|
||||||
|
.Returns(mockWebSocket.Object);
|
||||||
|
|
||||||
|
using var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
|
await _discordGatewayClient.ConnectAsync(cts.Token);
|
||||||
|
await Task.Delay(100);
|
||||||
|
await cts.CancelAsync();
|
||||||
|
|
||||||
|
var heartbeatEvent = new HeartbeatDiscordEvent(helloEvent.Sequence);
|
||||||
|
var heartbeatPayload = CreateEventPayload(heartbeatEvent);
|
||||||
|
|
||||||
|
// it should send a heartbeat
|
||||||
|
mockWebSocket
|
||||||
|
.Verify(
|
||||||
|
x => x.SendAsync(
|
||||||
|
It.Is<ArraySegment<byte>>(b => heartbeatPayload.Bytes.SequenceEqual(b.Array!)),
|
||||||
|
It.Is<WebSocketMessageType>(m => m == WebSocketMessageType.Text),
|
||||||
|
true,
|
||||||
|
It.IsAny<CancellationToken>()
|
||||||
|
),
|
||||||
|
Times.Once
|
||||||
|
);
|
||||||
|
|
||||||
|
// it should send a identify event
|
||||||
|
// mockWebSocket
|
||||||
|
// .Verify(
|
||||||
|
// x => x.SendAsync(),
|
||||||
|
// Times.Once
|
||||||
|
// );
|
||||||
|
}
|
||||||
|
|
||||||
private static void SetupReceiveMessageSequence(
|
private static void SetupReceiveMessageSequence(
|
||||||
Mock<IWebSocket> mockWebSocket,
|
Mock<IWebSocket> mockWebSocket,
|
||||||
Queue<(WebSocketReceiveResult, byte[])> messageQueue
|
Queue<(WebSocketReceiveResult, byte[])> messageQueue
|
||||||
|
|||||||
@@ -28,4 +28,55 @@ public class WelcomeMessageHandlerTests
|
|||||||
Times.Never
|
Times.Never
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task HandleAsync_WhenMessageCreateEventIsNotAUserJoinMessage_ItShouldNotCreateMessage()
|
||||||
|
{
|
||||||
|
var @event = new MessageCreateDiscordEvent()
|
||||||
|
{
|
||||||
|
Data = new()
|
||||||
|
{
|
||||||
|
Type = -1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
await WelcomeMessageHandler.HandleAsync(@event, _serviceProvider);
|
||||||
|
|
||||||
|
_mockDiscordRestClient
|
||||||
|
.Verify(
|
||||||
|
static c => c.CreateMessageAsync(It.IsAny<string>(), It.IsAny<CreateMessageRequest>(), It.IsAny<CancellationToken>()),
|
||||||
|
Times.Never
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task HandleAsync_WhenUserJoinMessageCreateEventReceived_ItShouldCreateWelcomeMessage()
|
||||||
|
{
|
||||||
|
_mockDiscordRestClient
|
||||||
|
.Setup(static c => c.CreateMessageAsync(It.IsAny<string>(), It.IsAny<CreateMessageRequest>(), It.IsAny<CancellationToken>()))
|
||||||
|
.ReturnsAsync(new DiscordMessage());
|
||||||
|
|
||||||
|
var @event = new MessageCreateDiscordEvent()
|
||||||
|
{
|
||||||
|
Data = new()
|
||||||
|
{
|
||||||
|
Type = DiscordMessageTypes.UserJoin,
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
ChannelId = Guid.NewGuid().ToString(),
|
||||||
|
GuildId = Guid.NewGuid().ToString(),
|
||||||
|
Author = new()
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await WelcomeMessageHandler.HandleAsync(@event, _serviceProvider);
|
||||||
|
|
||||||
|
_mockDiscordRestClient
|
||||||
|
.Verify(
|
||||||
|
static c => c.CreateMessageAsync(It.IsAny<string>(), It.IsAny<CreateMessageRequest>(), It.IsAny<CancellationToken>()),
|
||||||
|
Times.Once
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -218,7 +218,6 @@ internal sealed class DiscordGatewayClient : IDiscordGatewayClient
|
|||||||
|
|
||||||
private async Task HandleEventAsync(DiscordEvent e, CancellationToken cancellationToken)
|
private async Task HandleEventAsync(DiscordEvent e, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
// TODO: Handle other events
|
|
||||||
await SetSequenceAsync(e.Sequence, cancellationToken);
|
await SetSequenceAsync(e.Sequence, cancellationToken);
|
||||||
|
|
||||||
switch (e)
|
switch (e)
|
||||||
|
|||||||
Reference in New Issue
Block a user