summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/db/gloda/test/unit/base_index_messages.js
blob: bea2337d7f7537f7ed22f9ce443a7ff970ae04d8 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
/* 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/. */

/*
 * This file tests our indexing prowess.  This includes both our ability to
 *  properly be triggered by events taking place in thunderbird as well as our
 *  ability to correctly extract/index the right data.
 * In general, if these tests pass, things are probably working quite well.
 *
 * This test has local, IMAP online, IMAP offline, and IMAP online-become-offline
 *  variants.  See the text_index_messages_*.js files.
 *
 * Things we don't test that you think we might test:
 * - Full-text search.  Happens in query testing.
 */

var { MailUtils } = ChromeUtils.import("resource:///modules/MailUtils.jsm");
var { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
var { Gloda } = ChromeUtils.import("resource:///modules/gloda/GlodaPublic.jsm");
var { GlodaConstants } = ChromeUtils.import(
  "resource:///modules/gloda/GlodaConstants.jsm"
);
var { GlodaMsgIndexer } = ChromeUtils.import(
  "resource:///modules/gloda/IndexMsg.jsm"
);
var { GlodaIndexer } = ChromeUtils.import(
  "resource:///modules/gloda/GlodaIndexer.jsm"
);
var { queryExpect, sqlExpectCount } = ChromeUtils.import(
  "resource://testing-common/gloda/GlodaQueryHelper.jsm"
);
var {
  assertExpectedMessagesIndexed,
  waitForGlodaIndexer,
  nukeGlodaCachesAndCollections,
} = ChromeUtils.import("resource://testing-common/gloda/GlodaTestHelper.jsm");
var {
  configureGlodaIndexing,
  waitForGlodaDBFlush,
  waitForIndexingHang,
  resumeFromSimulatedHang,
  permuteMessages,
  makeABCardForAddressPair,
} = ChromeUtils.import(
  "resource://testing-common/gloda/GlodaTestHelperFunctions.jsm"
);
var { PromiseTestUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/PromiseTestUtils.jsm"
);
var { MessageInjection } = ChromeUtils.import(
  "resource://testing-common/mailnews/MessageInjection.jsm"
);
var { SyntheticMessageSet, SyntheticPartMultiMixed, SyntheticPartLeaf } =
  ChromeUtils.import("resource://testing-common/mailnews/MessageGenerator.jsm");
var { TagNoun } = ChromeUtils.import("resource:///modules/gloda/NounTag.jsm");

// Whether we can expect fulltext results
var expectFulltextResults = true;

/**
 * Should we force our folders offline after we have indexed them once.  We do
 * this in the online_to_offline test variant.
 */
var goOffline = false;

var messageInjection;
var msgGen;
var scenarios;

/* ===== Indexing Basics ===== */

/**
 * Index a message, wait for a commit, make sure the header gets the property
 *  set correctly.  Then modify the message, verify the dirty property shows
 *  up, flush again, and make sure the dirty property goes clean again.
 */
async function test_pending_commit_tracker_flushes_correctly() {
  let [, msgSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1 },
  ]);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet], { augment: true }));

  // Before the flush, there should be no gloda-id property.
  let msgHdr = msgSet.getMsgHdr(0);
  // Get it as a string to make sure it's empty rather than possessing a value.
  Assert.equal(msgHdr.getStringProperty("gloda-id"), "");

  await waitForGlodaDBFlush();

  // After the flush there should be a gloda-id property and it should
  //  equal the gloda id.
  let gmsg = msgSet.glodaMessages[0];
  Assert.equal(msgHdr.getUint32Property("gloda-id"), gmsg.id);

  // Make sure no dirty property was written.
  Assert.equal(msgHdr.getStringProperty("gloda-dirty"), "");

  // Modify the message.
  msgSet.setRead(true);
  await waitForGlodaIndexer(msgSet);
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));

  // Now there should be a dirty property and it should be 1.
  Assert.equal(
    msgHdr.getUint32Property("gloda-dirty"),
    GlodaMsgIndexer.kMessageDirty
  );

  // Flush.
  await waitForGlodaDBFlush();

  // Now dirty should be 0 and the gloda id should still be the same.
  Assert.equal(
    msgHdr.getUint32Property("gloda-dirty"),
    GlodaMsgIndexer.kMessageClean
  );
  Assert.equal(msgHdr.getUint32Property("gloda-id"), gmsg.id);
}

/**
 * Make sure that PendingCommitTracker causes a msgdb commit to occur so that
 *  if the nsIMsgFolder's msgDatabase attribute has already been nulled
 *  (which is normally how we force a msgdb commit), that the changes to the
 *  header actually hit the disk.
 */
async function test_pending_commit_causes_msgdb_commit() {
  // New message, index it.
  let [[folder], msgSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1 },
  ]);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet], { augment: true }));

  // Force the msgDatabase closed; the sqlite commit will not yet have occurred.
  messageInjection.getRealInjectionFolder(folder).msgDatabase = null;
  // Make the commit happen, this causes the header to get set.
  await waitForGlodaDBFlush();

  // Force a GC. This will kill off the header and the database, losing data
  //  if we are not protecting it.
  Cu.forceGC();

  // Now retrieve the header and make sure it has the gloda id set!
  let msgHdr = msgSet.getMsgHdr(0);
  Assert.equal(
    msgHdr.getUint32Property("gloda-id"),
    msgSet.glodaMessages[0].id
  );
}

/**
 * Give the indexing sweep a workout.
 *
 * This includes:
 * - Basic indexing sweep across never-before-indexed folders.
 * - Indexing sweep across folders with just some changes.
 * - Filthy pass.
 */
