| 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 {act, fireEvent, render, screen, waitFor} from '@testing-library/react'; |
| b69ab31 | | | 9 | import App from '../App'; |
| b69ab31 | | | 10 | import {generatedFileCache} from '../GeneratedFile'; |
| b69ab31 | | | 11 | import {__TEST__} from '../UncommittedChanges'; |
| b69ab31 | | | 12 | import {readAtom, writeAtom} from '../jotaiUtils'; |
| b69ab31 | | | 13 | import platform from '../platform'; |
| b69ab31 | | | 14 | import {ignoreRTL} from '../testQueries'; |
| b69ab31 | | | 15 | import { |
| b69ab31 | | | 16 | closeCommitInfoSidebar, |
| b69ab31 | | | 17 | COMMIT, |
| b69ab31 | | | 18 | expectMessageSentToServer, |
| b69ab31 | | | 19 | openCommitInfoSidebar, |
| b69ab31 | | | 20 | resetTestMessages, |
| b69ab31 | | | 21 | simulateCommits, |
| b69ab31 | | | 22 | simulateMessageFromServer, |
| b69ab31 | | | 23 | simulateRepoConnected, |
| b69ab31 | | | 24 | simulateUncommittedChangedFiles, |
| b69ab31 | | | 25 | } from '../testUtils'; |
| b69ab31 | | | 26 | import {GeneratedStatus} from '../types'; |
| b69ab31 | | | 27 | |
| b69ab31 | | | 28 | /** Generated `num` files, in the repeating pattern: generated, partially generated, manual */ |
| b69ab31 | | | 29 | async function simulateGeneratedFiles(num: number) { |
| b69ab31 | | | 30 | const files = new Array(num).fill(null).map((_, i) => `file_${zeroPad(i)}.txt`); |
| b69ab31 | | | 31 | act(() => { |
| b69ab31 | | | 32 | simulateUncommittedChangedFiles({ |
| b69ab31 | | | 33 | value: files.map(path => ({ |
| b69ab31 | | | 34 | path, |
| b69ab31 | | | 35 | status: 'M', |
| b69ab31 | | | 36 | })), |
| b69ab31 | | | 37 | }); |
| b69ab31 | | | 38 | }); |
| b69ab31 | | | 39 | await waitFor(() => { |
| b69ab31 | | | 40 | expectMessageSentToServer({ |
| b69ab31 | | | 41 | type: 'fetchGeneratedStatuses', |
| b69ab31 | | | 42 | paths: expect.anything(), |
| b69ab31 | | | 43 | }); |
| b69ab31 | | | 44 | }); |
| b69ab31 | | | 45 | act(() => { |
| b69ab31 | | | 46 | simulateMessageFromServer({ |
| b69ab31 | | | 47 | type: 'fetchedGeneratedStatuses', |
| b69ab31 | | | 48 | results: Object.fromEntries( |
| b69ab31 | | | 49 | files.map((path, i) => [ |
| b69ab31 | | | 50 | path, |
| b69ab31 | | | 51 | i % 3 === 0 |
| b69ab31 | | | 52 | ? GeneratedStatus.Generated |
| b69ab31 | | | 53 | : i % 3 === 1 |
| b69ab31 | | | 54 | ? GeneratedStatus.PartiallyGenerated |
| b69ab31 | | | 55 | : GeneratedStatus.Manual, |
| b69ab31 | | | 56 | ]), |
| b69ab31 | | | 57 | ), |
| b69ab31 | | | 58 | }); |
| b69ab31 | | | 59 | }); |
| b69ab31 | | | 60 | } |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | function zeroPad(n: number): string { |
| b69ab31 | | | 63 | return ('000' + n.toString()).slice(-3); |
| b69ab31 | | | 64 | } |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | describe('Generated Files', () => { |
| b69ab31 | | | 67 | beforeEach(() => { |
| b69ab31 | | | 68 | resetTestMessages(); |
| b69ab31 | | | 69 | render(<App />); |
| b69ab31 | | | 70 | generatedFileCache.clear(); |
| b69ab31 | | | 71 | act(() => { |
| b69ab31 | | | 72 | simulateRepoConnected(); |
| b69ab31 | | | 73 | closeCommitInfoSidebar(); |
| b69ab31 | | | 74 | expectMessageSentToServer({ |
| b69ab31 | | | 75 | type: 'subscribe', |
| b69ab31 | | | 76 | kind: 'smartlogCommits', |
| b69ab31 | | | 77 | subscriptionID: expect.anything(), |
| b69ab31 | | | 78 | }); |
| b69ab31 | | | 79 | simulateCommits({ |
| b69ab31 | | | 80 | value: [ |
| b69ab31 | | | 81 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| b69ab31 | | | 82 | COMMIT('a', 'My Commit', '1'), |
| b69ab31 | | | 83 | COMMIT('b', 'Another Commit', 'a', {isDot: true}), |
| b69ab31 | | | 84 | ], |
| b69ab31 | | | 85 | }); |
| b69ab31 | | | 86 | expectMessageSentToServer({ |
| b69ab31 | | | 87 | type: 'subscribe', |
| b69ab31 | | | 88 | kind: 'uncommittedChanges', |
| b69ab31 | | | 89 | subscriptionID: expect.anything(), |
| b69ab31 | | | 90 | }); |
| b69ab31 | | | 91 | }); |
| b69ab31 | | | 92 | }); |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | it('fetches generated files for uncommitted changes', async () => { |
| b69ab31 | | | 95 | await simulateGeneratedFiles(5); |
| b69ab31 | | | 96 | expectMessageSentToServer({ |
| b69ab31 | | | 97 | type: 'fetchGeneratedStatuses', |
| b69ab31 | | | 98 | paths: ['file_000.txt', 'file_001.txt', 'file_002.txt', 'file_003.txt', 'file_004.txt'], |
| b69ab31 | | | 99 | }); |
| b69ab31 | | | 100 | }); |
| b69ab31 | | | 101 | |
| b69ab31 | | | 102 | it('Shows generated files in their own sections', async () => { |
| b69ab31 | | | 103 | await simulateGeneratedFiles(10); |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | expect(screen.getByText(ignoreRTL('file_002.txt'))).toBeInTheDocument(); |
| b69ab31 | | | 106 | expect(screen.getByText(ignoreRTL('file_005.txt'))).toBeInTheDocument(); |
| b69ab31 | | | 107 | expect(screen.getByText(ignoreRTL('file_008.txt'))).toBeInTheDocument(); |
| b69ab31 | | | 108 | expect(screen.getByText('Generated Files')).toBeInTheDocument(); |
| b69ab31 | | | 109 | expect(screen.getByText('Partially Generated Files')).toBeInTheDocument(); |
| b69ab31 | | | 110 | }); |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | function goToNextPage() { |
| b69ab31 | | | 113 | fireEvent.click(screen.getByTestId('changed-files-next-page')); |
| b69ab31 | | | 114 | } |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | function expectHasPartiallyGeneratedFiles() { |
| b69ab31 | | | 117 | expect(screen.queryByText('Partially Generated Files')).toBeInTheDocument(); |
| b69ab31 | | | 118 | } |
| b69ab31 | | | 119 | function expectHasGeneratedFiles() { |
| b69ab31 | | | 120 | expect(screen.queryByText('Generated Files')).toBeInTheDocument(); |
| b69ab31 | | | 121 | } |
| b69ab31 | | | 122 | function expectNOTHasGeneratedFiles() { |
| b69ab31 | | | 123 | expect(screen.queryByText('Generated Files')).not.toBeInTheDocument(); |
| b69ab31 | | | 124 | } |
| b69ab31 | | | 125 | |
| b69ab31 | | | 126 | function getChangedFiles() { |
| b69ab31 | | | 127 | const found = [...document.querySelectorAll('.changed-file-path-text')].map(e => |
| b69ab31 | | | 128 | (e as HTMLElement).innerHTML.replace(/\u200E/g, ''), |
| b69ab31 | | | 129 | ); |
| b69ab31 | | | 130 | return found; |
| b69ab31 | | | 131 | } |
| b69ab31 | | | 132 | |
| b69ab31 | | | 133 | it('Paginates generated files', async () => { |
| b69ab31 | | | 134 | await simulateGeneratedFiles(1200); |
| b69ab31 | | | 135 | // 1200 files, but 1000 files per fetched batch of generated statuses. |
| b69ab31 | | | 136 | // Sorted by status, that puts 1000/3 manual files, then 1000/3 partially generated, then 1000/3 generated, |
| b69ab31 | | | 137 | // then the remaining 200/3 manual, 200/3 partially generated, and 200/3 generated, |
| b69ab31 | | | 138 | // all in pages of 500 at a time. |
| b69ab31 | | | 139 | |
| b69ab31 | | | 140 | // first page is manual and partial |
| b69ab31 | | | 141 | expectHasPartiallyGeneratedFiles(); |
| b69ab31 | | | 142 | expectNOTHasGeneratedFiles(); |
| b69ab31 | | | 143 | expect(getChangedFiles()).toMatchSnapshot(); |
| b69ab31 | | | 144 | |
| b69ab31 | | | 145 | // next page has partial and generated |
| b69ab31 | | | 146 | goToNextPage(); |
| b69ab31 | | | 147 | expectHasPartiallyGeneratedFiles(); |
| b69ab31 | | | 148 | expectHasGeneratedFiles(); |
| b69ab31 | | | 149 | expect(getChangedFiles()).toMatchSnapshot(); |
| b69ab31 | | | 150 | |
| b69ab31 | | | 151 | // next page has remaining files from all 3 types |
| b69ab31 | | | 152 | goToNextPage(); |
| b69ab31 | | | 153 | expectHasPartiallyGeneratedFiles(); |
| b69ab31 | | | 154 | expectHasGeneratedFiles(); |
| b69ab31 | | | 155 | expect(getChangedFiles()).toMatchSnapshot(); |
| b69ab31 | | | 156 | }); |
| b69ab31 | | | 157 | |
| b69ab31 | | | 158 | it('Warns about too many files to fetch all generated statuses', async () => { |
| b69ab31 | | | 159 | await simulateGeneratedFiles(1001); |
| b69ab31 | | | 160 | expect( |
| b69ab31 | | | 161 | screen.getByText('There are more than 1000 files, some files may appear out of order'), |
| b69ab31 | | | 162 | ).toBeInTheDocument(); |
| b69ab31 | | | 163 | }); |
| b69ab31 | | | 164 | |
| b69ab31 | | | 165 | it('remembers expanded state', async () => { |
| b69ab31 | | | 166 | writeAtom(__TEST__.generatedFilesInitiallyExpanded, true); |
| b69ab31 | | | 167 | |
| b69ab31 | | | 168 | await simulateGeneratedFiles(1); |
| b69ab31 | | | 169 | |
| b69ab31 | | | 170 | expect(screen.getByText(ignoreRTL('file_000.txt'))).toBeInTheDocument(); |
| b69ab31 | | | 171 | expect(screen.getByText('Generated Files')).toBeInTheDocument(); |
| b69ab31 | | | 172 | }); |
| b69ab31 | | | 173 | |
| b69ab31 | | | 174 | it('writes expanded state', async () => { |
| b69ab31 | | | 175 | expect(readAtom(__TEST__.generatedFilesInitiallyExpanded)).toEqual(false); |
| b69ab31 | | | 176 | |
| b69ab31 | | | 177 | await simulateGeneratedFiles(1); |
| b69ab31 | | | 178 | |
| b69ab31 | | | 179 | fireEvent.click(screen.getByText('Generated Files')); |
| b69ab31 | | | 180 | |
| b69ab31 | | | 181 | expect(readAtom(__TEST__.generatedFilesInitiallyExpanded)).toEqual(true); |
| b69ab31 | | | 182 | }); |
| b69ab31 | | | 183 | |
| b69ab31 | | | 184 | it('clears generated files cache on refresh click', async () => { |
| b69ab31 | | | 185 | act(() => { |
| b69ab31 | | | 186 | simulateUncommittedChangedFiles({ |
| b69ab31 | | | 187 | value: [ |
| b69ab31 | | | 188 | { |
| b69ab31 | | | 189 | path: 'file.txt', |
| b69ab31 | | | 190 | status: 'M', |
| b69ab31 | | | 191 | }, |
| b69ab31 | | | 192 | ], |
| b69ab31 | | | 193 | }); |
| b69ab31 | | | 194 | }); |
| b69ab31 | | | 195 | await waitFor(() => { |
| b69ab31 | | | 196 | expectMessageSentToServer({ |
| b69ab31 | | | 197 | type: 'fetchGeneratedStatuses', |
| b69ab31 | | | 198 | paths: ['file.txt'], |
| b69ab31 | | | 199 | }); |
| b69ab31 | | | 200 | }); |
| b69ab31 | | | 201 | act(() => { |
| b69ab31 | | | 202 | simulateMessageFromServer({ |
| b69ab31 | | | 203 | type: 'fetchedGeneratedStatuses', |
| b69ab31 | | | 204 | results: Object.fromEntries([['file.txt', GeneratedStatus.Manual]]), |
| b69ab31 | | | 205 | }); |
| b69ab31 | | | 206 | }); |
| b69ab31 | | | 207 | |
| b69ab31 | | | 208 | expect(screen.queryByText('Generated Files')).not.toBeInTheDocument(); |
| b69ab31 | | | 209 | |
| b69ab31 | | | 210 | act(() => { |
| b69ab31 | | | 211 | fireEvent.click(screen.getByTestId('refresh-button')); |
| b69ab31 | | | 212 | }); |
| b69ab31 | | | 213 | await waitFor(() => { |
| b69ab31 | | | 214 | expectMessageSentToServer({ |
| b69ab31 | | | 215 | type: 'fetchGeneratedStatuses', |
| b69ab31 | | | 216 | paths: ['file.txt'], |
| b69ab31 | | | 217 | }); |
| b69ab31 | | | 218 | }); |
| b69ab31 | | | 219 | act(() => { |
| b69ab31 | | | 220 | simulateMessageFromServer({ |
| b69ab31 | | | 221 | type: 'fetchedGeneratedStatuses', |
| b69ab31 | | | 222 | results: Object.fromEntries([['file.txt', GeneratedStatus.Generated]]), |
| b69ab31 | | | 223 | }); |
| b69ab31 | | | 224 | }); |
| b69ab31 | | | 225 | |
| b69ab31 | | | 226 | expect(screen.getByText('Generated Files')).toBeInTheDocument(); |
| b69ab31 | | | 227 | }); |
| b69ab31 | | | 228 | |
| b69ab31 | | | 229 | describe('Open All Files', () => { |
| b69ab31 | | | 230 | beforeEach(() => act(() => openCommitInfoSidebar())); |
| b69ab31 | | | 231 | async function simulateCommitWithFiles(files: Record<string, GeneratedStatus>) { |
| b69ab31 | | | 232 | act(() => { |
| b69ab31 | | | 233 | simulateCommits({ |
| b69ab31 | | | 234 | value: [ |
| b69ab31 | | | 235 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| b69ab31 | | | 236 | COMMIT('a', 'Commit A', '1', { |
| b69ab31 | | | 237 | isDot: true, |
| b69ab31 | | | 238 | totalFileCount: 3, |
| b69ab31 | | | 239 | filePathsSample: Object.keys(files), |
| b69ab31 | | | 240 | }), |
| b69ab31 | | | 241 | ], |
| b69ab31 | | | 242 | }); |
| b69ab31 | | | 243 | }); |
| b69ab31 | | | 244 | await waitFor(() => { |
| b69ab31 | | | 245 | expectMessageSentToServer({ |
| b69ab31 | | | 246 | type: 'fetchGeneratedStatuses', |
| b69ab31 | | | 247 | paths: expect.anything(), |
| b69ab31 | | | 248 | }); |
| b69ab31 | | | 249 | }); |
| b69ab31 | | | 250 | act(() => { |
| b69ab31 | | | 251 | simulateMessageFromServer({ |
| b69ab31 | | | 252 | type: 'fetchedGeneratedStatuses', |
| b69ab31 | | | 253 | results: files, |
| b69ab31 | | | 254 | }); |
| b69ab31 | | | 255 | }); |
| b69ab31 | | | 256 | } |
| b69ab31 | | | 257 | |
| b69ab31 | | | 258 | it('No generated files, opens all files', async () => { |
| b69ab31 | | | 259 | const openSpy = jest.spyOn(platform, 'openFiles').mockImplementation(() => {}); |
| b69ab31 | | | 260 | await simulateCommitWithFiles({ |
| b69ab31 | | | 261 | 'file_partial.txt': GeneratedStatus.PartiallyGenerated, |
| b69ab31 | | | 262 | 'file_manual.txt': GeneratedStatus.Manual, |
| b69ab31 | | | 263 | }); |
| b69ab31 | | | 264 | |
| b69ab31 | | | 265 | fireEvent.click(screen.getByText('Open All Files')); |
| b69ab31 | | | 266 | expect(openSpy).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 267 | expect(openSpy).toHaveBeenCalledWith(['file_partial.txt', 'file_manual.txt']); |
| b69ab31 | | | 268 | }); |
| b69ab31 | | | 269 | |
| b69ab31 | | | 270 | it('Some generated files, opens all non-generated files', async () => { |
| b69ab31 | | | 271 | const openSpy = jest.spyOn(platform, 'openFiles').mockImplementation(() => {}); |
| b69ab31 | | | 272 | await simulateCommitWithFiles({ |
| b69ab31 | | | 273 | 'file_gen.txt': GeneratedStatus.Generated, |
| b69ab31 | | | 274 | 'file_partial.txt': GeneratedStatus.PartiallyGenerated, |
| b69ab31 | | | 275 | 'file_manual.txt': GeneratedStatus.Manual, |
| b69ab31 | | | 276 | }); |
| b69ab31 | | | 277 | |
| b69ab31 | | | 278 | fireEvent.click(screen.getByText('Open Non-Generated Files')); |
| b69ab31 | | | 279 | expect(openSpy).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 280 | expect(openSpy).toHaveBeenCalledWith(['file_partial.txt', 'file_manual.txt']); |
| b69ab31 | | | 281 | }); |
| b69ab31 | | | 282 | |
| b69ab31 | | | 283 | it('All generated files, opens all files', async () => { |
| b69ab31 | | | 284 | const openSpy = jest.spyOn(platform, 'openFiles').mockImplementation(() => {}); |
| b69ab31 | | | 285 | await simulateCommitWithFiles({ |
| b69ab31 | | | 286 | 'file_gen1.txt': GeneratedStatus.Generated, |
| b69ab31 | | | 287 | 'file_gen2.txt': GeneratedStatus.Generated, |
| b69ab31 | | | 288 | }); |
| b69ab31 | | | 289 | |
| b69ab31 | | | 290 | fireEvent.click(screen.getByText('Open All Files')); |
| b69ab31 | | | 291 | expect(openSpy).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 292 | expect(openSpy).toHaveBeenCalledWith(['file_gen1.txt', 'file_gen2.txt']); |
| b69ab31 | | | 293 | }); |
| b69ab31 | | | 294 | }); |
| b69ab31 | | | 295 | }); |