@@ -5,7 +5,7 @@ import * as vscode from 'vscode';
55import * as sinon from 'sinon' ;
66import * as chaiAsPromised from 'chai-as-promised' ;
77import { logger } from '../../logging' ;
8- import { QueryHistoryManager , HistoryTreeDataProvider } from '../../query-history' ;
8+ import { QueryHistoryManager , HistoryTreeDataProvider , SortOrder } from '../../query-history' ;
99import { QueryEvaluationInfo , QueryWithResults } from '../../run-queries' ;
1010import { QueryHistoryConfigListener } from '../../config' ;
1111import * as messages from '../../pure/messages' ;
@@ -356,6 +356,155 @@ describe('query-history', () => {
356356 } ) ;
357357 } ) ;
358358
359+ describe ( 'determineSelection' , ( ) => {
360+ const singleItem = 'a' ;
361+ const multipleItems = [ 'b' , 'c' , 'd' ] ;
362+
363+ it ( 'should get the selection from parameters' , async ( ) => {
364+ queryHistoryManager = await createMockQueryHistory ( allHistory ) ;
365+ const selection = ( queryHistoryManager as any ) . determineSelection ( singleItem , multipleItems ) ;
366+ expect ( selection ) . to . deep . eq ( {
367+ finalSingleItem : singleItem ,
368+ finalMultiSelect : multipleItems
369+ } ) ;
370+ } ) ;
371+
372+ it ( 'should get the selection when single selection is empty' , async ( ) => {
373+ queryHistoryManager = await createMockQueryHistory ( allHistory ) ;
374+ const selection = ( queryHistoryManager as any ) . determineSelection ( undefined , multipleItems ) ;
375+ expect ( selection ) . to . deep . eq ( {
376+ finalSingleItem : multipleItems [ 0 ] ,
377+ finalMultiSelect : multipleItems
378+ } ) ;
379+ } ) ;
380+
381+ it ( 'should get the selection when multi-selection is empty' , async ( ) => {
382+ queryHistoryManager = await createMockQueryHistory ( allHistory ) ;
383+ const selection = ( queryHistoryManager as any ) . determineSelection ( singleItem , undefined ) ;
384+ expect ( selection ) . to . deep . eq ( {
385+ finalSingleItem : singleItem ,
386+ finalMultiSelect : [ singleItem ]
387+ } ) ;
388+ } ) ;
389+
390+ it ( 'should get the selection from the treeView when both selections are empty' , async ( ) => {
391+ queryHistoryManager = await createMockQueryHistory ( allHistory ) ;
392+ await ( queryHistoryManager as any ) . treeView . reveal ( allHistory [ 1 ] , { select : true } ) ;
393+ const selection = ( queryHistoryManager as any ) . determineSelection ( undefined , undefined ) ;
394+ expect ( selection ) . to . deep . eq ( {
395+ finalSingleItem : allHistory [ 1 ] ,
396+ finalMultiSelect : [ allHistory [ 1 ] ]
397+ } ) ;
398+ } ) ;
399+
400+ it ( 'should get the selection from the treeDataProvider when both selections and the treeView are empty' , async ( ) => {
401+ queryHistoryManager = await createMockQueryHistory ( allHistory ) ;
402+ ( queryHistoryManager as any ) . treeDataProvider . current = allHistory [ 1 ] ;
403+ const selection = ( queryHistoryManager as any ) . determineSelection ( undefined , undefined ) ;
404+ expect ( selection ) . to . deep . eq ( {
405+ finalSingleItem : allHistory [ 1 ] ,
406+ finalMultiSelect : [ allHistory [ 1 ] ]
407+ } ) ;
408+ } ) ;
409+ } ) ;
410+
411+ describe ( 'getChildren' , ( ) => {
412+ const history = [
413+ item ( 'a' , 10 , 20 ) ,
414+ item ( 'b' , 5 , 30 ) ,
415+ item ( 'c' , 1 , 25 ) ,
416+ ] ;
417+ let treeDataProvider : HistoryTreeDataProvider ;
418+
419+ beforeEach ( async ( ) => {
420+ queryHistoryManager = await createMockQueryHistory ( allHistory ) ;
421+ ( queryHistoryManager . treeDataProvider as any ) . history = [ ...history ] ;
422+ treeDataProvider = queryHistoryManager . treeDataProvider ;
423+ } ) ;
424+
425+ it ( 'should get children for name ascending' , async ( ) => {
426+ const expected = [ ...history ] ;
427+ treeDataProvider . sortOrder = SortOrder . NameAsc ;
428+
429+ const children = await treeDataProvider . getChildren ( ) ;
430+ expect ( children ) . to . deep . eq ( expected ) ;
431+ } ) ;
432+
433+ it ( 'should get children for name descending' , async ( ) => {
434+ const expected = [ ...history ] . reverse ( ) ;
435+ treeDataProvider . sortOrder = SortOrder . NameDesc ;
436+
437+ const children = await treeDataProvider . getChildren ( ) ;
438+ expect ( children ) . to . deep . eq ( expected ) ;
439+ } ) ;
440+
441+ it ( 'should get children for date ascending' , async ( ) => {
442+ const expected = [ history [ 2 ] , history [ 1 ] , history [ 0 ] ] ;
443+ treeDataProvider . sortOrder = SortOrder . DateAsc ;
444+
445+ const children = await treeDataProvider . getChildren ( ) ;
446+ expect ( children ) . to . deep . eq ( expected ) ;
447+ } ) ;
448+
449+ it ( 'should get children for date descending' , async ( ) => {
450+ const expected = [ history [ 0 ] , history [ 1 ] , history [ 2 ] ] ;
451+ treeDataProvider . sortOrder = SortOrder . DateDesc ;
452+
453+ const children = await treeDataProvider . getChildren ( ) ;
454+ expect ( children ) . to . deep . eq ( expected ) ;
455+ } ) ;
456+
457+ it ( 'should get children for result count ascending' , async ( ) => {
458+ const expected = [ history [ 0 ] , history [ 2 ] , history [ 1 ] ] ;
459+ treeDataProvider . sortOrder = SortOrder . CountAsc ;
460+
461+ const children = await treeDataProvider . getChildren ( ) ;
462+ expect ( children ) . to . deep . eq ( expected ) ;
463+ } ) ;
464+
465+ it ( 'should get children for result count descending' , async ( ) => {
466+ const expected = [ history [ 1 ] , history [ 2 ] , history [ 0 ] ] ;
467+ treeDataProvider . sortOrder = SortOrder . CountDesc ;
468+
469+ const children = await treeDataProvider . getChildren ( ) ;
470+ expect ( children ) . to . deep . eq ( expected ) ;
471+ } ) ;
472+
473+ it ( 'should get children for result count ascending when there are no results' , async ( ) => {
474+ // fall back to name
475+ const thisHistory = [ item ( 'a' , 10 ) , item ( 'b' , 50 ) , item ( 'c' , 1 ) ] ;
476+ ( queryHistoryManager ! . treeDataProvider as any ) . history = [ ...thisHistory ] ;
477+ const expected = [ ...thisHistory ] ;
478+ treeDataProvider . sortOrder = SortOrder . CountAsc ;
479+
480+ const children = await treeDataProvider . getChildren ( ) ;
481+ expect ( children ) . to . deep . eq ( expected ) ;
482+ } ) ;
483+
484+ it ( 'should get children for result count descending when there are no results' , async ( ) => {
485+ // fall back to name
486+ const thisHistory = [ item ( 'a' , 10 ) , item ( 'b' , 50 ) , item ( 'c' , 1 ) ] ;
487+ ( queryHistoryManager ! . treeDataProvider as any ) . history = [ ...thisHistory ] ;
488+ const expected = [ ...thisHistory ] . reverse ( ) ;
489+ treeDataProvider . sortOrder = SortOrder . CountDesc ;
490+
491+ const children = await treeDataProvider . getChildren ( ) ;
492+ expect ( children ) . to . deep . eq ( expected ) ;
493+ } ) ;
494+
495+ function item ( label : string , start : number , resultCount ?: number ) {
496+ return {
497+ label,
498+ initialInfo : {
499+ start : new Date ( start ) ,
500+ } ,
501+ completedQuery : {
502+ resultCount,
503+ }
504+ } ;
505+ }
506+ } ) ;
507+
359508 function createMockFullQueryInfo ( dbName = 'a' , queryWitbResults ?: QueryWithResults , isFail = false ) : FullQueryInfo {
360509 const fqi = new FullQueryInfo (
361510 {
0 commit comments