about summary refs log tree commit diff
path: root/Tests/LibMatrix.Tests/Tests/RoomTests/SpaceTests.cs
blob: 148b5fe88957607dedf5e094ccfaeae187da828c (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
using System.Diagnostics;
using System.Text;
using ArcaneLibs.Extensions;
using LibMatrix.EventTypes.Spec.State;
using LibMatrix.Homeservers;
using LibMatrix.Responses;
using LibMatrix.RoomTypes;
using LibMatrix.Services;
using LibMatrix.Tests.Abstractions;
using LibMatrix.Tests.Fixtures;
using Xunit.Abstractions;
using Xunit.Microsoft.DependencyInjection.Abstracts;

namespace LibMatrix.Tests.Tests;

public class SpaceTests : TestBed<TestFixture> {
    private readonly HomeserverAbstraction _hsAbstraction;

    public SpaceTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : base(testOutputHelper, fixture) {
        _hsAbstraction = _fixture.GetService<HomeserverAbstraction>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(HomeserverAbstraction)}");
    }

    [Fact]
    public async Task AddChildAsync() {
        var hs = await _hsAbstraction.GetConfiguredHomeserver();
        var crq = new CreateRoomRequest() {
            Name = "Test space"
        };
        crq.CreationContent["type"] = SpaceRoom.TypeName;
        var space = (await hs.CreateRoom(crq)).AsSpace;

        var child = await hs.CreateRoom(new CreateRoomRequest() {
            Name = "Test child"
        });

        await space.AddChildAsync(child);

        //validate children
        var children = space.GetChildrenAsync().ToBlockingEnumerable().ToList();
        Assert.NotNull(children);
        Assert.NotEmpty(children);
        Assert.Single(children, x => x.RoomId == child.RoomId);
    }

    [Fact]
    public async Task AddChildByIdAsync() {
        var hs = await _hsAbstraction.GetConfiguredHomeserver();
        var crq = new CreateRoomRequest() {
            Name = "Test space"
        };
        crq.CreationContent["type"] = SpaceRoom.TypeName;
        var space = (await hs.CreateRoom(crq)).AsSpace;

        var child = await hs.CreateRoom(new CreateRoomRequest() {
            Name = "Test child"
        });

        await space.AddChildByIdAsync(child.RoomId);
        
        //validate children
        var children = space.GetChildrenAsync().ToBlockingEnumerable().ToList();
        Assert.NotNull(children);
        Assert.NotEmpty(children);
        Assert.Single(children, x => x.RoomId == child.RoomId);
    }
    
    [Fact]
    public async Task GetChildrenAsync() {
        var hs = await _hsAbstraction.GetConfiguredHomeserver();
        var expectedChildren = Enumerable.Range(0, 10).Select(async _ => {
            var room = await hs.CreateRoom(new CreateRoomRequest() {
                Name = "Test child"
            });
            return room;
        }).ToAsyncEnumerable().ToBlockingEnumerable().ToList();
        
        var crq = new CreateRoomRequest() {
            Name = "Test space",
            InitialState = expectedChildren.Select(c => new StateEvent() {
                Type = "m.space.child",
                StateKey = c.RoomId,
                TypedContent = new SpaceChildEventContent() {
                    Via = new List<string> {
                        c.RoomId.Split(":")[1]
                    }
                }
            }).ToList()
        };
        crq.CreationContent["type"] = SpaceRoom.TypeName;
        var space = (await hs.CreateRoom(crq)).AsSpace;

        var children = space.GetChildrenAsync().ToBlockingEnumerable().ToList();
        Assert.NotNull(children);
        Assert.NotEmpty(children);
        Assert.Equal(expectedChildren.Count, children.Count);
        foreach (var expectedChild in expectedChildren)
        {
            Assert.Single(children, x => x.RoomId == expectedChild.RoomId);
        }
    }
}