summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/irc/test/test_ircCommands.js
blob: 4bd6ab2954964a88db36ce5ae3fd1160058ae1f4 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

var { IMServices } = ChromeUtils.importESModule(
  "resource:///modules/IMServices.sys.mjs"
);
var { commands } = ChromeUtils.importESModule(
  "resource:///modules/ircCommands.sys.mjs"
);
var { ircProtocol } = ChromeUtils.importESModule(
  "resource:///modules/irc.sys.mjs"
);
var { ircAccount, ircConversation } = ChromeUtils.importESModule(
  "resource:///modules/ircAccount.sys.mjs"
);

// Ensure the commands have been initialized.
IMServices.conversations.initConversations();

var fakeProto = {
  id: "fake-proto",
  usernameSplits: ircProtocol.prototype.usernameSplits,
  splitUsername: ircProtocol.prototype.splitUsername,
};

function run_test() {
  add_test(testUserModeCommand);
  add_test(testModeCommand);
  run_next_test();
}

// Test the /mode command.
function testModeCommand() {
  const testChannelCommands = [
    {
      msg: "",
      channel: "#instantbird",
      expectedMessage: "MODE #instantbird",
    },
    {
      msg: "#instantbird",
      channel: "#instantbird",
      expectedMessage: "MODE #instantbird",
    },
    {
      msg: "-s",
      channel: "#Fins",
      expectedMessage: "MODE #Fins -s",
    },
    {
      msg: "#introduction +is",
      channel: "#introduction",
      expectedMessage: "MODE #introduction +is",
    },
    {
      msg: "-s",
      channel: "&Gills",
      expectedMessage: "MODE &Gills -s",
    },
    {
      msg: "#Gamers +o KennyS",
      channel: "#Gamers",
      expectedMessage: "MODE #Gamers +o KennyS",
    },
    {
      msg: "+o lisp",
      channel: "&IB",
      expectedMessage: "MODE &IB +o lisp",
    },
    {
      msg: "+b nick!abc@server",
      channel: "#Alphabet",
      expectedMessage: "MODE #Alphabet +b nick!abc@server",
    },
    {
      msg: "+b nick",
      channel: "#Alphabet",
      expectedMessage: "MODE #Alphabet +b nick",
    },
    {
      msg: "#instantbird +b nick!abc@server",
      channel: "#instantbird",
      expectedMessage: "MODE #instantbird +b nick!abc@server",
    },
    {
      msg: "+v Wiz",
      channel: "#TheMatrix",
      expectedMessage: "MODE #TheMatrix +v Wiz",
    },
    {
      msg: "+k passcode",
      channel: "#TheMatrix",
      expectedMessage: "MODE #TheMatrix +k passcode",
    },
    {
      msg: "#Mafia +k keyword",
      channel: "#Mafia",
      expectedMessage: "MODE #Mafia +k keyword",
    },
    {
      msg: "#introduction +l 100",
      channel: "#introduction",
      expectedMessage: "MODE #introduction +l 100",
    },
    {
      msg: "+l 100",
      channel: "#introduction",
      expectedMessage: "MODE #introduction +l 100",
    },
  ];

  const testUserCommands = [
    {
      msg: "nickolas +x",
      expectedMessage: "MODE nickolas +x",
    },
    {
      msg: "matrixisreal -x",
      expectedMessage: "MODE matrixisreal -x",
    },
    {
      msg: "matrixisreal_19 +oWp",
      expectedMessage: "MODE matrixisreal_19 +oWp",
    },
    {
      msg: "nick",
      expectedMessage: "MODE nick",
    },
  ];

  let account = new ircAccount(fakeProto, {
    name: "defaultnick@instantbird.org",
  });

  // check if the message being sent is same as expected message.
  account.sendRawMessage = aMessage => {
    equal(aMessage, account._expectedMessage);
  };

  const command = _getRunCommand("mode");

  // First test Channel Commands.
  for (let test of testChannelCommands) {
    let conv = new ircConversation(account, test.channel);
    account._expectedMessage = test.expectedMessage;
    command(test.msg, conv);
  }

  // Now test the User Commands.
  let conv = new ircConversation(account, "dummyConversation");
  account._nickname = "test_nick";
  for (let test of testUserCommands) {
    account._expectedMessage = test.expectedMessage;
    command(test.msg, conv);
  }

  run_next_test();
}

// Test the /umode command.
function testUserModeCommand() {
  const testData = [
    {
      msg: "+x",
      expectedMessage: "MODE test_nick +x",
    },
    {
      msg: "-x",
      expectedMessage: "MODE test_nick -x",
    },
    {
      msg: "-pa",
      expectedMessage: "MODE test_nick -pa",
    },
    {
      msg: "+oWp",
      expectedMessage: "MODE test_nick +oWp",
    },
    {
      msg: "",
      expectedMessage: "MODE test_nick",
    },
  ];

  let account = new ircAccount(fakeProto, {
    name: "test_nick@instantbird.org",
  });
  account._nickname = "test_nick";
  let conv = new ircConversation(account, "newconv");

  // check if the message being sent is same as expected message.
  account.sendRawMessage = aMessage => {
    equal(aMessage, account._expectedMessage);
  };

  const command = _getRunCommand("umode");

  // change the nick and runUserModeCommand for each test
  for (let test of testData) {
    account._expectedMessage = test.expectedMessage;
    command(test.msg, conv);
  }

  run_next_test();
}

// 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; // Shut-up eslint.
}