async function test_indexing_sweep() {
  // -- Never-before-indexed folders.
  // Turn off event-driven indexing.
  configureGlodaIndexing({ event: false });

  let [[folderA], setA1, setA2] = await messageInjection.makeFoldersWithSets(
    1,
    [{ count: 3 }, { count: 2 }]
  );
  let [, setB1, setB2] = await messageInjection.makeFoldersWithSets(1, [
    { count: 3 },
    { count: 2 },
  ]);
  let [[folderC], setC1, setC2] = await messageInjection.makeFoldersWithSets(
    1,
    [{ count: 3 }, { count: 2 }]
  );

  // Make sure that event-driven job gets nuked out of existence
  GlodaIndexer.purgeJobsUsingFilter(() => true);

  // Turn on event-driven indexing again; this will trigger a sweep.
  configureGlodaIndexing({ event: true });
  GlodaMsgIndexer.indexingSweepNeeded = true;
  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([setA1, setA2, setB1, setB2, setC1, setC2])
  );

  // -- Folders with some changes, pending commits.
  // Indexing off.
  configureGlodaIndexing({ event: false });

  setA1.setRead(true);
  setB2.setRead(true);

  // Indexing on, killing all outstanding jobs, trigger sweep.
  GlodaIndexer.purgeJobsUsingFilter(() => true);
  configureGlodaIndexing({ event: true });
  GlodaMsgIndexer.indexingSweepNeeded = true;

  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([setA1, setB2]));

  // -- Folders with some changes, no pending commits.
  // Force a commit to clear out our pending commits.
  await waitForGlodaDBFlush();
  // Indexing off.
  configureGlodaIndexing({ event: false });

  setA2.setRead(true);
  setB1.setRead(true);

  // Indexing on, killing all outstanding jobs, trigger sweep.
  GlodaIndexer.purgeJobsUsingFilter(() => true);
  configureGlodaIndexing({ event: true });
  GlodaMsgIndexer.indexingSweepNeeded = true;

  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([setA2, setB1]));

  // -- Filthy foldering indexing.
  // Just mark the folder filthy and make sure that we reindex everyone.
  // IMPORTANT!  The trick of marking the folder filthy only works because
  //  we flushed/committed the database above; the PendingCommitTracker
  //  is not aware of bogus filthy-marking of folders.
  // We leave the verification of the implementation details to
  //  test_index_sweep_folder.js.
  let glodaFolderC = Gloda.getFolderForFolder(
    messageInjection.getRealInjectionFolder(folderC)
  );
  // Marked gloda folder dirty.
  glodaFolderC._dirtyStatus = glodaFolderC.kFolderFilthy;
  GlodaMsgIndexer.indexingSweepNeeded = true;
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([setC1, setC2]));

  // -- Forced folder indexing.
  var callbackInvoked = false;
  GlodaMsgIndexer.indexFolder(
    messageInjection.getRealInjectionFolder(folderA),
    {
      force: true,
      callback() {
        callbackInvoked = true;
      },
    }
  );
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([setA1, setA2]));
  Assert.ok(callbackInvoked);
}

/**
 * We used to screw up and downgrade filthy folders to dirty if we saw an event
 *  happen in the folder before we got to the folder; this tests that we no
 *  longer do that.
 */
async function test_event_driven_indexing_does_not_mess_with_filthy_folders() {
  // Add a folder with a message.
  let [[folder], msgSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1 },
  ]);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));

  // Fake marking the folder filthy.
  let glodaFolder = Gloda.getFolderForFolder(
    messageInjection.getRealInjectionFolder(folder)
  );
  glodaFolder._dirtyStatus = glodaFolder.kFolderFilthy;

  // Generate an event in the folder.
  msgSet.setRead(true);
  // Make sure the indexer did not do anything and the folder is still filthy.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([]));
  Assert.equal(glodaFolder._dirtyStatus, glodaFolder.kFolderFilthy);
  // Also, the message should not have actually gotten marked dirty.
  Assert.equal(msgSet.getMsgHdr(0).getUint32Property("gloda-dirty"), 0);

  // Let's make the message un-read again for consistency with the gloda state.
  msgSet.setRead(false);
  // Make the folder dirty and let an indexing sweep take care of this so we
  //  don't get extra events in subsequent tests.
  glodaFolder._dirtyStatus = glodaFolder.kFolderDirty;
  GlodaMsgIndexer.indexingSweepNeeded = true;
  // The message won't get indexed though.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([]));
}

async function test_indexing_never_priority() {
  // Add a folder with a bunch of messages.
  let [[folder], msgSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1 },
  ]);

  // Index it, and augment the msgSet with the glodaMessages array
  //  for later use by sqlExpectCount.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet], { augment: true }));

  // Explicitly tell gloda to never index this folder.
  let XPCOMFolder = messageInjection.getRealInjectionFolder(folder);
  let glodaFolder = Gloda.getFolderForFolder(XPCOMFolder);
  GlodaMsgIndexer.setFolderIndexingPriority(
    XPCOMFolder,
    glodaFolder.kIndexingNeverPriority
  );

  // Verify that the setter and getter do the right thing.
  Assert.equal(
    glodaFolder.indexingPriority,
    glodaFolder.kIndexingNeverPriority
  );

  // Check that existing message is marked as deleted.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([], { deleted: [msgSet] }));

  // Make sure the deletion hit the database.
  await sqlExpectCount(
    1,
    "SELECT COUNT(*) from folderLocations WHERE id = ? AND indexingPriority = ?",
    glodaFolder.id,
    glodaFolder.kIndexingNeverPriority
  );

  // Add another message.
  await messageInjection.makeNewSetsInFolders([folder], [{ count: 1 }]);

  // Make sure that indexing returns nothing.
  GlodaMsgIndexer.indexingSweepNeeded = true;
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([]));
}

async function test_setting_indexing_priority_never_while_indexing() {
  if (!messageInjection.messageInjectionIsLocal()) {
    return;
  }

  // Configure the gloda indexer to hang while streaming the message.
  configureGlodaIndexing({ hangWhile: "streaming" });

  // Create a folder with a message inside.
  let [[folder]] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1 },
  ]);

  await waitForIndexingHang();

  // Explicitly tell gloda to never index this folder.
  let XPCOMFolder = messageInjection.getRealInjectionFolder(folder);
  let glodaFolder = Gloda.getFolderForFolder(XPCOMFolder);
  GlodaMsgIndexer.setFolderIndexingPriority(
    XPCOMFolder,
    glodaFolder.kIndexingNeverPriority
  );

  // Reset indexing to not hang.
  configureGlodaIndexing({});

  // Sorta get the event chain going again.
  await resumeFromSimulatedHang(true);

  // Because the folder was dirty it should actually end up getting indexed,
  //  so in the end the message will get indexed.  Also, make sure a cleanup
  //  was observed.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([], { cleanedUp: 1 }));
}

