summaryrefslogtreecommitdiffstats
path: root/comm/chat/components/src/test/test_commands.js
blob: de0fd0e665e6c77d88fbec7fce6580bc2cd245a1 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var { IMServices } = ChromeUtils.importESModule(
  "resource:///modules/IMServices.sys.mjs"
);
// We don't load the command service via Services as we want to access
// _findCommands in order to avoid having to intercept command execution.
var { CommandsService } = ChromeUtils.importESModule(
  "resource:///modules/imCommands.sys.mjs"
);

var kPrplId = "green";
var kPrplId2 = "red";

var fakeAccount = {
  connected: true,
  protocol: { id: kPrplId },
};
var fakeDisconnectedAccount = {
  connected: false,
  protocol: { id: kPrplId },
};
var fakeAccount2 = {
  connected: true,
  protocol: { id: kPrplId2 },
};

var fakeConversation = {
  account: fakeAccount,
  isChat: true,
};

function fakeCommand(aName, aUsageContext) {
  this.name = aName;
  if (aUsageContext) {
    this.usageContext = aUsageContext;
  }
}
fakeCommand.prototype = {
  get helpString() {
    return "";
  },
  usageContext: Ci.imICommand.CMD_CONTEXT_ALL,
  priority: Ci.imICommand.CMD_PRIORITY_PRPL,
  run: (aMsg, aConv) => true,
};

function run_test() {
  let cmdserv = new CommandsService();
  cmdserv.initCommands();

  // Some commands providing multiple possible completions.
  cmdserv.registerCommand(new fakeCommand("banana"), kPrplId2);
  cmdserv.registerCommand(new fakeCommand("baloney"), kPrplId2);

  // MUC-only command.
  cmdserv.registerCommand(
    new fakeCommand("balderdash", Ci.imICommand.CMD_CONTEXT_CHAT),
    kPrplId
  );

  // Name clashes with global command.
  cmdserv.registerCommand(new fakeCommand("offline"), kPrplId);

  // Name starts with another command name.
  cmdserv.registerCommand(new fakeCommand("helpme"), kPrplId);

  // Command name contains numbers.
  cmdserv.registerCommand(new fakeCommand("r9kbeta"), kPrplId);

  // Array of (possibly partial) command names as entered by the user.
  let testCmds = [
    "x",
    "b",
    "ba",
    "bal",
    "back",
    "hel",
    "help",
    "off",
    "offline",
  ];

  // We test an array of different possible conversations.
  // cmdlist lists all the available commands for the given conversation.
  // results is an array which for each testCmd provides an array containing
  // data with which the return value of _findCommands can be checked. In
  // particular, the name of the command and whether the first (i.e. preferred)
  // entry in the returned array of commands is a prpl command. (If the latter
  // boolean is not given, false is assumed, if the name is not given, that
  // corresponds to no commands being returned.)
  let testData = [
    {
      desc: "No conversation argument.",
      cmdlist: "away, back, busy, dnd, help, offline, raw, say",
      results: [
        [],
        [],
        ["back"],
        [],
        ["back"],
        ["help"],
        ["help"],
        ["offline"],
        ["offline"],
      ],
    },
    {
      desc: "Disconnected conversation with fakeAccount.",
      conv: {
        account: fakeDisconnectedAccount,
      },
      cmdlist:
        "away, back, busy, dnd, help, helpme, offline, offline, r9kbeta, raw, say",
      results: [
        [],
        [],
        ["back"],
        [],
        ["back"],
        ["help"],
        ["help"],
        ["offline"],
        ["offline"],
      ],
    },
    {
      desc: "Conversation with fakeAccount.",
      conv: {
        account: fakeAccount,
      },
      cmdlist:
        "away, back, busy, dnd, help, helpme, offline, offline, r9kbeta, raw, say",
      results: [
        [],
        [],
        ["back"],
        [],
        ["back"],
        [],
        ["help"],
        ["offline"],
        ["offline"],
      ],
    },
    {
      desc: "MUC with fakeAccount.",
      conv: {
        account: fakeAccount,
        isChat: true,
      },
      cmdlist:
        "away, back, balderdash, busy, dnd, help, helpme, offline, offline, r9kbeta, raw, say",
      results: [
        [],
        [],
        [],
        ["balderdash", true],
        ["back"],
        [],
        ["help"],
        ["offline"],
        ["offline"],
      ],
    },
    {
      desc: "Conversation with fakeAccount2.",
      conv: {
        account: fakeAccount2,
      },
      cmdlist:
        "away, back, baloney, banana, busy, dnd, help, offline, raw, say",
      results: [
        [],
        [],
        [],
        ["baloney", true],
        ["back"],
        ["help"],
        ["help"],
        ["offline"],
        ["offline"],
      ],
    },
    {
      desc: "MUC with fakeAccount2.",
      conv: {
        account: fakeAccount2,
        isChat: true,
      },
      cmdlist:
        "away, back, baloney, banana, busy, dnd, help, offline, raw, say",
      results: [
        [],
        [],
        [],
        ["baloney", true],
        ["back"],
        ["help"],
        ["help"],
        ["offline"],
        ["offline"],
      ],
    },
  ];

  for (let test of testData) {
    info("The following tests are with: " + test.desc);

    // Check which commands are available in which context.
    let cmdlist = cmdserv
      .listCommandsForConversation(test.conv)
      .map(aCmd => aCmd.name)
      .sort()
      .join(", ");
    Assert.equal(cmdlist, test.cmdlist);

    for (let testCmd of testCmds) {
      info("Testing command found for '" + testCmd + "'");
      let expectedResult = test.results.shift();
      let cmdArray = cmdserv._findCommands(test.conv, testCmd);
      // Check whether commands are only returned when appropriate.
      Assert.equal(cmdArray.length > 0, expectedResult.length > 0);
      if (cmdArray.length) {
        // Check if the right command was returned.
        Assert.equal(cmdArray[0].name, expectedResult[0]);
        Assert.equal(
          cmdArray[0].priority == Ci.imICommand.CMD_PRIORITY_PRPL,
          !!expectedResult[1]
        );
      }
    }
  }

  // Array of messages to test command execution of.
  let testMessages = [
    {
      message: "/r9kbeta",
      result: true,
    },
    {
      message: "/helpme 2 arguments",
      result: true,
    },
    {
      message: "nocommand",
      result: false,
    },
    {
      message: "/-a",
      result: false,
    },
    {
      message: "/notregistered",
      result: false,
    },
  ];

  // Test command execution.
  for (let executionTest of testMessages) {
    info("Testing command execution for '" + executionTest.message + "'");
    Assert.equal(
      cmdserv.executeCommand(executionTest.message, fakeConversation),
      executionTest.result
    );
  }

  cmdserv.unInitCommands();
}