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

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

function waitForTopic(target, targetTopic) {
  return new Promise(resolve => {
    let observer = {
      observe(subject, topic, data) {
        if (topic === targetTopic) {
          resolve({ subject, data });
          target.removeObserver(observer);
        }
      },
    };
    target.addObserver(observer);
  });
}

function getChannel(account) {
  const channelStub = {
    _observers: [],
    _name: "#test",
    _account: {
      _currentServerName: "test",
      imAccount: {
        statusInfo: {},
      },
      _nickname: "user",
      _activeCAPs: new Set(),
      ...account,
    },
  };
  Object.setPrototypeOf(channelStub, ircChannel.prototype);
  return channelStub;
}

add_task(async function test_dispatchMessage_normal() {
  let didSend = false;
  const channelStub = getChannel({
    sendMessage(type, data) {
      equal(type, "PRIVMSG");
      deepEqual(data, ["#test", "foo"]);
      didSend = true;
      return true;
    },
  });
  const newText = waitForTopic(channelStub, "new-text");
  channelStub.dispatchMessage("foo");
  ok(didSend);
  const { subject: sentMessage } = await newText;
  equal(sentMessage.message, "foo");
  ok(sentMessage.outgoing);
  ok(!sentMessage.notification);
  equal(sentMessage.who, "user");
});

add_task(async function test_dispatchMessage_empty() {
  let didSend = false;
  const channelStub = getChannel({
    sendMessage(type, data) {
      ok(false, "Should not send empty message");
      didSend = true;
      return true;
    },
  });
  channelStub.writeMessage = () => {
    ok(false, "Should not display empty unsent message");
    didSend = true;
  };
  ircChannel.prototype.dispatchMessage.call(channelStub, "");
  ok(!didSend);
});

add_task(async function test_dispatchMessage_echoed() {
  let didSend = false;
  let didWrite = false;
  const channelStub = getChannel({
    sendMessage(type, data) {
      equal(type, "PRIVMSG");
      deepEqual(data, ["#test", "foo"]);
      didSend = true;
      return true;
    },
  });
  channelStub._account._activeCAPs.add("echo-message");
  channelStub.writeMessage = () => {
    ok(false, "Should not write message when echo is on");
    didWrite = true;
  };
  ircChannel.prototype.dispatchMessage.call(channelStub, "foo");
  ok(didSend);
  ok(!didWrite);
});

add_task(async function test_dispatchMessage_error() {
  let didSend = false;
  const channelStub = getChannel({
    sendMessage(type, data) {
      equal(type, "PRIVMSG");
      deepEqual(data, ["#test", "foo"]);
      didSend = true;
      return false;
    },
  });
  const newText = waitForTopic(channelStub, "new-text");
  ircChannel.prototype.dispatchMessage.call(channelStub, "foo");
  ok(didSend);
  const { subject: writtenMessage } = await newText;
  ok(writtenMessage.error);
  ok(writtenMessage.system);
  equal(writtenMessage.who, "test");
});

add_task(async function test_dispatchMessage_action() {
  let didSend = false;
  const channelStub = getChannel({
    sendMessage(type, data) {
      ok(false, "Action should not be sent as normal message");
      return false;
    },
    sendCTCPMessage(target, isNotice, command, params) {
      equal(target, "#test");
      ok(!isNotice);
      equal(command, "ACTION");
      equal(params, "foo");
      didSend = true;
      return true;
    },
  });
  const newText = waitForTopic(channelStub, "new-text");
  ircChannel.prototype.dispatchMessage.call(channelStub, "foo", true);
  ok(didSend);
  const { subject: sentMessage } = await newText;
  equal(sentMessage.message, "foo");
  ok(sentMessage.outgoing);
  ok(!sentMessage.notification);
  ok(sentMessage.action);
  equal(sentMessage.who, "user");
});

add_task(async function test_dispatchMessage_actionError() {
  let didSend = false;
  const channelStub = getChannel({
    sendMessage(type, data) {
      ok(false, "Action should not be sent as normal message");
      return false;
    },
    sendCTCPMessage(target, isNotice, command, params) {
      equal(target, "#test");
      ok(!isNotice);
      equal(command, "ACTION");
      equal(params, "foo");
      didSend = true;
      return false;
    },
  });
  const newText = waitForTopic(channelStub, "new-text");
  ircChannel.prototype.dispatchMessage.call(channelStub, "foo", true);
  ok(didSend, "Message was sent");
  const { subject: sentMessage } = await newText;
  ok(sentMessage.error, "Shown message is error");
  ok(sentMessage.system, "Shown message is from system");
  equal(sentMessage.who, "test");
});

add_task(async function test_dispatchMessage_notice() {
  let didSend = false;
  const channelStub = getChannel({
    sendMessage(type, data) {
      equal(type, "NOTICE");
      deepEqual(data, ["#test", "foo"]);
      didSend = true;
      return true;
    },
  });
  const newText = waitForTopic(channelStub, "new-text");
  ircChannel.prototype.dispatchMessage.call(channelStub, "foo", false, true);
  ok(didSend);
  const { subject: sentMessage } = await newText;
  equal(sentMessage.message, "foo");
  ok(sentMessage.outgoing);
  ok(sentMessage.notification);
  equal(sentMessage.who, "user");
});