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