about summary refs log tree commit diff
path: root/Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs
blob: 76b8c8c0a41f75943e7363edfed92d82bbc46700 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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 crq = new CreateRoomRequest() {
            Name = "LibMatrix Test Room",
            // Visibility = CreateRoomVisibility.Public,
            RoomAliasName = Guid.NewGuid().ToString()
        };
        crq.InitialState ??= new();
        crq.InitialState.Add(new StateEvent() {
            Type = "m.room.topic",
            StateKey = "",
            TypedContent = new RoomTopicEventContent() {
                Topic = "LibMatrix Test Room " + DateTime.Now.ToString("O")
            }
        });
        crq.InitialState.Add(new StateEvent() {
            Type = "m.room.name",
            StateKey = "",
            TypedContent = new RoomNameEventContent() {
                Name = "LibMatrix Test Room " + DateTime.Now.ToString("O")
            }
        });
        crq.InitialState.Add(new StateEvent() {
            Type = "m.room.avatar",
            StateKey = "",
            TypedContent = new RoomAvatarEventContent() {
                Url = "mxc://conduit.rory.gay/r9KiT0f9eQbv8pv4RxwBZFuzhfKjGWHx"
            }
        });
        crq.InitialState.Add(new StateEvent() {
            Type = "m.room.aliases",
            StateKey = "",
            TypedContent = new RoomAliasEventContent() {
                Aliases = Enumerable
                    .Range(0, 100)
                    .Select(_ => $"#{Guid.NewGuid()}:matrixunittests.rory.gay").ToList()
            }
        });
        var testRoom = await hs.CreateRoom(crq);

        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;
    }
}