/* ===== Threading / Conversation Grouping ===== */

var gSynMessages = [];
function allMessageInSameConversation(aSynthMessage, aGlodaMessage, aConvID) {
  if (aConvID === undefined) {
    return aGlodaMessage.conversationID;
  }
  Assert.equal(aConvID, aGlodaMessage.conversationID);
  // Cheat and stash the synthetic message (we need them for one of the IMAP
  //  tests).
  gSynMessages.push(aSynthMessage);
  return aConvID;
}

/**
 * Test our conversation/threading logic in the straight-forward direct
 *  reply case, the missing intermediary case, and the siblings with missing
 *  parent case.  We also test all permutations of receipt of those messages.
 * (Also tests that we index new messages.)
 */
async function test_threading_direct_reply() {
  let permutationMessages = await permuteMessages(
    scenarios.directReply,
    messageInjection
  );
  for (const preparedMessage of permutationMessages) {
    let message = await preparedMessage();
    await waitForGlodaIndexer();
    Assert.ok(
      ...assertExpectedMessagesIndexed([message], allMessageInSameConversation)
    );
  }
}

async function test_threading_missing_intermediary() {
  let permutationMessages = await permuteMessages(
    scenarios.missingIntermediary,
    messageInjection
  );
  for (const preparedMessage of permutationMessages) {
    let message = await preparedMessage();
    await waitForGlodaIndexer();
    Assert.ok(
      ...assertExpectedMessagesIndexed([message], allMessageInSameConversation)
    );
  }
}
async function test_threading_siblings_missing_parent() {
  let permutationMessages = await permuteMessages(
    scenarios.siblingsMissingParent,
    messageInjection
  );
  for (const preparedMessage of permutationMessages) {
    let message = await preparedMessage();
    await waitForGlodaIndexer();
    Assert.ok(
      ...assertExpectedMessagesIndexed([message], allMessageInSameConversation)
    );
  }
}

/**
 * Test the bit that says "if we're fulltext-indexing the message and we
 *  discover it didn't have any attachments, clear the attachment bit from the
 *  message header".
 */
async function test_attachment_flag() {
  // Create a synthetic message with an attachment that won't normally be listed
  //  in the attachment pane (Content-Disposition: inline, no filename, and
  //  displayable inline).
  let smsg = msgGen.makeMessage({
    name: "test message with part 1.2 attachment",
    attachments: [
      {
        body: "attachment",
        filename: "",
        format: "",
      },
    ],
  });
  // Save it off for test_attributes_fundamental_from_disk.
  let msgSet = new SyntheticMessageSet([smsg]);
  let folder = (fundamentalFolderHandle =
    await messageInjection.makeEmptyFolder());
  await messageInjection.addSetsToFolders([folder], [msgSet]);

  // If we need to go offline, let the indexing pass run, then force us offline.
  if (goOffline) {
    await waitForGlodaIndexer();
    Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
    await messageInjection.makeFolderAndContentsOffline(folder);
    // Now the next indexer wait will wait for the next indexing pass.
  }

  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([msgSet], {
      verifier: verify_attachment_flag,
    })
  );
}

function verify_attachment_flag(smsg, gmsg) {
  // -- Attachments. We won't have these if we don't have fulltext results.
  if (expectFulltextResults) {
    Assert.equal(gmsg.attachmentNames.length, 0);
    Assert.equal(gmsg.attachmentInfos.length, 0);
    Assert.equal(
      false,
      gmsg.folderMessage.flags & Ci.nsMsgMessageFlags.Attachment
    );
  }
}
/* ===== Fundamental Attributes (per GlodaFundAttr.jsm) ===== */

/**
 * Save the synthetic message created in test_attributes_fundamental for the
 *  benefit of test_attributes_fundamental_from_disk.
 */
var fundamentalSyntheticMessage;
var fundamentalFolderHandle;
/**
 * We're saving this one so that we can move the message later and verify that
 * the attributes are consistent.
 */
var fundamentalMsgSet;
var fundamentalGlodaMsgAttachmentUrls;
/**
 * Save the resulting gloda message id corresponding to the
 *  fundamentalSyntheticMessage so we can use it to query the message from disk.
 */
var fundamentalGlodaMessageId;

/**
 * Test that we extract the 'fundamental attributes' of a message properly
 *  'Fundamental' in this case is talking about the attributes defined/extracted
 *  by gloda's GlodaFundAttr.jsm and perhaps the core message indexing logic itself
 *  (which show up as kSpecial* attributes in GlodaFundAttr.jsm anyways.)
 */
async function test_attributes_fundamental() {
  // Create a synthetic message with attachment.
  let smsg = msgGen.makeMessage({
    name: "test message",
    bodyPart: new SyntheticPartMultiMixed([
      new SyntheticPartLeaf({ body: "I like cheese!" }),
      msgGen.makeMessage({ body: { body: "I like wine!" } }), // That's one attachment.
    ]),
    attachments: [
      { filename: "bob.txt", body: "I like bread!" }, // And that's another one.
    ],
  });
  // Save it off for test_attributes_fundamental_from_disk.
  fundamentalSyntheticMessage = smsg;
  let msgSet = new SyntheticMessageSet([smsg]);
  fundamentalMsgSet = msgSet;
  let folder = (fundamentalFolderHandle =
    await messageInjection.makeEmptyFolder());
  await messageInjection.addSetsToFolders([folder], [msgSet]);

  // If we need to go offline, let the indexing pass run, then force us offline.
  if (goOffline) {
    await waitForGlodaIndexer();
    Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
    await messageInjection.makeFolderAndContentsOffline(folder);
    // Now the next indexer wait will wait for the next indexing pass.
  }

  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([msgSet], {
      verifier: verify_attributes_fundamental,
    })
  );
}

