about summary refs log tree commit diff
path: root/Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-29 19:38:00 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-29 19:38:00 +0200
commit46df5b8e335754f1582fc4d41d9546808ed8ee66 (patch)
tree29fed832bed495f1bd233c37275cb560c19f34cf /Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs
parentAdd more stuff, add unit tests (diff)
downloadLibMatrix-46df5b8e335754f1582fc4d41d9546808ed8ee66.tar.xz
Unit tests, small refactors
Diffstat (limited to '')
-rw-r--r--Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs76
1 files changed, 76 insertions, 0 deletions
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;
+    }
+}