summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/local/test/unit/test_duplicateKey.js
blob: 845a25cc1a41a23fa182699fb4f8c812482f58b0 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * This test deletes intermediate messages, then compacts, then adds more
 * messages, testing for duplicated keys in bug 1202105.
 */

/* import-globals-from ../../../test/resources/POP3pump.js */
load("../../../resources/POP3pump.js");
const { PromiseTestUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/PromiseTestUtils.jsm"
);

add_task(async function runPump() {
  gPOP3Pump.files = [
    "../../../data/bugmail1",
    "../../../data/bugmail1",
    "../../../data/bugmail1",
    "../../../data/bugmail1",
    "../../../data/bugmail1",
  ];
  await gPOP3Pump.run();

  // get message headers for the inbox folder
  var hdrs = showMessages(localAccountUtils.inboxFolder);
  Assert.equal(hdrs.length, 5, "Check initial db count");

  // Deletes 2 middle messages.
  let deletes = [hdrs[1], hdrs[2]];

  // Note the listener won't work because this is a sync delete,
  // but it should!
  localAccountUtils.inboxFolder.deleteMessages(
    deletes,
    null, // in nsIMsgWindow msgWindow,
    true, // in boolean deleteStorage,
    true, // in boolean isMove,
    null, // in nsIMsgCopyServiceListener,
    false
  ); // in boolean allowUndo

  dump("Messages after delete\n");
  hdrs = showMessages(localAccountUtils.inboxFolder);
  Assert.equal(hdrs.length, 3, "Check db length after deleting two messages");

  // compact
  var listener = new PromiseTestUtils.PromiseUrlListener();
  localAccountUtils.inboxFolder.compact(listener, null);
  await listener.promise;

  dump("Messages after compact\n");
  hdrs = showMessages(localAccountUtils.inboxFolder);
  Assert.equal(hdrs.length, 3, "Check db length after compact");

  // Add some more messages. This fails in nsMsgDatabase::AddNewHdrToDB with
  // NS_ERROR("adding hdr that already exists") before bug 1202105.
  gPOP3Pump.files = ["../../../data/draft1"];
  await gPOP3Pump.run();

  dump("Messages after new message\n");
  hdrs = showMessages(localAccountUtils.inboxFolder);
  Assert.equal(hdrs.length, 4, "Check db length after adding one message");

  gPOP3Pump = null;
});

function showMessages(folder) {
  var hdrs = [];
  for (let hdr of folder.msgDatabase.enumerateMessages()) {
    hdrs.push(hdr);
    dump(
      "key " +
        (hdrs.length - 1) +
        " is " +
        hdrs[hdrs.length - 1].messageKey +
        "\n"
    );
  }
  return hdrs;
}