Clean up tests a little

This commit is contained in:
Rory&
2026-06-09 00:35:42 +02:00
parent 0573c394cf
commit 51d5b65073
10 changed files with 96 additions and 132 deletions
@@ -23,4 +23,9 @@ public class UserAbstraction(Config _config, SpacebarClientProviderService _clie
return client;
}
private static AuthenticatedSpacebarClient? _authenticatedSpacebarClient;
public async Task<AuthenticatedSpacebarClient> GetSharedUser() {
return _authenticatedSpacebarClient ??= await GetFreshUser();
}
}
@@ -8,4 +8,5 @@ public class Config {
}
public string TestInstance { get; set; }
public int RegisterConcurrentCount { get; set; }
}
@@ -0,0 +1,11 @@
using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Extensions;
public static class TestBedFixtureExtensions {
extension(TestBedFixture tbf) {
public T GetRequiredService<T>(ITestOutputHelper toh) {
return tbf.GetService<T>(toh) ?? throw new InvalidOperationException($"Failed to get service: {typeof(T).FullName}");
}
}
}
@@ -7,18 +7,14 @@ using Spacebar.Sdk.Core;
using Spacebar.Tests.Extensions;
using Spacebar.Tests.Fixtures;
using Xunit.Microsoft.DependencyInjection.Abstracts;
using Xunit.Sdk;
namespace Spacebar.Tests.Tests;
public class AuthenticationTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) {
private readonly Config _config = fixture.GetService<Config>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}");
private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService<SpacebarClientWellKnownResolverService>(testOutputHelper) ??
throw new InvalidOperationException(
$"Failed to get {nameof(SpacebarClientWellKnownResolverService)}");
private readonly SpacebarClientProviderService _clientProvider = fixture.GetService<SpacebarClientProviderService>(testOutputHelper) ??
throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
private readonly Config _config = fixture.GetRequiredService<Config>(testOutputHelper);
private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService<SpacebarClientWellKnownResolverService>(testOutputHelper);
private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService<SpacebarClientProviderService>(testOutputHelper);
[Fact]
public async Task RegisterUser() {
@@ -29,11 +25,13 @@ public class AuthenticationTests(ITestOutputHelper testOutputHelper, TestFixture
DateOfBirth = new(),
Consent = true
});
await Assert.HttpSuccess(res);
}
[Fact]
public async Task ConcurrentRegister50Users() {
var tasks = Enumerable.Range(0, 50).Select(async _ => {
public async Task RegisterUsersConcurrent() {
testOutputHelper.WriteLine($"Registering {_config.RegisterConcurrentCount} users concurrently...");
var tasks = Enumerable.Range(0, _config.RegisterConcurrentCount).Select(async _ => {
var sw = Stopwatch.StartNew();
var rr = new RegisterRequest() {
Email = $"{Guid.NewGuid().ToString()}@{Guid.NewGuid().ToString()}.tld",
@@ -6,10 +6,8 @@ using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Tests;
public class BasicWellKnownTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) {
private readonly Config _config = fixture.GetService<Config>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}");
private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService<SpacebarClientWellKnownResolverService>(testOutputHelper) ??
throw new InvalidOperationException($"Failed to get {nameof(Config)}");
private readonly Config _config = fixture.GetRequiredService<Config>(testOutputHelper);
private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService<SpacebarClientWellKnownResolverService>(testOutputHelper);
[Fact]
public async Task ValidateTestConfig() {
@@ -8,60 +8,50 @@ using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Tests;
public class ChannelTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) {
private readonly Config _config = fixture.GetService<Config>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}");
public class ChannelTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture), IAsyncLifetime {
private readonly Config _config = fixture.GetRequiredService<Config>(testOutputHelper);
private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService<SpacebarClientWellKnownResolverService>(testOutputHelper);
private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService<SpacebarClientProviderService>(testOutputHelper);
private readonly UserAbstraction _userAbstraction = fixture.GetRequiredService<UserAbstraction>(testOutputHelper);
private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService<SpacebarClientWellKnownResolverService>(testOutputHelper) ??
throw new InvalidOperationException(
$"Failed to get {nameof(SpacebarClientWellKnownResolverService)}");
private static AuthenticatedSpacebarClient Client { get; set; } = null!;
private static Guild? Guild { get; set; }
private readonly SpacebarClientProviderService _clientProvider = fixture.GetService<SpacebarClientProviderService>(testOutputHelper) ??
throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
private readonly UserAbstraction _userAbstraction = fixture.GetService<UserAbstraction>(testOutputHelper) ??
throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
[Fact]
public async Task CreateChannel() {
var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
var guild = await client.CreateGuild(new() {
public async ValueTask InitializeAsync() {
testOutputHelper.WriteLine("Running InitializeAsync");
// All these tests can share a single client
Client = await _userAbstraction.GetSharedUser();
// ...and a guild
Guild ??= await Client.CreateGuild(new() {
Name = "Test guild"
});
Assert.Equal("Test guild", guild.Name);
}
var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() {
[Fact]
public async Task CreateChannel() {
Assert.Equal("Test guild", Guild!.Name);
var channel = await Client!.GetGuild(Guild.Id).CreateChannelAsync(new() {
Name = "test",
Type = 0
});
Assert.Equal("test", channel.Name);
}
[Fact]
public async Task GetChannel() {
var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
var guild = await client.CreateGuild(new() {
Name = "Test guild"
});
Assert.Equal("Test guild", guild.Name);
// await Task.Delay(1000, TestContext.Current.CancellationToken); // TODO: unflake
var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() {
Assert.Equal("Test guild", Guild!.Name);
var channel = await Client!.GetGuild(Guild.Id).CreateChannelAsync(new() {
Name = "test",
Type = 0
});
Assert.Equal("test", channel.Name);
// await Task.Delay(1000, TestContext.Current.CancellationToken); // TODO: unflake
var res = await client.ApiHttpClient.GetAsync("channels/" + channel.Id, TestContext.Current.CancellationToken);
var res = await Client.ApiHttpClient.GetAsync("channels/" + channel.Id, TestContext.Current.CancellationToken);
await Assert.HttpSuccess(res);
var channelResp = await res.Content.ReadFromJsonAsync<Channel>(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(channel.Name, channelResp!.Name);
Assert.Equal(channel.Id, channelResp!.Id);
}
}
@@ -7,21 +7,14 @@ using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Tests;
public class GuildTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) {
private readonly Config _config = fixture.GetService<Config>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}");
private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService<SpacebarClientWellKnownResolverService>(testOutputHelper) ??
throw new InvalidOperationException(
$"Failed to get {nameof(SpacebarClientWellKnownResolverService)}");
private readonly SpacebarClientProviderService _clientProvider = fixture.GetService<SpacebarClientProviderService>(testOutputHelper) ??
throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
private readonly UserAbstraction _userAbstraction = fixture.GetService<UserAbstraction>(testOutputHelper) ??
throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
private readonly Config _config = fixture.GetRequiredService<Config>(testOutputHelper);
private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService<SpacebarClientWellKnownResolverService>(testOutputHelper);
private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService<SpacebarClientProviderService>(testOutputHelper);
private readonly UserAbstraction _userAbstraction = fixture.GetRequiredService<UserAbstraction>(testOutputHelper);
[Fact]
public async Task CreateGuild() {
var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
var client = await _userAbstraction.GetSharedUser();
var guild = await client.CreateGuild(new() {
Name = "Test guild"
});
@@ -31,7 +24,7 @@ public class GuildTests(ITestOutputHelper testOutputHelper, TestFixture fixture)
[Fact]
public async Task GetChannels() {
var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
var client = await _userAbstraction.GetSharedUser();
var guild = await client.CreateGuild(new() {
Name = "Test guild"
});
@@ -6,7 +6,7 @@ using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Tests.Meta;
public class UserAbstractionTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) {
private readonly UserAbstraction _config = fixture.GetService<UserAbstraction>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(UserAbstraction)}");
private readonly UserAbstraction _config = fixture.GetRequiredService<UserAbstraction>(testOutputHelper);
[Fact]
public async Task CanGetUser() {
@@ -7,61 +7,58 @@ using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Tests;
public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) {
private readonly Config _config = fixture.GetService<Config>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}");
public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture), IAsyncLifetime {
private readonly Config _config = fixture.GetRequiredService<Config>(testOutputHelper);
private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService<SpacebarClientWellKnownResolverService>(testOutputHelper);
private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService<SpacebarClientProviderService>(testOutputHelper);
private readonly UserAbstraction _userAbstraction = fixture.GetRequiredService<UserAbstraction>(testOutputHelper);
private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService<SpacebarClientWellKnownResolverService>(testOutputHelper) ??
throw new InvalidOperationException(
$"Failed to get {nameof(SpacebarClientWellKnownResolverService)}");
private static AuthenticatedSpacebarClient Client = null!;
private static SpacebarClientGuild? Guild;
private readonly SpacebarClientProviderService _clientProvider = fixture.GetService<SpacebarClientProviderService>(testOutputHelper) ??
throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
private static SpacebarClientChannel? Channel = null!;
public async ValueTask InitializeAsync() {
Client = await _userAbstraction.GetSharedUser();
if (Guild is null)
await Client.CreateGuild(new() {
Name = "Test guild"
}).ContinueWith(g => {
Assert.Equal("Test guild", g.Result.Name);
Guild = Client.GetGuild(g.Result.Id);
});
if (Channel is null)
await Guild!.CreateChannelAsync(new() {
Name = "test",
Type = 0
}).ContinueWith(c => {
Assert.Equal("test", c.Result.Name);
Channel = Client.GetChannel(c.Result.Id);
});
}
private readonly UserAbstraction _userAbstraction = fixture.GetService<UserAbstraction>(testOutputHelper) ??
throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}");
[Fact]
public async Task CreateWebhook() {
var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
var guild = await client.CreateGuild(new() {
Name = "Test guild"
});
Assert.Equal("Test guild", guild.Name);
var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() {
Name = "test",
Type = 0
});
Assert.Equal("test", channel.Name);
var cChannel = client.GetChannel(channel.Id);
var wh = await cChannel.CreateWebhookAsync(new() {
var wh = await Channel!.CreateWebhookAsync(new() {
Name = "meow"
});
Assert.Equal("meow", wh.Name);
Assert.StringNotNullOrWhitespace(wh.Url);
}
[Fact]
public async Task CreateMultipleWebhooks() {
var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
var guild = await client.CreateGuild(new() {
Name = "Test guild"
});
Assert.Equal("Test guild", guild.Name);
var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() {
var channel = await Guild!.CreateChannelAsync(new() {
Name = "test",
Type = 0
});
Assert.Equal("test", channel.Name);
var cChannel = client.GetChannel(channel.Id);
var cChannel = Client.GetChannel(channel.Id);
var count = Random.Shared.Next(10);
testOutputHelper.WriteLine($"Creating {count} webhooks...");
@@ -76,22 +73,7 @@ public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixtur
[Fact]
public async Task SendWebhookMessageWithWait() {
var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
var guild = await client.CreateGuild(new() {
Name = "Test guild"
});
Assert.Equal("Test guild", guild.Name);
var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() {
Name = "test",
Type = 0
});
Assert.Equal("test", channel.Name);
var cChannel = client.GetChannel(channel.Id);
var wh = await cChannel.CreateWebhookAsync(new() {
var wh = await Channel!.CreateWebhookAsync(new() {
Name = "meow"
});
@@ -102,25 +84,10 @@ public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixtur
{ "content", "meow" }
});
}
[Fact]
public async Task SendWebhookMessage() {
var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true);
var guild = await client.CreateGuild(new() {
Name = "Test guild"
});
Assert.Equal("Test guild", guild.Name);
var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() {
Name = "test",
Type = 0
});
Assert.Equal("test", channel.Name);
var cChannel = client.GetChannel(channel.Id);
var wh = await cChannel.CreateWebhookAsync(new() {
var wh = await Channel!.CreateWebhookAsync(new() {
Name = "meow"
});
@@ -1,5 +1,6 @@
{
"Configuration": {
"TestInstance": "http://localhost:3001"
"TestInstance": "http://localhost:3001",
"RegisterConcurrentCount": 15
}
}