-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathquery-history-manager.test.ts
More file actions
1175 lines (1015 loc) · 41.2 KB
/
query-history-manager.test.ts
File metadata and controls
1175 lines (1015 loc) · 41.2 KB
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
import { join } from "path";
import * as vscode from "vscode";
import { extLogger } from "../../../../src/common";
import { QueryHistoryManager } from "../../../../src/query-history/query-history-manager";
import { QueryHistoryConfigListener } from "../../../../src/config";
import { LocalQueryInfo } from "../../../../src/query-results";
import { DatabaseManager } from "../../../../src/local-databases";
import { tmpDir } from "../../../../src/helpers";
import { HistoryItemLabelProvider } from "../../../../src/query-history/history-item-label-provider";
import { ResultsView } from "../../../../src/interface";
import { EvalLogViewer } from "../../../../src/eval-log-viewer";
import { QueryRunner } from "../../../../src/queryRunner";
import { VariantAnalysisManager } from "../../../../src/variant-analysis/variant-analysis-manager";
import { QueryHistoryInfo } from "../../../../src/query-history/query-history-info";
import {
createMockLocalQueryInfo,
createMockQueryWithResults,
} from "../../../factories/query-history/local-query-history-item";
import { shuffleHistoryItems } from "../../utils/query-history-helpers";
import { createMockVariantAnalysisHistoryItem } from "../../../factories/query-history/variant-analysis-history-item";
import { VariantAnalysisHistoryItem } from "../../../../src/query-history/variant-analysis-history-item";
import { QueryStatus } from "../../../../src/query-status";
import { VariantAnalysisStatus } from "../../../../src/variant-analysis/shared/variant-analysis";
import { TextEditor } from "vscode";
import { WebviewReveal } from "../../../../src/interface-utils";
import * as helpers from "../../../../src/helpers";
import { mockedObject, mockedQuickPickItem } from "../../utils/mocking.helpers";
import { createMockQueryHistoryDirs } from "../../../factories/query-history/query-history-dirs";
describe("QueryHistoryManager", () => {
const mockExtensionLocation = join(tmpDir.name, "mock-extension-location");
let configListener: QueryHistoryConfigListener;
let showTextDocumentSpy: jest.SpiedFunction<
typeof vscode.window.showTextDocument
>;
let showInformationMessageSpy: jest.SpiedFunction<
typeof vscode.window.showInformationMessage
>;
let showQuickPickSpy: jest.SpiedFunction<typeof vscode.window.showQuickPick>;
let executeCommandSpy: jest.SpiedFunction<
typeof vscode.commands.executeCommand
>;
let cancelVariantAnalysisSpy: jest.SpiedFunction<
typeof variantAnalysisManagerStub.cancelVariantAnalysis
>;
const doCompareCallback = jest.fn();
let queryHistoryManager: QueryHistoryManager;
let localQueriesResultsViewStub: ResultsView;
let variantAnalysisManagerStub: VariantAnalysisManager;
let tryOpenExternalFile: Function;
let allHistory: QueryHistoryInfo[];
let localQueryHistory: LocalQueryInfo[];
let variantAnalysisHistory: VariantAnalysisHistoryItem[];
beforeEach(() => {
showTextDocumentSpy = jest
.spyOn(vscode.window, "showTextDocument")
.mockResolvedValue(mockedObject<TextEditor>({}));
showInformationMessageSpy = jest
.spyOn(vscode.window, "showInformationMessage")
.mockResolvedValue(undefined);
showQuickPickSpy = jest
.spyOn(vscode.window, "showQuickPick")
.mockResolvedValue(undefined);
executeCommandSpy = jest
.spyOn(vscode.commands, "executeCommand")
.mockResolvedValue(undefined);
jest.spyOn(extLogger, "log").mockResolvedValue(undefined);
tryOpenExternalFile = (QueryHistoryManager.prototype as any)
.tryOpenExternalFile;
configListener = new QueryHistoryConfigListener();
localQueriesResultsViewStub = {
showResults: jest.fn(),
} as any as ResultsView;
variantAnalysisManagerStub = {
onVariantAnalysisAdded: jest.fn(),
onVariantAnalysisStatusUpdated: jest.fn(),
onVariantAnalysisRemoved: jest.fn(),
removeVariantAnalysis: jest.fn(),
cancelVariantAnalysis: jest.fn(),
showView: jest.fn(),
} as any as VariantAnalysisManager;
cancelVariantAnalysisSpy = jest
.spyOn(variantAnalysisManagerStub, "cancelVariantAnalysis")
.mockResolvedValue(undefined);
localQueryHistory = [
// completed
createMockLocalQueryInfo({
dbName: "a",
queryWithResults: createMockQueryWithResults({
didRunSuccessfully: true,
}),
}),
// completed
createMockLocalQueryInfo({
dbName: "b",
queryWithResults: createMockQueryWithResults({
didRunSuccessfully: true,
}),
}),
// failed
createMockLocalQueryInfo({
dbName: "a",
queryWithResults: createMockQueryWithResults({
didRunSuccessfully: false,
}),
}),
// completed
createMockLocalQueryInfo({
dbName: "a",
queryWithResults: createMockQueryWithResults({
didRunSuccessfully: true,
}),
}),
// in progress
createMockLocalQueryInfo({ resultCount: 0 }),
// in progress
createMockLocalQueryInfo({ resultCount: 0 }),
];
variantAnalysisHistory = [
createMockVariantAnalysisHistoryItem({
historyItemStatus: QueryStatus.Completed,
variantAnalysisStatus: VariantAnalysisStatus.Succeeded,
}),
createMockVariantAnalysisHistoryItem({
historyItemStatus: QueryStatus.InProgress,
variantAnalysisStatus: VariantAnalysisStatus.InProgress,
}),
createMockVariantAnalysisHistoryItem({
historyItemStatus: QueryStatus.Failed,
variantAnalysisStatus: VariantAnalysisStatus.Failed,
}),
createMockVariantAnalysisHistoryItem({
historyItemStatus: QueryStatus.InProgress,
variantAnalysisStatus: VariantAnalysisStatus.InProgress,
}),
];
allHistory = shuffleHistoryItems([
...localQueryHistory,
...variantAnalysisHistory,
]);
});
afterEach(async () => {
if (queryHistoryManager) {
queryHistoryManager.dispose();
}
});
describe("tryOpenExternalFile", () => {
it("should open an external file", async () => {
await tryOpenExternalFile("xxx");
expect(showTextDocumentSpy).toHaveBeenCalledTimes(1);
expect(showTextDocumentSpy).toHaveBeenCalledWith(
vscode.Uri.file("xxx"),
expect.anything(),
);
expect(executeCommandSpy).not.toBeCalled();
});
[
"too large to open",
"Files above 50MB cannot be synchronized with extensions",
].forEach((msg) => {
it(`should fail to open a file because "${msg}" and open externally`, async () => {
showTextDocumentSpy.mockRejectedValue(new Error(msg));
showInformationMessageSpy.mockResolvedValue({ title: "Yes" });
await tryOpenExternalFile("xxx");
const uri = vscode.Uri.file("xxx");
expect(showTextDocumentSpy).toHaveBeenCalledTimes(1);
expect(showTextDocumentSpy).toHaveBeenCalledWith(
uri,
expect.anything(),
);
expect(executeCommandSpy).toHaveBeenCalledWith("revealFileInOS", uri);
});
it(`should fail to open a file because "${msg}" and NOT open externally`, async () => {
showTextDocumentSpy.mockRejectedValue(new Error(msg));
showInformationMessageSpy.mockResolvedValue({ title: "No" });
await tryOpenExternalFile("xxx");
const uri = vscode.Uri.file("xxx");
expect(showTextDocumentSpy).toHaveBeenCalledTimes(1);
expect(showTextDocumentSpy).toHaveBeenCalledWith(
uri,
expect.anything(),
);
expect(showInformationMessageSpy).toBeCalled();
expect(executeCommandSpy).not.toBeCalled();
});
});
});
describe("handleItemClicked", () => {
describe("single click", () => {
describe("local query", () => {
describe("when complete", () => {
it("should show results", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const itemClicked = localQueryHistory[0];
await queryHistoryManager.handleItemClicked(itemClicked, [
itemClicked,
]);
expect(
localQueriesResultsViewStub.showResults,
).toHaveBeenCalledTimes(1);
expect(
localQueriesResultsViewStub.showResults,
).toHaveBeenCalledWith(itemClicked, WebviewReveal.Forced, false);
expect(queryHistoryManager.treeDataProvider.getCurrent()).toBe(
itemClicked,
);
});
});
describe("when incomplete", () => {
it("should do nothing", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const itemClicked = localQueryHistory[2];
await queryHistoryManager.handleItemClicked(itemClicked, [
itemClicked,
]);
expect(
localQueriesResultsViewStub.showResults,
).not.toHaveBeenCalled();
});
});
});
describe("variant analysis", () => {
describe("when complete", () => {
it("should show results", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const itemClicked = variantAnalysisHistory[0];
await queryHistoryManager.handleItemClicked(itemClicked, [
itemClicked,
]);
expect(variantAnalysisManagerStub.showView).toHaveBeenCalledTimes(
1,
);
expect(variantAnalysisManagerStub.showView).toHaveBeenCalledWith(
itemClicked.variantAnalysis.id,
);
expect(queryHistoryManager.treeDataProvider.getCurrent()).toBe(
itemClicked,
);
});
});
describe("when incomplete", () => {
it("should show results", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const itemClicked = variantAnalysisHistory[1];
await queryHistoryManager.handleItemClicked(itemClicked, [
itemClicked,
]);
expect(variantAnalysisManagerStub.showView).toHaveBeenCalledTimes(
1,
);
expect(variantAnalysisManagerStub.showView).toHaveBeenCalledWith(
itemClicked.variantAnalysis.id,
);
expect(queryHistoryManager.treeDataProvider.getCurrent()).toBe(
itemClicked,
);
});
});
});
});
describe("double click", () => {
it("should do nothing", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const itemClicked = allHistory[0];
const secondItemClicked = allHistory[1];
await queryHistoryManager.handleItemClicked(itemClicked, [
itemClicked,
secondItemClicked,
]);
expect(localQueriesResultsViewStub.showResults).not.toHaveBeenCalled();
expect(variantAnalysisManagerStub.showView).not.toBeCalled();
expect(
queryHistoryManager.treeDataProvider.getCurrent(),
).toBeUndefined();
});
});
describe("no selection", () => {
it("should do nothing", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
await queryHistoryManager.handleItemClicked(undefined!, []);
expect(localQueriesResultsViewStub.showResults).not.toHaveBeenCalled();
expect(variantAnalysisManagerStub.showView).not.toHaveBeenCalled();
expect(
queryHistoryManager.treeDataProvider.getCurrent(),
).toBeUndefined();
});
});
});
describe("handleRemoveHistoryItem", () => {
describe("when the item is a local query", () => {
describe("when the item being removed is not selected", () => {
// deleting the first item when a different item is selected
// will not change the selection
let toDelete: LocalQueryInfo;
let selected: LocalQueryInfo;
beforeEach(async () => {
toDelete = localQueryHistory[1];
selected = localQueryHistory[3];
queryHistoryManager = await createMockQueryHistory(localQueryHistory);
// initialize the selection
await queryHistoryManager.treeView.reveal(localQueryHistory[0], {
select: true,
});
// select the item we want
await queryHistoryManager.treeView.reveal(selected, {
select: true,
});
// should be selected
expect(queryHistoryManager.treeDataProvider.getCurrent()).toEqual(
selected,
);
// remove an item
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [
toDelete,
]);
});
it("should remove the item", () => {
expect(queryHistoryManager.treeDataProvider.allHistory).toEqual(
expect.not.arrayContaining([toDelete]),
);
});
it("should not change the selection", () => {
expect(queryHistoryManager.treeDataProvider.getCurrent()).toEqual(
selected,
);
expect(localQueriesResultsViewStub.showResults).toHaveBeenCalledTimes(
1,
);
expect(localQueriesResultsViewStub.showResults).toHaveBeenCalledWith(
selected,
WebviewReveal.Forced,
false,
);
});
});
describe("when the item being removed is selected", () => {
// deleting the selected item automatically selects next item
let toDelete: LocalQueryInfo;
let newSelected: LocalQueryInfo;
beforeEach(async () => {
toDelete = localQueryHistory[1];
newSelected = localQueryHistory[2];
queryHistoryManager = await createMockQueryHistory(localQueryHistory);
// select the item we want
await queryHistoryManager.treeView.reveal(toDelete, {
select: true,
});
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [
toDelete,
]);
});
it("should remove the item", () => {
expect(queryHistoryManager.treeDataProvider.allHistory).toEqual(
expect.not.arrayContaining([toDelete]),
);
});
it.skip("should change the selection", () => {
expect(queryHistoryManager.treeDataProvider.getCurrent()).toBe(
newSelected,
);
expect(localQueriesResultsViewStub.showResults).toHaveBeenCalledTimes(
1,
);
expect(localQueriesResultsViewStub.showResults).toHaveBeenCalledWith(
newSelected,
WebviewReveal.Forced,
false,
);
});
});
});
describe("when the item is a variant analysis", () => {
let showBinaryChoiceDialogSpy: jest.SpiedFunction<
typeof helpers.showBinaryChoiceDialog
>;
let showInformationMessageWithActionSpy: jest.SpiedFunction<
typeof helpers.showInformationMessageWithAction
>;
beforeEach(() => {
// Choose 'Yes' when asked "Are you sure?"
showBinaryChoiceDialogSpy = jest
.spyOn(helpers, "showBinaryChoiceDialog")
.mockResolvedValue(true);
showInformationMessageWithActionSpy = jest.spyOn(
helpers,
"showInformationMessageWithAction",
);
});
describe("when in progress", () => {
describe("when the item being removed is not selected", () => {
let toDelete: VariantAnalysisHistoryItem;
let selected: VariantAnalysisHistoryItem;
beforeEach(async () => {
// deleting the first item when a different item is selected
// will not change the selection
toDelete = variantAnalysisHistory[1];
selected = variantAnalysisHistory[3];
queryHistoryManager = await createMockQueryHistory(allHistory);
// initialize the selection
await queryHistoryManager.treeView.reveal(
variantAnalysisHistory[0],
{
select: true,
},
);
// select the item we want
await queryHistoryManager.treeView.reveal(selected, {
select: true,
});
// should be selected
expect(queryHistoryManager.treeDataProvider.getCurrent()).toEqual(
selected,
);
});
it("should remove the item", async () => {
// remove an item
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [
toDelete,
]);
expect(
variantAnalysisManagerStub.removeVariantAnalysis,
).toHaveBeenCalledWith(toDelete.variantAnalysis);
expect(
queryHistoryManager.treeDataProvider.allHistory,
).not.toContain(toDelete);
});
it("should not change the selection", async () => {
// remove an item
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [
toDelete,
]);
expect(queryHistoryManager.treeDataProvider.getCurrent()).toEqual(
selected,
);
expect(variantAnalysisManagerStub.showView).toHaveBeenCalledWith(
selected.variantAnalysis.id,
);
});
it("should show a modal asking 'Are you sure?'", async () => {
// remove an item
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [
toDelete,
]);
expect(showBinaryChoiceDialogSpy).toHaveBeenCalledWith(
"You are about to delete this query: a-query-name (javascript). Are you sure?",
);
});
it("should show a toast notification with a link to GitHub Actions", async () => {
// remove an item
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [
toDelete,
]);
expect(showInformationMessageWithActionSpy).toHaveBeenCalled();
});
describe("when you choose 'No' in the 'Are you sure?' modal", () => {
beforeEach(async () => {
showBinaryChoiceDialogSpy.mockResolvedValue(false);
});
it("should not delete the item", async () => {
// remove an item
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [
toDelete,
]);
expect(queryHistoryManager.treeDataProvider.allHistory).toContain(
toDelete,
);
});
it("should not show a toast notification", async () => {
// remove an item
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [
toDelete,
]);
expect(
showInformationMessageWithActionSpy,
).not.toHaveBeenCalled();
});
});
});
describe("when the item being removed is selected", () => {
let toDelete: VariantAnalysisHistoryItem;
let newSelected: VariantAnalysisHistoryItem;
beforeEach(async () => {
// deleting the selected item automatically selects next item
toDelete = variantAnalysisHistory[1];
newSelected = variantAnalysisHistory[2];
queryHistoryManager = await createMockQueryHistory(
variantAnalysisHistory,
);
// select the item we want
await queryHistoryManager.treeView.reveal(toDelete, {
select: true,
});
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [
toDelete,
]);
});
it("should remove the item", () => {
expect(
variantAnalysisManagerStub.removeVariantAnalysis,
).toHaveBeenCalledWith(toDelete.variantAnalysis);
expect(
queryHistoryManager.treeDataProvider.allHistory,
).not.toContain(toDelete);
});
it.skip("should change the selection", () => {
expect(queryHistoryManager.treeDataProvider.getCurrent()).toEqual(
newSelected,
);
expect(variantAnalysisManagerStub.showView).toHaveBeenCalledWith(
newSelected.variantAnalysis.id,
);
});
it("should show a modal asking 'Are you sure?'", () => {
expect(showBinaryChoiceDialogSpy).toHaveBeenCalledWith(
"You are about to delete this query: a-query-name (javascript). Are you sure?",
);
});
});
});
describe("when not in progress", () => {
describe("when the item being removed is not selected", () => {
let toDelete: VariantAnalysisHistoryItem;
let selected: VariantAnalysisHistoryItem;
beforeEach(async () => {
// deleting the first item when a different item is selected
// will not change the selection
toDelete = variantAnalysisHistory[2];
selected = variantAnalysisHistory[3];
queryHistoryManager = await createMockQueryHistory(allHistory);
// initialize the selection
await queryHistoryManager.treeView.reveal(
variantAnalysisHistory[0],
{
select: true,
},
);
// select the item we want
await queryHistoryManager.treeView.reveal(selected, {
select: true,
});
// should be selected
expect(queryHistoryManager.treeDataProvider.getCurrent()).toEqual(
selected,
);
// remove an item
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [
toDelete,
]);
});
it("should remove the item", () => {
expect(
variantAnalysisManagerStub.removeVariantAnalysis,
).toHaveBeenCalledWith(toDelete.variantAnalysis);
expect(
queryHistoryManager.treeDataProvider.allHistory,
).not.toContain(toDelete);
});
it("should not change the selection", () => {
expect(queryHistoryManager.treeDataProvider.getCurrent()).toEqual(
selected,
);
expect(variantAnalysisManagerStub.showView).toHaveBeenCalledWith(
selected.variantAnalysis.id,
);
});
it("should not show a modal asking 'Are you sure?'", () => {
expect(showBinaryChoiceDialogSpy).not.toHaveBeenCalled();
});
});
describe("when the item being removed is selected", () => {
let toDelete: VariantAnalysisHistoryItem;
let newSelected: VariantAnalysisHistoryItem;
beforeEach(async () => {
// deleting the selected item automatically selects next item
toDelete = variantAnalysisHistory[0];
newSelected = variantAnalysisHistory[2];
queryHistoryManager = await createMockQueryHistory(
variantAnalysisHistory,
);
// select the item we want
await queryHistoryManager.treeView.reveal(toDelete, {
select: true,
});
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [
toDelete,
]);
});
it("should remove the item", () => {
expect(
variantAnalysisManagerStub.removeVariantAnalysis,
).toHaveBeenCalledWith(toDelete.variantAnalysis);
expect(
queryHistoryManager.treeDataProvider.allHistory,
).not.toContain(toDelete);
});
it.skip("should change the selection", () => {
expect(queryHistoryManager.treeDataProvider.getCurrent()).toEqual(
newSelected,
);
expect(variantAnalysisManagerStub.showView).toHaveBeenCalledWith(
newSelected.variantAnalysis.id,
);
});
it("should not show a modal asking 'Are you sure?'", () => {
expect(showBinaryChoiceDialogSpy).not.toHaveBeenCalled();
});
});
});
});
});
describe("handleCancel", () => {
describe("if the item is in progress", () => {
it("should cancel a single local query", async () => {
queryHistoryManager = await createMockQueryHistory(localQueryHistory);
// cancelling the selected item
const inProgress1 = localQueryHistory[4];
const cancelSpy = jest.spyOn(inProgress1, "cancel");
await queryHistoryManager.handleCancel(inProgress1, [inProgress1]);
expect(cancelSpy).toBeCalledTimes(1);
});
it("should cancel multiple local queries", async () => {
queryHistoryManager = await createMockQueryHistory(localQueryHistory);
// cancelling the selected item
const inProgress1 = localQueryHistory[4];
const inProgress2 = localQueryHistory[5];
const cancelSpy1 = jest.spyOn(inProgress1, "cancel");
const cancelSpy2 = jest.spyOn(inProgress2, "cancel");
await queryHistoryManager.handleCancel(inProgress1, [
inProgress1,
inProgress2,
]);
expect(cancelSpy1).toBeCalled();
expect(cancelSpy2).toBeCalled();
});
it("should cancel a single variant analysis", async () => {
queryHistoryManager = await createMockQueryHistory(localQueryHistory);
// cancelling the selected item
const inProgress1 = variantAnalysisHistory[1];
await queryHistoryManager.handleCancel(inProgress1, [inProgress1]);
expect(cancelVariantAnalysisSpy).toBeCalledWith(
inProgress1.variantAnalysis.id,
);
});
it("should cancel multiple variant analyses", async () => {
queryHistoryManager = await createMockQueryHistory(localQueryHistory);
// cancelling the selected item
const inProgress1 = variantAnalysisHistory[1];
const inProgress2 = variantAnalysisHistory[3];
await queryHistoryManager.handleCancel(inProgress1, [
inProgress1,
inProgress2,
]);
expect(cancelVariantAnalysisSpy).toBeCalledWith(
inProgress1.variantAnalysis.id,
);
expect(cancelVariantAnalysisSpy).toBeCalledWith(
inProgress2.variantAnalysis.id,
);
});
});
describe("if the item is not in progress", () => {
it("should not cancel a single local query", async () => {
queryHistoryManager = await createMockQueryHistory(localQueryHistory);
// cancelling the selected item
const completed = localQueryHistory[0];
const cancelSpy = jest.spyOn(completed, "cancel");
await queryHistoryManager.handleCancel(completed, [completed]);
expect(cancelSpy).not.toBeCalledTimes(1);
});
it("should not cancel multiple local queries", async () => {
queryHistoryManager = await createMockQueryHistory(localQueryHistory);
// cancelling the selected item
const completed = localQueryHistory[0];
const failed = localQueryHistory[2];
const cancelSpy = jest.spyOn(completed, "cancel");
const cancelSpy2 = jest.spyOn(failed, "cancel");
await queryHistoryManager.handleCancel(completed, [completed, failed]);
expect(cancelSpy).not.toBeCalledTimes(1);
expect(cancelSpy2).not.toBeCalledTimes(1);
});
it("should not cancel a single variant analysis", async () => {
queryHistoryManager = await createMockQueryHistory(localQueryHistory);
// cancelling the selected item
const completedVariantAnalysis = variantAnalysisHistory[0];
await queryHistoryManager.handleCancel(completedVariantAnalysis, [
completedVariantAnalysis,
]);
expect(cancelVariantAnalysisSpy).not.toBeCalledWith(
completedVariantAnalysis.variantAnalysis,
);
});
it("should not cancel multiple variant analyses", async () => {
queryHistoryManager = await createMockQueryHistory(localQueryHistory);
// cancelling the selected item
const completedVariantAnalysis = variantAnalysisHistory[0];
const failedVariantAnalysis = variantAnalysisHistory[2];
await queryHistoryManager.handleCancel(completedVariantAnalysis, [
completedVariantAnalysis,
failedVariantAnalysis,
]);
expect(cancelVariantAnalysisSpy).not.toBeCalledWith(
completedVariantAnalysis.variantAnalysis.id,
);
expect(cancelVariantAnalysisSpy).not.toBeCalledWith(
failedVariantAnalysis.variantAnalysis.id,
);
});
});
});
describe("handleCopyRepoList", () => {
it("should not call a command for a local query", async () => {
queryHistoryManager = await createMockQueryHistory(localQueryHistory);
const item = localQueryHistory[4];
await queryHistoryManager.handleCopyRepoList(item, [item]);
expect(executeCommandSpy).not.toBeCalled();
});
it("should copy repo list for a single variant analysis", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const item = variantAnalysisHistory[1];
await queryHistoryManager.handleCopyRepoList(item, [item]);
expect(executeCommandSpy).toBeCalledWith(
"codeQL.copyVariantAnalysisRepoList",
item.variantAnalysis.id,
);
});
it("should not copy repo list for multiple variant analyses", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const item1 = variantAnalysisHistory[1];
const item2 = variantAnalysisHistory[3];
await queryHistoryManager.handleCopyRepoList(item1, [item1, item2]);
expect(executeCommandSpy).not.toBeCalled();
});
});
describe("handleExportResults", () => {
it("should not call a command for a local query", async () => {
queryHistoryManager = await createMockQueryHistory(localQueryHistory);
const item = localQueryHistory[4];
await queryHistoryManager.handleExportResults(item, [item]);
expect(executeCommandSpy).not.toBeCalled();
});
it("should export results for a single variant analysis", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const item = variantAnalysisHistory[1];
await queryHistoryManager.handleExportResults(item, [item]);
expect(executeCommandSpy).toBeCalledWith(
"codeQL.exportVariantAnalysisResults",
item.variantAnalysis.id,
);
});
it("should not export results for multiple variant analyses", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const item1 = variantAnalysisHistory[1];
const item2 = variantAnalysisHistory[3];
await queryHistoryManager.handleExportResults(item1, [item1, item2]);
expect(executeCommandSpy).not.toBeCalled();
});
});
describe("determineSelection", () => {
const singleItem = "a";
const multipleItems = ["b", "c", "d"];
it("should get the selection from parameters", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const selection = (queryHistoryManager as any).determineSelection(
singleItem,
multipleItems,
);
expect(selection).toEqual({
finalSingleItem: singleItem,
finalMultiSelect: multipleItems,
});
});
it("should get the selection when single selection is empty", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const selection = (queryHistoryManager as any).determineSelection(
undefined,
multipleItems,
);
expect(selection).toEqual({
finalSingleItem: multipleItems[0],
finalMultiSelect: multipleItems,
});
});
it("should get the selection when multi-selection is empty", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const selection = (queryHistoryManager as any).determineSelection(
singleItem,
undefined,
);
expect(selection).toEqual({
finalSingleItem: singleItem,
finalMultiSelect: [singleItem],
});
});
it("should get the selection from the treeView when both selections are empty", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const p = new Promise<void>((done) => {
queryHistoryManager!.treeView.onDidChangeSelection((s) => {
if (s.selection[0] !== allHistory[1]) {
return;
}
const selection = (queryHistoryManager as any).determineSelection(
undefined,
undefined,
);
expect(selection).toEqual({
finalSingleItem: allHistory[1],
finalMultiSelect: [allHistory[1]],
});
done();
});
});
// I can't explain why, but the first time the onDidChangeSelection event fires, the selection is
// not correct (it is inexplicably allHistory[2]). So we fire the event a second time to get the
// correct selection.
await queryHistoryManager.treeView.reveal(allHistory[0], {
select: true,
});
await queryHistoryManager.treeView.reveal(allHistory[1], {
select: true,
});
await p;
});
it.skip("should get the selection from the treeDataProvider when both selections and the treeView are empty", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
await queryHistoryManager.treeView.reveal(allHistory[1], {
select: true,
});
const selection = (queryHistoryManager as any).determineSelection(
undefined,
undefined,
);
expect(selection).toEqual({
finalSingleItem: allHistory[1],
finalMultiSelect: [allHistory[1]],
});
});
});
describe("Local Queries", () => {
describe("findOtherQueryToCompare", () => {
it("should find the second query to compare when one is selected", async () => {
const thisQuery = localQueryHistory[3];
queryHistoryManager = await createMockQueryHistory(allHistory);
showQuickPickSpy.mockResolvedValue(
mockedQuickPickItem({
label: "Query 1",
query: localQueryHistory[0],
}),
);
const otherQuery = await (
queryHistoryManager as any
).findOtherQueryToCompare(thisQuery, []);
expect(otherQuery).toBe(localQueryHistory[0]);
// only called with first item, other items filtered out
expect(showQuickPickSpy).toHaveBeenCalledTimes(1);
expect(showQuickPickSpy).toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
query: localQueryHistory[0],
}),
]),