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

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

var fakeProto = {
  id: "fake-proto",
  options: { alternateNicks: "" },
  _getOptionDefault(aOption) {
    return this.options[aOption];
  },
  usernameSplits: ircProtocol.prototype.usernameSplits,
  splitUsername: ircProtocol.prototype.splitUsername,
};

function test_tryNewNick() {
  const testData = {
    clokep: "clokep1",
    clokep1: "clokep2",
    clokep10: "clokep11",
    clokep0: "clokep1",
    clokep01: "clokep02",
    clokep09: "clokep10",

    // Now put a number in the "first part".
    clo1kep: "clo1kep1",
    clo1kep1: "clo1kep2",
    clo1kep10: "clo1kep11",
    clo1kep0: "clo1kep1",
    clo1kep01: "clo1kep02",
    clo1kep09: "clo1kep10",
  };

  let account = new ircAccount(fakeProto, {
    name: "clokep@instantbird.org",
  });
  account.LOG = function (aStr) {};
  account.normalize = aStr => aStr;

  for (let currentNick in testData) {
    account._sentNickname = currentNick;
    account.sendMessage = (aCommand, aNewNick) =>
      equal(aNewNick, testData[currentNick]);

    account.tryNewNick(currentNick);
  }

  run_next_test();
}

// This tests a bunch of cases near the max length by maintaining the state
// through a series of test nicks.
function test_maxLength() {
  let testData = [
    // First try adding a digit, as normal.
    ["abcdefghi", "abcdefghi1"],
    // The "received" nick back will now be the same though, so it was too long.
    ["abcdefghi", "abcdefgh1"],
    // And just ensure we're iterating properly.
    ["abcdefgh1", "abcdefgh2"],
    ["abcdefgh2", "abcdefgh3"],
    ["abcdefgh3", "abcdefgh4"],
    ["abcdefgh4", "abcdefgh5"],
    ["abcdefgh5", "abcdefgh6"],
    ["abcdefgh6", "abcdefgh7"],
    ["abcdefgh7", "abcdefgh8"],
    ["abcdefgh8", "abcdefgh9"],
    ["abcdefgh9", "abcdefgh10"],
    ["abcdefgh1", "abcdefg10"],
    ["abcdefg10", "abcdefg11"],
    ["abcdefg99", "abcdefg100"],
    ["abcdefg10", "abcdef100"],
    ["a99999999", "a100000000"],
    ["a10000000", "a00000000"],
  ];

  let account = new ircAccount(fakeProto, {
    name: "clokep@instantbird.org",
  });
  account.LOG = function (aStr) {};
  account._sentNickname = "abcdefghi";
  account.normalize = aStr => aStr;

  for (let currentNick of testData) {
    account.sendMessage = (aCommand, aNewNick) =>
      equal(aNewNick, currentNick[1]);

    account.tryNewNick(currentNick[0]);
  }

  run_next_test();
}

function test_altNicks() {
  const altNicks = ["clokep_", "clokep|"];
  const testData = {
    // Test account nick.
    clokep: [altNicks, "clokep_"],
    // Test first element in list.
    clokep_: [altNicks, "clokep|"],
    // Test last element in list.
    "clokep|": [altNicks, "clokep|1"],
    // Test element not in list with number at end.
    clokep1: [altNicks, "clokep2"],

    // Test messy alternatives.
    "clokep[": [" clokep ,\n clokep111,,,\tclokep[, clokep_", "clokep_"],
  };

  let account = new ircAccount(fakeProto, {
    name: "clokep@instantbird.org",
  });
  account.LOG = function (aStr) {};
  account.normalize = aStr => aStr;

  for (let currentNick in testData) {
    // Only one pref is touched in here, override the default to return
    // what this test needs.
    account.getString = function (aStr) {
      let data = testData[currentNick][0];
      if (Array.isArray(data)) {
        return data.join(",");
      }
      return data;
    };
    account._sentNickname = currentNick;

    account.sendMessage = (aCommand, aNewNick) =>
      equal(aNewNick, testData[currentNick][1]);

    account.tryNewNick(currentNick);
  }

  run_next_test();
}

function run_test() {
  add_test(test_tryNewNick);
  add_test(test_maxLength);
  add_test(test_altNicks);

  run_next_test();
}