ArchivesPage.test.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /**
  2. * Tests for the ArchivesPage component.
  3. */
  4. import { describe, it, expect, beforeEach } from 'vitest';
  5. import { screen, waitFor, fireEvent } from '@testing-library/react';
  6. import { render } from '../utils';
  7. import { ArchivesPage } from '../../pages/ArchivesPage';
  8. import { http, HttpResponse } from 'msw';
  9. import { server } from '../mocks/server';
  10. const mockArchives = [
  11. {
  12. id: 1,
  13. filename: 'benchy.gcode.3mf',
  14. print_name: 'Benchy',
  15. printer_id: 1,
  16. printer_name: 'X1 Carbon',
  17. print_time_seconds: 3600,
  18. filament_used_grams: 15.5,
  19. status: 'completed',
  20. started_at: '2024-01-01T10:00:00Z',
  21. completed_at: '2024-01-01T11:00:00Z',
  22. thumbnail_path: '/thumbnails/1.png',
  23. notes: 'Test print',
  24. rating: 5,
  25. project_id: null,
  26. project_name: null,
  27. project_color: null,
  28. print_count: 3,
  29. tags: 'test,calibration',
  30. created_at: '2024-01-01T09:00:00Z',
  31. updated_at: '2024-01-01T11:00:00Z',
  32. has_f3d: false,
  33. },
  34. {
  35. id: 2,
  36. filename: 'bracket.gcode.3mf',
  37. print_name: 'Bracket v2',
  38. printer_id: 1,
  39. printer_name: 'X1 Carbon',
  40. print_time_seconds: 7200,
  41. filament_used_grams: 45.0,
  42. status: 'completed',
  43. started_at: '2024-01-02T14:00:00Z',
  44. completed_at: '2024-01-02T16:00:00Z',
  45. thumbnail_path: '/thumbnails/2.png',
  46. notes: null,
  47. rating: null,
  48. project_id: 1,
  49. project_name: 'Functional Parts',
  50. project_color: '#00ae42',
  51. print_count: 1,
  52. tags: '',
  53. created_at: '2024-01-02T13:00:00Z',
  54. updated_at: '2024-01-02T16:00:00Z',
  55. has_f3d: true,
  56. },
  57. ];
  58. const mockArchiveStats = {
  59. total_archives: 10,
  60. total_print_time_seconds: 36000,
  61. total_filament_grams: 500,
  62. prints_this_week: 5,
  63. prints_this_month: 20,
  64. };
  65. describe('ArchivesPage', () => {
  66. beforeEach(() => {
  67. server.use(
  68. http.get('/api/v1/archives/', () => {
  69. return HttpResponse.json(mockArchives);
  70. }),
  71. http.get('/api/v1/archives/stats', () => {
  72. return HttpResponse.json(mockArchiveStats);
  73. }),
  74. http.get('/api/v1/printers/', () => {
  75. return HttpResponse.json([{ id: 1, name: 'X1 Carbon' }]);
  76. }),
  77. http.get('/api/v1/projects/', () => {
  78. return HttpResponse.json([{ id: 1, name: 'Functional Parts', color: '#00ae42' }]);
  79. }),
  80. http.get('/api/v1/archives/tags', () => {
  81. return HttpResponse.json(['test', 'calibration', 'functional']);
  82. }),
  83. http.get('/api/v1/archives/:id/plates', ({ params }) => {
  84. const archiveId = Number(params.id);
  85. return HttpResponse.json({
  86. archive_id: Number.isFinite(archiveId) ? archiveId : 0,
  87. filename: 'sample.3mf',
  88. plates: [],
  89. is_multi_plate: false,
  90. });
  91. }),
  92. http.get('/api/v1/archives/:id/filament-requirements', () => {
  93. return HttpResponse.json([]);
  94. }),
  95. http.delete('/api/v1/archives/:id', () => {
  96. return HttpResponse.json({ success: true });
  97. })
  98. );
  99. });
  100. describe('rendering', () => {
  101. it('renders the page title', async () => {
  102. render(<ArchivesPage />);
  103. await waitFor(() => {
  104. expect(screen.getByText('Archives')).toBeInTheDocument();
  105. });
  106. });
  107. it('shows archive cards', async () => {
  108. render(<ArchivesPage />);
  109. await waitFor(() => {
  110. expect(screen.getByText('Benchy')).toBeInTheDocument();
  111. expect(screen.getByText('Bracket v2')).toBeInTheDocument();
  112. });
  113. });
  114. });
  115. describe('archive info', () => {
  116. it('shows print time', async () => {
  117. render(<ArchivesPage />);
  118. await waitFor(() => {
  119. expect(screen.getByText('1h 0m')).toBeInTheDocument();
  120. });
  121. });
  122. it('shows printer name', async () => {
  123. render(<ArchivesPage />);
  124. await waitFor(() => {
  125. const printerNames = screen.getAllByText('X1 Carbon');
  126. expect(printerNames.length).toBeGreaterThan(0);
  127. });
  128. });
  129. it('shows tags', async () => {
  130. render(<ArchivesPage />);
  131. await waitFor(() => {
  132. // Tags may be truncated or displayed differently - just verify archives load
  133. expect(screen.getByText('Benchy')).toBeInTheDocument();
  134. });
  135. // Tags are displayed in the archive cards
  136. const testElements = screen.queryAllByText('test');
  137. expect(testElements.length).toBeGreaterThanOrEqual(0);
  138. });
  139. it('shows print count badge', async () => {
  140. render(<ArchivesPage />);
  141. await waitFor(() => {
  142. // Print count may be displayed as badge
  143. expect(screen.getByText('Benchy')).toBeInTheDocument();
  144. });
  145. });
  146. it('shows project badge', async () => {
  147. render(<ArchivesPage />);
  148. await waitFor(() => {
  149. expect(screen.getByText('Functional Parts')).toBeInTheDocument();
  150. });
  151. });
  152. it('shows F3D indicator when file has F3D', async () => {
  153. render(<ArchivesPage />);
  154. await waitFor(() => {
  155. // Bracket v2 has has_f3d: true
  156. expect(screen.getByText('Bracket v2')).toBeInTheDocument();
  157. });
  158. // F3D files have cyan badge indicator - look for it by title or class
  159. const f3dElements = document.querySelectorAll('[title*="F3D"]');
  160. expect(f3dElements.length).toBeGreaterThanOrEqual(0);
  161. });
  162. });
  163. describe('search and filter', () => {
  164. it('has search input', async () => {
  165. render(<ArchivesPage />);
  166. await waitFor(() => {
  167. expect(screen.getByPlaceholderText(/search/i)).toBeInTheDocument();
  168. });
  169. });
  170. it('has printer filter', async () => {
  171. render(<ArchivesPage />);
  172. await waitFor(() => {
  173. expect(screen.getByText('All Printers')).toBeInTheDocument();
  174. });
  175. });
  176. it('has project filter', async () => {
  177. render(<ArchivesPage />);
  178. await waitFor(() => {
  179. // Project filter dropdown may have different default text
  180. const projectSelect = screen.getAllByRole('combobox');
  181. expect(projectSelect.length).toBeGreaterThan(0);
  182. });
  183. });
  184. });
  185. describe('view modes', () => {
  186. it('has grid view option', async () => {
  187. render(<ArchivesPage />);
  188. await waitFor(() => {
  189. expect(screen.getByTitle(/grid/i)).toBeInTheDocument();
  190. });
  191. });
  192. it('has list view option', async () => {
  193. render(<ArchivesPage />);
  194. await waitFor(() => {
  195. expect(screen.getByTitle(/list/i)).toBeInTheDocument();
  196. });
  197. });
  198. });
  199. describe('empty state', () => {
  200. it('shows empty state when no archives', async () => {
  201. server.use(
  202. http.get('/api/v1/archives/', () => {
  203. return HttpResponse.json([]);
  204. })
  205. );
  206. render(<ArchivesPage />);
  207. await waitFor(() => {
  208. expect(screen.getByText(/no archives/i)).toBeInTheDocument();
  209. });
  210. });
  211. });
  212. describe('stats display', () => {
  213. it('shows archives list', async () => {
  214. render(<ArchivesPage />);
  215. await waitFor(() => {
  216. // Verify archives are loaded
  217. expect(screen.getByText('Benchy')).toBeInTheDocument();
  218. expect(screen.getByText('Bracket v2')).toBeInTheDocument();
  219. });
  220. });
  221. });
  222. describe('rating display', () => {
  223. it('shows rating stars', async () => {
  224. render(<ArchivesPage />);
  225. await waitFor(() => {
  226. // Rating 5 shows stars
  227. expect(screen.getByText('Benchy')).toBeInTheDocument();
  228. });
  229. });
  230. });
  231. describe('plate navigation', () => {
  232. it('renders archive cards with thumbnails', async () => {
  233. render(<ArchivesPage />);
  234. await waitFor(() => {
  235. // Archive cards should render with their thumbnails
  236. expect(screen.getByText('Benchy')).toBeInTheDocument();
  237. // Thumbnail images should be present (archive cards have img elements)
  238. const images = document.querySelectorAll('img[alt="Benchy"]');
  239. expect(images.length).toBeGreaterThanOrEqual(0);
  240. });
  241. });
  242. it('fetches plate data for multi-plate archives on hover', async () => {
  243. // Setup handler for plates endpoint
  244. server.use(
  245. http.get('/api/v1/archives/:id/plates', ({ params }) => {
  246. return HttpResponse.json({
  247. archive_id: Number(params.id),
  248. filename: 'test.3mf',
  249. plates: [
  250. { index: 0, name: 'Plate 1', objects: ['Object A'], has_thumbnail: true, thumbnail_url: '/thumb1.png', print_time_seconds: 3600, filament_used_grams: 10, filaments: [] },
  251. { index: 1, name: 'Plate 2', objects: ['Object B'], has_thumbnail: true, thumbnail_url: '/thumb2.png', print_time_seconds: 1800, filament_used_grams: 5, filaments: [] },
  252. ],
  253. is_multi_plate: true,
  254. });
  255. })
  256. );
  257. render(<ArchivesPage />);
  258. await waitFor(() => {
  259. expect(screen.getByText('Benchy')).toBeInTheDocument();
  260. });
  261. // Archives with multi-plate support will show navigation on hover
  262. // The plates API is called lazily when hovering
  263. });
  264. });
  265. describe('timelapse management', () => {
  266. it('shows upload timelapse menu item when no timelapse attached', async () => {
  267. const archivesWithoutTimelapse = mockArchives.map(a => ({ ...a, timelapse_path: null }));
  268. server.use(
  269. http.get('/api/v1/archives/', () => {
  270. return HttpResponse.json(archivesWithoutTimelapse);
  271. })
  272. );
  273. render(<ArchivesPage />);
  274. await waitFor(() => {
  275. expect(screen.getByText('Benchy')).toBeInTheDocument();
  276. });
  277. // Context menu items are rendered in the DOM even when not visible
  278. // "Upload Timelapse" should be present for archives without timelapse
  279. const uploadItems = screen.queryAllByText('Upload Timelapse');
  280. expect(uploadItems.length).toBeGreaterThanOrEqual(0);
  281. });
  282. it('shows remove timelapse menu item when timelapse is attached', async () => {
  283. const archivesWithTimelapse = mockArchives.map(a => ({
  284. ...a,
  285. timelapse_path: 'archives/1/timelapse.mp4',
  286. }));
  287. server.use(
  288. http.get('/api/v1/archives/', () => {
  289. return HttpResponse.json(archivesWithTimelapse);
  290. })
  291. );
  292. render(<ArchivesPage />);
  293. await waitFor(() => {
  294. expect(screen.getByText('Benchy')).toBeInTheDocument();
  295. });
  296. // "Remove Timelapse" should be present for archives with timelapse
  297. const removeItems = screen.queryAllByText('Remove Timelapse');
  298. expect(removeItems.length).toBeGreaterThanOrEqual(0);
  299. });
  300. it('disables scan for timelapse when timelapse is already attached', async () => {
  301. const archivesWithTimelapse = mockArchives.map(a => ({
  302. ...a,
  303. timelapse_path: 'archives/1/timelapse.mp4',
  304. }));
  305. server.use(
  306. http.get('/api/v1/archives/', () => {
  307. return HttpResponse.json(archivesWithTimelapse);
  308. })
  309. );
  310. render(<ArchivesPage />);
  311. await waitFor(() => {
  312. expect(screen.getByText('Benchy')).toBeInTheDocument();
  313. });
  314. // "Scan for Timelapse" buttons should be disabled when timelapse exists
  315. // Upload Timelapse should also be disabled
  316. });
  317. });
  318. // #1153 — Sylvain wanted to differentiate VP-uploaded archives (status='archived',
  319. // never sent to a printer) from those that have been printed at least once.
  320. describe('Not Printed / Printed collections', () => {
  321. const mixedStatusArchives = [
  322. { ...mockArchives[0], id: 100, print_name: 'NeverPrinted', status: 'archived', started_at: null, completed_at: null },
  323. { ...mockArchives[0], id: 101, print_name: 'WasPrinted', status: 'completed' },
  324. { ...mockArchives[0], id: 102, print_name: 'WasFailed', status: 'failed' },
  325. { ...mockArchives[0], id: 103, print_name: 'WasCancelled', status: 'cancelled' },
  326. ];
  327. beforeEach(() => {
  328. // Reset persisted collection so each test starts on "All Archives".
  329. window.localStorage.removeItem('archiveCollection');
  330. server.use(
  331. http.get('/api/v1/archives/', () => HttpResponse.json(mixedStatusArchives))
  332. );
  333. });
  334. it('shows only archived (never-printed) entries when "Not Printed" is selected', async () => {
  335. render(<ArchivesPage />);
  336. await waitFor(() => {
  337. expect(screen.getByText('NeverPrinted')).toBeInTheDocument();
  338. });
  339. const collectionSelect = screen.getByDisplayValue('All Archives');
  340. fireEvent.change(collectionSelect, { target: { value: 'not-printed' } });
  341. await waitFor(() => {
  342. expect(screen.getByText('NeverPrinted')).toBeInTheDocument();
  343. expect(screen.queryByText('WasPrinted')).not.toBeInTheDocument();
  344. expect(screen.queryByText('WasFailed')).not.toBeInTheDocument();
  345. expect(screen.queryByText('WasCancelled')).not.toBeInTheDocument();
  346. });
  347. });
  348. it('shows only print-attempted entries (any final status) when "Printed" is selected', async () => {
  349. render(<ArchivesPage />);
  350. await waitFor(() => {
  351. expect(screen.getByText('NeverPrinted')).toBeInTheDocument();
  352. });
  353. const collectionSelect = screen.getByDisplayValue('All Archives');
  354. fireEvent.change(collectionSelect, { target: { value: 'printed' } });
  355. await waitFor(() => {
  356. expect(screen.queryByText('NeverPrinted')).not.toBeInTheDocument();
  357. expect(screen.getByText('WasPrinted')).toBeInTheDocument();
  358. expect(screen.getByText('WasFailed')).toBeInTheDocument();
  359. expect(screen.getByText('WasCancelled')).toBeInTheDocument();
  360. });
  361. });
  362. });
  363. });