function verify_attributes_fundamental(smsg, gmsg) {
  // Save off the message id for test_attributes_fundamental_from_disk.
  fundamentalGlodaMessageId = gmsg.id;
  if (gmsg.attachmentInfos) {
    fundamentalGlodaMsgAttachmentUrls = gmsg.attachmentInfos.map(
      att => att.url
    );
  } else {
    fundamentalGlodaMsgAttachmentUrls = [];
  }

  Assert.equal(
    gmsg.folderURI,
    messageInjection.getRealInjectionFolder(fundamentalFolderHandle).URI
  );

  // -- Subject
  Assert.equal(smsg.subject, gmsg.conversation.subject);
  Assert.equal(smsg.subject, gmsg.subject);

  // -- Contact/identity information.
  // - From
  // Check the e-mail address.
  Assert.equal(gmsg.from.kind, "email");
  Assert.equal(smsg.fromAddress, gmsg.from.value);
  // Check the name.
  Assert.equal(smsg.fromName, gmsg.from.contact.name);

  // - To
  Assert.equal(smsg.toAddress, gmsg.to[0].value);
  Assert.equal(smsg.toName, gmsg.to[0].contact.name);

  // Date
  Assert.equal(smsg.date.valueOf(), gmsg.date.valueOf());

  // -- Message ID
  Assert.equal(smsg.messageId, gmsg.headerMessageID);

  // -- Attachments. We won't have these if we don't have fulltext results.
  if (expectFulltextResults) {
    Assert.equal(gmsg.attachmentTypes.length, 1);
    Assert.equal(gmsg.attachmentTypes[0], "text/plain");
    Assert.equal(gmsg.attachmentNames.length, 1);
    Assert.equal(gmsg.attachmentNames[0], "bob.txt");

    let expectedInfos = [
      // The name for that one is generated randomly.
      { contentType: "message/rfc822" },
      { name: "bob.txt", contentType: "text/plain" },
    ];
    let expectedSize = 14;
    Assert.equal(gmsg.attachmentInfos.length, 2);
    for (let [i, attInfos] of gmsg.attachmentInfos.entries()) {
      for (let k in expectedInfos[i]) {
        Assert.equal(attInfos[k], expectedInfos[i][k]);
      }
      // Because it's unreliable and depends on the platform.
      Assert.ok(Math.abs(attInfos.size - expectedSize) <= 2);
      // Check that the attachment URLs are correct.
      let channel = NetUtil.newChannel({
        uri: attInfos.url,
        loadingPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
        securityFlags:
          Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
        contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER,
      });

      try {
        // Will throw if the URL is invalid.
        channel.asyncOpen(new PromiseTestUtils.PromiseStreamListener());
      } catch (e) {
        do_throw(new Error("Invalid attachment URL"));
      }
    }
  } else {
    // Make sure we don't actually get attachments!
    Assert.equal(gmsg.attachmentTypes, null);
    Assert.equal(gmsg.attachmentNames, null);
  }
}

/**
 * We now move the message into another folder, wait for it to be indexed,
 * and make sure the magic url getter for GlodaAttachment returns a proper
 * URL.
 */
async function test_moved_message_attributes() {
  if (!expectFulltextResults) {
    return;
  }

  // Don't ask me why, let destFolder = MessageInjection.make_empty_folder would result in a
  //  random error when running test_index_messages_imap_offline.js ...
  let [[destFolder], ignoreSet] = await messageInjection.makeFoldersWithSets(
    1,
    [{ count: 2 }]
  );
  fundamentalFolderHandle = destFolder;
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([ignoreSet]));

  // This is a fast move (third parameter set to true).
  await messageInjection.moveMessages(fundamentalMsgSet, destFolder, true);

  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([fundamentalMsgSet], {
      verifier(newSynMsg, newGlodaMsg) {
        // Verify we still have the same number of attachments.
        Assert.equal(
          fundamentalGlodaMsgAttachmentUrls.length,
          newGlodaMsg.attachmentInfos.length
        );
        for (let [i, attInfos] of newGlodaMsg.attachmentInfos.entries()) {
          // Verify the url has changed.
          Assert.notEqual(fundamentalGlodaMsgAttachmentUrls[i], attInfos.url);
          // And verify that the new url is still valid.
          let channel = NetUtil.newChannel({
            uri: attInfos.url,
            loadingPrincipal:
              Services.scriptSecurityManager.getSystemPrincipal(),
            securityFlags:
              Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
            contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER,
          });
          try {
            channel.asyncOpen(new PromiseTestUtils.PromiseStreamListener());
          } catch (e) {
            new Error("Invalid attachment URL");
          }
        }
      },
      fullyIndexed: 0,
    })
  );
}

/**
 * We want to make sure that all of the fundamental properties also are there
 *  when we load them from disk.  Nuke our cache, query the message back up.
 *  We previously used getMessagesByMessageID to get the message back, but he
 *  does not perform a full load-out like a query does, so we need to use our
 *  query mechanism for this.
 */
async function test_attributes_fundamental_from_disk() {
  nukeGlodaCachesAndCollections();

  let query = Gloda.newQuery(GlodaConstants.NOUN_MESSAGE).id(
    fundamentalGlodaMessageId
  );
  await queryExpect(
    query,
    [fundamentalSyntheticMessage],
    verify_attributes_fundamental_from_disk,
    function (smsg) {
      return smsg.messageId;
    }
  );
}

/**
 * We are just a wrapper around verify_attributes_fundamental, adapting the
 *  return callback from getMessagesByMessageID.
 *
 * @param aGlodaMessageLists This should be [[theGlodaMessage]].
 */
function verify_attributes_fundamental_from_disk(aGlodaMessage) {
  // Teturn the message id for test_attributes_fundamental_from_disk's benefit.
  verify_attributes_fundamental(fundamentalSyntheticMessage, aGlodaMessage);
  return aGlodaMessage.headerMessageID;
}

/* ===== Explicit Attributes (per GlodaExplicitAttr.jsm) ===== */

