diff --git a/extensions/ql-vscode/test/vscode-tests/minimal-workspace/databases/db-panel.test.ts b/extensions/ql-vscode/test/vscode-tests/minimal-workspace/databases/db-panel.test.ts index bce5bf8a5..c582e7e78 100644 --- a/extensions/ql-vscode/test/vscode-tests/minimal-workspace/databases/db-panel.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/minimal-workspace/databases/db-panel.test.ts @@ -9,10 +9,7 @@ import { DbManager } from "../../../../src/databases/db-manager"; import { DbConfigStore } from "../../../../src/databases/config/db-config-store"; import { DbTreeDataProvider } from "../../../../src/databases/ui/db-tree-data-provider"; import { DbItemKind, DbListKind } from "../../../../src/databases/db-item"; -import { - DbTreeViewItem, - SELECTED_DB_ITEM_RESOURCE_URI, -} from "../../../../src/databases/ui/db-tree-view-item"; +import { DbTreeViewItem } from "../../../../src/databases/ui/db-tree-view-item"; import { ExtensionApp } from "../../../../src/common/vscode/vscode-app"; import { createMockExtensionContext } from "../../../factories/extension-context"; import { createDbConfig } from "../../../factories/db-config-factories"; @@ -51,113 +48,6 @@ describe("db panel", () => { await remove(workspaceStoragePath); }); - describe("selecting an item", () => { - it("should mark selected remote db list as selected", async () => { - const dbConfig: DbConfig = createDbConfig({ - remoteLists: [ - { - name: "my-list-1", - repositories: ["owner1/repo1", "owner1/repo2"], - }, - { - name: "my-list-2", - repositories: ["owner2/repo1", "owner2/repo2"], - }, - ], - selected: { - kind: SelectedDbItemKind.VariantAnalysisUserDefinedList, - listName: "my-list-2", - }, - }); - - await saveDbConfig(dbConfig); - - const dbTreeItems = await dbTreeDataProvider.getChildren(); - - expect(dbTreeItems).toBeTruthy(); - const items = dbTreeItems!; - - const remoteRootNode = items[0]; - expect(remoteRootNode.dbItem).toBeTruthy(); - expect(remoteRootNode.dbItem?.kind).toEqual(DbItemKind.RootRemote); - - const list1 = remoteRootNode.children.find( - (c) => - c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList && - c.dbItem?.listName === "my-list-1", - ); - const list2 = remoteRootNode.children.find( - (c) => - c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList && - c.dbItem?.listName === "my-list-2", - ); - - expect(list1).toBeTruthy(); - expect(list2).toBeTruthy(); - expect(isTreeViewItemSelectable(list1!)).toBeTruthy(); - expect(isTreeViewItemSelected(list2!)).toBeTruthy(); - }); - - it("should mark selected remote db inside list as selected", async () => { - const dbConfig: DbConfig = createDbConfig({ - remoteLists: [ - { - name: "my-list-1", - repositories: ["owner1/repo1", "owner1/repo2"], - }, - { - name: "my-list-2", - repositories: ["owner1/repo1", "owner2/repo2"], - }, - ], - remoteRepos: ["owner1/repo1"], - selected: { - kind: SelectedDbItemKind.VariantAnalysisRepository, - repositoryName: "owner1/repo1", - listName: "my-list-2", - }, - }); - - await saveDbConfig(dbConfig); - - const dbTreeItems = await dbTreeDataProvider.getChildren(); - - expect(dbTreeItems).toBeTruthy(); - const items = dbTreeItems!; - - const remoteRootNode = items[0]; - expect(remoteRootNode.dbItem).toBeTruthy(); - expect(remoteRootNode.dbItem?.kind).toEqual(DbItemKind.RootRemote); - - const list2 = remoteRootNode.children.find( - (c) => - c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList && - c.dbItem?.listName === "my-list-2", - ); - expect(list2).toBeTruthy(); - - const repo1Node = list2?.children.find( - (c) => - c.dbItem?.kind === DbItemKind.RemoteRepo && - c.dbItem?.repoFullName === "owner1/repo1", - ); - expect(repo1Node).toBeTruthy(); - expect(isTreeViewItemSelected(repo1Node!)).toBeTruthy(); - - const repo2Node = list2?.children.find( - (c) => - c.dbItem?.kind === DbItemKind.RemoteRepo && - c.dbItem?.repoFullName === "owner2/repo2", - ); - expect(repo2Node).toBeTruthy(); - expect(isTreeViewItemSelectable(repo2Node!)).toBeTruthy(); - - for (const item of remoteRootNode.children) { - expect(isTreeViewItemSelectable(item)).toBeTruthy(); - } - }); - }); - describe("addNewRemoteRepo", () => { it("should add a new remote repo", async () => { const dbConfig: DbConfig = createDbConfig({ @@ -498,22 +388,6 @@ describe("db panel", () => { expect(item.children.length).toBe(0); } - function isTreeViewItemSelectable(treeViewItem: DbTreeViewItem) { - return ( - treeViewItem.resourceUri === undefined && - treeViewItem.contextValue?.includes("canBeSelected") - ); - } - - function isTreeViewItemSelected(treeViewItem: DbTreeViewItem) { - return ( - treeViewItem.resourceUri?.toString(true) === - SELECTED_DB_ITEM_RESOURCE_URI && - (treeViewItem.contextValue === undefined || - !treeViewItem.contextValue.includes("canBeSelected")) - ); - } - async function readDbConfigDirectly(): Promise { // Read the workspace databases JSON file directly to check that the new list has been added. // We can't use the dbConfigStore's `read` function here because it depends on the file watcher diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/databases/db-panel-selection.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/databases/db-panel-selection.test.ts new file mode 100644 index 000000000..5336a3076 --- /dev/null +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/databases/db-panel-selection.test.ts @@ -0,0 +1,184 @@ +import { join } from "path"; +import { ensureDir, remove, writeJson } from "fs-extra"; +import { + DbConfig, + SelectedDbItemKind, +} from "../../../../src/databases/config/db-config"; +import { DbManager } from "../../../../src/databases/db-manager"; +import { DbConfigStore } from "../../../../src/databases/config/db-config-store"; +import { DbTreeDataProvider } from "../../../../src/databases/ui/db-tree-data-provider"; +import { DbItemKind } from "../../../../src/databases/db-item"; +import { + DbTreeViewItem, + SELECTED_DB_ITEM_RESOURCE_URI, +} from "../../../../src/databases/ui/db-tree-view-item"; +import { ExtensionApp } from "../../../../src/common/vscode/vscode-app"; +import { createMockExtensionContext } from "../../../factories/extension-context"; +import { createDbConfig } from "../../../factories/db-config-factories"; + +describe("db panel selection", () => { + const workspaceStoragePath = join(__dirname, "test-workspace-storage"); + const globalStoragePath = join(__dirname, "test-global-storage"); + const extensionPath = join(__dirname, "../../../../"); + const dbConfigFilePath = join( + workspaceStoragePath, + DbConfigStore.databaseConfigFileName, + ); + let dbTreeDataProvider: DbTreeDataProvider; + let dbManager: DbManager; + let dbConfigStore: DbConfigStore; + + beforeAll(async () => { + const extensionContext = createMockExtensionContext({ + extensionPath, + globalStoragePath, + workspaceStoragePath, + }); + await ensureDir(workspaceStoragePath); + + const app = new ExtensionApp(extensionContext); + + dbConfigStore = new DbConfigStore(app, false); + dbManager = new DbManager(app, dbConfigStore); + }); + + beforeEach(async () => { + await ensureDir(workspaceStoragePath); + }); + + afterEach(async () => { + await remove(workspaceStoragePath); + }); + + it("should mark selected remote db list as selected", async () => { + const dbConfig: DbConfig = createDbConfig({ + remoteLists: [ + { + name: "my-list-1", + repositories: ["owner1/repo1", "owner1/repo2"], + }, + { + name: "my-list-2", + repositories: ["owner2/repo1", "owner2/repo2"], + }, + ], + selected: { + kind: SelectedDbItemKind.VariantAnalysisUserDefinedList, + listName: "my-list-2", + }, + }); + + await saveDbConfig(dbConfig); + + const dbTreeItems = await dbTreeDataProvider.getChildren(); + + expect(dbTreeItems).toBeTruthy(); + const items = dbTreeItems!; + + const remoteRootNode = items[0]; + expect(remoteRootNode.dbItem).toBeTruthy(); + expect(remoteRootNode.dbItem?.kind).toEqual(DbItemKind.RootRemote); + + const list1 = remoteRootNode.children.find( + (c) => + c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList && + c.dbItem?.listName === "my-list-1", + ); + const list2 = remoteRootNode.children.find( + (c) => + c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList && + c.dbItem?.listName === "my-list-2", + ); + + expect(list1).toBeTruthy(); + expect(list2).toBeTruthy(); + expect(isTreeViewItemSelectable(list1!)).toBeTruthy(); + expect(isTreeViewItemSelected(list2!)).toBeTruthy(); + }); + + it("should mark selected remote db inside list as selected", async () => { + const dbConfig: DbConfig = createDbConfig({ + remoteLists: [ + { + name: "my-list-1", + repositories: ["owner1/repo1", "owner1/repo2"], + }, + { + name: "my-list-2", + repositories: ["owner1/repo1", "owner2/repo2"], + }, + ], + remoteRepos: ["owner1/repo1"], + selected: { + kind: SelectedDbItemKind.VariantAnalysisRepository, + repositoryName: "owner1/repo1", + listName: "my-list-2", + }, + }); + + await saveDbConfig(dbConfig); + + const dbTreeItems = await dbTreeDataProvider.getChildren(); + + expect(dbTreeItems).toBeTruthy(); + const items = dbTreeItems!; + + const remoteRootNode = items[0]; + expect(remoteRootNode.dbItem).toBeTruthy(); + expect(remoteRootNode.dbItem?.kind).toEqual(DbItemKind.RootRemote); + + const list2 = remoteRootNode.children.find( + (c) => + c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList && + c.dbItem?.listName === "my-list-2", + ); + expect(list2).toBeTruthy(); + + const repo1Node = list2?.children.find( + (c) => + c.dbItem?.kind === DbItemKind.RemoteRepo && + c.dbItem?.repoFullName === "owner1/repo1", + ); + expect(repo1Node).toBeTruthy(); + expect(isTreeViewItemSelected(repo1Node!)).toBeTruthy(); + + const repo2Node = list2?.children.find( + (c) => + c.dbItem?.kind === DbItemKind.RemoteRepo && + c.dbItem?.repoFullName === "owner2/repo2", + ); + expect(repo2Node).toBeTruthy(); + expect(isTreeViewItemSelectable(repo2Node!)).toBeTruthy(); + + for (const item of remoteRootNode.children) { + expect(isTreeViewItemSelectable(item)).toBeTruthy(); + } + }); + + async function saveDbConfig(dbConfig: DbConfig): Promise { + await writeJson(dbConfigFilePath, dbConfig); + + // Ideally we would just initialise the db config store at the start + // of each test and then rely on the file watcher to update the config. + // However, this requires adding sleep to the tests to allow for the + // file watcher to catch up, so we instead initialise the config store here. + await dbConfigStore.initialize(); + dbTreeDataProvider = new DbTreeDataProvider(dbManager); + } + + function isTreeViewItemSelectable(treeViewItem: DbTreeViewItem) { + return ( + treeViewItem.resourceUri === undefined && + treeViewItem.contextValue?.includes("canBeSelected") + ); + } + + function isTreeViewItemSelected(treeViewItem: DbTreeViewItem) { + return ( + treeViewItem.resourceUri?.toString(true) === + SELECTED_DB_ITEM_RESOURCE_URI && + (treeViewItem.contextValue === undefined || + !treeViewItem.contextValue.includes("canBeSelected")) + ); + } +});