|
| 1 | +import { join } from "path"; |
| 2 | +import { ensureDir, remove, writeJson } from "fs-extra"; |
| 3 | +import { |
| 4 | + DbConfig, |
| 5 | + SelectedDbItemKind, |
| 6 | +} from "../../../../src/databases/config/db-config"; |
| 7 | +import { DbManager } from "../../../../src/databases/db-manager"; |
| 8 | +import { DbConfigStore } from "../../../../src/databases/config/db-config-store"; |
| 9 | +import { DbTreeDataProvider } from "../../../../src/databases/ui/db-tree-data-provider"; |
| 10 | +import { DbItemKind } from "../../../../src/databases/db-item"; |
| 11 | +import { |
| 12 | + DbTreeViewItem, |
| 13 | + SELECTED_DB_ITEM_RESOURCE_URI, |
| 14 | +} from "../../../../src/databases/ui/db-tree-view-item"; |
| 15 | +import { ExtensionApp } from "../../../../src/common/vscode/vscode-app"; |
| 16 | +import { createMockExtensionContext } from "../../../factories/extension-context"; |
| 17 | +import { createDbConfig } from "../../../factories/db-config-factories"; |
| 18 | + |
| 19 | +describe("db panel selection", () => { |
| 20 | + const workspaceStoragePath = join(__dirname, "test-workspace-storage"); |
| 21 | + const globalStoragePath = join(__dirname, "test-global-storage"); |
| 22 | + const extensionPath = join(__dirname, "../../../../"); |
| 23 | + const dbConfigFilePath = join( |
| 24 | + workspaceStoragePath, |
| 25 | + DbConfigStore.databaseConfigFileName, |
| 26 | + ); |
| 27 | + let dbTreeDataProvider: DbTreeDataProvider; |
| 28 | + let dbManager: DbManager; |
| 29 | + let dbConfigStore: DbConfigStore; |
| 30 | + |
| 31 | + beforeAll(async () => { |
| 32 | + const extensionContext = createMockExtensionContext({ |
| 33 | + extensionPath, |
| 34 | + globalStoragePath, |
| 35 | + workspaceStoragePath, |
| 36 | + }); |
| 37 | + await ensureDir(workspaceStoragePath); |
| 38 | + |
| 39 | + const app = new ExtensionApp(extensionContext); |
| 40 | + |
| 41 | + dbConfigStore = new DbConfigStore(app, false); |
| 42 | + dbManager = new DbManager(app, dbConfigStore); |
| 43 | + }); |
| 44 | + |
| 45 | + beforeEach(async () => { |
| 46 | + await ensureDir(workspaceStoragePath); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(async () => { |
| 50 | + await remove(workspaceStoragePath); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should mark selected remote db list as selected", async () => { |
| 54 | + const dbConfig: DbConfig = createDbConfig({ |
| 55 | + remoteLists: [ |
| 56 | + { |
| 57 | + name: "my-list-1", |
| 58 | + repositories: ["owner1/repo1", "owner1/repo2"], |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "my-list-2", |
| 62 | + repositories: ["owner2/repo1", "owner2/repo2"], |
| 63 | + }, |
| 64 | + ], |
| 65 | + selected: { |
| 66 | + kind: SelectedDbItemKind.VariantAnalysisUserDefinedList, |
| 67 | + listName: "my-list-2", |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + await saveDbConfig(dbConfig); |
| 72 | + |
| 73 | + const dbTreeItems = await dbTreeDataProvider.getChildren(); |
| 74 | + |
| 75 | + expect(dbTreeItems).toBeTruthy(); |
| 76 | + const items = dbTreeItems!; |
| 77 | + |
| 78 | + const remoteRootNode = items[0]; |
| 79 | + expect(remoteRootNode.dbItem).toBeTruthy(); |
| 80 | + expect(remoteRootNode.dbItem?.kind).toEqual(DbItemKind.RootRemote); |
| 81 | + |
| 82 | + const list1 = remoteRootNode.children.find( |
| 83 | + (c) => |
| 84 | + c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList && |
| 85 | + c.dbItem?.listName === "my-list-1", |
| 86 | + ); |
| 87 | + const list2 = remoteRootNode.children.find( |
| 88 | + (c) => |
| 89 | + c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList && |
| 90 | + c.dbItem?.listName === "my-list-2", |
| 91 | + ); |
| 92 | + |
| 93 | + expect(list1).toBeTruthy(); |
| 94 | + expect(list2).toBeTruthy(); |
| 95 | + expect(isTreeViewItemSelectable(list1!)).toBeTruthy(); |
| 96 | + expect(isTreeViewItemSelected(list2!)).toBeTruthy(); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should mark selected remote db inside list as selected", async () => { |
| 100 | + const dbConfig: DbConfig = createDbConfig({ |
| 101 | + remoteLists: [ |
| 102 | + { |
| 103 | + name: "my-list-1", |
| 104 | + repositories: ["owner1/repo1", "owner1/repo2"], |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "my-list-2", |
| 108 | + repositories: ["owner1/repo1", "owner2/repo2"], |
| 109 | + }, |
| 110 | + ], |
| 111 | + remoteRepos: ["owner1/repo1"], |
| 112 | + selected: { |
| 113 | + kind: SelectedDbItemKind.VariantAnalysisRepository, |
| 114 | + repositoryName: "owner1/repo1", |
| 115 | + listName: "my-list-2", |
| 116 | + }, |
| 117 | + }); |
| 118 | + |
| 119 | + await saveDbConfig(dbConfig); |
| 120 | + |
| 121 | + const dbTreeItems = await dbTreeDataProvider.getChildren(); |
| 122 | + |
| 123 | + expect(dbTreeItems).toBeTruthy(); |
| 124 | + const items = dbTreeItems!; |
| 125 | + |
| 126 | + const remoteRootNode = items[0]; |
| 127 | + expect(remoteRootNode.dbItem).toBeTruthy(); |
| 128 | + expect(remoteRootNode.dbItem?.kind).toEqual(DbItemKind.RootRemote); |
| 129 | + |
| 130 | + const list2 = remoteRootNode.children.find( |
| 131 | + (c) => |
| 132 | + c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList && |
| 133 | + c.dbItem?.listName === "my-list-2", |
| 134 | + ); |
| 135 | + expect(list2).toBeTruthy(); |
| 136 | + |
| 137 | + const repo1Node = list2?.children.find( |
| 138 | + (c) => |
| 139 | + c.dbItem?.kind === DbItemKind.RemoteRepo && |
| 140 | + c.dbItem?.repoFullName === "owner1/repo1", |
| 141 | + ); |
| 142 | + expect(repo1Node).toBeTruthy(); |
| 143 | + expect(isTreeViewItemSelected(repo1Node!)).toBeTruthy(); |
| 144 | + |
| 145 | + const repo2Node = list2?.children.find( |
| 146 | + (c) => |
| 147 | + c.dbItem?.kind === DbItemKind.RemoteRepo && |
| 148 | + c.dbItem?.repoFullName === "owner2/repo2", |
| 149 | + ); |
| 150 | + expect(repo2Node).toBeTruthy(); |
| 151 | + expect(isTreeViewItemSelectable(repo2Node!)).toBeTruthy(); |
| 152 | + |
| 153 | + for (const item of remoteRootNode.children) { |
| 154 | + expect(isTreeViewItemSelectable(item)).toBeTruthy(); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + async function saveDbConfig(dbConfig: DbConfig): Promise<void> { |
| 159 | + await writeJson(dbConfigFilePath, dbConfig); |
| 160 | + |
| 161 | + // Ideally we would just initialise the db config store at the start |
| 162 | + // of each test and then rely on the file watcher to update the config. |
| 163 | + // However, this requires adding sleep to the tests to allow for the |
| 164 | + // file watcher to catch up, so we instead initialise the config store here. |
| 165 | + await dbConfigStore.initialize(); |
| 166 | + dbTreeDataProvider = new DbTreeDataProvider(dbManager); |
| 167 | + } |
| 168 | + |
| 169 | + function isTreeViewItemSelectable(treeViewItem: DbTreeViewItem) { |
| 170 | + return ( |
| 171 | + treeViewItem.resourceUri === undefined && |
| 172 | + treeViewItem.contextValue?.includes("canBeSelected") |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + function isTreeViewItemSelected(treeViewItem: DbTreeViewItem) { |
| 177 | + return ( |
| 178 | + treeViewItem.resourceUri?.toString(true) === |
| 179 | + SELECTED_DB_ITEM_RESOURCE_URI && |
| 180 | + (treeViewItem.contextValue === undefined || |
| 181 | + !treeViewItem.contextValue.includes("canBeSelected")) |
| 182 | + ); |
| 183 | + } |
| 184 | +}); |
0 commit comments