summaryrefslogtreecommitdiffstats
path: root/nixos/tests/matrix/continuwuity.nix
blob: f84d0bd39c2090259cff24b87ab6c1ed147fab68 (plain)
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
{ lib, ... }:
let
  name = "continuwuity";
  user = "alice";
  pass = "my-secret-password";
in
{
  inherit name;

  nodes = {
    continuwuity = {
      services.matrix-continuwuity = {
        enable = true;
        settings.global = {
          server_name = name;
          address = [ "0.0.0.0" ];
          admin_execute = [ "users create ${user} ${pass}" ];
        };
        extraEnvironment.RUST_BACKTRACE = "yes";
      };
      networking.firewall.allowedTCPPorts = [ 6167 ];
    };

    client =
      { pkgs, ... }:
      {
        environment.systemPackages = [
          (pkgs.writers.writePython3Bin "do_test" { libraries = [ pkgs.python3Packages.mautrix ]; } ''
            import asyncio

            from mautrix.client import Client
            from mautrix.types import EventType, RoomFilter, Filter


            async def main() -> None:
                # Connect to continuwuity
                client = Client(
                    mxid="@${user}:${name}",
                    base_url="http://continuwuity:6167",
                )

                # Log in as user alice
                await client.login(password="${pass}")

                # Create a new room
                room_id = await client.create_room()
                print("Created room:", room_id)

                # Join the room
                await client.join_room_by_id(room_id)
                print("Joined room")

                # Send a message to the room
                received = asyncio.Event()
                msg = "Hello continuwuity!"

                async def on_message(evt):
                    if (
                        evt.room_id != room_id
                        or evt.sender != client.mxid
                        or evt.type != EventType.ROOM_MESSAGE
                    ):
                        return

                    assert evt.content.body == msg
                    received.set()

                client.add_event_handler(EventType.ROOM_MESSAGE, on_message)
                sync_task = client.start(Filter(room=RoomFilter(rooms=[room_id])))

                await client.send_text(room_id, msg)

                # Sync until message is received
                await asyncio.wait_for(received.wait(), timeout=30)

                # Leave the room
                await client.leave_room(room_id)
                print("Left room")

                # Close the client
                client.stop()
                await sync_task


            if __name__ == "__main__":
                asyncio.run(main())
          '')
        ];
      };
  };

  testScript = ''
    start_all()

    with subtest("start continuwuity"):
          continuwuity.wait_for_unit("continuwuity.service")
          continuwuity.wait_for_open_port(6167)

    with subtest("ensure messages can be exchanged"):
          client.succeed("do_test >&2")
  '';

  meta.maintainers = with lib.maintainers; [
    bartoostveen
    nyabinary
    snaki
  ];
}