summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/extensions/bayesian-spam-filter/test/unit/test_traits.js
blob: b005db72cce4910a44281c8d63d1a372f9cd7ce8 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* 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 bayes trait analysis

// I make this an instance so that I know I can reset and get
// a completely new component. Should be getService in production code.
var nsIJunkMailPlugin = Cc[
  "@mozilla.org/messenger/filter-plugin;1?name=bayesianfilter"
].createInstance(Ci.nsIJunkMailPlugin);

// command functions for test data
var kTrain = 0; // train a file as a trait
var kClass = 1; // classify files with traits
var kReset = 2; // reload plugin, reading in data from disk
var kDetail = 3; // test details

var gTest; // currently active test

// The tests array defines the tests to attempt. Format of
// an element "test" of this array:
//
//   test.command: function to perform, see definitions above
//   test.fileName: file(s) containing message(s) to test
//   test.traitIds: Array of traits to train (kTrain) or pro trait (kClass)
//   test.traitAntiIds: Array of anti traits to classify
//   test.percents: array of arrays (1 per message, 1 per trait) of
//                  expected results from the classifier

var tests = [
  // train two different combinations of messages
  {
    command: kTrain,
    fileName: "ham1.eml",
    traitIds: [3, 6],
  },
  {
    command: kTrain,
    fileName: "spam1.eml",
    traitIds: [4],
  },
  {
    command: kTrain,
    fileName: "spam4.eml",
    traitIds: [5],
  },
  // test the message classifications using both singular and plural classifier
  {
    command: kClass,
    fileName: "ham1.eml",
    traitIds: [4, 6],
    traitAntiIds: [3, 5],
    // ham1 is trained "anti" for first test, "pro" for second
    percents: [[0, 100]],
  },
  {
    command: kClass,
    fileName: "ham2.eml",
    traitIds: [4, 6],
    traitAntiIds: [3, 5],
    // these are partial percents for an untrained message. ham2 is similar to ham1
    percents: [[8, 95]],
  },
  {
    command: kDetail,
    fileName: "spam2.eml",
    traitIds: [4],
    traitAntiIds: [3],
    percents: {
      lots: 84,
      money: 84,
      make: 84,
      your: 16,
    },
    runnings: [84, 92, 95, 81],
  },
  {
    command: kClass,
    fileName: "spam1.eml,spam2.eml,spam3.eml,spam4.eml",
    traitIds: [4, 6],
    traitAntiIds: [3, 5],
    // spam1 trained as "pro" for first pro/anti pair
    // spam4 trained as "anti" for second pro/anti pair
    // others are partials
    percents: [
      [100, 50],
      [81, 0],
      [98, 50],
      [81, 0],
    ],
  },
  // reset the plugin, read in data, and retest the classification
  // this tests the trait file writing
  {
    command: kReset,
  },
  {
    command: kClass,
    fileName: "ham1.eml",
    traitIds: [4, 6],
    traitAntiIds: [3, 5],
    percents: [[0, 100]],
  },
  {
    command: kClass,
    fileName: "ham2.eml",
    traitIds: [4, 6],
    traitAntiIds: [3, 5],
    percents: [[8, 95]],
  },
  {
    command: kClass,
    fileName: "spam1.eml,spam2.eml,spam3.eml,spam4.eml",
    traitIds: [4, 6],
    traitAntiIds: [3, 5],
    percents: [
      [100, 50],
      [81, 0],
      [98, 50],
      [81, 0],
    ],
  },
];

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

  startCommand();
}

