autofixSteps.spec.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {AutofixCodebaseChangeData} from 'sentry-fixture/autofixCodebaseChangeData';
  2. import {AutofixDataFixture} from 'sentry-fixture/autofixData';
  3. import {AutofixProgressItemFixture} from 'sentry-fixture/autofixProgressItem';
  4. import {AutofixStepFixture} from 'sentry-fixture/autofixStep';
  5. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  6. import {addSuccessMessage} from 'sentry/actionCreators/indicator';
  7. import {AutofixSteps} from 'sentry/components/events/autofix/autofixSteps';
  8. import {
  9. AutofixStatus,
  10. type AutofixStep,
  11. AutofixStepType,
  12. } from 'sentry/components/events/autofix/types';
  13. jest.mock('sentry/actionCreators/indicator');
  14. describe('AutofixSteps', () => {
  15. beforeEach(() => {
  16. (addSuccessMessage as jest.Mock).mockClear();
  17. MockApiClient.clearMockResponses();
  18. jest.clearAllMocks();
  19. });
  20. const defaultProps = {
  21. data: AutofixDataFixture({
  22. steps: [
  23. AutofixStepFixture({
  24. id: '1',
  25. type: AutofixStepType.DEFAULT,
  26. status: AutofixStatus.COMPLETED,
  27. insights: [],
  28. progress: [],
  29. }),
  30. AutofixStepFixture({
  31. id: '2',
  32. type: AutofixStepType.ROOT_CAUSE_ANALYSIS,
  33. status: AutofixStatus.COMPLETED,
  34. causes: [
  35. {
  36. id: 'cause1',
  37. description: 'Root cause 1',
  38. title: 'cause 1',
  39. code_context: [],
  40. },
  41. ],
  42. selection: null,
  43. progress: [],
  44. }),
  45. ],
  46. repositories: [],
  47. created_at: '2023-01-01T00:00:00Z',
  48. run_id: '1',
  49. status: AutofixStatus.PROCESSING,
  50. }),
  51. groupId: 'group1',
  52. runId: 'run1',
  53. onRetry: jest.fn(),
  54. };
  55. it('renders steps correctly', () => {
  56. render(<AutofixSteps {...defaultProps} />);
  57. expect(screen.getByText('Root cause 1')).toBeInTheDocument();
  58. expect(screen.getByText('Use suggested root cause')).toBeInTheDocument();
  59. });
  60. it('handles root cause selection', async () => {
  61. MockApiClient.addMockResponse({
  62. url: '/issues/group1/autofix/update/',
  63. method: 'POST',
  64. body: {},
  65. });
  66. render(<AutofixSteps {...defaultProps} />);
  67. await userEvent.click(screen.getByRole('button', {name: 'Use suggested root cause'}));
  68. const input = screen.getByPlaceholderText(
  69. '(Optional) Provide any instructions for the fix...'
  70. );
  71. await userEvent.type(input, 'Custom root cause');
  72. await userEvent.click(screen.getByRole('button', {name: 'Find a Fix'}));
  73. await waitFor(() => {
  74. expect(addSuccessMessage).toHaveBeenCalledWith(
  75. "Great, let's move forward with this root cause."
  76. );
  77. });
  78. });
  79. it('selects default root cause when input is empty', async () => {
  80. MockApiClient.addMockResponse({
  81. url: '/issues/group1/autofix/update/',
  82. method: 'POST',
  83. body: {},
  84. });
  85. render(<AutofixSteps {...defaultProps} />);
  86. await userEvent.click(screen.getByRole('button', {name: 'Use suggested root cause'}));
  87. await userEvent.click(screen.getByRole('button', {name: 'Find a Fix'}));
  88. await waitFor(() => {
  89. expect(addSuccessMessage).toHaveBeenCalledWith(
  90. "Great, let's move forward with this root cause."
  91. );
  92. });
  93. });
  94. it('renders AutofixMessageBox with correct props', async () => {
  95. render(<AutofixSteps {...defaultProps} />);
  96. await userEvent.click(screen.getByRole('button', {name: 'Use suggested root cause'}));
  97. const messageBox = screen.getByPlaceholderText(
  98. '(Optional) Provide any instructions for the fix...'
  99. );
  100. expect(messageBox).toBeInTheDocument();
  101. const sendButton = screen.getByRole('button', {name: 'Find a Fix'});
  102. expect(sendButton).toBeInTheDocument();
  103. expect(sendButton).toBeEnabled();
  104. });
  105. it('updates message box based on last step', () => {
  106. const propsWithProgress = {
  107. ...defaultProps,
  108. data: {
  109. ...defaultProps.data,
  110. steps: [
  111. ...(defaultProps.data.steps as AutofixStep[]),
  112. AutofixStepFixture({
  113. id: '3',
  114. type: AutofixStepType.DEFAULT,
  115. status: AutofixStatus.PROCESSING,
  116. progress: [
  117. AutofixProgressItemFixture({
  118. message: 'Log message',
  119. timestamp: '2023-01-01T00:00:00Z',
  120. }),
  121. ],
  122. insights: [],
  123. }),
  124. ],
  125. },
  126. };
  127. render(<AutofixSteps {...propsWithProgress} />);
  128. expect(screen.getByText('Log message')).toBeInTheDocument();
  129. });
  130. it('handles iterating on changes step', async () => {
  131. MockApiClient.addMockResponse({
  132. url: '/issues/group1/autofix/setup/?check_write_access=true',
  133. method: 'GET',
  134. body: {
  135. genAIConsent: {ok: true},
  136. integration: {ok: true},
  137. githubWriteIntegration: {
  138. repos: [],
  139. },
  140. },
  141. });
  142. MockApiClient.addMockResponse({
  143. url: '/issues/group1/autofix/update/',
  144. method: 'POST',
  145. body: {},
  146. });
  147. const changeData = AutofixCodebaseChangeData();
  148. changeData.pull_request = undefined;
  149. const propsWithChanges = {
  150. ...defaultProps,
  151. data: {
  152. ...defaultProps.data,
  153. steps: [
  154. AutofixStepFixture({
  155. id: '1',
  156. type: AutofixStepType.DEFAULT,
  157. status: AutofixStatus.COMPLETED,
  158. insights: [],
  159. progress: [],
  160. index: 0,
  161. }),
  162. AutofixStepFixture({
  163. id: '2',
  164. type: AutofixStepType.CHANGES,
  165. status: AutofixStatus.COMPLETED,
  166. progress: [],
  167. changes: [changeData],
  168. }),
  169. ],
  170. },
  171. };
  172. render(<AutofixSteps {...propsWithChanges} />);
  173. await userEvent.click(screen.getByRole('button', {name: 'Iterate'}));
  174. const input = screen.getByPlaceholderText('Share helpful context or directions...');
  175. await userEvent.type(input, 'Feedback on changes');
  176. await userEvent.click(screen.getByRole('button', {name: 'Send'}));
  177. await waitFor(() => {
  178. expect(addSuccessMessage).toHaveBeenCalledWith('Thanks, rethinking this...');
  179. });
  180. });
  181. });