/**
 * Test the attributes defined by GlodaExplicitAttr.jsm.
 */
async function test_attributes_explicit() {
  let [, msgSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1 },
  ]);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet], { augment: true }));
  let gmsg = msgSet.glodaMessages[0];

  // -- Star
  msgSet.setStarred(true);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
  Assert.equal(gmsg.starred, true);

  msgSet.setStarred(false);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
  Assert.equal(gmsg.starred, false);

  // -- Read / Unread
  msgSet.setRead(true);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
  Assert.equal(gmsg.read, true);

  msgSet.setRead(false);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
  Assert.equal(gmsg.read, false);

  // -- Tags
  // Note that the tag service does not guarantee stable nsIMsgTag references,
  //  nor does noun_tag go too far out of its way to provide stability.
  //  However, it is stable as long as we don't spook it by bringing new tags
  //  into the equation.
  let tagOne = TagNoun.getTag("$label1");
  let tagTwo = TagNoun.getTag("$label2");

  msgSet.addTag(tagOne.key);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
  Assert.notEqual(gmsg.tags.indexOf(tagOne), -1);

  msgSet.addTag(tagTwo.key);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
  Assert.notEqual(gmsg.tags.indexOf(tagOne), -1);
  Assert.notEqual(gmsg.tags.indexOf(tagTwo), -1);

  msgSet.removeTag(tagOne.key);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
  Assert.equal(gmsg.tags.indexOf(tagOne), -1);
  Assert.notEqual(gmsg.tags.indexOf(tagTwo), -1);

  msgSet.removeTag(tagTwo.key);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
  Assert.equal(gmsg.tags.indexOf(tagOne), -1);
  Assert.equal(gmsg.tags.indexOf(tagTwo), -1);

  // -- Replied To

  // -- Forwarded
}

/**
 * Test non-query-able attributes
 */
async function test_attributes_cant_query() {
  let [, msgSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1 },
  ]);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet], { augment: true }));
  let gmsg = msgSet.glodaMessages[0];

  // -- Star
  msgSet.setStarred(true);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
  Assert.equal(gmsg.starred, true);

  msgSet.setStarred(false);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
  Assert.equal(gmsg.starred, false);

  // -- Read / Unread
  msgSet.setRead(true);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
  Assert.equal(gmsg.read, true);

  msgSet.setRead(false);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
  Assert.equal(gmsg.read, false);

  let readDbAttr = Gloda.getAttrDef(GlodaConstants.BUILT_IN, "read");
  let readId = readDbAttr.id;

  await sqlExpectCount(
    0,
    "SELECT COUNT(*) FROM messageAttributes WHERE attributeID = ?1",
    readId
  );

  // -- Replied To

  // -- Forwarded
}

/**
 * Have the participants be in our addressbook prior to indexing so that we can
 *  verify that the hand-off to the addressbook indexer does not cause breakage.
 */
async function test_people_in_addressbook() {
  var senderPair = msgGen.makeNameAndAddress(),
    recipPair = msgGen.makeNameAndAddress();

  // - Add both people to the address book.
  makeABCardForAddressPair(senderPair);
  makeABCardForAddressPair(recipPair);

  let [, msgSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1, to: [recipPair], from: senderPair },
  ]);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet], { augment: true }));
  let gmsg = msgSet.glodaMessages[0],
    senderIdentity = gmsg.from,
    recipIdentity = gmsg.to[0];

  Assert.notEqual(senderIdentity.contact, null);
  Assert.ok(senderIdentity.inAddressBook);

  Assert.notEqual(recipIdentity.contact, null);
  Assert.ok(recipIdentity.inAddressBook);
}

/* ===== Fulltexts Indexing ===== */

/**
 * Make sure that we are using the saneBodySize flag.  This is basically the
 *  test_sane_bodies test from test_mime_emitter but we pull the indexedBodyText
 *  off the message to check and also make sure that the text contents slice
 *  off the end rather than the beginning.
 */
async function test_streamed_bodies_are_size_capped() {
  if (!expectFulltextResults) {
    return;
  }

  let hugeString =
    "qqqqxxxx qqqqxxx qqqqxxx qqqqxxx qqqqxxx qqqqxxx qqqqxxx \r\n";
  const powahsOfTwo = 10;
  for (let i = 0; i < powahsOfTwo; i++) {
    hugeString = hugeString + hugeString;
  }
  let bodyString = "aabb" + hugeString + "xxyy";

  let synMsg = msgGen.makeMessage({
    body: { body: bodyString, contentType: "text/plain" },
  });
  let msgSet = new SyntheticMessageSet([synMsg]);
  let folder = await messageInjection.makeEmptyFolder();
  await messageInjection.addSetsToFolders([folder], [msgSet]);

  if (goOffline) {
    await waitForGlodaIndexer();
    Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
    await messageInjection.makeFolderAndContentsOffline(folder);
  }

  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet], { augment: true }));
  let gmsg = msgSet.glodaMessages[0];
  Assert.ok(gmsg.indexedBodyText.startsWith("aabb"));
  Assert.ok(!gmsg.indexedBodyText.includes("xxyy"));

  if (gmsg.indexedBodyText.length > 20 * 1024 + 58 + 10) {
    do_throw(
      "Indexed body text is too big! (" + gmsg.indexedBodyText.length + ")"
    );
  }
}

/* ===== Message Deletion ===== */
/**
 * Test actually deleting a message on a per-message basis (not just nuking the
 *  folder like emptying the trash does.)
 *
 * Logic situations:
 * - Non-last message in a conversation, twin.
 * - Non-last message in a conversation, not a twin.
 * - Last message in a conversation
 */
