summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/matrix/test/test_matrixCommands.js
blob: e65ff195e792eb79db4457679d2ecd4b41a7eee3 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

var { commands } = ChromeUtils.importESModule(
  "resource:///modules/matrixCommands.sys.mjs"
);

add_task(function testUnhandledEmptyCommands() {
  const noopCommands = [
    "ban",
    "unban",
    "invite",
    "kick",
    "nick",
    "op",
    "deop",
    "topic",
    "roomname",
    "addalias",
    "removealias",
    "upgraderoom",
    "me",
    "msg",
    "join",
  ];
  for (const command of commands) {
    if (noopCommands.includes(command.name)) {
      ok(
        !command.run(""),
        "Command " + command.name + " reports it handled no arguments"
      );
      ok(
        !command.run("   "),
        "Command " +
          command.name +
          " reports it handled purely whitespace arguments"
      );
    }
  }
});

add_task(function testHelpString() {
  for (const command of commands) {
    const helpString = command.helpString;
    equal(
      typeof helpString,
      "string",
      "Usage help for " + command.name + " is not a string"
    );
    ok(
      helpString.includes(command.name),
      command.name + " is not mentioned in its help string"
    );
  }
});

add_task(function testTopic() {
  const conversation = {
    wrappedJSObject: {
      set topic(value) {
        conversation._topic = value;
      },
    },
  };
  const topic = "foo bar";
  const command = _getRunCommand("topic");
  const result = command(topic, conversation);
  ok(result, "Setting topic was not handled");
  equal(conversation._topic, topic, "Topic not correctly set");
});

add_task(async function testMsgSuccess() {
  const targetUser = "@test:example.com";
  const directMessage = "lorem ipsum";
  let onMessage;
  const sendMsgPromise = new Promise(resolve => {
    onMessage = resolve;
  });
  const dm = {
    waitForRoom() {
      return Promise.resolve(this);
    },
    sendMsg(message) {
      onMessage(message);
    },
  };
  const conversation = {
    wrappedJSObject: {
      _account: {
        getDirectConversation(userId) {
          if (userId === targetUser) {
            return dm;
          }
          return null;
        },
      },
    },
  };
  const command = _getRunCommand("msg");
  const result = command(targetUser + " " + directMessage, conversation);
  ok(result, "Sending direct message was not handled");
  const message = await sendMsgPromise;
  equal(message, directMessage, "Message was not sent in DM room");
});

add_task(function testMsgMissingMessage() {
  const targetUser = "@test:example.com";
  const conversation = {};
  const command = _getRunCommand("msg");
  const result = command(targetUser, conversation);
  ok(!result, "Sending direct message was handled");
});

add_task(function testMsgNoRoom() {
  const targetUser = "@test:example.com";
  const directMessage = "lorem ipsum";
  const conversation = {
    wrappedJSObject: {
      _account: {
        getDirectConversation(userId) {
          conversation.userId = userId;
          return null;
        },
        ERROR(errorMsg) {
          conversation.errorMsg = errorMsg;
        },
      },
    },
  };
  const command = _getRunCommand("msg");
  const result = command(targetUser + " " + directMessage, conversation);
  ok(result, "Sending direct message was not handled");
  equal(
    conversation.userId,
    targetUser,
    "Did not try to get the conversation for the target user"
  );
  ok(conversation.errorMsg, "Did not report an error");
});

add_task(function testJoinSuccess() {
  const roomName = "#test:example.com";
  const conversation = {
    wrappedJSObject: {
      _account: {
        getGroupConversation(roomId) {
          conversation.roomId = roomId;
        },
      },
    },
  };
  const command = _getRunCommand("join");
  const result = command(roomName, conversation);
  ok(result, "Did not handle join command");
  equal(conversation.roomId, roomName, "Did not try to join expected room");
});

add_task(function testJoinNotRoomId() {
  const roomName = "!asdf:example.com";
  const conversation = {};
  const command = _getRunCommand("join");
  const result = command(roomName, conversation);
  ok(!result, "Handled join command for unsupported room Id");
});

// Fetch the run() of a named command.
function _getRunCommand(aCommandName) {
  for (let command of commands) {
    if (command.name == aCommandName) {
      return command.run;
    }
  }

  // Fail if no command was found.
  ok(false, "Could not find the '" + aCommandName + "' command.");
  return null;
}