diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/AutomodTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/AutomodTests.cs index b92a014b5..d52df2beb 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/AutomodTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/AutomodTests.cs @@ -91,8 +91,7 @@ public class AutomodTests(ITestOutputHelper testOutputHelper, TestFixture fixtur AutomodTriggerType.GuildPolicy => new AutomodRuleCreateRequest.GuildPolicy(), _ => throw new ArgumentOutOfRangeException(nameof(triggerType), triggerType, null) }; - - // TriggerType = triggerType, + automodRule.Enabled = true; automodRule.EventType = eventType; automodRule.Actions = actions.CastOrToList(); diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/GatewayTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/GatewayTests.cs index e02f26c4b..4a13b11c6 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/GatewayTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/GatewayTests.cs @@ -21,8 +21,7 @@ public class GatewayTests(ITestOutputHelper testOutputHelper, TestFixture fixtur public async ValueTask InitializeAsync() { testOutputHelper.WriteLine("Running InitializeAsync"); - // All these tests can share a single client - Client = await _userAbstraction.GetSharedUser(); + Client = await _userAbstraction.GetFreshUser(true); } [Fact] diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/PresenceTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/PresenceTests.cs index 603bae7b8..15debd756 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/PresenceTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/PresenceTests.cs @@ -4,6 +4,7 @@ using System.Net.WebSockets; using System.Text.Json; using System.Text.Json.Nodes; using ArcaneLibs.Extensions; +using Microsoft.Extensions.Logging; using Spacebar.Models.Gateway; using Spacebar.Models.Generic; using Spacebar.Sdk.Core; @@ -15,40 +16,41 @@ using Xunit.Microsoft.DependencyInjection.Abstracts; namespace Spacebar.Tests.Tests; -public class PresenceTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed(testOutputHelper, fixture), IAsyncLifetime -{ +public class PresenceTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed(testOutputHelper, fixture), IAsyncLifetime { private readonly Config _config = fixture.GetRequiredService(testOutputHelper); private readonly UserAbstraction _userAbstraction = fixture.GetRequiredService(testOutputHelper); private static AuthenticatedSpacebarClient Client { get; set; } = null!; - public async ValueTask InitializeAsync() - { + public async ValueTask InitializeAsync() { testOutputHelper.WriteLine("Running InitializeAsync"); - // All these tests can share a single client - Client = await _userAbstraction.GetSharedUser(); + Client = await _userAbstraction.GetFreshUser(true); } [Fact] - public async Task CanSendCustomPresence() - { - Client.Gateway.OnceGatewayMessage.Add(async payload => - { - if (payload is { Opcode: GatewayOpcode.S2CDispatch, DispatchEventType: "READY" }) - { + public async Task CanSendCustomPresence() { + var resolved = false; + var resolvedTask = Task.Run(async () => { + // ReSharper disable once LoopVariableIsNeverChangedInsideLoop + while (!resolved) { + testOutputHelper.WriteLine("Waiting for lock to resolve..."); + await Task.Delay(100); + } + }, TestContext.Current.CancellationToken); + Client.Gateway.OnceGatewayMessage.Add(async payload => { + if (payload is { Opcode: GatewayOpcode.S2CDispatch, DispatchEventType: "READY" }) { _testOutputHelper.WriteLine("Sending presence..."); - var presencePayload = new JsonObject() - { + var presencePayload = new JsonObject() { { "status", "online" }, { "since", 0 }, - { "afk", false }, - { "activities", new JsonArray() { + { "afk", false }, { + "activities", new JsonArray() { new JsonObject() { { "name", "Custom Status" }, { "type", 4 }, - { "state", "meowmeow custom status meow meow" }, - { "timestamps", new JsonObject() { + { "state", "meowmeow custom status meow meow" }, { + "timestamps", new JsonObject() { { "end", 1788544741128 } } }, @@ -59,12 +61,12 @@ public class PresenceTests(ITestOutputHelper testOutputHelper, TestFixture fixtu } }; - await Client.Gateway.RawClientWebSocket.SendAsync(JsonSerializer.SerializeToUtf8Bytes(new GatewayPayload() - { + await Client.Gateway.RawClientWebSocket.SendAsync(JsonSerializer.SerializeToUtf8Bytes(new GatewayPayload() { Opcode = GatewayOpcode.C2SPresenceUpdate, EventData = presencePayload.ToJsonNode().AsObject() }), WebSocketMessageType.Text, WebSocketMessageFlags.EndOfMessage, CancellationToken.None); _testOutputHelper.WriteLine("Sent presence payload..."); + resolved = true; return true; } @@ -73,13 +75,15 @@ public class PresenceTests(ITestOutputHelper testOutputHelper, TestFixture fixtu }); // Client.Gateway.TraceGatewayMessages = true; await Client.Gateway.Connect(); - try - { - await Client.Gateway.Start(); - } - catch (Exception e) - { - Assert.True(Client.Gateway.RawClientWebSocket.State == WebSocketState.Open, $"Client.Gateway.RawClientWebSocket.State is {Client.Gateway.RawClientWebSocket.State}: {Client.Gateway.RawClientWebSocket.CloseStatus}/{Client.Gateway.RawClientWebSocket.CloseStatusDescription}"); - } + _ = Client.Gateway.Start().ContinueWith(_ => { + Assert.True(Client.Gateway.RawClientWebSocket.State == WebSocketState.Open, + $"Client.Gateway.RawClientWebSocket.State is {Client.Gateway.RawClientWebSocket.State}: {Client.Gateway.RawClientWebSocket.CloseStatus}/{Client.Gateway.RawClientWebSocket.CloseStatusDescription}"); + }, TestContext.Current.CancellationToken); + + await resolvedTask; + _testOutputHelper.WriteLine("Presence lock resolved..."); + await Task.Delay(250, TestContext.Current.CancellationToken); + Assert.True(Client.Gateway.RawClientWebSocket.State == WebSocketState.Open, + $"Client.Gateway.RawClientWebSocket.State is {Client.Gateway.RawClientWebSocket.State}: {Client.Gateway.RawClientWebSocket.CloseStatus}/{Client.Gateway.RawClientWebSocket.CloseStatusDescription}"); } } \ No newline at end of file