| 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 {DateTuple} from 'shared/types/common'; |
| b69ab31 | | | 9 | import type {CodeReviewSystem} from '../types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {act, fireEvent, render, screen, waitFor, within} from '@testing-library/react'; |
| b69ab31 | | | 12 | import * as utils from 'shared/utils'; |
| b69ab31 | | | 13 | import App from '../App'; |
| b69ab31 | | | 14 | import { |
| b69ab31 | | | 15 | closeCommitInfoSidebar, |
| b69ab31 | | | 16 | expectMessageNOTSentToServer, |
| b69ab31 | | | 17 | expectMessageSentToServer, |
| b69ab31 | | | 18 | getLastMessageOfTypeSentToServer, |
| b69ab31 | | | 19 | resetTestMessages, |
| b69ab31 | | | 20 | simulateCommits, |
| b69ab31 | | | 21 | simulateMessageFromServer, |
| b69ab31 | | | 22 | TEST_COMMIT_HISTORY, |
| b69ab31 | | | 23 | } from '../testUtils'; |
| b69ab31 | | | 24 | import {CommandRunner} from '../types'; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | const EXPORT_STACK_DATA = [ |
| b69ab31 | | | 27 | { |
| b69ab31 | | | 28 | requested: false, |
| b69ab31 | | | 29 | node: 'd', |
| b69ab31 | | | 30 | author: 'username', |
| b69ab31 | | | 31 | date: [1715719789, 25200] as DateTuple, |
| b69ab31 | | | 32 | text: 'Commit D', |
| b69ab31 | | | 33 | immutable: false, |
| b69ab31 | | | 34 | relevantFiles: { |
| b69ab31 | | | 35 | 'myFile.js': { |
| b69ab31 | | | 36 | data: 'hello\nworld!\n', |
| b69ab31 | | | 37 | }, |
| b69ab31 | | | 38 | }, |
| b69ab31 | | | 39 | }, |
| b69ab31 | | | 40 | { |
| b69ab31 | | | 41 | requested: true, |
| b69ab31 | | | 42 | node: 'e', |
| b69ab31 | | | 43 | author: 'username', |
| b69ab31 | | | 44 | date: [1715719789, 25200] as DateTuple, |
| b69ab31 | | | 45 | text: 'Commit E', |
| b69ab31 | | | 46 | immutable: false, |
| b69ab31 | | | 47 | parents: ['d'], |
| b69ab31 | | | 48 | files: { |
| b69ab31 | | | 49 | 'myFile.js': { |
| b69ab31 | | | 50 | data: 'hello (changed)\nworld!\n', |
| b69ab31 | | | 51 | }, |
| b69ab31 | | | 52 | }, |
| b69ab31 | | | 53 | }, |
| b69ab31 | | | 54 | ]; |
| b69ab31 | | | 55 | |
| b69ab31 | | | 56 | describe('Interactive Split', () => { |
| b69ab31 | | | 57 | beforeEach(() => { |
| b69ab31 | | | 58 | resetTestMessages(); |
| b69ab31 | | | 59 | render(<App />); |
| b69ab31 | | | 60 | act(() => { |
| b69ab31 | | | 61 | closeCommitInfoSidebar(); |
| b69ab31 | | | 62 | simulateMessageFromServer({ |
| b69ab31 | | | 63 | type: 'repoInfo', |
| b69ab31 | | | 64 | info: { |
| b69ab31 | | | 65 | type: 'success', |
| b69ab31 | | | 66 | repoRoot: '/path/to/repo', |
| b69ab31 | | | 67 | dotdir: '/path/to/repo/.sl', |
| b69ab31 | | | 68 | command: 'sl', |
| b69ab31 | | | 69 | pullRequestDomain: undefined, |
| b69ab31 | | | 70 | codeReviewSystem: {type: 'github'} as CodeReviewSystem, |
| b69ab31 | | | 71 | isEdenFs: false, |
| b69ab31 | | | 72 | }, |
| b69ab31 | | | 73 | }); |
| b69ab31 | | | 74 | expectMessageSentToServer({ |
| b69ab31 | | | 75 | type: 'subscribe', |
| b69ab31 | | | 76 | kind: 'smartlogCommits', |
| b69ab31 | | | 77 | subscriptionID: expect.anything(), |
| b69ab31 | | | 78 | }); |
| b69ab31 | | | 79 | simulateCommits({ |
| b69ab31 | | | 80 | value: TEST_COMMIT_HISTORY, |
| b69ab31 | | | 81 | }); |
| b69ab31 | | | 82 | }); |
| b69ab31 | | | 83 | |
| b69ab31 | | | 84 | const mockObserveFn = () => { |
| b69ab31 | | | 85 | return { |
| b69ab31 | | | 86 | observe: jest.fn(), |
| b69ab31 | | | 87 | unobserve: jest.fn(), |
| b69ab31 | | | 88 | disconnect: jest.fn(), |
| b69ab31 | | | 89 | }; |
| b69ab31 | | | 90 | }; |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | window.IntersectionObserver = jest.fn().mockImplementation(mockObserveFn); |
| b69ab31 | | | 93 | }); |
| b69ab31 | | | 94 | |
| b69ab31 | | | 95 | it('shows split button on dot commit', () => { |
| b69ab31 | | | 96 | expect(screen.getByText('Split')).toBeInTheDocument(); |
| b69ab31 | | | 97 | }); |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | it('show split modal with spinner on click', async () => { |
| b69ab31 | | | 100 | fireEvent.click(screen.getByText('Split')); |
| b69ab31 | | | 101 | await waitFor(() => expect(screen.getByTestId('edit-stack-loading')).toBeInTheDocument()); |
| b69ab31 | | | 102 | }); |
| b69ab31 | | | 103 | |
| b69ab31 | | | 104 | it('requests debugexportstack data', async () => { |
| b69ab31 | | | 105 | fireEvent.click(screen.getByText('Split')); |
| b69ab31 | | | 106 | |
| b69ab31 | | | 107 | await waitFor(() => expectMessageSentToServer({type: 'exportStack', revs: 'e'})); |
| b69ab31 | | | 108 | }); |
| b69ab31 | | | 109 | |
| b69ab31 | | | 110 | it('shows errors', async () => { |
| b69ab31 | | | 111 | fireEvent.click(screen.getByText('Split')); |
| b69ab31 | | | 112 | await waitFor(() => expectMessageSentToServer({type: 'exportStack', revs: 'e'})); |
| b69ab31 | | | 113 | act(() => { |
| b69ab31 | | | 114 | simulateMessageFromServer({ |
| b69ab31 | | | 115 | type: 'exportedStack', |
| b69ab31 | | | 116 | revs: 'e', |
| b69ab31 | | | 117 | assumeTracked: [], |
| b69ab31 | | | 118 | error: 'test error', |
| b69ab31 | | | 119 | stack: [], |
| b69ab31 | | | 120 | }); |
| b69ab31 | | | 121 | }); |
| b69ab31 | | | 122 | await waitFor(() => expect(screen.getByText('test error')).toBeInTheDocument()); |
| b69ab31 | | | 123 | }); |
| b69ab31 | | | 124 | |
| b69ab31 | | | 125 | it('waits for existing commands to finish running before loading stack', async () => { |
| b69ab31 | | | 126 | fireEvent.click(screen.getByText('Pull', {selector: 'button'})); |
| b69ab31 | | | 127 | const message = await waitFor(() => |
| b69ab31 | | | 128 | utils.nullthrows(getLastMessageOfTypeSentToServer('runOperation')), |
| b69ab31 | | | 129 | ); |
| b69ab31 | | | 130 | const id = message.operation.id; |
| b69ab31 | | | 131 | |
| b69ab31 | | | 132 | fireEvent.click(screen.getByText('Split')); |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | expectMessageNOTSentToServer({type: 'exportStack', revs: 'e'}); |
| b69ab31 | | | 135 | |
| b69ab31 | | | 136 | act(() => |
| b69ab31 | | | 137 | simulateMessageFromServer({ |
| b69ab31 | | | 138 | type: 'operationProgress', |
| b69ab31 | | | 139 | id, |
| b69ab31 | | | 140 | kind: 'exit', |
| b69ab31 | | | 141 | exitCode: 0, |
| b69ab31 | | | 142 | timestamp: 0, |
| b69ab31 | | | 143 | }), |
| b69ab31 | | | 144 | ); |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | await waitFor(() => expectMessageSentToServer({type: 'exportStack', revs: 'e'})); |
| b69ab31 | | | 147 | }); |
| b69ab31 | | | 148 | |
| b69ab31 | | | 149 | describe('with loaded stack data', () => { |
| b69ab31 | | | 150 | beforeEach(async () => { |
| b69ab31 | | | 151 | fireEvent.click(screen.getByText('Split')); |
| b69ab31 | | | 152 | await waitFor(() => expectMessageSentToServer({type: 'exportStack', revs: 'e'})); |
| b69ab31 | | | 153 | act(() => { |
| b69ab31 | | | 154 | simulateMessageFromServer({ |
| b69ab31 | | | 155 | type: 'exportedStack', |
| b69ab31 | | | 156 | revs: 'e', |
| b69ab31 | | | 157 | assumeTracked: [], |
| b69ab31 | | | 158 | error: undefined, |
| b69ab31 | | | 159 | stack: EXPORT_STACK_DATA, |
| b69ab31 | | | 160 | }); |
| b69ab31 | | | 161 | }); |
| b69ab31 | | | 162 | await waitFor(() => |
| b69ab31 | | | 163 | expect(screen.getByTestId('interactive-split-modal')).toBeInTheDocument(), |
| b69ab31 | | | 164 | ); |
| b69ab31 | | | 165 | }); |
| b69ab31 | | | 166 | |
| b69ab31 | | | 167 | it('loads exported stack into UI', () => { |
| b69ab31 | | | 168 | expect( |
| b69ab31 | | | 169 | within(screen.getByTestId('interactive-split-modal')).getByText('myFile.js'), |
| b69ab31 | | | 170 | ).toBeInTheDocument(); |
| b69ab31 | | | 171 | |
| b69ab31 | | | 172 | expect( |
| b69ab31 | | | 173 | within(screen.getByTestId('interactive-split-modal')).getByText('hello'), |
| b69ab31 | | | 174 | ).toBeInTheDocument(); |
| b69ab31 | | | 175 | |
| b69ab31 | | | 176 | expect( |
| b69ab31 | | | 177 | within(screen.getByTestId('interactive-split-modal')).getByText('hello (changed)'), |
| b69ab31 | | | 178 | ).toBeInTheDocument(); |
| b69ab31 | | | 179 | }); |
| b69ab31 | | | 180 | |
| b69ab31 | | | 181 | it('moves lines and requests importing', async () => { |
| b69ab31 | | | 182 | jest.useFakeTimers().setSystemTime(new Date('2020-01-01')); |
| b69ab31 | | | 183 | |
| b69ab31 | | | 184 | const arrows = screen.getAllByTitle('Move this line change right'); |
| b69ab31 | | | 185 | fireEvent.click(arrows[1]); |
| b69ab31 | | | 186 | fireEvent.click(screen.getByTestId('confirm-edit-stack-button')); |
| b69ab31 | | | 187 | |
| b69ab31 | | | 188 | const message = await waitFor(() => |
| b69ab31 | | | 189 | utils.nullthrows(getLastMessageOfTypeSentToServer('runOperation')), |
| b69ab31 | | | 190 | ); |
| b69ab31 | | | 191 | const id = message.operation.id; |
| b69ab31 | | | 192 | |
| b69ab31 | | | 193 | expectMessageSentToServer({ |
| b69ab31 | | | 194 | type: 'runOperation', |
| b69ab31 | | | 195 | operation: { |
| b69ab31 | | | 196 | id, |
| b69ab31 | | | 197 | trackEventName: 'ImportStackOperation', |
| b69ab31 | | | 198 | args: ['debugimportstack'], |
| b69ab31 | | | 199 | stdin: JSON.stringify([ |
| b69ab31 | | | 200 | [ |
| b69ab31 | | | 201 | 'commit', |
| b69ab31 | | | 202 | { |
| b69ab31 | | | 203 | mark: ':r1', |
| b69ab31 | | | 204 | author: 'username', |
| b69ab31 | | | 205 | date: [1577836800, 25200], |
| b69ab31 | | | 206 | text: 'Commit E', |
| b69ab31 | | | 207 | parents: ['d'], |
| b69ab31 | | | 208 | predecessors: ['e'], |
| b69ab31 | | | 209 | files: {'myFile.js': {data: 'world!\n', flags: ''}}, |
| b69ab31 | | | 210 | }, |
| b69ab31 | | | 211 | ], |
| b69ab31 | | | 212 | [ |
| b69ab31 | | | 213 | 'commit', |
| b69ab31 | | | 214 | { |
| b69ab31 | | | 215 | mark: ':r2', |
| b69ab31 | | | 216 | author: 'username', |
| b69ab31 | | | 217 | date: [1577836800, 25200], |
| b69ab31 | | | 218 | text: 'Split of "Commit E"', |
| b69ab31 | | | 219 | parents: [':r1'], |
| b69ab31 | | | 220 | predecessors: ['e'], |
| b69ab31 | | | 221 | files: {'myFile.js': {data: 'hello (changed)\nworld!\n', flags: ''}}, |
| b69ab31 | | | 222 | }, |
| b69ab31 | | | 223 | ], |
| b69ab31 | | | 224 | ['goto', {mark: ':r2'}], |
| b69ab31 | | | 225 | ]), |
| b69ab31 | | | 226 | runner: CommandRunner.Sapling, |
| b69ab31 | | | 227 | }, |
| b69ab31 | | | 228 | }); |
| b69ab31 | | | 229 | }); |
| b69ab31 | | | 230 | }); |
| b69ab31 | | | 231 | }); |