diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-09-29 19:38:00 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-09-29 19:38:00 +0200 |
commit | 46df5b8e335754f1582fc4d41d9546808ed8ee66 (patch) | |
tree | 29fed832bed495f1bd233c37275cb560c19f34cf /Tests/LibMatrix.Tests/Abstractions | |
parent | Add more stuff, add unit tests (diff) | |
download | LibMatrix-46df5b8e335754f1582fc4d41d9546808ed8ee66.tar.xz |
Unit tests, small refactors
Diffstat (limited to 'Tests/LibMatrix.Tests/Abstractions')
-rw-r--r-- | Tests/LibMatrix.Tests/Abstractions/HomeserverAbstraction.cs | 73 | ||||
-rw-r--r-- | Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs | 76 |
2 files changed, 149 insertions, 0 deletions
diff --git a/Tests/LibMatrix.Tests/Abstractions/HomeserverAbstraction.cs b/Tests/LibMatrix.Tests/Abstractions/HomeserverAbstraction.cs new file mode 100644 index 0000000..abd3e99 --- /dev/null +++ b/Tests/LibMatrix.Tests/Abstractions/HomeserverAbstraction.cs @@ -0,0 +1,73 @@ +using ArcaneLibs.Extensions; +using LibMatrix.Homeservers; +using LibMatrix.Responses; + +namespace LibMatrix.Tests.Abstractions; + +public static class HomeserverAbstraction { + public static async Task<AuthenticatedHomeserverGeneric> GetHomeserver() { + var rhs = new RemoteHomeServer("https://matrixunittests.rory.gay"); + // string username = Guid.NewGuid().ToString(); + // string password = Guid.NewGuid().ToString(); + string username = "@f1a2d2d6-1924-421b-91d0-893b347b2a49:matrixunittests.rory.gay"; + string password = "d6d782d6-8bc9-4fac-9cd8-78e101b4298b"; + LoginResponse reg; + try { + reg = await rhs.LoginAsync(username, password); + } + catch (MatrixException e) { + if (e.ErrorCode == "M_FORBIDDEN") { + await rhs.RegisterAsync(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Unit tests!"); + reg = await rhs.RegisterAsync(username, password, "Unit tests!"); + } + else throw new Exception("Failed to register", e); + } + + var hs = await reg.GetAuthenticatedHomeserver("https://matrixunittests.rory.gay"); + + var rooms = await hs.GetJoinedRooms(); + + var disbandRoomTasks = rooms.Select(async room => { + // await room.DisbandRoomAsync(); + await room.LeaveAsync(); + await room.ForgetAsync(); + return room; + }).ToList(); + await Task.WhenAll(disbandRoomTasks); + + // foreach (var room in rooms) { + // // await room.DisbandRoomAsync(); + // await room.LeaveAsync(); + // await room.ForgetAsync(); + // } + + return hs; + } + + public static async Task<AuthenticatedHomeserverGeneric> GetRandomHomeserver() { + var rhs = new RemoteHomeServer("https://matrixunittests.rory.gay"); + LoginResponse reg = await rhs.RegisterAsync(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Unit tests!"); + var hs = await reg.GetAuthenticatedHomeserver("https://matrixunittests.rory.gay"); + + var rooms = await hs.GetJoinedRooms(); + + var disbandRoomTasks = rooms.Select(async room => { + // await room.DisbandRoomAsync(); + await room.LeaveAsync(); + await room.ForgetAsync(); + return room; + }).ToList(); + await Task.WhenAll(disbandRoomTasks); + + return hs; + } + + public static async IAsyncEnumerable<AuthenticatedHomeserverGeneric> GetRandomHomeservers(int count = 1) { + var createSpaceTasks = Enumerable + .Range(0, count) + .Select(_ => GetRandomHomeserver()).ToAsyncEnumerable(); + await foreach (var hs in createSpaceTasks) { + yield return hs; + } + } +} diff --git a/Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs b/Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs new file mode 100644 index 0000000..44c35da --- /dev/null +++ b/Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs @@ -0,0 +1,76 @@ +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.Homeservers; +using LibMatrix.Responses; +using LibMatrix.RoomTypes; + +namespace LibMatrix.Tests.Abstractions; + +public static class RoomAbstraction { + public static async Task<GenericRoom> GetTestRoom(AuthenticatedHomeserverGeneric hs) { + var testRoom = await hs.CreateRoom(new CreateRoomRequest() { + Name = "LibMatrix Test Room", + // Visibility = CreateRoomVisibility.Public, + RoomAliasName = Guid.NewGuid().ToString() + }); + + await testRoom.SendStateEventAsync("gay.rory.libmatrix.unit_test_room", new()); + + return testRoom; + } + + private static SemaphoreSlim _spaceSemaphore = null!; + + public static async Task<SpaceRoom> GetTestSpace(AuthenticatedHomeserverGeneric hs, int roomCount = 100, bool addSpaces = false, int spaceSizeReduction = 10) { + _spaceSemaphore ??= new(roomCount / spaceSizeReduction, roomCount / spaceSizeReduction); + var crq = new CreateRoomRequest() { + Name = $"LibMatrix Test Space ({roomCount} children)", + // Visibility = CreateRoomVisibility.Public, + RoomAliasName = Guid.NewGuid().ToString(), + InitialState = new() + }; + crq._creationContentBaseType.Type = "m.space"; + + + var createRoomTasks = Enumerable.Range(0, roomCount) + .Select(_ => hs.CreateRoom(new CreateRoomRequest() { + Name = $"LibMatrix Test Room {Guid.NewGuid()}", + // Visibility = CreateRoomVisibility.Public, + RoomAliasName = Guid.NewGuid().ToString() + })).ToAsyncEnumerable(); + + await foreach (var room in createRoomTasks) { + crq.InitialState.Add(new() { + Type = "m.space.child", + StateKey = room.RoomId, + TypedContent = new SpaceChildEventContent() { + Via = new() { + room.RoomId.Split(":")[1] + } + } + }); + } + + if (addSpaces) { + for (int i = 0; i < roomCount; i++) { + var space = await GetTestSpace(hs, roomCount - spaceSizeReduction, true, spaceSizeReduction); + crq.InitialState.Add(new() { + Type = "m.space.child", + StateKey = space.RoomId, + TypedContent = new SpaceChildEventContent() { + Via = new() { + space.RoomId.Split(":")[1] + } + } + }); + } + } + + var testSpace = (await hs.CreateRoom(crq)).AsSpace; + + await testSpace.SendStateEventAsync("gay.rory.libmatrix.unit_test_room", new()); + + // _spaceSemaphore.Release(); + return testSpace; + } +} |