summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/irc/test/test_sendBufferedCommand.js
blob: 5558979db3eb87332d97947d723b73024eee3aa2 (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
/* 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"
);
var { ircAccount } = ChromeUtils.importESModule(
  "resource:///modules/ircAccount.sys.mjs"
);
var { clearTimeout, setTimeout } = ChromeUtils.importESModule(
  "resource://gre/modules/Timer.sys.mjs"
);

function FakeAccount() {
  this._commandBuffers = new Map();
  this.callbacks = [];
}
FakeAccount.prototype = {
  __proto__: ircAccount.prototype,
  maxMessageLength: 60,
  callbacks: [],
  sendMessage(aCommand, aParams) {
    this.callbacks.shift()(aCommand, aParams);
  },
};

var account = new FakeAccount();

function run_test() {
  test_parameterCollect();
  test_maxLength();
  run_next_test();
}

function test_parameterCollect() {
  // Individual tests, data consisting of [channel, key] pairs.
  let tests = [
    {
      data: [["one"], ["one"]], // also tests deduplication
      result: "JOIN one",
    },
    {
      data: [["one", ""]], // explicit empty password string
      result: "JOIN one",
    },
    {
      data: [["one"], ["two"], ["three"]],
      result: "JOIN one,two,three",
    },
    {
      data: [["one"], ["two", "password"], ["three"]],
      result: "JOIN two,one,three password",
    },
    {
      data: [
        ["one"],
        ["two", "password"],
        ["three"],
        ["four", "anotherpassword"],
      ],
      result: "JOIN two,four,one,three password,anotherpassword",
    },
  ];

  for (let test of tests) {
    let timeout;
    // Destructure test to local variables so each function
    // generated here gets the correct value in its scope.
    let { data, result } = test;
    account.callbacks.push((aCommand, aParams) => {
      let msg = account.buildMessage(aCommand, aParams);
      equal(msg, result, "Test buffering of parameters");
      clearTimeout(timeout);
      account._lastCommandSendTime = 0;
      run_next_test();
    });
    add_test(() => {
      // This timeout lets the test fail more quickly if
      // some of the callbacks we added don't get called.
      // Not strictly speaking necessary.
      // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
      timeout = setTimeout(() => {
        ok(false, "test_parameterCollect failed after timeout.");
        run_next_test();
      }, 2000);
      for (let [channel, key] of data) {
        account.sendBufferedCommand("JOIN", channel, key);
      }
    });
  }

  // Test this still works when adding commands on different ticks of
  // the event loop.
  account._lastCommandSendTime = 0;
  for (let test of tests) {
    let timeout;
    let { data, result } = test;
    account.callbacks.push((aCommand, aParams) => {
      let msg = account.buildMessage(aCommand, aParams);
      equal(msg, result, "Test buffering with setTimeout");
      clearTimeout(timeout);
      run_next_test();
    });
    add_test(() => {
      // This timeout lets the test fail more quickly if
      // some of the callbacks we added don't get called.
      // Not strictly speaking necessary.
      // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
      timeout = setTimeout(() => {
        ok(false, "test_parameterCollect failed after timeout.");
        run_next_test();
      }, 2000);
      let delay = 0;
      for (let params of data) {
        let [channel, key] = params;
        delay += 200;
        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
        setTimeout(() => {
          account.sendBufferedCommand("JOIN", channel, key);
        }, delay);
      }
    });
  }
}

function test_maxLength() {
  let tests = [
    {
      data: [
        ["applecustard"],
        ["pearpie"],
        ["strawberryfield"],
        ["blueberrypancake"],
        ["mangojuice"],
        ["raspberryberet"],
        ["pineapplesoup"],
        ["limejelly"],
        ["lemonsorbet"],
      ],
      results: [
        "JOIN applecustard,pearpie,strawberryfield,blueberrypancake",
        "JOIN mangojuice,raspberryberet,pineapplesoup,limejelly",
        "JOIN lemonsorbet",
      ],
    },
    {
      data: [
        ["applecustard"],
        ["pearpie"],
        ["strawberryfield", "password1"],
        ["blueberrypancake"],
        ["mangojuice"],
        ["raspberryberet"],
        ["pineapplesoup"],
        ["limejelly", "password2"],
        ["lemonsorbet"],
      ],
      results: [
        "JOIN strawberryfield,applecustard,pearpie password1",
        "JOIN blueberrypancake,mangojuice,raspberryberet",
        "JOIN limejelly,pineapplesoup,lemonsorbet password2",
      ],
    },
  ];

  account._lastCommandSendTime = 0;
  for (let test of tests) {
    let timeout;
    // Destructure test to local variables so each function
    // generated here gets the correct value in its scope.
    let { data, results } = test;
    for (let r of results) {
      let result = r;
      account.callbacks.push((aCommand, aParams) => {
        let msg = account.buildMessage(aCommand, aParams);
        equal(msg, result, "Test maximum message length constraint");
        // After all results are checked, run the next test.
        if (result == results[results.length - 1]) {
          clearTimeout(timeout);
          run_next_test();
        }
      });
    }
    add_test(() => {
      // This timeout lets the test fail more quickly if
      // some of the callbacks we added don't get called.
      // Not strictly speaking necessary.
      // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
      timeout = setTimeout(() => {
        ok(false, "test_maxLength failed after timeout.");
        run_next_test();
      }, 2000);
      for (let [channel, key] of data) {
        account.sendBufferedCommand("JOIN", channel, key);
      }
    });
  }
}