11import { ensureDir , readJSON , remove , writeJson } from "fs-extra" ;
22import { join } from "path" ;
3+ import { App } from "../../../src/common/app" ;
34import {
45 DbConfig ,
56 SelectedDbItemKind ,
@@ -18,11 +19,17 @@ import {
1819 RemoteRepoDbItem ,
1920 VariantAnalysisUserDefinedListDbItem ,
2021} from "../../../src/databases/db-item" ;
22+ import {
23+ ExpandedDbItem ,
24+ ExpandedDbItemKind ,
25+ VariantAnalysisUserDefinedListExpandedDbItem ,
26+ } from "../../../src/databases/db-item-expansion" ;
2127import { DbManager } from "../../../src/databases/db-manager" ;
2228import {
2329 createDbConfig ,
2430 createLocalDbConfigItem ,
2531} from "../../factories/db-config-factories" ;
32+ import { createVariantAnalysisUserDefinedListDbItem } from "../../factories/db-item-factories" ;
2633import { createMockApp } from "../../__mocks__/appMock" ;
2734
2835// Note: Although these are "unit tests" (i.e. not integrating with VS Code), they do
@@ -32,12 +39,13 @@ describe("db manager", () => {
3239 let dbConfigStore : DbConfigStore ;
3340 let tempWorkspaceStoragePath : string ;
3441 let dbConfigFilePath : string ;
42+ let app : App ;
3543
3644 beforeEach ( async ( ) => {
3745 tempWorkspaceStoragePath = join ( __dirname , "db-manager-test-workspace" ) ;
3846
3947 const extensionPath = join ( __dirname , "../../.." ) ;
40- const app = createMockApp ( {
48+ app = createMockApp ( {
4149 extensionPath,
4250 workspaceStoragePath : tempWorkspaceStoragePath ,
4351 } ) ;
@@ -362,6 +370,129 @@ describe("db manager", () => {
362370 } ) ;
363371 } ) ;
364372
373+ describe ( "expanded behaviours" , ( ) => {
374+ it ( "should add item to expanded state" , async ( ) => {
375+ // Add item to config
376+ const listName = "my-list-1" ;
377+ const dbConfig = createDbConfig ( {
378+ remoteLists : [ { name : listName , repositories : [ ] } ] ,
379+ } ) ;
380+
381+ await saveDbConfig ( dbConfig ) ;
382+
383+ // Add item to expanded state
384+ const dbItem = createVariantAnalysisUserDefinedListDbItem ( {
385+ listName,
386+ } ) ;
387+
388+ await dbManager . addDbItemToExpandedState ( dbItem ) ;
389+ const expandedItems = app . workspaceState . get < ExpandedDbItem [ ] > (
390+ DbManager . DB_EXPANDED_STATE_KEY ,
391+ ) ;
392+
393+ expect ( expandedItems ?. length ) . toEqual ( 1 ) ;
394+ const expandedItem =
395+ expandedItems ! [ 0 ] as VariantAnalysisUserDefinedListExpandedDbItem ;
396+ expect ( expandedItem . listName ) . toEqual ( listName ) ;
397+ } ) ;
398+
399+ it ( "should remove item from expanded state" , async ( ) => {
400+ const listName = "my-list-2" ;
401+ const variantAnalysisList = {
402+ kind : ExpandedDbItemKind . RemoteUserDefinedList ,
403+ listName,
404+ } ;
405+
406+ // Add item to expanded state
407+ await app . workspaceState . update ( DbManager . DB_EXPANDED_STATE_KEY , [
408+ variantAnalysisList ,
409+ ] ) ;
410+
411+ // Remove item from expanded state
412+ const dbItem = createVariantAnalysisUserDefinedListDbItem ( {
413+ listName,
414+ } ) ;
415+
416+ await dbManager . removeDbItemFromExpandedState ( dbItem ) ;
417+ const expandedItems = app . workspaceState . get < ExpandedDbItem [ ] > (
418+ DbManager . DB_EXPANDED_STATE_KEY ,
419+ ) ;
420+
421+ expect ( expandedItems ?. length ) . toEqual ( 0 ) ;
422+ } ) ;
423+
424+ it ( "should rename item in expanded state" , async ( ) => {
425+ // Add item to config
426+ const listName = "my-list-3" ;
427+ const dbConfig = createDbConfig ( {
428+ remoteLists : [ { name : listName , repositories : [ ] } ] ,
429+ } ) ;
430+ await saveDbConfig ( dbConfig ) ;
431+
432+ // Add item to expanded state
433+ const variantAnalysisList = {
434+ kind : ExpandedDbItemKind . RemoteUserDefinedList ,
435+ listName,
436+ } ;
437+
438+ await app . workspaceState . update ( DbManager . DB_EXPANDED_STATE_KEY , [
439+ variantAnalysisList ,
440+ ] ) ;
441+
442+ // Rename item
443+ const dbItem = createVariantAnalysisUserDefinedListDbItem ( {
444+ listName,
445+ } ) ;
446+
447+ await dbManager . renameList ( dbItem , "new-list-name" ) ;
448+ const expandedItems = app . workspaceState . get < ExpandedDbItem [ ] > (
449+ DbManager . DB_EXPANDED_STATE_KEY ,
450+ ) ;
451+
452+ expect ( expandedItems ?. length ) . toEqual ( 1 ) ;
453+ const expandedItem =
454+ expandedItems ! [ 0 ] as VariantAnalysisUserDefinedListExpandedDbItem ;
455+ expect ( expandedItem . listName ) . toEqual ( "new-list-name" ) ;
456+ } ) ;
457+
458+ it ( "should remove non existent items in expanded state when item is expanded" , async ( ) => {
459+ // We remove items from the expanded state if they are not in the config
460+
461+ // Add item to config
462+ const listName = "my-list-4" ;
463+ const dbConfig = createDbConfig ( {
464+ remoteLists : [ { name : listName , repositories : [ ] } ] ,
465+ } ) ;
466+ await saveDbConfig ( dbConfig ) ;
467+
468+ // Populate expanded state with item
469+ const removedListName = "my-list-5" ;
470+ const removedVariantAnalysisList = {
471+ kind : ExpandedDbItemKind . RemoteUserDefinedList ,
472+ listName : removedListName ,
473+ } ;
474+
475+ await app . workspaceState . update ( DbManager . DB_EXPANDED_STATE_KEY , [
476+ removedVariantAnalysisList ,
477+ ] ) ;
478+
479+ // Trigger adding an item that is not in the config
480+ const dbItem = createVariantAnalysisUserDefinedListDbItem ( {
481+ listName,
482+ } ) ;
483+
484+ await dbManager . addDbItemToExpandedState ( dbItem ) ;
485+ const expandedItems = app . workspaceState . get < ExpandedDbItem [ ] > (
486+ DbManager . DB_EXPANDED_STATE_KEY ,
487+ ) ;
488+
489+ expect ( expandedItems ?. length ) . toEqual ( 1 ) ;
490+ const expandedItem =
491+ expandedItems ! [ 0 ] as VariantAnalysisUserDefinedListExpandedDbItem ;
492+ expect ( expandedItem . listName ) . toEqual ( "my-list-4" ) ;
493+ } ) ;
494+ } ) ;
495+
365496 async function saveDbConfig ( dbConfig : DbConfig ) : Promise < void > {
366497 await writeJson ( dbConfigFilePath , dbConfig ) ;
367498
0 commit comments