2025-05-13 23:51:14 -05:00
|
|
|
namespace StevesBot.Worker.Tests.Unit;
|
|
|
|
|
|
|
|
|
|
public sealed class DiscordGatewayClientTests : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly Mock<IDiscordRestClient> _mockDiscordRestClient = new();
|
|
|
|
|
private readonly Mock<IWebSocketFactory> _mockWebSocketFactory = new();
|
|
|
|
|
private readonly Mock<ILogger<DiscordGatewayClient>> _mockLogger = new();
|
2025-05-15 01:38:00 -05:00
|
|
|
private readonly Mock<TimeProvider> _mockTimeProvider = new();
|
2025-05-18 12:23:00 -05:00
|
|
|
private readonly Mock<IServiceScopeFactory> _mockServiceScopeFactory = new();
|
2025-05-13 23:51:14 -05:00
|
|
|
private readonly DiscordClientOptions _options = new();
|
|
|
|
|
private readonly DiscordGatewayClient _discordGatewayClient;
|
|
|
|
|
|
|
|
|
|
public DiscordGatewayClientTests()
|
|
|
|
|
{
|
2025-05-15 01:38:00 -05:00
|
|
|
_mockTimeProvider
|
|
|
|
|
.Setup(static x => x.GetUtcNow())
|
|
|
|
|
.Returns(DateTimeOffset.UtcNow);
|
|
|
|
|
|
2025-05-13 23:51:14 -05:00
|
|
|
_discordGatewayClient = new DiscordGatewayClient(
|
|
|
|
|
_options,
|
|
|
|
|
_mockWebSocketFactory.Object,
|
|
|
|
|
_mockLogger.Object,
|
2025-05-15 01:38:00 -05:00
|
|
|
_mockDiscordRestClient.Object,
|
2025-05-18 12:23:00 -05:00
|
|
|
_mockTimeProvider.Object,
|
|
|
|
|
_mockServiceScopeFactory.Object
|
2025-05-13 23:51:14 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 00:02:40 -05:00
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_WhenCalledAndOptionsIsNull_ItShouldThrowArgumentNullException()
|
|
|
|
|
{
|
|
|
|
|
var act = () => new DiscordGatewayClient(
|
|
|
|
|
null!,
|
|
|
|
|
_mockWebSocketFactory.Object,
|
|
|
|
|
_mockLogger.Object,
|
2025-05-15 01:38:00 -05:00
|
|
|
_mockDiscordRestClient.Object,
|
2025-05-18 12:23:00 -05:00
|
|
|
_mockTimeProvider.Object,
|
|
|
|
|
_mockServiceScopeFactory.Object
|
2025-05-15 00:02:40 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
act.Should().Throw<ArgumentNullException>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_WhenCalledAndWebSocketFactoryIsNull_ItShouldThrowArgumentNullException()
|
|
|
|
|
{
|
|
|
|
|
var act = () => new DiscordGatewayClient(
|
|
|
|
|
_options,
|
|
|
|
|
null!,
|
|
|
|
|
_mockLogger.Object,
|
2025-05-15 01:38:00 -05:00
|
|
|
_mockDiscordRestClient.Object,
|
2025-05-18 12:23:00 -05:00
|
|
|
_mockTimeProvider.Object,
|
|
|
|
|
_mockServiceScopeFactory.Object
|
2025-05-15 00:02:40 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
act.Should().Throw<ArgumentNullException>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_WhenCalledAndLoggerIsNull_ItShouldThrowArgumentNullException()
|
|
|
|
|
{
|
|
|
|
|
var act = () => new DiscordGatewayClient(
|
|
|
|
|
_options,
|
|
|
|
|
_mockWebSocketFactory.Object,
|
|
|
|
|
null!,
|
2025-05-15 01:38:00 -05:00
|
|
|
_mockDiscordRestClient.Object,
|
2025-05-18 12:23:00 -05:00
|
|
|
_mockTimeProvider.Object,
|
|
|
|
|
_mockServiceScopeFactory.Object
|
2025-05-15 00:02:40 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
act.Should().Throw<ArgumentNullException>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_WhenCalledAndDiscordRestClientIsNull_ItShouldThrowArgumentNullException()
|
|
|
|
|
{
|
|
|
|
|
var act = () => new DiscordGatewayClient(
|
|
|
|
|
_options,
|
|
|
|
|
_mockWebSocketFactory.Object,
|
|
|
|
|
_mockLogger.Object,
|
2025-05-15 01:38:00 -05:00
|
|
|
null!,
|
2025-05-18 12:23:00 -05:00
|
|
|
_mockTimeProvider.Object,
|
|
|
|
|
_mockServiceScopeFactory.Object
|
2025-05-15 01:38:00 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
act.Should().Throw<ArgumentNullException>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_WhenCalledAndTimeProviderIsNull_ItShouldThrowArgumentNullException()
|
|
|
|
|
{
|
|
|
|
|
var act = () => new DiscordGatewayClient(
|
|
|
|
|
_options,
|
|
|
|
|
_mockWebSocketFactory.Object,
|
|
|
|
|
_mockLogger.Object,
|
|
|
|
|
_mockDiscordRestClient.Object,
|
2025-05-18 12:23:00 -05:00
|
|
|
null!,
|
|
|
|
|
_mockServiceScopeFactory.Object
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
act.Should().Throw<ArgumentNullException>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_WhenCalledAndServiceScopeFactoryIsNull_ItShouldThrowArgumentNullException()
|
|
|
|
|
{
|
|
|
|
|
var act = () => new DiscordGatewayClient(
|
|
|
|
|
_options,
|
|
|
|
|
_mockWebSocketFactory.Object,
|
|
|
|
|
_mockLogger.Object,
|
|
|
|
|
_mockDiscordRestClient.Object,
|
|
|
|
|
_mockTimeProvider.Object,
|
2025-05-15 00:02:40 -05:00
|
|
|
null!
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
act.Should().Throw<ArgumentNullException>();
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-13 23:51:14 -05:00
|
|
|
[Fact]
|
|
|
|
|
public async Task ConnectAsync_WhenCalled_ItShouldConnectToGateway()
|
|
|
|
|
{
|
|
|
|
|
_mockDiscordRestClient
|
|
|
|
|
.Setup(static x => x.GetGatewayUrlAsync(It.IsAny<CancellationToken>()))
|
|
|
|
|
.ReturnsAsync("wss://gateway.discord.gg");
|
|
|
|
|
|
|
|
|
|
var mockWebSocket = new Mock<IWebSocket>();
|
|
|
|
|
|
|
|
|
|
_mockWebSocketFactory
|
|
|
|
|
.Setup(static x => x.Create())
|
|
|
|
|
.Returns(mockWebSocket.Object);
|
|
|
|
|
|
|
|
|
|
await _discordGatewayClient.ConnectAsync(CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Verify(
|
|
|
|
|
static x => x.ConnectAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()),
|
|
|
|
|
Times.Once
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-22 23:00:42 -05:00
|
|
|
[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
|
|
|
|
|
// );
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-14 16:45:44 -05:00
|
|
|
private static void SetupReceiveMessageSequence(
|
|
|
|
|
Mock<IWebSocket> mockWebSocket,
|
|
|
|
|
Queue<(WebSocketReceiveResult, byte[])> messageQueue
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Setup(static x => x.ReceiveAsync(It.IsAny<ArraySegment<byte>>(), It.IsAny<CancellationToken>()))
|
|
|
|
|
.Returns((ArraySegment<byte> buffer, CancellationToken token) =>
|
|
|
|
|
{
|
|
|
|
|
if (messageQueue.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return Task.Delay(-1, token)
|
|
|
|
|
.ContinueWith(
|
|
|
|
|
_ => new WebSocketReceiveResult(0, WebSocketMessageType.Text, true),
|
|
|
|
|
TaskScheduler.Default
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (result, messageBytes) = messageQueue.Dequeue();
|
|
|
|
|
Array.Copy(messageBytes, 0, buffer.Array!, buffer.Offset, Math.Min(messageBytes.Length, buffer.Count));
|
|
|
|
|
return Task.FromResult(result);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static (byte[] Bytes, string Json) CreateEventPayload(object e)
|
|
|
|
|
{
|
|
|
|
|
var json = JsonSerializer.Serialize(e);
|
|
|
|
|
var bytes = Encoding.UTF8.GetBytes(json);
|
|
|
|
|
return (bytes, json);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-13 23:51:14 -05:00
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2025-05-14 16:45:44 -05:00
|
|
|
_discordGatewayClient.Dispose();
|
2025-05-13 23:51:14 -05:00
|
|
|
}
|
|
|
|
|
}
|