var listener = {
  // nsIMsgTraitClassificationListener implementation
  onMessageTraitsClassified(aMsgURI, aTraits, aPercents) {
    // print("Message URI is " + aMsgURI);
    if (!aMsgURI) {
      // Ignore end-of-batch signal.
      return;
    }

    switch (gTest.command) {
      case kClass:
        Assert.equal(gTest.files[gTest.currentIndex], aMsgURI);
        var currentPercents = gTest.percents[gTest.currentIndex];
        for (let i = 0; i < currentPercents.length; i++) {
          // print("expecting score " + currentPercents[i] +
          //      " got score " + aPercents[i]);
          Assert.equal(currentPercents[i], aPercents[i]);
        }
        gTest.currentIndex++;
        break;

      case kTrain: // We tested this some in test_junkAsTraits.js, so let's not bother
      default:
        break;
    }
    if (!--gTest.callbacks) {
      // All done, start the next test
      startCommand();
    }
  },
  onMessageTraitDetails(
    aMsgURI,
    aProTrait,
    aTokenString,
    aTokenPercents,
    aRunningPercents
  ) {
    print("Details for " + aMsgURI);
    for (let i = 0; i < aTokenString.length; i++) {
      print(
        "Percent " +
          aTokenPercents[i] +
          " Running " +
          aRunningPercents[i] +
          " Token " +
          aTokenString[i]
      );
      Assert.ok(aTokenString[i] in gTest.percents);

      Assert.equal(gTest.percents[aTokenString[i]], aTokenPercents[i]);
      Assert.equal(gTest.runnings[i], aRunningPercents[i]);
      delete gTest.percents[aTokenString[i]];
    }
    Assert.equal(Object.keys(gTest.percents).length, 0);
    if (gTest.command == kClass) {
      gTest.currentIndex++;
    }
    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
      let proArray = [];
      for (let i = 0; i < gTest.traitIds.length; i++) {
        proArray.push(gTest.traitIds[i]);
      }
      gTest.callbacks = 1;

      nsIJunkMailPlugin.setMsgTraitClassification(
        getSpec(gTest.fileName), // aMsgURI
        [], // aOldTraits
        proArray, // aNewTraits
        listener
      ); // [optional] in nsIMsgTraitClassificationListener aTraitListener
      // null,      // [optional] in nsIMsgWindow aMsgWindow
      // null,      // [optional] in nsIJunkMailClassificationListener aJunkListener
      break;
    }
    case kClass: {
      // classify message
      var antiArray = [];
      let proArray = [];
      for (let i = 0; i < gTest.traitIds.length; i++) {
        antiArray.push(gTest.traitAntiIds[i]);
        proArray.push(gTest.traitIds[i]);
      }
      gTest.files = gTest.fileName.split(",");
      gTest.callbacks = gTest.files.length;
      gTest.currentIndex = 0;
      for (let i = 0; i < gTest.files.length; i++) {
        gTest.files[i] = getSpec(gTest.files[i]);
      }
      if (gTest.files.length == 1) {
        // use the singular classifier
        nsIJunkMailPlugin.classifyTraitsInMessage(
          getSpec(gTest.fileName), // in string aMsgURI
          proArray, // in array aProTraits,
          antiArray, // in array aAntiTraits
          listener
        ); // in nsIMsgTraitClassificationListener aTraitListener
        // null,      // [optional] in nsIMsgWindow aMsgWindow
        // null,      // [optional] in nsIJunkMailClassificationListener aJunkListener
      } else {
        // use the plural classifier
        nsIJunkMailPlugin.classifyTraitsInMessages(
          gTest.files, // in Array<ACString> aMsgURIs,
          proArray, // in array aProTraits,
          antiArray, // in array aAntiTraits
          listener
        ); // in nsIMsgTraitClassificationListener aTraitListener
        // null,      // [optional] in nsIMsgWindow aMsgWindow
        // null,      // [optional] in nsIJunkMailClassificationListener aJunkListener
      }
      break;
    }
    case kDetail:
      // detail message
      nsIJunkMailPlugin.detailMessage(
        getSpec(gTest.fileName), // in string aMsgURI
        gTest.traitIds[0], // proTrait
        gTest.traitAntiIds[0], // antiTrait
        listener
      ); // in nsIMsgTraitDetailListener aDetailListener
      break;
    case kReset:
      // reload a new nsIJunkMailPlugin, reading file in the process
      nsIJunkMailPlugin.shutdown(); // writes files
      nsIJunkMailPlugin = null;
      nsIJunkMailPlugin = Cc[
        "@mozilla.org/messenger/filter-plugin;1?name=bayesianfilter"
      ].createInstance(Ci.nsIJunkMailPlugin);
      // does not do a callback, so we must restart next command
      startCommand();
      break;
  }
}