async function test_message_deletion() {
  // Non-last message in conv, twin.
  // Create and index two messages in a conversation.
  let [, convSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 2, msgsPerThread: 2 },
  ]);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([convSet], { augment: true }));

  // Twin the first message in a different folder owing to our reliance on
  //  message-id's in the SyntheticMessageSet logic.  (This is also why we broke
  //  up the indexing waits too.)
  let twinFolder = await messageInjection.makeEmptyFolder();
  let twinSet = new SyntheticMessageSet([convSet.synMessages[0]]);
  await messageInjection.addSetsToFolders([twinFolder], [twinSet]);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([twinSet], { augment: true }));

  // Split the conv set into two helper sets.
  let firstSet = convSet.slice(0, 1); // The twinned first message in the thread.
  let secondSet = convSet.slice(1, 2); // The un-twinned second thread message.

  // Make sure we can find the message (paranoia).
  let firstQuery = Gloda.newQuery(GlodaConstants.NOUN_MESSAGE);
  firstQuery.id(firstSet.glodaMessages[0].id);
  let firstColl = await queryExpect(firstQuery, firstSet);

  // Delete it (not trash! delete!).
  await MessageInjection.deleteMessages(firstSet);
  // Which should result in an apparent deletion.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([], { deleted: [firstSet] }));
  // And our collection from that query should now be empty.
  Assert.equal(firstColl.items.length, 0);

  // Make sure it no longer shows up in a standard query.
  firstColl = await queryExpect(firstQuery, []);

  // Make sure it shows up in a privileged query.
  let privQuery = Gloda.newQuery(GlodaConstants.NOUN_MESSAGE, {
    noDbQueryValidityConstraints: true,
  });
  let firstGlodaId = firstSet.glodaMessages[0].id;
  privQuery.id(firstGlodaId);
  await queryExpect(privQuery, firstSet);

  // Force a deletion pass.
  GlodaMsgIndexer.indexingSweepNeeded = true;
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([]));

  // Make sure it no longer shows up in a privileged query; since it has a twin
  //  we don't need to leave it as a ghost.
  await queryExpect(privQuery, []);

  // Make sure that the messagesText entry got blown away.
  await sqlExpectCount(
    0,
    "SELECT COUNT(*) FROM messagesText WHERE docid = ?1",
    firstGlodaId
  );

  // Make sure the conversation still exists.
  let conv = twinSet.glodaMessages[0].conversation;
  let convQuery = Gloda.newQuery(GlodaConstants.NOUN_CONVERSATION);
  convQuery.id(conv.id);
  let convColl = await queryExpect(convQuery, [conv]);

  // -- Non-last message, no longer a twin => ghost.

  // Make sure nuking the twin didn't somehow kill them both.
  let twinQuery = Gloda.newQuery(GlodaConstants.NOUN_MESSAGE);
  // Let's search on the message-id now that there is no ambiguity.
  twinQuery.headerMessageID(twinSet.synMessages[0].messageId);
  let twinColl = await queryExpect(twinQuery, twinSet);

  // Delete the twin.
  await MessageInjection.deleteMessages(twinSet);
  // Which should result in an apparent deletion.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([], { deleted: [twinSet] }));
  // It should disappear from the collection.
  Assert.equal(twinColl.items.length, 0);

  // No longer show up in the standard query.
  twinColl = await queryExpect(twinQuery, []);

  // Still show up in a privileged query.
  privQuery = Gloda.newQuery(GlodaConstants.NOUN_MESSAGE, {
    noDbQueryValidityConstraints: true,
  });
  privQuery.headerMessageID(twinSet.synMessages[0].messageId);
  await queryExpect(privQuery, twinSet);

  // Force a deletion pass.
  GlodaMsgIndexer.indexingSweepNeeded = true;
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([]));

  // The message should be marked as a ghost now that the deletion pass.
  // Ghosts have no fulltext rows, so check for that.
  await sqlExpectCount(
    0,
    "SELECT COUNT(*) FROM messagesText WHERE docid = ?1",
    twinSet.glodaMessages[0].id
  );

  // It still should show up in the privileged query; it's a ghost!
  let privColl = await queryExpect(privQuery, twinSet);
  // Make sure it looks like a ghost.
  let twinGhost = privColl.items[0];
  Assert.equal(twinGhost._folderID, null);
  Assert.equal(twinGhost._messageKey, null);

  // Make sure the conversation still exists.
  await queryExpect(convQuery, [conv]);

  // -- Non-last message, not a twin.
  // This should blow away the message, the ghosts, and the conversation.

  // Second message should still be around.
  let secondQuery = Gloda.newQuery(GlodaConstants.NOUN_MESSAGE);
  secondQuery.headerMessageID(secondSet.synMessages[0].messageId);
  let secondColl = await queryExpect(secondQuery, secondSet);

  // Delete it and make sure it gets marked deleted appropriately.
  await MessageInjection.deleteMessages(secondSet);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([], { deleted: [secondSet] }));
  Assert.equal(secondColl.items.length, 0);

  // Still show up in a privileged query.
  privQuery = Gloda.newQuery(GlodaConstants.NOUN_MESSAGE, {
    noDbQueryValidityConstraints: true,
  });
  privQuery.headerMessageID(secondSet.synMessages[0].messageId);
  await queryExpect(privQuery, secondSet);

  // Force a deletion pass.
  GlodaMsgIndexer.indexingSweepNeeded = true;
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([]));

  // It should no longer show up in a privileged query; we killed the ghosts.
  await queryExpect(privQuery, []);

  // - The conversation should have disappeared too.
  // (we have no listener to watch for it to have disappeared from convQuery but
  //  this is basically how glodaTestHelper does its thing anyways.)
  Assert.equal(convColl.items.length, 0);

  // Make sure the query fails to find it too.
  await queryExpect(convQuery, []);

  // -- Identity culling verification.
  // The identities associated with that message should no longer exist, nor
  //  should their contacts.
}

