| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {act, fireEvent, render, screen, within} from '@testing-library/react'; |
| 9 | import App from '../App'; |
| 10 | import {__TEST__} from '../repositoryData'; |
| 11 | import {CommitTreeListTestUtils} from '../testQueries'; |
| 12 | import { |
| 13 | closeCommitInfoSidebar, |
| 14 | COMMIT, |
| 15 | expectMessageSentToServer, |
| 16 | resetTestMessages, |
| 17 | simulateCommits, |
| 18 | simulateMessageFromServer, |
| 19 | simulateRepoConnected, |
| 20 | } from '../testUtils'; |
| 21 | const {isIrrelevantToCwd} = __TEST__; |
| 22 | |
| 23 | const {clickGoto} = CommitTreeListTestUtils; |
| 24 | |
| 25 | describe('cwd', () => { |
| 26 | beforeEach(() => { |
| 27 | render(<App />); |
| 28 | act(() => { |
| 29 | simulateRepoConnected(); |
| 30 | closeCommitInfoSidebar(); |
| 31 | expectMessageSentToServer({ |
| 32 | type: 'subscribe', |
| 33 | kind: 'smartlogCommits', |
| 34 | subscriptionID: expect.anything(), |
| 35 | }); |
| 36 | simulateCommits({ |
| 37 | value: [ |
| 38 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| 39 | COMMIT('a', 'My Commit', '1'), |
| 40 | COMMIT('b', 'Another Commit', 'a', {isDot: true}), |
| 41 | ], |
| 42 | }); |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | const openCwdDropdown = () => { |
| 47 | const cwdDropdown = screen.getByTestId('cwd-dropdown-button'); |
| 48 | act(() => { |
| 49 | fireEvent.click(cwdDropdown); |
| 50 | }); |
| 51 | expectMessageSentToServer({ |
| 52 | type: 'platform/subscribeToAvailableCwds', |
| 53 | }); |
| 54 | act(() => { |
| 55 | simulateMessageFromServer({ |
| 56 | type: 'platform/availableCwds', |
| 57 | options: [ |
| 58 | { |
| 59 | cwd: '/path/to/repo1', |
| 60 | repoRoot: '/path/to/repo1', |
| 61 | repoRelativeCwdLabel: 'repo1', |
| 62 | }, |
| 63 | { |
| 64 | cwd: '/path/to/repo2', |
| 65 | repoRoot: '/path/to/repo2', |
| 66 | repoRelativeCwdLabel: 'repo2', |
| 67 | }, |
| 68 | { |
| 69 | cwd: '/path/to/repo2/some/subdir', |
| 70 | repoRoot: '/path/to/repo2', |
| 71 | repoRelativeCwdLabel: 'repo2/some/subdir', |
| 72 | }, |
| 73 | ], |
| 74 | }); |
| 75 | }); |
| 76 | }; |
| 77 | |
| 78 | it('shows repo+relative cwd in the cwd button', () => { |
| 79 | act(() => { |
| 80 | simulateRepoConnected('/path/to/repo', '/path/to/repo/some/subdir'); |
| 81 | }); |
| 82 | expect(screen.getByText('repo/some/subdir')).toBeInTheDocument(); |
| 83 | |
| 84 | act(() => { |
| 85 | simulateRepoConnected('C:\\path\\to\\repo', 'C:\\path\\to\\repo\\some\\subdir'); |
| 86 | }); |
| 87 | expect(screen.getByText('repo\\some\\subdir')).toBeInTheDocument(); |
| 88 | }); |
| 89 | |
| 90 | it('shows cwd options from the platform', () => { |
| 91 | openCwdDropdown(); |
| 92 | |
| 93 | const dropdown = screen.getByTestId('cwd-details-dropdown'); |
| 94 | |
| 95 | expect(within(dropdown).getByText('repo1')).toBeInTheDocument(); |
| 96 | expect(within(dropdown).getByText('repo2')).toBeInTheDocument(); |
| 97 | }); |
| 98 | |
| 99 | it('shows cwd options from the platform with repo relative cwd paths', () => { |
| 100 | openCwdDropdown(); |
| 101 | |
| 102 | const dropdown = screen.getByTestId('cwd-details-dropdown'); |
| 103 | |
| 104 | expect(within(dropdown).getByText('repo1')).toBeInTheDocument(); |
| 105 | expect(within(dropdown).getByText('repo2')).toBeInTheDocument(); |
| 106 | expect(within(dropdown).getByText('repo2/some/subdir')).toBeInTheDocument(); |
| 107 | }); |
| 108 | |
| 109 | it('requests new data for subscriptions after changing cwd', () => { |
| 110 | openCwdDropdown(); |
| 111 | |
| 112 | resetTestMessages(); |
| 113 | |
| 114 | const dropdown = screen.getByTestId('cwd-details-dropdown'); |
| 115 | act(() => { |
| 116 | fireEvent.click(within(dropdown).getByText('repo2')); |
| 117 | }); |
| 118 | |
| 119 | expectMessageSentToServer({type: 'changeCwd', cwd: '/path/to/repo2'}); |
| 120 | expectMessageSentToServer({type: 'requestRepoInfo'}); |
| 121 | expectMessageSentToServer({ |
| 122 | type: 'subscribe', |
| 123 | kind: 'smartlogCommits', |
| 124 | subscriptionID: expect.anything(), |
| 125 | }); |
| 126 | expectMessageSentToServer({ |
| 127 | type: 'subscribe', |
| 128 | kind: 'uncommittedChanges', |
| 129 | subscriptionID: expect.anything(), |
| 130 | }); |
| 131 | }); |
| 132 | |
| 133 | it('clears out saved state when changing repos', async () => { |
| 134 | await clickGoto('a'); |
| 135 | |
| 136 | expect(screen.getByText('sl goto --rev a')).toBeInTheDocument(); |
| 137 | |
| 138 | openCwdDropdown(); |
| 139 | |
| 140 | resetTestMessages(); |
| 141 | |
| 142 | const dropdown = screen.getByTestId('cwd-details-dropdown'); |
| 143 | act(() => { |
| 144 | fireEvent.click(within(dropdown).getByText('repo2')); |
| 145 | simulateRepoConnected('/path/to/repo2'); |
| 146 | }); |
| 147 | |
| 148 | expect(screen.queryByText('sl goto --rev a')).not.toBeInTheDocument(); |
| 149 | }); |
| 150 | |
| 151 | it('dismisses dropdown when changing cwd', () => { |
| 152 | openCwdDropdown(); |
| 153 | |
| 154 | const dropdown = screen.getByTestId('cwd-details-dropdown'); |
| 155 | act(() => { |
| 156 | fireEvent.click(within(dropdown).getByText('repo2')); |
| 157 | }); |
| 158 | |
| 159 | expect(screen.queryByTestId('cwd-details-dropdown')).not.toBeInTheDocument(); |
| 160 | }); |
| 161 | }); |
| 162 | |
| 163 | describe('isIrrelevantToCwd', () => { |
| 164 | const C = (maxPrefix: string) => COMMIT('1', 'title', '0', {maxCommonPathPrefix: maxPrefix}); |
| 165 | it('handles files outside cwd', () => { |
| 166 | expect(isIrrelevantToCwd(C('www/'), 'fbcode/')).toBe(true); |
| 167 | expect(isIrrelevantToCwd(C('www/subdir/'), 'fbcode/')).toBe(true); |
| 168 | }); |
| 169 | it('handles files inside cwd', () => { |
| 170 | expect(isIrrelevantToCwd(C('www/'), 'www/')).toBe(false); |
| 171 | expect(isIrrelevantToCwd(C('www/subdir/'), 'www/')).toBe(false); |
| 172 | }); |
| 173 | it('handles files above cwd', () => { |
| 174 | expect(isIrrelevantToCwd(C('addons/isl/'), 'addons/isl')).toBe(false); |
| 175 | expect(isIrrelevantToCwd(C('addons/'), 'addons/isl')).toBe(false); |
| 176 | expect(isIrrelevantToCwd(C(''), 'addons/isl')).toBe(false); |
| 177 | }); |
| 178 | it('handles cwd is root', () => { |
| 179 | expect(isIrrelevantToCwd(C('addons/isl/'), '')).toBe(false); |
| 180 | expect(isIrrelevantToCwd(C('addons/'), '')).toBe(false); |
| 181 | expect(isIrrelevantToCwd(C(''), '')).toBe(false); |
| 182 | expect(isIrrelevantToCwd(C('addons/isl/'), '/')).toBe(false); |
| 183 | expect(isIrrelevantToCwd(C(''), '/')).toBe(false); |
| 184 | expect(isIrrelevantToCwd(C('/'), '/')).toBe(false); |
| 185 | }); |
| 186 | }); |
| 187 | |