|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as fs from 'fs-extra'; |
| 5 | +import * as pq from 'proxyquire'; |
| 6 | +import { DbConfig } from '../../../databases/db-config'; |
| 7 | +import { DbManager } from '../../../databases/db-manager'; |
| 8 | +import { DbConfigStore } from '../../../databases/db-config-store'; |
| 9 | +import { DbTreeDataProvider } from '../../../databases/ui/db-tree-data-provider'; |
| 10 | +import { DbPanel } from '../../../databases/ui/db-panel'; |
| 11 | +import { DbItemKind } from '../../../databases/db-item'; |
| 12 | +import { DbTreeViewItem } from '../../../databases/ui/db-tree-view-item'; |
| 13 | + |
| 14 | +const proxyquire = pq.noPreserveCache(); |
| 15 | + |
| 16 | +describe('db panel', async () => { |
| 17 | + const workspaceStoragePath = path.join(__dirname, 'test-workspace'); |
| 18 | + const extensionPath = path.join(__dirname, '../../../../'); |
| 19 | + const dbConfigFilePath = path.join(workspaceStoragePath, 'workspace-databases.json'); |
| 20 | + let dbTreeDataProvider: DbTreeDataProvider; |
| 21 | + let dbManager: DbManager; |
| 22 | + let dbConfigStore: DbConfigStore; |
| 23 | + let dbPanel: DbPanel; |
| 24 | + |
| 25 | + before(async () => { |
| 26 | + dbConfigStore = new DbConfigStore(workspaceStoragePath, extensionPath); |
| 27 | + dbManager = new DbManager(dbConfigStore); |
| 28 | + |
| 29 | + // Create a modified version of the DbPanel module that allows |
| 30 | + // us to override the creation of the DbTreeDataProvider |
| 31 | + const mod = proxyquire('../../../databases/ui/db-panel', { |
| 32 | + './db-tree-data-provider': { |
| 33 | + DbTreeDataProvider: class { |
| 34 | + constructor() { |
| 35 | + return dbTreeDataProvider; |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + // Initialize the panel using the modified module |
| 42 | + dbPanel = new mod.DbPanel(dbManager) as DbPanel; |
| 43 | + await dbPanel.initialize(); |
| 44 | + }); |
| 45 | + |
| 46 | + beforeEach(async () => { |
| 47 | + await fs.ensureDir(workspaceStoragePath); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(async () => { |
| 51 | + await fs.remove(workspaceStoragePath); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should render default local and remote nodes when the config is empty', async () => { |
| 55 | + const dbConfig: DbConfig = { |
| 56 | + remote: { |
| 57 | + repositoryLists: [], |
| 58 | + owners: [], |
| 59 | + repositories: [] |
| 60 | + }, |
| 61 | + }; |
| 62 | + |
| 63 | + await saveDbConfig(dbConfig); |
| 64 | + |
| 65 | + const dbTreeItems = await dbTreeDataProvider.getChildren(); |
| 66 | + |
| 67 | + expect(dbTreeItems).to.be.ok; |
| 68 | + const items = dbTreeItems!; |
| 69 | + expect(items.length).to.equal(2); |
| 70 | + |
| 71 | + const remoteRootNode = items[0]; |
| 72 | + expect(remoteRootNode.dbItem).to.be.ok; |
| 73 | + expect(remoteRootNode.dbItem?.kind).to.equal(DbItemKind.RootRemote); |
| 74 | + expect(remoteRootNode.label).to.equal('remote'); |
| 75 | + expect(remoteRootNode.tooltip).to.equal('Remote databases'); |
| 76 | + expect(remoteRootNode.collapsibleState).to.equal(vscode.TreeItemCollapsibleState.Collapsed); |
| 77 | + expect(remoteRootNode.children).to.be.ok; |
| 78 | + expect(remoteRootNode.children.length).to.equal(3); |
| 79 | + |
| 80 | + const systemDefinedListItems = remoteRootNode.children.filter(item => item.dbItem?.kind === DbItemKind.RemoteSystemDefinedList); |
| 81 | + expect(systemDefinedListItems.length).to.equal(3); |
| 82 | + checkRemoteSystemDefinedListItem(systemDefinedListItems[0], 10); |
| 83 | + checkRemoteSystemDefinedListItem(systemDefinedListItems[1], 100); |
| 84 | + checkRemoteSystemDefinedListItem(systemDefinedListItems[2], 1000); |
| 85 | + |
| 86 | + const localRootNode = items[1]; |
| 87 | + expect(localRootNode.dbItem).to.be.ok; |
| 88 | + expect(localRootNode.dbItem?.kind).to.equal(DbItemKind.RootLocal); |
| 89 | + expect(localRootNode.label).to.equal('local'); |
| 90 | + expect(localRootNode.tooltip).to.equal('Local databases'); |
| 91 | + expect(localRootNode.collapsibleState).to.equal(vscode.TreeItemCollapsibleState.Collapsed); |
| 92 | + expect(localRootNode.children).to.be.ok; |
| 93 | + expect(localRootNode.children.length).to.equal(0); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should render remote repository list nodes', async () => { |
| 97 | + const dbConfig: DbConfig = { |
| 98 | + remote: { |
| 99 | + repositoryLists: [ |
| 100 | + { |
| 101 | + name: 'my-list-1', |
| 102 | + repositories: [ |
| 103 | + 'owner1/repo1', |
| 104 | + 'owner1/repo2' |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + name: 'my-list-2', |
| 109 | + repositories: [ |
| 110 | + 'owner1/repo1', |
| 111 | + 'owner2/repo1', |
| 112 | + 'owner2/repo2' |
| 113 | + ] |
| 114 | + }, |
| 115 | + ], |
| 116 | + owners: [], |
| 117 | + repositories: [] |
| 118 | + }, |
| 119 | + }; |
| 120 | + |
| 121 | + await saveDbConfig(dbConfig); |
| 122 | + |
| 123 | + const dbTreeItems = await dbTreeDataProvider.getChildren(); |
| 124 | + |
| 125 | + expect(dbTreeItems).to.be.ok; |
| 126 | + const items = dbTreeItems!; |
| 127 | + expect(items.length).to.equal(2); |
| 128 | + |
| 129 | + const remoteRootNode = items[0]; |
| 130 | + expect(remoteRootNode.dbItem).to.be.ok; |
| 131 | + expect(remoteRootNode.collapsibleState).to.equal(vscode.TreeItemCollapsibleState.Collapsed); |
| 132 | + expect(remoteRootNode.children).to.be.ok; |
| 133 | + expect(remoteRootNode.children.length).to.equal(5); |
| 134 | + |
| 135 | + const systemDefinedListItems = remoteRootNode.children.filter(item => item.dbItem?.kind === DbItemKind.RemoteSystemDefinedList); |
| 136 | + expect(systemDefinedListItems.length).to.equal(3); |
| 137 | + |
| 138 | + const userDefinedListItems = remoteRootNode.children.filter(item => item.dbItem?.kind === DbItemKind.RemoteUserDefinedList); |
| 139 | + expect(userDefinedListItems.length).to.equal(2); |
| 140 | + checkUserDefinedListItem(userDefinedListItems[0], 'my-list-1', ['owner1/repo1', 'owner1/repo2']); |
| 141 | + checkUserDefinedListItem(userDefinedListItems[1], 'my-list-2', ['owner1/repo1', 'owner2/repo1', 'owner2/repo2']); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should render owner list nodes', async () => { |
| 145 | + const dbConfig: DbConfig = { |
| 146 | + remote: { |
| 147 | + repositoryLists: [], |
| 148 | + owners: ['owner1', 'owner2'], |
| 149 | + repositories: [] |
| 150 | + }, |
| 151 | + }; |
| 152 | + |
| 153 | + await saveDbConfig(dbConfig); |
| 154 | + |
| 155 | + const dbTreeItems = await dbTreeDataProvider.getChildren(); |
| 156 | + |
| 157 | + expect(dbTreeItems).to.be.ok; |
| 158 | + const items = dbTreeItems!; |
| 159 | + expect(items.length).to.equal(2); |
| 160 | + |
| 161 | + const remoteRootNode = items[0]; |
| 162 | + expect(remoteRootNode.dbItem).to.be.ok; |
| 163 | + expect(remoteRootNode.collapsibleState).to.equal(vscode.TreeItemCollapsibleState.Collapsed); |
| 164 | + expect(remoteRootNode.children).to.be.ok; |
| 165 | + expect(remoteRootNode.children.length).to.equal(5); |
| 166 | + |
| 167 | + const ownerListItems = remoteRootNode.children.filter(item => item.dbItem?.kind === DbItemKind.RemoteOwner); |
| 168 | + expect(ownerListItems.length).to.equal(2); |
| 169 | + checkOwnerItem(ownerListItems[0], 'owner1'); |
| 170 | + checkOwnerItem(ownerListItems[1], 'owner2'); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should render repository nodes', async () => { |
| 174 | + const dbConfig: DbConfig = { |
| 175 | + remote: { |
| 176 | + repositoryLists: [], |
| 177 | + owners: [], |
| 178 | + repositories: ['owner1/repo1', 'owner1/repo2'] |
| 179 | + }, |
| 180 | + }; |
| 181 | + |
| 182 | + await saveDbConfig(dbConfig); |
| 183 | + |
| 184 | + const dbTreeItems = await dbTreeDataProvider.getChildren(); |
| 185 | + |
| 186 | + expect(dbTreeItems).to.be.ok; |
| 187 | + const items = dbTreeItems!; |
| 188 | + expect(items.length).to.equal(2); |
| 189 | + |
| 190 | + const remoteRootNode = items[0]; |
| 191 | + expect(remoteRootNode.dbItem).to.be.ok; |
| 192 | + expect(remoteRootNode.collapsibleState).to.equal(vscode.TreeItemCollapsibleState.Collapsed); |
| 193 | + expect(remoteRootNode.children).to.be.ok; |
| 194 | + expect(remoteRootNode.children.length).to.equal(5); |
| 195 | + |
| 196 | + const repoItems = remoteRootNode.children.filter(item => item.dbItem?.kind === DbItemKind.RemoteRepo); |
| 197 | + expect(repoItems.length).to.equal(2); |
| 198 | + checkRemoteRepoItem(repoItems[0], 'owner1/repo1'); |
| 199 | + checkRemoteRepoItem(repoItems[1], 'owner1/repo2'); |
| 200 | + }); |
| 201 | + |
| 202 | + async function saveDbConfig(dbConfig: DbConfig): Promise<void> { |
| 203 | + await fs.writeJson(dbConfigFilePath, dbConfig); |
| 204 | + |
| 205 | + // Once we have watching of the db config, this can happen |
| 206 | + // at the start of the test. |
| 207 | + await dbConfigStore.initialize(); |
| 208 | + dbTreeDataProvider = new DbTreeDataProvider(dbManager); |
| 209 | + } |
| 210 | + |
| 211 | + function checkRemoteSystemDefinedListItem( |
| 212 | + item: DbTreeViewItem, |
| 213 | + n: number |
| 214 | + ): void { |
| 215 | + expect(item.label).to.equal(`Top ${n} repositories`); |
| 216 | + expect(item.tooltip).to.equal(`Top ${n} repositories of a language`); |
| 217 | + expect(item.iconPath).to.deep.equal(new vscode.ThemeIcon('github')); |
| 218 | + expect(item.collapsibleState).to.equal(vscode.TreeItemCollapsibleState.None); |
| 219 | + } |
| 220 | + |
| 221 | + function checkUserDefinedListItem( |
| 222 | + item: DbTreeViewItem, |
| 223 | + listName: string, |
| 224 | + repos: string[] |
| 225 | + ): void { |
| 226 | + expect(item.label).to.equal(listName); |
| 227 | + expect(item.tooltip).to.be.undefined; |
| 228 | + expect(item.iconPath).to.be.undefined; |
| 229 | + expect(item.collapsibleState).to.equal(vscode.TreeItemCollapsibleState.Collapsed); |
| 230 | + expect(item.children).to.be.ok; |
| 231 | + expect(item.children.length).to.equal(repos.length); |
| 232 | + |
| 233 | + for (let i = 0; i < repos.length; i++) { |
| 234 | + checkRemoteRepoItem(item.children[i], repos[i]); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + function checkOwnerItem( |
| 239 | + item: DbTreeViewItem, |
| 240 | + ownerName: string |
| 241 | + ): void { |
| 242 | + expect(item.label).to.equal(ownerName); |
| 243 | + expect(item.tooltip).to.be.undefined; |
| 244 | + expect(item.iconPath).to.deep.equal(new vscode.ThemeIcon('organization')); |
| 245 | + expect(item.collapsibleState).to.equal(vscode.TreeItemCollapsibleState.None); |
| 246 | + expect(item.children).to.be.ok; |
| 247 | + expect(item.children.length).to.equal(0); |
| 248 | + } |
| 249 | + |
| 250 | + function checkRemoteRepoItem( |
| 251 | + item: DbTreeViewItem, |
| 252 | + repoName: string |
| 253 | + ): void { |
| 254 | + expect(item.label).to.equal(repoName); |
| 255 | + expect(item.tooltip).to.be.undefined; |
| 256 | + expect(item.iconPath).to.deep.equal(new vscode.ThemeIcon('database')); |
| 257 | + expect(item.collapsibleState).to.equal(vscode.TreeItemCollapsibleState.None); |
| 258 | + } |
| 259 | +}); |
0 commit comments