about summary refs log tree commit diff
path: root/Tests/LibMatrix.Tests/Abstractions/HomeserverAbstraction.cs
blob: 13b5c1be18346c23276a7e4cdee4510bcd4eda00 (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
using ArcaneLibs.Extensions;
using LibMatrix.Homeservers;
using LibMatrix.Responses;
using LibMatrix.Services;
using Microsoft.Extensions.Logging.Abstractions;

namespace LibMatrix.Tests.Abstractions;

public static class HomeserverAbstraction {
    private static HomeserverResolverService _hsResolver = new HomeserverResolverService(NullLogger<HomeserverResolverService>.Instance);
    private static HomeserverProviderService _hsProvider = new HomeserverProviderService(NullLogger<HomeserverProviderService>.Instance, _hsResolver);

    public static async Task<AuthenticatedHomeserverGeneric> GetHomeserver() {
        var rhs = await _hsProvider.GetRemoteHomeserver("matrixunittests.rory.gay");
        var username = "@f1a2d2d6-1924-421b-91d0-893b347b2a49:matrixunittests.rory.gay";
        var 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 _hsProvider.GetAuthenticatedWithToken(reg.Homeserver, reg.AccessToken);
        //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 = await _hsProvider.GetRemoteHomeserver("matrixunittests.rory.gay");
        var reg = await rhs.RegisterAsync(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Unit tests!");
        var hs = await _hsProvider.GetAuthenticatedWithToken(reg.Homeserver, reg.AccessToken);

        // 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 createRandomUserTasks = Enumerable
            .Range(0, count)
            .Select(_ => GetRandomHomeserver()).ToAsyncEnumerable();
        await foreach (var hs in createRandomUserTasks) yield return hs;
    }
}