async function test_moving_to_trash_marks_deletion() {
  // Create and index two messages in a conversation.
  let [, msgSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 2, msgsPerThread: 2 },
  ]);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet], { augment: true }));

  let convId = msgSet.glodaMessages[0].conversation.id;
  let firstGlodaId = msgSet.glodaMessages[0].id;
  let secondGlodaId = msgSet.glodaMessages[1].id;

  // Move them to the trash.
  await messageInjection.trashMessages(msgSet);

  // We do not index the trash folder so this should actually make them appear
  //  deleted to an unprivileged query.
  let msgQuery = Gloda.newQuery(GlodaConstants.NOUN_MESSAGE);
  msgQuery.id(firstGlodaId, secondGlodaId);
  await queryExpect(msgQuery, []);

  // They will appear deleted after the events.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([], { deleted: [msgSet] }));

  // Force a sweep.
  GlodaMsgIndexer.indexingSweepNeeded = true;
  // There should be no apparent change as the result of this pass.
  // Well, the conversation will die, but we can't see that.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([]));

  // The conversation should be gone.
  let convQuery = Gloda.newQuery(GlodaConstants.NOUN_CONVERSATION);
  convQuery.id(convId);
  await queryExpect(convQuery, []);

  // The messages should be entirely gone.
  let msgPrivQuery = Gloda.newQuery(GlodaConstants.NOUN_MESSAGE, {
    noDbQueryValidityConstraints: true,
  });
  msgPrivQuery.id(firstGlodaId, secondGlodaId);
  await queryExpect(msgPrivQuery, []);
}

/**
 * Deletion that occurs because a folder got deleted.
 *  There is no hand-holding involving the headers that were in the folder.
 */
async function test_folder_nuking_message_deletion() {
  // Create and index two messages in a conversation.
  let [[folder], msgSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 2, msgsPerThread: 2 },
  ]);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet], { augment: true }));

  let convId = msgSet.glodaMessages[0].conversation.id;
  let firstGlodaId = msgSet.glodaMessages[0].id;
  let secondGlodaId = msgSet.glodaMessages[1].id;

  // Delete the folder.
  messageInjection.deleteFolder(folder);
  // That does generate the deletion events if the messages were in-memory,
  //  which these are.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([], { deleted: [msgSet] }));

  // This should have caused us to mark all the messages as deleted; the
  //  messages should no longer show up in an unprivileged query.
  let msgQuery = Gloda.newQuery(GlodaConstants.NOUN_MESSAGE);
  msgQuery.id(firstGlodaId, secondGlodaId);
  await queryExpect(msgQuery, []);

  // Force a sweep.
  GlodaMsgIndexer.indexingSweepNeeded = true;
  // There should be no apparent change as the result of this pass.
  // Well, the conversation will die, but we can't see that.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([]));

  // The conversation should be gone.
  let convQuery = Gloda.newQuery(GlodaConstants.NOUN_CONVERSATION);
  convQuery.id(convId);
  await queryExpect(convQuery, []);

  // The messages should be entirely gone.
  let msgPrivQuery = Gloda.newQuery(GlodaConstants.NOUN_MESSAGE, {
    noDbQueryValidityConstraints: true,
  });
  msgPrivQuery.id(firstGlodaId, secondGlodaId);
  await queryExpect(msgPrivQuery, []);
}

/* ===== Folder Move/Rename/Copy (Single and Nested) ===== */

async function test_folder_deletion_nested() {
  // Add a folder with a bunch of messages.
  let [[folder1], msgSet1] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1 },
  ]);

  let [[folder2], msgSet2] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1 },
  ]);

  // Index these folders, and augment the msgSet with the glodaMessages array
  //  for later use by sqlExpectCount.
  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([msgSet1, msgSet2], { augment: true })
  );
  // The move has to be performed after the indexing, because otherwise, on
  //  IMAP, the moved message header are different entities and it's not msgSet2
  //  that ends up indexed, but the fresh headers
  await MessageInjection.moveFolder(folder2, folder1);

  // Add a trash folder, and move folder1 into it.
  let trash = await messageInjection.makeEmptyFolder(null, [
    Ci.nsMsgFolderFlags.Trash,
  ]);
  await MessageInjection.moveFolder(folder1, trash);

  let folders = MessageInjection.get_nsIMsgFolder(trash).descendants;
  Assert.equal(folders.length, 2);
  let [newFolder1, newFolder2] = folders;

  let glodaFolder1 = Gloda.getFolderForFolder(newFolder1);
  let glodaFolder2 = Gloda.getFolderForFolder(newFolder2);

  // Verify that Gloda properly marked this folder as not to be indexed anymore.
  Assert.equal(
    glodaFolder1.indexingPriority,
    glodaFolder1.kIndexingNeverPriority
  );

  // Check that existing message is marked as deleted.
  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([], { deleted: [msgSet1, msgSet2] })
  );

  // Make sure the deletion hit the database.
  await sqlExpectCount(
    1,
    "SELECT COUNT(*) from folderLocations WHERE id = ? AND indexingPriority = ?",
    glodaFolder1.id,
    glodaFolder1.kIndexingNeverPriority
  );
  await sqlExpectCount(
    1,
    "SELECT COUNT(*) from folderLocations WHERE id = ? AND indexingPriority = ?",
    glodaFolder2.id,
    glodaFolder2.kIndexingNeverPriority
  );

  if (messageInjection.messageInjectionIsLocal()) {
    // Add another message.
    await messageInjection.makeNewSetsInFolders([newFolder1], [{ count: 1 }]);
    await messageInjection.makeNewSetsInFolders([newFolder2], [{ count: 1 }]);

    // Make sure that indexing returns nothing.
    GlodaMsgIndexer.indexingSweepNeeded = true;
    await waitForGlodaIndexer();
    Assert.ok(...assertExpectedMessagesIndexed([]));
  }
}

/* ===== IMAP Nuances ===== */

/**
 * Verify that for IMAP folders we still see an index a message that is added
 *  as read.
 */
async function test_imap_add_unread_to_folder() {
  if (messageInjection.messageInjectionIsLocal()) {
    return;
  }

  let [, msgSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1, read: true },
  ]);
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
}

/* ===== Message Moving ===== */

/**
 * Moving a message between folders should result in us knowing that the message
 *  is in the target location.
 */
