| 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 {CommitInfo} from 'isl/src/types'; |
| b69ab31 | | | 12 | |
| b69ab31 | | | 13 | import {makeServerSideTracker} from 'isl-server/src/analytics/serverSideTracker'; |
| b69ab31 | | | 14 | import { |
| b69ab31 | | | 15 | beforeRevsetForComparison, |
| b69ab31 | | | 16 | ComparisonType, |
| b69ab31 | | | 17 | currRevsetForComparison, |
| b69ab31 | | | 18 | } from 'shared/Comparison'; |
| b69ab31 | | | 19 | import {mockLogger} from 'shared/testUtils'; |
| b69ab31 | | | 20 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 21 | import * as vscode from 'vscode'; |
| b69ab31 | | | 22 | import { |
| b69ab31 | | | 23 | decodeSaplingDiffUri, |
| b69ab31 | | | 24 | encodeSaplingDiffUri, |
| b69ab31 | | | 25 | SaplingDiffContentProvider, |
| b69ab31 | | | 26 | } from '../DiffContentProvider'; |
| b69ab31 | | | 27 | |
| b69ab31 | | | 28 | const mockCancelToken = {} as vscode.CancellationToken; |
| b69ab31 | | | 29 | |
| b69ab31 | | | 30 | let activeReposCallback: undefined | ((repos: Array<Repository>) => unknown) = undefined; |
| b69ab31 | | | 31 | let activeRepo: (Repository & {mockChangeHeadCommit: (commit: CommitInfo) => void}) | undefined; |
| b69ab31 | | | 32 | jest.mock('isl-server/src/RepositoryCache', () => { |
| b69ab31 | | | 33 | return { |
| b69ab31 | | | 34 | repositoryCache: { |
| b69ab31 | | | 35 | onChangeActiveRepos(cb: (repos: Array<Repository>) => unknown) { |
| b69ab31 | | | 36 | activeReposCallback = cb; |
| b69ab31 | | | 37 | return () => (activeReposCallback = undefined); |
| b69ab31 | | | 38 | }, |
| b69ab31 | | | 39 | cachedRepositoryForPath(path: string): Repository | undefined { |
| b69ab31 | | | 40 | if (path.startsWith('/path/to/repo')) { |
| b69ab31 | | | 41 | return activeRepo; |
| b69ab31 | | | 42 | } |
| b69ab31 | | | 43 | return undefined; |
| b69ab31 | | | 44 | }, |
| b69ab31 | | | 45 | }, |
| b69ab31 | | | 46 | }; |
| b69ab31 | | | 47 | }); |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | const FILE1_CONTENT_HEAD = ` |
| b69ab31 | | | 50 | hello |
| b69ab31 | | | 51 | world |
| b69ab31 | | | 52 | `; |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | function mockRepoAdded(): NonNullable<typeof activeRepo> { |
| b69ab31 | | | 55 | let savedOnChangeHeadCommit: (commit: CommitInfo) => unknown; |
| b69ab31 | | | 56 | activeRepo = { |
| b69ab31 | | | 57 | // eslint-disable-next-line require-await |
| b69ab31 | | | 58 | cat: jest.fn(async (_ctx: RepositoryContext, file: string, rev: string) => { |
| b69ab31 | | | 59 | if (rev === '.' && file === '/path/to/repo/file1.txt') { |
| b69ab31 | | | 60 | return FILE1_CONTENT_HEAD; |
| b69ab31 | | | 61 | } |
| b69ab31 | | | 62 | throw new Error('unknown file'); |
| b69ab31 | | | 63 | }), |
| b69ab31 | | | 64 | info: { |
| b69ab31 | | | 65 | command: 'sl', |
| b69ab31 | | | 66 | repoRoot: '/path/to/repo', |
| b69ab31 | | | 67 | dotdir: '/path/to/repo/.sl', |
| b69ab31 | | | 68 | remoteRepo: {type: 'unknown', path: ''}, |
| b69ab31 | | | 69 | pullRequestDomain: undefined, |
| b69ab31 | | | 70 | }, |
| b69ab31 | | | 71 | subscribeToHeadCommit: jest.fn().mockImplementation(cb => { |
| b69ab31 | | | 72 | savedOnChangeHeadCommit = cb; |
| b69ab31 | | | 73 | return {dispose: jest.fn()}; |
| b69ab31 | | | 74 | }), |
| b69ab31 | | | 75 | mockChangeHeadCommit(commit: CommitInfo) { |
| b69ab31 | | | 76 | savedOnChangeHeadCommit(commit); |
| b69ab31 | | | 77 | }, |
| b69ab31 | | | 78 | initialConnectionContext: { |
| b69ab31 | | | 79 | cwd: '/path/to/repo', |
| b69ab31 | | | 80 | }, |
| b69ab31 | | | 81 | } as unknown as typeof activeRepo; |
| b69ab31 | | | 82 | activeReposCallback?.([nullthrows(activeRepo)]); |
| b69ab31 | | | 83 | return nullthrows(activeRepo); |
| b69ab31 | | | 84 | } |
| b69ab31 | | | 85 | function mockNoActiveRepo() { |
| b69ab31 | | | 86 | activeRepo = undefined; |
| b69ab31 | | | 87 | activeReposCallback?.([]); |
| b69ab31 | | | 88 | } |
| b69ab31 | | | 89 | |
| b69ab31 | | | 90 | const mockTracker = makeServerSideTracker( |
| b69ab31 | | | 91 | mockLogger, |
| b69ab31 | | | 92 | {platformName: 'test'} as ServerPlatform, |
| b69ab31 | | | 93 | '0.1', |
| b69ab31 | | | 94 | jest.fn(), |
| b69ab31 | | | 95 | ); |
| b69ab31 | | | 96 | |
| b69ab31 | | | 97 | describe('DiffContentProvider', () => { |
| b69ab31 | | | 98 | let ctx: RepositoryContext; |
| b69ab31 | | | 99 | beforeEach(() => { |
| b69ab31 | | | 100 | ctx = { |
| b69ab31 | | | 101 | cwd: 'cwd', |
| b69ab31 | | | 102 | cmd: 'sl', |
| b69ab31 | | | 103 | logger: mockLogger, |
| b69ab31 | | | 104 | tracker: mockTracker, |
| b69ab31 | | | 105 | }; |
| b69ab31 | | | 106 | }); |
| b69ab31 | | | 107 | |
| b69ab31 | | | 108 | const encodedFile1 = encodeSaplingDiffUri( |
| b69ab31 | | | 109 | vscode.Uri.file('/path/to/repo/file1.txt'), |
| b69ab31 | | | 110 | beforeRevsetForComparison({type: ComparisonType.UncommittedChanges}), |
| b69ab31 | | | 111 | ); |
| b69ab31 | | | 112 | |
| b69ab31 | | | 113 | it('provides file contents', async () => { |
| b69ab31 | | | 114 | const provider = new SaplingDiffContentProvider(ctx); |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | const repo = mockRepoAdded(); |
| b69ab31 | | | 117 | |
| b69ab31 | | | 118 | const content = await provider.provideTextDocumentContent(encodedFile1, mockCancelToken); |
| b69ab31 | | | 119 | |
| b69ab31 | | | 120 | expect(content).toEqual(FILE1_CONTENT_HEAD); |
| b69ab31 | | | 121 | expect(repo.cat).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 122 | expect(repo.cat).toHaveBeenCalledWith({cwd: '/path/to/repo'}, '/path/to/repo/file1.txt', '.'); |
| b69ab31 | | | 123 | provider.dispose(); |
| b69ab31 | | | 124 | }); |
| b69ab31 | | | 125 | |
| b69ab31 | | | 126 | it('caches file contents', async () => { |
| b69ab31 | | | 127 | const provider = new SaplingDiffContentProvider(ctx); |
| b69ab31 | | | 128 | const repo = mockRepoAdded(); |
| b69ab31 | | | 129 | await provider.provideTextDocumentContent(encodedFile1, mockCancelToken); |
| b69ab31 | | | 130 | await provider.provideTextDocumentContent(encodedFile1, mockCancelToken); |
| b69ab31 | | | 131 | expect(repo.cat).toHaveBeenCalledTimes(1); // second call hits file content cache |
| b69ab31 | | | 132 | provider.dispose(); |
| b69ab31 | | | 133 | }); |
| b69ab31 | | | 134 | |
| b69ab31 | | | 135 | it('invalidates files when the repository head changes', async () => { |
| b69ab31 | | | 136 | const commit1 = {hash: '1'} as CommitInfo; |
| b69ab31 | | | 137 | const commit2 = {hash: '2'} as CommitInfo; |
| b69ab31 | | | 138 | const provider = new SaplingDiffContentProvider(ctx); |
| b69ab31 | | | 139 | const onChange = jest.fn(); |
| b69ab31 | | | 140 | const onChangeDisposable = provider.onDidChange(onChange); |
| b69ab31 | | | 141 | const repo = mockRepoAdded(); |
| b69ab31 | | | 142 | repo.mockChangeHeadCommit(commit1); |
| b69ab31 | | | 143 | await provider.provideTextDocumentContent(encodedFile1, mockCancelToken); |
| b69ab31 | | | 144 | // changing the head commit has no effect since we hadn't yet provided content |
| b69ab31 | | | 145 | expect(onChange).toHaveBeenCalledTimes(0); |
| b69ab31 | | | 146 | |
| b69ab31 | | | 147 | repo.mockChangeHeadCommit(commit2); |
| b69ab31 | | | 148 | await provider.provideTextDocumentContent(encodedFile1, mockCancelToken); |
| b69ab31 | | | 149 | // now the provider knows that encodedFile1 is an active file, which triggers onChange. |
| b69ab31 | | | 150 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 151 | |
| b69ab31 | | | 152 | provider.dispose(); |
| b69ab31 | | | 153 | onChangeDisposable.dispose(); |
| b69ab31 | | | 154 | }); |
| b69ab31 | | | 155 | |
| b69ab31 | | | 156 | it('invalidates file content cache when the repository head changes', async () => { |
| b69ab31 | | | 157 | const commit1 = {hash: '1'} as CommitInfo; |
| b69ab31 | | | 158 | const commit2 = {hash: '2'} as CommitInfo; |
| b69ab31 | | | 159 | const provider = new SaplingDiffContentProvider(ctx); |
| b69ab31 | | | 160 | const repo = mockRepoAdded(); |
| b69ab31 | | | 161 | repo.mockChangeHeadCommit(commit1); |
| b69ab31 | | | 162 | |
| b69ab31 | | | 163 | await provider.provideTextDocumentContent(encodedFile1, mockCancelToken); |
| b69ab31 | | | 164 | expect(repo.cat).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 165 | |
| b69ab31 | | | 166 | repo.mockChangeHeadCommit(commit2); |
| b69ab31 | | | 167 | await provider.provideTextDocumentContent(encodedFile1, mockCancelToken); |
| b69ab31 | | | 168 | expect(repo.cat).toHaveBeenCalledTimes(2); |
| b69ab31 | | | 169 | |
| b69ab31 | | | 170 | provider.dispose(); |
| b69ab31 | | | 171 | }); |
| b69ab31 | | | 172 | |
| b69ab31 | | | 173 | it('files opened before repo created provide content once repo is ready', async () => { |
| b69ab31 | | | 174 | mockNoActiveRepo(); |
| b69ab31 | | | 175 | const provider = new SaplingDiffContentProvider(ctx); |
| b69ab31 | | | 176 | const onChange = jest.fn(); |
| b69ab31 | | | 177 | const onChangeDisposable = provider.onDidChange(onChange); |
| b69ab31 | | | 178 | |
| b69ab31 | | | 179 | const contentBeforeRepo = await provider.provideTextDocumentContent( |
| b69ab31 | | | 180 | encodedFile1, |
| b69ab31 | | | 181 | mockCancelToken, |
| b69ab31 | | | 182 | ); |
| b69ab31 | | | 183 | expect(contentBeforeRepo).toEqual(null); |
| b69ab31 | | | 184 | |
| b69ab31 | | | 185 | expect(onChange).not.toHaveBeenCalled(); |
| b69ab31 | | | 186 | mockRepoAdded(); |
| b69ab31 | | | 187 | // adding a repo triggers the content provider to tell vscode that the path changed... |
| b69ab31 | | | 188 | expect(onChange).toHaveBeenCalledWith(encodedFile1); |
| b69ab31 | | | 189 | |
| b69ab31 | | | 190 | // ...which means we re-run provideTextDocumentContent |
| b69ab31 | | | 191 | const contentAfterRepo = await provider.provideTextDocumentContent( |
| b69ab31 | | | 192 | encodedFile1, |
| b69ab31 | | | 193 | mockCancelToken, |
| b69ab31 | | | 194 | ); |
| b69ab31 | | | 195 | expect(contentAfterRepo).toEqual(FILE1_CONTENT_HEAD); |
| b69ab31 | | | 196 | provider.dispose(); |
| b69ab31 | | | 197 | onChangeDisposable.dispose(); |
| b69ab31 | | | 198 | }); |
| b69ab31 | | | 199 | |
| b69ab31 | | | 200 | it('closing a file disables telling vscode about file changes on checkout', async () => { |
| b69ab31 | | | 201 | let onCloseCallback: (e: vscode.TextDocument) => unknown = () => undefined; |
| b69ab31 | | | 202 | (vscode.workspace.onDidCloseTextDocument as jest.Mock).mockImplementation(cb => { |
| b69ab31 | | | 203 | onCloseCallback = cb; |
| b69ab31 | | | 204 | return {dispose: jest.fn()}; |
| b69ab31 | | | 205 | }); |
| b69ab31 | | | 206 | const commit1 = {hash: '1'} as CommitInfo; |
| b69ab31 | | | 207 | const commit2 = {hash: '2'} as CommitInfo; |
| b69ab31 | | | 208 | const provider = new SaplingDiffContentProvider(ctx); |
| b69ab31 | | | 209 | const onChange = jest.fn(); |
| b69ab31 | | | 210 | const onChangeDisposable = provider.onDidChange(onChange); |
| b69ab31 | | | 211 | const repo = mockRepoAdded(); |
| b69ab31 | | | 212 | await provider.provideTextDocumentContent(encodedFile1, mockCancelToken); |
| b69ab31 | | | 213 | expect(onChange).toHaveBeenCalledTimes(0); |
| b69ab31 | | | 214 | |
| b69ab31 | | | 215 | // normally if head changes, we detect it |
| b69ab31 | | | 216 | repo.mockChangeHeadCommit(commit1); |
| b69ab31 | | | 217 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 218 | |
| b69ab31 | | | 219 | // closing any old file doesn't do anything, we still detect head commit changes |
| b69ab31 | | | 220 | onCloseCallback({ |
| b69ab31 | | | 221 | uri: vscode.Uri.file('/some/unrelated/file'), |
| b69ab31 | | | 222 | } as unknown as vscode.TextDocument); |
| b69ab31 | | | 223 | repo.mockChangeHeadCommit(commit2); |
| b69ab31 | | | 224 | expect(onChange).toHaveBeenCalledTimes(2); |
| b69ab31 | | | 225 | |
| b69ab31 | | | 226 | // closing the encoded uri means we stop listening for changes |
| b69ab31 | | | 227 | onCloseCallback?.({uri: encodedFile1} as unknown as vscode.TextDocument); |
| b69ab31 | | | 228 | repo.mockChangeHeadCommit(commit2); |
| b69ab31 | | | 229 | expect(onChange).toHaveBeenCalledTimes(2); // no new call happened |
| b69ab31 | | | 230 | |
| b69ab31 | | | 231 | provider.dispose(); |
| b69ab31 | | | 232 | onChangeDisposable.dispose(); |
| b69ab31 | | | 233 | }); |
| b69ab31 | | | 234 | |
| b69ab31 | | | 235 | it('closing a file, then updating the head commit removes the file content cache', async () => { |
| b69ab31 | | | 236 | let onCloseCallback: (e: vscode.TextDocument) => unknown = () => undefined; |
| b69ab31 | | | 237 | (vscode.workspace.onDidCloseTextDocument as jest.Mock).mockImplementation(cb => { |
| b69ab31 | | | 238 | onCloseCallback = cb; |
| b69ab31 | | | 239 | return {dispose: jest.fn()}; |
| b69ab31 | | | 240 | }); |
| b69ab31 | | | 241 | const commit2 = {hash: '2'} as CommitInfo; |
| b69ab31 | | | 242 | const provider = new SaplingDiffContentProvider(ctx); |
| b69ab31 | | | 243 | const repo = mockRepoAdded(); |
| b69ab31 | | | 244 | await provider.provideTextDocumentContent(encodedFile1, mockCancelToken); |
| b69ab31 | | | 245 | expect(repo.cat).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 246 | |
| b69ab31 | | | 247 | onCloseCallback?.({uri: encodedFile1} as unknown as vscode.TextDocument); |
| b69ab31 | | | 248 | |
| b69ab31 | | | 249 | await provider.provideTextDocumentContent(encodedFile1, mockCancelToken); |
| b69ab31 | | | 250 | expect(repo.cat).toHaveBeenCalledTimes(1); // file still cached |
| b69ab31 | | | 251 | |
| b69ab31 | | | 252 | onCloseCallback?.({uri: encodedFile1} as unknown as vscode.TextDocument); |
| b69ab31 | | | 253 | |
| b69ab31 | | | 254 | repo.mockChangeHeadCommit(commit2); |
| b69ab31 | | | 255 | await provider.provideTextDocumentContent(encodedFile1, mockCancelToken); |
| b69ab31 | | | 256 | expect(repo.cat).toHaveBeenCalledTimes(2); // file no longer cached |
| b69ab31 | | | 257 | |
| b69ab31 | | | 258 | provider.dispose(); |
| b69ab31 | | | 259 | }); |
| b69ab31 | | | 260 | }); |
| b69ab31 | | | 261 | |
| b69ab31 | | | 262 | describe('SaplingDiffEncodedUri', () => { |
| b69ab31 | | | 263 | it('is reversible', () => { |
| b69ab31 | | | 264 | const encoded = encodeSaplingDiffUri( |
| b69ab31 | | | 265 | vscode.Uri.file('/path/to/myRepo'), |
| b69ab31 | | | 266 | currRevsetForComparison({ |
| b69ab31 | | | 267 | type: ComparisonType.UncommittedChanges, |
| b69ab31 | | | 268 | }), |
| b69ab31 | | | 269 | ); |
| b69ab31 | | | 270 | const decoded = decodeSaplingDiffUri(encoded); |
| b69ab31 | | | 271 | expect(decoded).toEqual({ |
| b69ab31 | | | 272 | originalUri: expect.anything(), |
| b69ab31 | | | 273 | revset: 'wdir()', |
| b69ab31 | | | 274 | }); |
| b69ab31 | | | 275 | expect(decoded.originalUri.toString()).toEqual('file:///path/to/myRepo'); |
| b69ab31 | | | 276 | }); |
| b69ab31 | | | 277 | }); |