BugReportBubble.test.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * Tests for the BugReportBubble component.
  3. */
  4. import { describe, it, expect } from 'vitest';
  5. import { render, screen, waitFor } from '../utils';
  6. import userEvent from '@testing-library/user-event';
  7. import { http, HttpResponse } from 'msw';
  8. import { server } from '../mocks/server';
  9. import { BugReportBubble } from '../../components/BugReportBubble';
  10. function getDescriptionTextarea() {
  11. return document.querySelector('textarea') as HTMLTextAreaElement;
  12. }
  13. function getSubmitButton() {
  14. const buttons = screen.getAllByRole('button');
  15. return buttons.find(
  16. (b) =>
  17. b.className.includes('bg-red-500') &&
  18. !b.className.includes('rounded-full') &&
  19. b.textContent !== ''
  20. );
  21. }
  22. function setupLoggingEndpoints() {
  23. server.use(
  24. http.post('*/bug-report/start-logging', () => {
  25. return HttpResponse.json({ started: true, was_debug: false });
  26. }),
  27. http.post('*/bug-report/stop-logging', () => {
  28. return HttpResponse.json({ logs: 'test debug logs' });
  29. })
  30. );
  31. }
  32. /** Mocks the printer list and per-printer diagnostic the form scans on open. */
  33. function setupDiagnosticEndpoints(
  34. printers: { id: number; name: string }[],
  35. results: Record<number, 'ok' | 'problems'>
  36. ) {
  37. server.use(
  38. http.get('*/printers/', () =>
  39. HttpResponse.json(
  40. printers.map((p) => ({
  41. id: p.id,
  42. name: p.name,
  43. serial_number: '00M09A000000000',
  44. ip_address: `192.168.1.${20 + p.id}`,
  45. is_active: true,
  46. model: 'X1C',
  47. nozzle_count: 1,
  48. }))
  49. )
  50. ),
  51. http.get('*/printers/:id/diagnostic', ({ params }) => {
  52. const overall = results[Number(params.id)] ?? 'ok';
  53. return HttpResponse.json({
  54. printer_id: Number(params.id),
  55. ip_address: `192.168.1.${20 + Number(params.id)}`,
  56. overall,
  57. checks: [{ id: 'port_mqtt', status: overall === 'problems' ? 'fail' : 'pass', params: {} }],
  58. });
  59. })
  60. );
  61. }
  62. describe('BugReportBubble', () => {
  63. it('renders the floating bug button', () => {
  64. render(<BugReportBubble />);
  65. const button = screen.getByRole('button');
  66. expect(button).toBeInTheDocument();
  67. });
  68. it('opens panel when bubble is clicked', async () => {
  69. const user = userEvent.setup();
  70. render(<BugReportBubble />);
  71. await user.click(screen.getByRole('button'));
  72. expect(getDescriptionTextarea()).toBeInTheDocument();
  73. });
  74. it('closes panel when X button is clicked', async () => {
  75. const user = userEvent.setup();
  76. render(<BugReportBubble />);
  77. // Open
  78. await user.click(screen.getByRole('button'));
  79. expect(getDescriptionTextarea()).toBeInTheDocument();
  80. // Close via the X button
  81. const buttons = screen.getAllByRole('button');
  82. const closeButton = buttons.find((b) => b.querySelector('.lucide-x'));
  83. if (closeButton) await user.click(closeButton);
  84. await waitFor(() => {
  85. expect(document.querySelector('textarea')).not.toBeInTheDocument();
  86. });
  87. });
  88. it('disables submit when description is empty', async () => {
  89. const user = userEvent.setup();
  90. render(<BugReportBubble />);
  91. await user.click(screen.getByRole('button'));
  92. expect(getSubmitButton()).toBeDisabled();
  93. });
  94. it('enables submit when description is provided', async () => {
  95. const user = userEvent.setup();
  96. render(<BugReportBubble />);
  97. await user.click(screen.getByRole('button'));
  98. await user.type(getDescriptionTextarea(), 'Something is broken');
  99. expect(getSubmitButton()).not.toBeDisabled();
  100. });
  101. it('shows logging state with step indicators after start', async () => {
  102. const user = userEvent.setup();
  103. setupLoggingEndpoints();
  104. render(<BugReportBubble />);
  105. await user.click(screen.getByRole('button'));
  106. await user.type(getDescriptionTextarea(), 'Test bug report');
  107. const submitBtn = getSubmitButton();
  108. if (submitBtn) await user.click(submitBtn);
  109. // Should show step indicators and elapsed timer
  110. await waitFor(() => {
  111. const reproduceText = screen.queryByText(/reproduce|Reproduce|reproduzieren|reproduire|riproduci|再現|reproduza|重现/i);
  112. expect(reproduceText).toBeInTheDocument();
  113. });
  114. // Should show elapsed timer (00:00 format)
  115. await waitFor(() => {
  116. const timer = screen.queryByText(/00:0/);
  117. expect(timer).toBeInTheDocument();
  118. });
  119. });
  120. it('shows success state after successful submission', async () => {
  121. const user = userEvent.setup();
  122. setupLoggingEndpoints();
  123. server.use(
  124. http.post('*/bug-report/submit', () => {
  125. return HttpResponse.json({
  126. success: true,
  127. message: 'Bug report submitted successfully!',
  128. issue_url: 'https://github.com/maziggy/bambuddy/issues/42',
  129. issue_number: 42,
  130. });
  131. })
  132. );
  133. render(<BugReportBubble />);
  134. await user.click(screen.getByRole('button'));
  135. await user.type(getDescriptionTextarea(), 'Test bug');
  136. const submitBtn = getSubmitButton();
  137. if (submitBtn) await user.click(submitBtn);
  138. // Wait for logging state, then click stop
  139. await waitFor(() => {
  140. expect(screen.queryByText(/reproduce|Reproduce|reproduzieren|reproduire|riproduci|再現|reproduza|重现/i)).toBeInTheDocument();
  141. });
  142. // Find and click the Stop & Submit button
  143. const stopBtn = screen.getAllByRole('button').find(
  144. (b) => b.className.includes('bg-red-500') && !b.className.includes('rounded-full')
  145. );
  146. if (stopBtn) await user.click(stopBtn);
  147. await waitFor(
  148. () => {
  149. expect(screen.getByText(/#42/)).toBeInTheDocument();
  150. },
  151. { timeout: 10000 }
  152. );
  153. });
  154. it('shows error state after failed submission', async () => {
  155. const user = userEvent.setup();
  156. setupLoggingEndpoints();
  157. server.use(
  158. http.post('*/bug-report/submit', () => {
  159. return HttpResponse.json({
  160. success: false,
  161. message: 'Relay not available',
  162. issue_url: null,
  163. issue_number: null,
  164. });
  165. })
  166. );
  167. render(<BugReportBubble />);
  168. await user.click(screen.getByRole('button'));
  169. await user.type(getDescriptionTextarea(), 'Test bug');
  170. const submitBtn = getSubmitButton();
  171. if (submitBtn) await user.click(submitBtn);
  172. // Wait for logging state, then click stop
  173. await waitFor(() => {
  174. expect(screen.queryByText(/reproduce|Reproduce|reproduzieren|reproduire|riproduci|再現|reproduza|重现/i)).toBeInTheDocument();
  175. });
  176. const stopBtn = screen.getAllByRole('button').find(
  177. (b) => b.className.includes('bg-red-500') && !b.className.includes('rounded-full')
  178. );
  179. if (stopBtn) await user.click(stopBtn);
  180. await waitFor(
  181. () => {
  182. expect(screen.getByText(/Relay not available/)).toBeInTheDocument();
  183. },
  184. { timeout: 10000 }
  185. );
  186. });
  187. it('has expandable data collection notice', async () => {
  188. const user = userEvent.setup();
  189. render(<BugReportBubble />);
  190. await user.click(screen.getByRole('button'));
  191. const details = document.querySelector('details');
  192. expect(details).toBeInTheDocument();
  193. });
  194. it('lists affected printers as collapsed rows, not stacked checklists', async () => {
  195. const user = userEvent.setup();
  196. setupDiagnosticEndpoints(
  197. [
  198. { id: 1, name: 'Printer Alpha' },
  199. { id: 2, name: 'Printer Beta' },
  200. { id: 3, name: 'Printer Gamma' },
  201. ],
  202. { 1: 'problems', 2: 'problems', 3: 'ok' }
  203. );
  204. render(<BugReportBubble />);
  205. await user.click(screen.getByRole('button'));
  206. // Summary counts problem printers against all scanned printers.
  207. expect(
  208. await screen.findByText('2 of 3 printers have connection issues')
  209. ).toBeInTheDocument();
  210. // Affected printers are listed by name; the healthy one is not.
  211. expect(screen.getByText('Printer Alpha')).toBeInTheDocument();
  212. expect(screen.getByText('Printer Beta')).toBeInTheDocument();
  213. expect(screen.queryByText('Printer Gamma')).not.toBeInTheDocument();
  214. // With more than one problem the per-printer checklists stay collapsed.
  215. expect(screen.queryByText(/Found problems that explain/)).not.toBeInTheDocument();
  216. // Expanding a row reveals just that printer's checklist.
  217. await user.click(screen.getByText('Printer Alpha'));
  218. expect(await screen.findByText(/Found problems that explain/)).toBeInTheDocument();
  219. });
  220. it('auto-expands the checklist when only one printer has problems', async () => {
  221. const user = userEvent.setup();
  222. setupDiagnosticEndpoints([{ id: 1, name: 'Solo Printer' }], { 1: 'problems' });
  223. render(<BugReportBubble />);
  224. await user.click(screen.getByRole('button'));
  225. expect(
  226. await screen.findByText('1 of 1 printers have connection issues')
  227. ).toBeInTheDocument();
  228. // Single problem → the checklist is expanded without a click.
  229. expect(await screen.findByText(/Found problems that explain/)).toBeInTheDocument();
  230. });
  231. it('shows the log-health panel when the scan finds known issues', async () => {
  232. const user = userEvent.setup();
  233. setupDiagnosticEndpoints([{ id: 1, name: 'Solo Printer' }], { 1: 'ok' });
  234. server.use(
  235. http.get('*/system/health', () =>
  236. HttpResponse.json({
  237. findings: [
  238. {
  239. signature_id: 'ftp-auth-rejected',
  240. severity: 'error',
  241. category: 'layer8',
  242. wiki_anchor: 'wrong-access-code',
  243. count: 3,
  244. first_seen: '2026-05-22 09:00:00,000',
  245. last_seen: '2026-05-22 10:00:00,000',
  246. sample: 'FTP connection permission error to [IP]',
  247. },
  248. ],
  249. scanned_entries: 500,
  250. log_available: true,
  251. summary: { total: 1, layer8: 1, environment: 0, bug: 0 },
  252. })
  253. )
  254. );
  255. render(<BugReportBubble />);
  256. await user.click(screen.getByRole('button'));
  257. expect(await screen.findByText('Known issues found in your logs')).toBeInTheDocument();
  258. expect(screen.getByText('Printer rejected the access code')).toBeInTheDocument();
  259. });
  260. });