2025-05-23 12:09:41 -05:00
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
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>();
|
|
|
|
|
|
2025-05-23 08:59:22 -05:00
|
|
|
mockWebSocket
|
|
|
|
|
.SetupSequence(static x => x.State)
|
|
|
|
|
.Returns(WebSocketState.Closed)
|
|
|
|
|
.Returns(WebSocketState.Open);
|
|
|
|
|
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Setup(static x => x.ConnectAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
|
|
|
|
|
.Returns(Task.CompletedTask);
|
|
|
|
|
|
2025-05-13 23:51:14 -05:00
|
|
|
_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()
|
|
|
|
|
{
|
|
|
|
|
_mockDiscordRestClient
|
|
|
|
|
.Setup(static x => x.GetGatewayUrlAsync(It.IsAny<CancellationToken>()))
|
|
|
|
|
.ReturnsAsync("wss://gateway.discord.gg");
|
|
|
|
|
|
|
|
|
|
var mockWebSocket = new Mock<IWebSocket>();
|
|
|
|
|
|
2025-05-23 08:59:22 -05:00
|
|
|
var socketState = WebSocketState.Closed;
|
|
|
|
|
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Setup(static x => x.State)
|
|
|
|
|
.Returns(() => socketState);
|
|
|
|
|
|
2025-05-22 23:00:42 -05:00
|
|
|
mockWebSocket
|
|
|
|
|
.Setup(static x => x.ConnectAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
|
2025-05-23 08:59:22 -05:00
|
|
|
.Callback(() => socketState = WebSocketState.Open)
|
2025-05-22 23:00:42 -05:00
|
|
|
.Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
var messagesToReceive = new Queue<(WebSocketReceiveResult, byte[])>();
|
2025-05-23 08:59:22 -05:00
|
|
|
var heartbeatInterval = 100;
|
|
|
|
|
var helloEvent = new HelloDiscordEvent()
|
|
|
|
|
{
|
|
|
|
|
Data = new()
|
|
|
|
|
{
|
|
|
|
|
HeartbeatInterval = heartbeatInterval,
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-05-23 12:09:41 -05:00
|
|
|
var helloPayload = CreateEventPayload(helloEvent);
|
|
|
|
|
var helloResult = new WebSocketReceiveResult(helloPayload.Bytes.Length, WebSocketMessageType.Text, true);
|
|
|
|
|
messagesToReceive.Enqueue((helloResult, helloPayload.Bytes));
|
2025-05-22 23:00:42 -05:00
|
|
|
|
|
|
|
|
SetupReceiveMessageSequence(mockWebSocket, messagesToReceive);
|
|
|
|
|
|
|
|
|
|
_mockWebSocketFactory
|
|
|
|
|
.Setup(static x => x.Create())
|
|
|
|
|
.Returns(mockWebSocket.Object);
|
|
|
|
|
|
|
|
|
|
using var cts = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
await _discordGatewayClient.ConnectAsync(cts.Token);
|
2025-05-23 08:59:22 -05:00
|
|
|
await Task.Delay((int)(heartbeatInterval * 1.5));
|
2025-05-22 23:00:42 -05:00
|
|
|
await cts.CancelAsync();
|
|
|
|
|
|
2025-05-23 12:09:41 -05:00
|
|
|
var expectedHeartbeatEvent = new HeartbeatDiscordEvent(helloEvent.Sequence);
|
|
|
|
|
var expectedHeartbeatPayload = CreateEventPayload(expectedHeartbeatEvent);
|
2025-05-22 23:00:42 -05:00
|
|
|
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Verify(
|
|
|
|
|
x => x.SendAsync(
|
2025-05-23 12:09:41 -05:00
|
|
|
It.Is<ArraySegment<byte>>(b => expectedHeartbeatPayload.Bytes.SequenceEqual(b.Array!)),
|
2025-05-22 23:00:42 -05:00
|
|
|
It.Is<WebSocketMessageType>(m => m == WebSocketMessageType.Text),
|
|
|
|
|
true,
|
|
|
|
|
It.IsAny<CancellationToken>()
|
|
|
|
|
),
|
|
|
|
|
Times.Once
|
|
|
|
|
);
|
|
|
|
|
|
2025-05-23 12:09:41 -05:00
|
|
|
var expectedIdentifyEvent = new IdentifyDiscordEvent(
|
2025-05-23 08:59:22 -05:00
|
|
|
_options.AppToken,
|
|
|
|
|
_options.Intents,
|
|
|
|
|
new UpdatePresenceData
|
|
|
|
|
{
|
|
|
|
|
Status = PresenceStatus.Online,
|
|
|
|
|
Activities = [
|
|
|
|
|
new()
|
|
|
|
|
{
|
|
|
|
|
Name = "Helping Stevan",
|
|
|
|
|
State = "Helping Stevan"
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
);
|
2025-05-23 12:09:41 -05:00
|
|
|
var expectedIdentifyPayload = CreateEventPayload(expectedIdentifyEvent);
|
2025-05-23 08:59:22 -05:00
|
|
|
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Verify(
|
|
|
|
|
x => x.SendAsync(
|
2025-05-23 12:09:41 -05:00
|
|
|
It.Is<ArraySegment<byte>>(b => expectedIdentifyPayload.Bytes.SequenceEqual(b.Array!)),
|
2025-05-23 08:59:22 -05:00
|
|
|
It.Is<WebSocketMessageType>(m => m == WebSocketMessageType.Text),
|
|
|
|
|
true,
|
|
|
|
|
It.IsAny<CancellationToken>()
|
|
|
|
|
),
|
|
|
|
|
Times.Once
|
|
|
|
|
);
|
2025-05-22 23:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-23 12:09:41 -05:00
|
|
|
[Fact]
|
2025-05-23 12:59:53 -05:00
|
|
|
public async Task ConnectAsync_OnceConnectedIfHeartbeatIsNotAcknowledged_ItShouldStopSendingHeartbeatsAndAttemptToResume()
|
2025-05-23 12:09:41 -05:00
|
|
|
{
|
|
|
|
|
_mockDiscordRestClient
|
|
|
|
|
.Setup(static x => x.GetGatewayUrlAsync(It.IsAny<CancellationToken>()))
|
|
|
|
|
.ReturnsAsync("wss://gateway.discord.gg");
|
|
|
|
|
|
|
|
|
|
var initialSocketState = WebSocketState.Closed;
|
|
|
|
|
|
|
|
|
|
var initialWebSocket = new Mock<IWebSocket>();
|
|
|
|
|
|
|
|
|
|
initialWebSocket
|
|
|
|
|
.Setup(static x => x.State)
|
|
|
|
|
.Returns(() => initialSocketState);
|
|
|
|
|
|
|
|
|
|
initialWebSocket
|
|
|
|
|
.Setup(static x => x.ConnectAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
|
|
|
|
|
.Callback(() => initialSocketState = WebSocketState.Open)
|
|
|
|
|
.Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
initialWebSocket
|
|
|
|
|
.Setup(static x => x.CloseAsync(It.IsAny<WebSocketCloseStatus>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
|
|
|
|
.Callback(() => initialSocketState = WebSocketState.Closed)
|
|
|
|
|
.Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
var resumingSocketState = WebSocketState.Closed;
|
|
|
|
|
|
|
|
|
|
var resumingWebSocket = new Mock<IWebSocket>();
|
|
|
|
|
|
|
|
|
|
resumingWebSocket
|
|
|
|
|
.Setup(static x => x.State)
|
|
|
|
|
.Returns(() => resumingSocketState);
|
|
|
|
|
|
|
|
|
|
resumingWebSocket
|
|
|
|
|
.Setup(static x => x.ConnectAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
|
|
|
|
|
.Callback(() => resumingSocketState = WebSocketState.Open)
|
|
|
|
|
.Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
resumingWebSocket
|
|
|
|
|
.Setup(static x => x.CloseAsync(It.IsAny<WebSocketCloseStatus>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
|
|
|
|
.Callback(() => resumingSocketState = WebSocketState.Closed)
|
|
|
|
|
.Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
var messagesToReceive = new Queue<(WebSocketReceiveResult, byte[])>();
|
|
|
|
|
var heartbeatInterval = 100;
|
|
|
|
|
var helloEvent = new HelloDiscordEvent()
|
|
|
|
|
{
|
|
|
|
|
Data = new()
|
|
|
|
|
{
|
|
|
|
|
HeartbeatInterval = heartbeatInterval,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var helloPayload = CreateEventPayload(helloEvent);
|
|
|
|
|
var helloResult = new WebSocketReceiveResult(helloPayload.Bytes.Length, WebSocketMessageType.Text, true);
|
|
|
|
|
messagesToReceive.Enqueue((helloResult, helloPayload.Bytes));
|
|
|
|
|
|
|
|
|
|
var sessionId = "session_id";
|
|
|
|
|
var resumeGatewayUrl = "wss://resume.discord.gg";
|
|
|
|
|
var readyEvent = new ReadyDiscordEvent()
|
|
|
|
|
{
|
|
|
|
|
Sequence = 1,
|
|
|
|
|
Data = new ReadyData()
|
|
|
|
|
{
|
|
|
|
|
SessionId = sessionId,
|
|
|
|
|
ResumeGatewayUrl = resumeGatewayUrl,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var readyPayload = CreateEventPayload(readyEvent);
|
|
|
|
|
var readyResult = new WebSocketReceiveResult(readyPayload.Bytes.Length, WebSocketMessageType.Text, true);
|
|
|
|
|
messagesToReceive.Enqueue((readyResult, readyPayload.Bytes));
|
|
|
|
|
|
|
|
|
|
SetupReceiveMessageSequence(initialWebSocket, messagesToReceive);
|
|
|
|
|
|
|
|
|
|
_mockWebSocketFactory
|
|
|
|
|
.SetupSequence(static x => x.Create())
|
|
|
|
|
.Returns(initialWebSocket.Object)
|
|
|
|
|
.Returns(resumingWebSocket.Object);
|
|
|
|
|
|
|
|
|
|
using var cts = new CancellationTokenSource();
|
|
|
|
|
|
2025-05-23 12:59:53 -05:00
|
|
|
await _discordGatewayClient.ConnectAsync(cts.Token);
|
2025-05-23 12:09:41 -05:00
|
|
|
await Task.Delay((int)(heartbeatInterval * 2.5));
|
2025-05-23 12:59:53 -05:00
|
|
|
await cts.CancelAsync();
|
2025-05-23 12:09:41 -05:00
|
|
|
|
|
|
|
|
var expectedHeartbeatEvent = new HeartbeatDiscordEvent(helloEvent.Sequence);
|
|
|
|
|
var expectedHeartbeatPayload = CreateEventPayload(expectedHeartbeatEvent);
|
|
|
|
|
|
|
|
|
|
initialWebSocket
|
|
|
|
|
.Verify(
|
|
|
|
|
x => x.SendAsync(
|
|
|
|
|
It.Is<ArraySegment<byte>>(b => expectedHeartbeatPayload.Bytes.SequenceEqual(b.Array!)),
|
|
|
|
|
It.Is<WebSocketMessageType>(m => m == WebSocketMessageType.Text),
|
|
|
|
|
true,
|
|
|
|
|
It.IsAny<CancellationToken>()
|
|
|
|
|
),
|
|
|
|
|
Times.AtMostOnce
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var expectedUri = new Uri($"{resumeGatewayUrl}/?v=10&encoding=json");
|
|
|
|
|
|
|
|
|
|
resumingWebSocket
|
|
|
|
|
.Verify(
|
|
|
|
|
x => x.ConnectAsync(
|
|
|
|
|
It.Is<Uri>(uri => uri.Equals(expectedUri)),
|
|
|
|
|
It.IsAny<CancellationToken>()
|
|
|
|
|
),
|
|
|
|
|
Times.Once
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2025-05-23 12:59:53 -05:00
|
|
|
public async Task ConnectAsync_OnceConnectedIfHeartbeatIsAcknowledged_ItShouldContinueSendingHeartbeats()
|
2025-05-23 12:09:41 -05:00
|
|
|
{
|
|
|
|
|
_mockDiscordRestClient
|
|
|
|
|
.Setup(static x => x.GetGatewayUrlAsync(It.IsAny<CancellationToken>()))
|
|
|
|
|
.ReturnsAsync("wss://gateway.discord.gg");
|
|
|
|
|
|
|
|
|
|
var mockWebSocket = new Mock<IWebSocket>();
|
|
|
|
|
|
|
|
|
|
var socketState = WebSocketState.Closed;
|
|
|
|
|
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Setup(static x => x.State)
|
|
|
|
|
.Returns(() => socketState);
|
|
|
|
|
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Setup(static x => x.ConnectAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
|
|
|
|
|
.Callback(() => socketState = WebSocketState.Open)
|
|
|
|
|
.Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
var messagesToReceive = new Queue<(WebSocketReceiveResult, byte[])>();
|
|
|
|
|
var heartbeatInterval = 100;
|
|
|
|
|
var helloEvent = new HelloDiscordEvent()
|
|
|
|
|
{
|
|
|
|
|
Data = new()
|
|
|
|
|
{
|
|
|
|
|
HeartbeatInterval = heartbeatInterval,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var helloPayload = CreateEventPayload(helloEvent);
|
|
|
|
|
var helloResult = new WebSocketReceiveResult(helloPayload.Bytes.Length, WebSocketMessageType.Text, true);
|
|
|
|
|
messagesToReceive.Enqueue((helloResult, helloPayload.Bytes));
|
|
|
|
|
|
|
|
|
|
var heartbeatAckEvent = new HeartbeatAckDiscordEvent();
|
|
|
|
|
var heartbeatAckPayload = CreateEventPayload(heartbeatAckEvent);
|
|
|
|
|
var heartbeatAckResult = new WebSocketReceiveResult(heartbeatAckPayload.Bytes.Length, WebSocketMessageType.Text, true);
|
|
|
|
|
messagesToReceive.Enqueue((heartbeatAckResult, heartbeatAckPayload.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((int)(heartbeatInterval * 2.5));
|
|
|
|
|
await cts.CancelAsync();
|
|
|
|
|
|
|
|
|
|
var expectedHeartbeatEvent = new HeartbeatDiscordEvent(helloEvent.Sequence);
|
|
|
|
|
var expectedHeartbeatPayload = CreateEventPayload(expectedHeartbeatEvent);
|
|
|
|
|
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Verify(
|
|
|
|
|
x => x.SendAsync(
|
|
|
|
|
It.Is<ArraySegment<byte>>(b => expectedHeartbeatPayload.Bytes.SequenceEqual(b.Array!)),
|
|
|
|
|
It.Is<WebSocketMessageType>(m => m == WebSocketMessageType.Text),
|
|
|
|
|
true,
|
|
|
|
|
It.IsAny<CancellationToken>()
|
|
|
|
|
),
|
|
|
|
|
Times.AtLeast(2)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-23 12:59:53 -05:00
|
|
|
[Fact]
|
|
|
|
|
public async Task ConnectAsync_OnceConnectedWhenHeartRequestReceived_ItShouldImmediatelySendHeartbeat()
|
|
|
|
|
{
|
|
|
|
|
_mockDiscordRestClient
|
|
|
|
|
.Setup(static x => x.GetGatewayUrlAsync(It.IsAny<CancellationToken>()))
|
|
|
|
|
.ReturnsAsync("wss://gateway.discord.gg");
|
|
|
|
|
|
|
|
|
|
var socketState = WebSocketState.Closed;
|
|
|
|
|
|
|
|
|
|
var mockWebSocket = new Mock<IWebSocket>();
|
|
|
|
|
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Setup(static x => x.State)
|
|
|
|
|
.Returns(() => socketState);
|
|
|
|
|
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Setup(static x => x.ConnectAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
|
|
|
|
|
.Callback(() => socketState = WebSocketState.Open)
|
|
|
|
|
.Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Setup(static x => x.CloseAsync(It.IsAny<WebSocketCloseStatus>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
|
|
|
|
.Callback(() => socketState = WebSocketState.Closed)
|
|
|
|
|
.Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
var messagesToReceive = new Queue<(WebSocketReceiveResult, byte[])>();
|
|
|
|
|
var heartbeatInterval = 100;
|
|
|
|
|
var helloEvent = new HelloDiscordEvent()
|
|
|
|
|
{
|
|
|
|
|
Data = new()
|
|
|
|
|
{
|
|
|
|
|
HeartbeatInterval = heartbeatInterval,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var helloPayload = CreateEventPayload(helloEvent);
|
|
|
|
|
var helloResult = new WebSocketReceiveResult(helloPayload.Bytes.Length, WebSocketMessageType.Text, true);
|
|
|
|
|
messagesToReceive.Enqueue((helloResult, helloPayload.Bytes));
|
|
|
|
|
|
|
|
|
|
var sessionId = "session_id";
|
|
|
|
|
var resumeGatewayUrl = "wss://resume.discord.gg";
|
|
|
|
|
var readyEvent = new ReadyDiscordEvent()
|
|
|
|
|
{
|
|
|
|
|
Data = new ReadyData()
|
|
|
|
|
{
|
|
|
|
|
SessionId = sessionId,
|
|
|
|
|
ResumeGatewayUrl = resumeGatewayUrl,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var readyPayload = CreateEventPayload(readyEvent);
|
|
|
|
|
var readyResult = new WebSocketReceiveResult(readyPayload.Bytes.Length, WebSocketMessageType.Text, true);
|
|
|
|
|
messagesToReceive.Enqueue((readyResult, readyPayload.Bytes));
|
|
|
|
|
|
|
|
|
|
var heartbeatAckEvent = new HeartbeatAckDiscordEvent();
|
|
|
|
|
var heartbeatAckPayload = CreateEventPayload(heartbeatAckEvent);
|
|
|
|
|
var heartbeatAckResult = new WebSocketReceiveResult(heartbeatAckPayload.Bytes.Length, WebSocketMessageType.Text, true);
|
|
|
|
|
messagesToReceive.Enqueue((heartbeatAckResult, heartbeatAckPayload.Bytes));
|
|
|
|
|
|
|
|
|
|
var heartbeatRequestEvent = new HeartbeatDiscordEvent(readyEvent.Sequence);
|
|
|
|
|
var heartbeatRequestPayload = CreateEventPayload(heartbeatRequestEvent);
|
|
|
|
|
var heartbeatRequestResult = new WebSocketReceiveResult(heartbeatRequestPayload.Bytes.Length, WebSocketMessageType.Text, true);
|
|
|
|
|
messagesToReceive.Enqueue((heartbeatRequestResult, heartbeatRequestPayload.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(heartbeatInterval * 2);
|
|
|
|
|
await cts.CancelAsync();
|
|
|
|
|
|
|
|
|
|
var expectedHeartbeatEvent = new HeartbeatDiscordEvent(helloEvent.Sequence);
|
|
|
|
|
var expectedHeartbeatPayload = CreateEventPayload(expectedHeartbeatEvent);
|
|
|
|
|
|
|
|
|
|
mockWebSocket
|
|
|
|
|
.Verify(
|
|
|
|
|
x => x.SendAsync(
|
|
|
|
|
It.Is<ArraySegment<byte>>(b => expectedHeartbeatPayload.Bytes.SequenceEqual(b.Array!)),
|
|
|
|
|
It.Is<WebSocketMessageType>(m => m == WebSocketMessageType.Text),
|
|
|
|
|
true,
|
|
|
|
|
It.IsAny<CancellationToken>()
|
|
|
|
|
),
|
|
|
|
|
Times.AtLeast(2)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public Task ConnectAsync_OnceConnectedWhenUnReconnectableCloseStatusIsReceived_ItShouldDisconnectAndNotTryToReconnect()
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public Task ConnectAsync_OnceConnectedWhenReconnectableButNonResumableCloseStatusIsReceived_ItShouldReconnect()
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public Task ConnectAsync_OnceConnectedWhenResumableCloseStatusIsReceived_ItShouldResume()
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public Task ConnectAsync_OnceConnectedWhenReconnectEventIsReceivedThatIndicatesClientCanResume_ItShouldResume()
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public Task ConnectAsync_OnceConnectedWhenReconnectEventIsReceivedThatIndicatesClientCannotResume_ItShouldReconnect()
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|