summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/extensions/bayesian-spam-filter/test/unit/test_customTokenization.js
blob: 222a9557d865c6dcd19119cf351d1f7096954dc7 (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
/* 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/. */

// Tests use of custom tokenization, originally introduced in bug 476389

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

// command functions for test data
var kTrain = 0; // train a file
var kTest = 1; // test headers returned from detail
var kSetup = 2; // run a setup function

// trait ids
var kProArray = [3];
var kAntiArray = [4];

var gTest; // currently active test

// The tests array defines the tests to attempt.

var tests = [
  // test a few tokens using defaults
  {
    command: kTrain,
    fileName: "tokenTest.eml",
  },
  {
    command: kTest,
    fileName: "tokenTest.eml",
    tokens: ["important", "subject:eat", "message-id:14159", "http://www"],
    nottokens: ["idonotexist", "subject:to"],
  },

  // enable received, disable message-id
  // switch tokenization of body to catch full urls (no "." delimiter)
  // enable sender, keeping full value
  {
    command: kSetup,
    operation() {
      Services.prefs.setCharPref(
        "mailnews.bayesian_spam_filter.tokenizeheader.received",
        "standard"
      );
      Services.prefs.setCharPref(
        "mailnews.bayesian_spam_filter.tokenizeheader.message-id",
        "false"
      );
      Services.prefs.setCharPref(
        "mailnews.bayesian_spam_filter.body_delimiters",
        " \t\r\n\v"
      );
      Services.prefs.setCharPref(
        "mailnews.bayesian_spam_filter.tokenizeheader.sender",
        "full"
      );
    },
  },
  {
    command: kTrain,
    fileName: "tokenTest.eml",
  },
  {
    command: kTest,
    fileName: "tokenTest.eml",
    tokens: [
      "important",
      "subject:eat",
      "received:reader@example",
      "skip:h 20",
      "sender:bugzilla test setup <noreply@example.org>",
      "received:<someone@example",
    ],
    nottokens: ["message-id:14159", "http://www"],
  },

  // increase the length of the maximum token to catch full URLs in the body
  // add <>;, remove . from standard header delimiters to better capture emails
  // use custom delimiters on sender, without "." or "<>"
  {
    command: kSetup,
    operation() {
      Services.prefs.setIntPref(
        "mailnews.bayesian_spam_filter.maxlengthfortoken",
        50
      );
      Services.prefs.setCharPref(
        "mailnews.bayesian_spam_filter.header_delimiters",
        " ;<>\t\r\n\v"
      );
      Services.prefs.setCharPref(
        "mailnews.bayesian_spam_filter.tokenizeheader.sender",
        " \t\r\n\v"
      );
    },
  },
  {
    command: kTrain,
    fileName: "tokenTest.eml",
  },
  {
    command: kTest,
    fileName: "tokenTest.eml",
    tokens: [
      "received:someone@example.com",
      "http://www.example.org",
      "received:reader@example.org",
      "sender:<noreply@example.org>",
    ],
    nottokens: ["skip:h 20", "received:<someone@example"],
  },
];

// main test
function run_test() {
  localAccountUtils.loadLocalMailAccount();
  do_test_pending();

  startCommand();
}

var listener = {
  // nsIMsgTraitClassificationListener implementation
  onMessageTraitsClassified(aMsgURI, aTraits, aPercents) {
    startCommand();
  },

  onMessageTraitDetails(
    aMsgURI,
    aProTrait,
    aTokenString,
    aTokenPercents,
    aRunningPercents
  ) {
    print("Details for " + aMsgURI);
    for (let i = 0; i < aTokenString.length; i++) {
      print("Token " + aTokenString[i]);
    }

    // we should have these tokens
    for (let value of gTest.tokens) {
      print("We should have '" + value + "'? ");
      Assert.ok(aTokenString.includes(value));
    }

    // should not have these tokens
    for (let value of gTest.nottokens) {
      print("We should not have '" + value + "'? ");
      Assert.ok(!aTokenString.includes(value));
    }
    startCommand();
  },
};

// start the next test command
function startCommand() {
  if (!tests.length) {
    // Do we have more commands?
    // no, all done
    do_test_finished();
    return;
  }

  gTest = tests.shift();
  // print("StartCommand command = " + gTest.command + ", remaining tests " + tests.length);
  switch (gTest.command) {
    case kTrain:
      // train message

      MailServices.junk.setMsgTraitClassification(
        getSpec(gTest.fileName), // aMsgURI
        [], // aOldTraits
        kProArray, // aNewTraits
        listener
      ); // [optional] in nsIMsgTraitClassificationListener aTraitListener
      // null,      // [optional] in nsIMsgWindow aMsgWindow
      // null,      // [optional] in nsIJunkMailClassificationListener aJunkListener
      break;

    case kTest:
      // test headers from detail message
      MailServices.junk.detailMessage(
        getSpec(gTest.fileName), // in string aMsgURI
        kProArray[0], // proTrait
        kAntiArray[0], // antiTrait
        listener
      ); // in nsIMsgTraitDetailListener aDetailListener
      break;

    case kSetup:
      gTest.operation();
      startCommand();
      break;
  }
}