async function test_message_moving() {
  // - Inject and insert.
  // Source folder with the message we care about.
  let [[srcFolder], msgSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1 },
  ]);
  // Dest folder with some messages in it to test some wacky local folder moving
  //  logic.  (Local moves try and update the correspondence immediately.)
  let [[destFolder], ignoreSet] = await messageInjection.makeFoldersWithSets(
    1,
    [{ count: 2 }]
  );

  // We want the gloda message mapping.
  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([msgSet, ignoreSet], { augment: true })
  );
  let gmsg = msgSet.glodaMessages[0];
  // Save off the message key so we can make sure it changes.
  let oldMessageKey = msgSet.getMsgHdr(0).messageKey;

  // - Fastpath (offline) move it to a new folder.
  // Initial move.
  await messageInjection.moveMessages(msgSet, destFolder, true);

  // - Make sure gloda sees it in the new folder.
  // Since we are doing offline IMAP moves, the fast-path should be taken and
  //  so we should receive an itemsModified notification without a call to
  //  Gloda.grokNounItem.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet], { fullyIndexed: 0 }));

  Assert.equal(
    gmsg.folderURI,
    messageInjection.getRealInjectionFolder(destFolder).URI
  );

  // - Make sure the message key is correct!
  Assert.equal(gmsg.messageKey, msgSet.getMsgHdr(0).messageKey);
  // Sanity check that the messageKey actually changed for the message.
  Assert.notEqual(gmsg.messageKey, oldMessageKey);

  // - Make sure the indexer's _keyChangedBatchInfo dict is empty.
  for (let evilKey in GlodaMsgIndexer._keyChangedBatchInfo) {
    let evilValue = GlodaMsgIndexer._keyChangedBatchInfo[evilKey];
    throw new Error(
      "GlodaMsgIndexer._keyChangedBatchInfo should be empty but" +
        "has key:\n" +
        evilKey +
        "\nAnd value:\n",
      evilValue + "."
    );
  }

  // - Slowpath (IMAP online) move it back to its origin folder.
  // Move it back.
  await messageInjection.moveMessages(msgSet, srcFolder, false);
  // In the IMAP case we will end up reindexing the message because we will
  //  not be able to fast-path, but the local case will still be fast-pathed.
  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([msgSet], {
      fullyIndexed: messageInjection.messageInjectionIsLocal() ? 0 : 1,
    })
  );
  Assert.equal(
    gmsg.folderURI,
    messageInjection.getRealInjectionFolder(srcFolder).URI
  );
  Assert.equal(gmsg.messageKey, msgSet.getMsgHdr(0).messageKey);
}

/**
 * Moving a gloda-indexed message out of a filthy folder should result in the
 *  destination message not having a gloda-id.
 */

/* ===== Message Copying ===== */

/* ===== Sweep Complications ==== */

/**
 * Make sure that a message indexed by event-driven indexing does not
 *  get reindexed by sweep indexing that follows.
 */
async function test_sweep_indexing_does_not_reindex_event_indexed() {
  let [[folder], msgSet] = await messageInjection.makeFoldersWithSets(1, [
    { count: 1 },
  ]);

  // Wait for the event sweep to complete.
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));

  // Force a sweep of the folder.
  GlodaMsgIndexer.indexFolder(messageInjection.getRealInjectionFolder(folder));
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([]));
}

/**
 * Verify that moving apparently gloda-indexed messages from a filthy folder or
 *  one that simply should not be gloda indexed does not result in the target
 *  messages having the gloda-id property on them.  To avoid messing with too
 *  many invariants we do the 'folder should not be gloda indexed' case.
 * Uh, and of course, the message should still get indexed once we clear the
 *  filthy gloda-id off of it given that it is moving from a folder that is not
 *  indexed to one that is indexed.
 */
async function test_filthy_moves_slash_move_from_unindexed_to_indexed() {
  // - Inject.
  // The source folder needs a flag so we don't index it.
  let srcFolder = await messageInjection.makeEmptyFolder(null, [
    Ci.nsMsgFolderFlags.Junk,
  ]);
  // The destination folder has to be something we want to index though.
  let destFolder = await messageInjection.makeEmptyFolder();
  let [msgSet] = await messageInjection.makeNewSetsInFolders(
    [srcFolder],
    [{ count: 1 }]
  );

  // - Mark with a bogus gloda-id.
  msgSet.getMsgHdr(0).setUint32Property("gloda-id", 9999);

  // - Disable event driven indexing so we don't get interference from indexing.
  configureGlodaIndexing({ event: false });

  // - Move.
  await messageInjection.moveMessages(msgSet, destFolder);

  // - Verify the target has no gloda-id!
  dump(`checking  ${msgSet.getMsgHdr(0)}`);
  Assert.equal(msgSet.getMsgHdr(0).getUint32Property("gloda-id"), 0);

  // - Re-enable indexing and let the indexer run.
  // We don't want to affect other tests.
  configureGlodaIndexing({});
  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([msgSet]));
}

function test_sanity_test_environment() {
  Assert.ok(msgGen, "Sanity that msgGen is set.");
  Assert.ok(scenarios, "Sanity that scenarios is set");
  Assert.ok(messageInjection, "Sanity that messageInjection is set.");
}

var base_index_messages_tests = [
  test_sanity_test_environment,
  test_pending_commit_tracker_flushes_correctly,
  test_pending_commit_causes_msgdb_commit,
  test_indexing_sweep,
  test_event_driven_indexing_does_not_mess_with_filthy_folders,

  test_threading_direct_reply,
  test_threading_missing_intermediary,
  test_threading_siblings_missing_parent,
  test_attachment_flag,
  test_attributes_fundamental,
  test_moved_message_attributes,
  test_attributes_fundamental_from_disk,
  test_attributes_explicit,
  test_attributes_cant_query,

  test_people_in_addressbook,

  test_streamed_bodies_are_size_capped,

  test_imap_add_unread_to_folder,
  test_message_moving,

  test_message_deletion,
  test_moving_to_trash_marks_deletion,
  test_folder_nuking_message_deletion,

  test_sweep_indexing_does_not_reindex_event_indexed,

  test_filthy_moves_slash_move_from_unindexed_to_indexed,

  test_indexing_never_priority,
  test_setting_indexing_priority_never_while_indexing,

  test_folder_deletion_nested,
];