| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Repository} from 'isl-server/src/Repository'; |
| b69ab31 | | | 9 | import type {ServerPlatform} from 'isl-server/src/serverPlatform'; |
| b69ab31 | | | 10 | import type {RepositoryContext} from 'isl-server/src/serverTypes'; |
| b69ab31 | | | 11 | import type {RepoInfo, ValidatedRepoInfo} from 'isl/src/types'; |
| b69ab31 | | | 12 | import type {EnabledSCMApiFeature} from '../types'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | import {repositoryCache} from 'isl-server/src/RepositoryCache'; |
| b69ab31 | | | 15 | import {makeServerSideTracker} from 'isl-server/src/analytics/serverSideTracker'; |
| b69ab31 | | | 16 | import {Logger} from 'isl-server/src/logger'; |
| b69ab31 | | | 17 | import {TypedEventEmitter} from 'shared/TypedEventEmitter'; |
| b69ab31 | | | 18 | import {nextTick} from 'shared/testUtils'; |
| b69ab31 | | | 19 | import * as vscode from 'vscode'; |
| b69ab31 | | | 20 | import {VSCodeReposList} from '../VSCodeRepo'; |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | export class MockLogger extends Logger { |
| b69ab31 | | | 23 | write() { |
| b69ab31 | | | 24 | // noop |
| b69ab31 | | | 25 | } |
| b69ab31 | | | 26 | } |
| b69ab31 | | | 27 | export const mockLogger = new MockLogger(); |
| b69ab31 | | | 28 | |
| b69ab31 | | | 29 | const mockTracker = makeServerSideTracker( |
| b69ab31 | | | 30 | mockLogger, |
| b69ab31 | | | 31 | {platformName: 'test'} as ServerPlatform, |
| b69ab31 | | | 32 | '0.1', |
| b69ab31 | | | 33 | jest.fn(), |
| b69ab31 | | | 34 | ); |
| b69ab31 | | | 35 | |
| b69ab31 | | | 36 | jest.mock('isl-server/src/Repository', () => { |
| b69ab31 | | | 37 | class MockRepository implements Partial<Repository> { |
| b69ab31 | | | 38 | static getRepoInfo = jest.fn((ctx: RepositoryContext): Promise<RepoInfo> => { |
| b69ab31 | | | 39 | let root: string; |
| b69ab31 | | | 40 | // resolve cwd into shared mock locations |
| b69ab31 | | | 41 | if (ctx.cwd.includes('/path/to/repo1')) { |
| b69ab31 | | | 42 | root = '/path/to/repo1'; |
| b69ab31 | | | 43 | } else if (ctx.cwd.includes('/path/to/repo2')) { |
| b69ab31 | | | 44 | root = '/path/to/repo2'; |
| b69ab31 | | | 45 | } else { |
| b69ab31 | | | 46 | return Promise.resolve({type: 'cwdNotARepository', cwd: ctx.cwd}); |
| b69ab31 | | | 47 | } |
| b69ab31 | | | 48 | return Promise.resolve({ |
| b69ab31 | | | 49 | type: 'success', |
| b69ab31 | | | 50 | repoRoot: root, |
| b69ab31 | | | 51 | dotdir: root + '/.sl', |
| b69ab31 | | | 52 | command: 'sl', |
| b69ab31 | | | 53 | preferredSubmitCommand: 'pr', |
| b69ab31 | | | 54 | codeReviewSystem: {type: 'unknown', path: ''}, |
| b69ab31 | | | 55 | pullRequestDomain: undefined, |
| b69ab31 | | | 56 | isEdenFs: false, |
| b69ab31 | | | 57 | }); |
| b69ab31 | | | 58 | }); |
| b69ab31 | | | 59 | constructor( |
| b69ab31 | | | 60 | public info: ValidatedRepoInfo, |
| b69ab31 | | | 61 | public logger?: Logger, |
| b69ab31 | | | 62 | ) {} |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | public disposables: Array<() => void> = []; |
| b69ab31 | | | 65 | public dispose() { |
| b69ab31 | | | 66 | this.disposables.forEach(d => d()); |
| b69ab31 | | | 67 | } |
| b69ab31 | | | 68 | public onDidDispose = (cb: () => void) => this.disposables.push(cb); |
| b69ab31 | | | 69 | public subscribeToUncommittedChanges = jest.fn(); |
| b69ab31 | | | 70 | public onChangeConflictState = jest.fn(); |
| b69ab31 | | | 71 | public getUncommittedChanges = jest.fn(); |
| b69ab31 | | | 72 | public getMergeConflicts = jest.fn(); |
| b69ab31 | | | 73 | } |
| b69ab31 | | | 74 | return { |
| b69ab31 | | | 75 | Repository: MockRepository as unknown as Repository, |
| b69ab31 | | | 76 | }; |
| b69ab31 | | | 77 | }); |
| b69ab31 | | | 78 | |
| b69ab31 | | | 79 | describe('adding and removing repositories', () => { |
| b69ab31 | | | 80 | let foldersEmitter: TypedEventEmitter<'value', vscode.WorkspaceFoldersChangeEvent>; |
| b69ab31 | | | 81 | beforeEach(() => { |
| b69ab31 | | | 82 | foldersEmitter = new TypedEventEmitter(); |
| b69ab31 | | | 83 | (vscode.workspace.onDidChangeWorkspaceFolders as jest.Mock).mockImplementation(cb => { |
| b69ab31 | | | 84 | foldersEmitter.on('value', cb); |
| b69ab31 | | | 85 | return {dispose: () => foldersEmitter.off('value', cb)}; |
| b69ab31 | | | 86 | }); |
| b69ab31 | | | 87 | }); |
| b69ab31 | | | 88 | |
| b69ab31 | | | 89 | afterEach(() => { |
| b69ab31 | | | 90 | jest.clearAllMocks(); |
| b69ab31 | | | 91 | repositoryCache.clearCache(); |
| b69ab31 | | | 92 | foldersEmitter.removeAllListeners(); |
| b69ab31 | | | 93 | }); |
| b69ab31 | | | 94 | |
| b69ab31 | | | 95 | const ENABLED = new Set<EnabledSCMApiFeature>(['blame', 'sidebar']); |
| b69ab31 | | | 96 | |
| b69ab31 | | | 97 | it('creates repositories for workspace folders', async () => { |
| b69ab31 | | | 98 | const repos = new VSCodeReposList(mockLogger, mockTracker, ENABLED); |
| b69ab31 | | | 99 | foldersEmitter.emit('value', { |
| b69ab31 | | | 100 | added: [{name: 'my folder', index: 0, uri: vscode.Uri.file('/path/to/repo1')}], |
| b69ab31 | | | 101 | removed: [], |
| b69ab31 | | | 102 | }); |
| b69ab31 | | | 103 | await nextTick(); |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | expect(vscode.scm.createSourceControl).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 106 | repos.dispose(); |
| b69ab31 | | | 107 | }); |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | it('deduplicates among shared repos', async () => { |
| b69ab31 | | | 110 | const repos = new VSCodeReposList(mockLogger, mockTracker, ENABLED); |
| b69ab31 | | | 111 | foldersEmitter.emit('value', { |
| b69ab31 | | | 112 | added: [{name: 'my folder', index: 0, uri: vscode.Uri.file('/path/to/repo1/foo')}], |
| b69ab31 | | | 113 | removed: [], |
| b69ab31 | | | 114 | }); |
| b69ab31 | | | 115 | await nextTick(); |
| b69ab31 | | | 116 | foldersEmitter.emit('value', { |
| b69ab31 | | | 117 | added: [{name: 'my folder', index: 1, uri: vscode.Uri.file('/path/to/repo1/bar')}], |
| b69ab31 | | | 118 | removed: [], |
| b69ab31 | | | 119 | }); |
| b69ab31 | | | 120 | await nextTick(); |
| b69ab31 | | | 121 | |
| b69ab31 | | | 122 | expect(vscode.scm.createSourceControl).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 123 | |
| b69ab31 | | | 124 | foldersEmitter.emit('value', { |
| b69ab31 | | | 125 | added: [{name: 'my folder', index: 1, uri: vscode.Uri.file('/path/to/repo2/foobar')}], |
| b69ab31 | | | 126 | removed: [], |
| b69ab31 | | | 127 | }); |
| b69ab31 | | | 128 | await nextTick(); |
| b69ab31 | | | 129 | expect(vscode.scm.createSourceControl).toHaveBeenCalledTimes(2); |
| b69ab31 | | | 130 | |
| b69ab31 | | | 131 | repos.dispose(); |
| b69ab31 | | | 132 | }); |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | it('deletes repositories for workspace folders', async () => { |
| b69ab31 | | | 135 | const repos = new VSCodeReposList(mockLogger, mockTracker, ENABLED); |
| b69ab31 | | | 136 | |
| b69ab31 | | | 137 | // add repo twice, only creates 1 repo |
| b69ab31 | | | 138 | foldersEmitter.emit('value', { |
| b69ab31 | | | 139 | added: [{name: 'my folder', index: 0, uri: vscode.Uri.file('/path/to/repo1/foo')}], |
| b69ab31 | | | 140 | removed: [], |
| b69ab31 | | | 141 | }); |
| b69ab31 | | | 142 | await nextTick(); |
| b69ab31 | | | 143 | foldersEmitter.emit('value', { |
| b69ab31 | | | 144 | added: [{name: 'my folder', index: 0, uri: vscode.Uri.file('/path/to/repo1/bar')}], |
| b69ab31 | | | 145 | removed: [], |
| b69ab31 | | | 146 | }); |
| b69ab31 | | | 147 | await nextTick(); |
| b69ab31 | | | 148 | expect(vscode.scm.createSourceControl).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 149 | |
| b69ab31 | | | 150 | // remove that repo |
| b69ab31 | | | 151 | foldersEmitter.emit('value', { |
| b69ab31 | | | 152 | added: [], |
| b69ab31 | | | 153 | removed: [{name: 'my folder', index: 1, uri: vscode.Uri.file('/path/to/repo1/foo')}], |
| b69ab31 | | | 154 | }); |
| b69ab31 | | | 155 | await nextTick(); |
| b69ab31 | | | 156 | foldersEmitter.emit('value', { |
| b69ab31 | | | 157 | added: [], |
| b69ab31 | | | 158 | removed: [{name: 'my folder', index: 1, uri: vscode.Uri.file('/path/to/repo1/bar')}], |
| b69ab31 | | | 159 | }); |
| b69ab31 | | | 160 | await nextTick(); |
| b69ab31 | | | 161 | |
| b69ab31 | | | 162 | // adding the same repo again must create it again |
| b69ab31 | | | 163 | foldersEmitter.emit('value', { |
| b69ab31 | | | 164 | added: [{name: 'my folder', index: 0, uri: vscode.Uri.file('/path/to/repo1/foo')}], |
| b69ab31 | | | 165 | removed: [], |
| b69ab31 | | | 166 | }); |
| b69ab31 | | | 167 | await nextTick(); |
| b69ab31 | | | 168 | expect(vscode.scm.createSourceControl).toHaveBeenCalledTimes(2); |
| b69ab31 | | | 169 | |
| b69ab31 | | | 170 | repos.dispose(); |
| b69ab31 | | | 171 | }); |
| b69ab31 | | | 172 | |
| b69ab31 | | | 173 | it('looks up repos by prefix', async () => { |
| b69ab31 | | | 174 | const repos = new VSCodeReposList(mockLogger, mockTracker, ENABLED); |
| b69ab31 | | | 175 | |
| b69ab31 | | | 176 | foldersEmitter.emit('value', { |
| b69ab31 | | | 177 | added: [{name: 'my folder', index: 0, uri: vscode.Uri.file('/path/to/repo1/foo')}], |
| b69ab31 | | | 178 | removed: [], |
| b69ab31 | | | 179 | }); |
| b69ab31 | | | 180 | await nextTick(); |
| b69ab31 | | | 181 | |
| b69ab31 | | | 182 | expect(repos.repoForPath('/path/to/repo1/foo')).not.toBeUndefined(); |
| b69ab31 | | | 183 | expect(repos.repoForPath('/path/to/repo1/foo/myFile.txt')).not.toBeUndefined(); |
| b69ab31 | | | 184 | expect(repos.repoForPath('/path/to/repo2/foo')).toBeUndefined(); |
| b69ab31 | | | 185 | |
| b69ab31 | | | 186 | foldersEmitter.emit('value', { |
| b69ab31 | | | 187 | added: [], |
| b69ab31 | | | 188 | removed: [{name: 'my folder', index: 1, uri: vscode.Uri.file('/path/to/repo1/foo')}], |
| b69ab31 | | | 189 | }); |
| b69ab31 | | | 190 | await nextTick(); |
| b69ab31 | | | 191 | |
| b69ab31 | | | 192 | expect(repos.repoForPath('/path/to/repo1/foo')).toBeUndefined(); |
| b69ab31 | | | 193 | expect(repos.repoForPath('/path/to/repo1/foo/myFile.txt')).toBeUndefined(); |
| b69ab31 | | | 194 | |
| b69ab31 | | | 195 | repos.dispose(); |
| b69ab31 | | | 196 | }); |
| b69ab31 | | | 197 | }); |