FileManagerHighlight3336.test.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * /files?highlight=<id> (#3336): the queue card's "View in File Manager" link.
  3. * The link carries only the file id; the page looks up the file's folder,
  4. * opens it and marks the file. It used to point at /library, which is no route.
  5. */
  6. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
  7. import { fireEvent, screen, waitFor } from '@testing-library/react';
  8. import { useNavigate } from 'react-router-dom';
  9. import { http, HttpResponse } from 'msw';
  10. import { render } from '../utils';
  11. import { FileManagerPage } from '../../pages/FileManagerPage';
  12. import { server } from '../mocks/server';
  13. function libraryFile(id: number, filename: string, folderId: number | null) {
  14. return {
  15. id,
  16. filename,
  17. file_type: '3mf',
  18. file_path: `/library/${filename}`,
  19. file_size: 4096,
  20. folder_id: folderId,
  21. thumbnail_path: null,
  22. print_name: null,
  23. print_time_seconds: null,
  24. print_count: 0,
  25. duplicate_count: 0,
  26. created_at: '2026-01-01T00:00:00Z',
  27. };
  28. }
  29. const rootFile = libraryFile(1, 'at-the-root.3mf', null);
  30. const target = libraryFile(5, 'the-queued-one.3mf', 3);
  31. const sibling = libraryFile(6, 'its-neighbour.3mf', 3);
  32. let listedFolders: (string | null)[] = [];
  33. // Folder 3's listing without the file, as cached before it was moved there.
  34. let folder3BeforeTheMove = false;
  35. /** Follows a link inside the running app, as the queue page's does. */
  36. function GoTo({ to }: { to: string }) {
  37. const navigate = useNavigate();
  38. return <button onClick={() => navigate(to)}>go</button>;
  39. }
  40. function setViewMode(mode: 'grid' | 'list' | 'columns') {
  41. (localStorage.getItem as ReturnType<typeof vi.fn>).mockImplementation((key: string) =>
  42. key === 'library-view-mode' ? mode : null,
  43. );
  44. }
  45. function card(name: string): HTMLElement {
  46. return screen.getByText(name).closest('[data-file-id]') as HTMLElement;
  47. }
  48. describe('FileManagerPage ?highlight=', () => {
  49. beforeEach(() => {
  50. listedFolders = [];
  51. folder3BeforeTheMove = false;
  52. setViewMode('grid');
  53. server.use(
  54. http.get('/api/v1/library/folders', () =>
  55. HttpResponse.json([
  56. { id: 3, name: 'Brackets', parent_id: null, project_id: null, is_external: false, file_count: 2, children: [] },
  57. { id: 4, name: 'Elsewhere', parent_id: null, project_id: null, is_external: false, file_count: 0, children: [] },
  58. ]),
  59. ),
  60. http.get('/api/v1/library/files', async ({ request }) => {
  61. const folderId = new URL(request.url).searchParams.get('folder_id');
  62. listedFolders.push(folderId);
  63. if (folderId === '4') return HttpResponse.json([]);
  64. if (folderId === '3' && folder3BeforeTheMove) return HttpResponse.json([sibling]);
  65. // Slower than the page's own timers, so nothing can be decided on a list
  66. // that is still on its way.
  67. if (folderId === '3') {
  68. await new Promise((resolve) => setTimeout(resolve, 300));
  69. return HttpResponse.json([target, sibling]);
  70. }
  71. return HttpResponse.json([rootFile]);
  72. }),
  73. http.get('/api/v1/library/files/5', () => HttpResponse.json(target)),
  74. // Readable on its own but not in its folder's listing.
  75. http.get('/api/v1/library/files/7', () => HttpResponse.json(libraryFile(7, 'unlisted.3mf', 3))),
  76. http.get('/api/v1/library/files/404', () => HttpResponse.json({ detail: 'File not found' }, { status: 404 })),
  77. http.get('/api/v1/library/stats', () =>
  78. HttpResponse.json({
  79. total_files: 3,
  80. total_folders: 1,
  81. total_size_bytes: 1024,
  82. disk_free_bytes: 1024 * 1024,
  83. disk_total_bytes: 2048 * 1024,
  84. }),
  85. ),
  86. http.get('/api/v1/settings/', () => HttpResponse.json({ check_updates: false })),
  87. http.get('/api/v1/projects/', () => HttpResponse.json([])),
  88. http.get('/api/v1/archives/', () => HttpResponse.json([])),
  89. );
  90. });
  91. afterEach(() => {
  92. window.history.pushState({}, '', '/');
  93. });
  94. it("opens the file's folder and marks the file", async () => {
  95. window.history.pushState({}, '', '/files?highlight=5');
  96. render(<FileManagerPage />);
  97. await screen.findByText('the-queued-one.3mf');
  98. expect(listedFolders).toContain('3');
  99. await waitFor(() => expect(card('the-queued-one.3mf').style.outline).toContain('solid'));
  100. expect(card('its-neighbour.3mf').style.outline).toBe('');
  101. });
  102. it('marks the row in the list view', async () => {
  103. setViewMode('list');
  104. window.history.pushState({}, '', '/files?highlight=5');
  105. render(<FileManagerPage />);
  106. await screen.findByText('the-queued-one.3mf');
  107. await waitFor(() => expect(card('the-queued-one.3mf').style.outline).toContain('solid'));
  108. });
  109. it('focuses the file in the columns view', async () => {
  110. setViewMode('columns');
  111. window.history.pushState({}, '', '/files?highlight=5');
  112. render(<FileManagerPage />);
  113. await waitFor(() => expect(screen.getAllByText('the-queued-one.3mf').length).toBeGreaterThan(0));
  114. await waitFor(() => {
  115. const row = screen.getAllByText('the-queued-one.3mf')[0].closest('[data-file-id]') as HTMLElement;
  116. expect(row.style.outline).toContain('solid');
  117. expect(row.className).toContain('ring-1');
  118. });
  119. });
  120. it('warns when the file is gone or not readable', async () => {
  121. window.history.pushState({}, '', '/files?highlight=404');
  122. render(<FileManagerPage />);
  123. expect(await screen.findByText(/File not found - it may have been deleted/)).toBeInTheDocument();
  124. expect(await screen.findByText('at-the-root.3mf')).toBeInTheDocument();
  125. });
  126. it("warns when the file is not in its folder's listing", async () => {
  127. window.history.pushState({}, '', '/files?highlight=7');
  128. render(<FileManagerPage />);
  129. expect(await screen.findByText(/File not found - it may have been deleted/)).toBeInTheDocument();
  130. expect(listedFolders).toContain('3');
  131. });
  132. it('looks for the file in a fresh listing, not one cached before it was moved in', async () => {
  133. folder3BeforeTheMove = true;
  134. window.history.pushState({}, '', '/files?folder=3');
  135. render(<><FileManagerPage /><GoTo to="/files?highlight=5" /></>);
  136. await screen.findByText('its-neighbour.3mf');
  137. folder3BeforeTheMove = false;
  138. fireEvent.click(screen.getByText('go'));
  139. await waitFor(() => expect(card('the-queued-one.3mf').style.outline).toContain('solid'));
  140. expect(screen.queryByText(/File not found - it may have been deleted/)).not.toBeInTheDocument();
  141. });
  142. it('does not pull someone back who opened another folder while the file was looked up', async () => {
  143. let answer: () => void = () => {};
  144. server.use(
  145. http.get('/api/v1/library/files/5', async () => {
  146. await new Promise<void>((resolve) => { answer = resolve; });
  147. return HttpResponse.json(target);
  148. }),
  149. );
  150. window.history.pushState({}, '', '/files?highlight=5');
  151. render(<FileManagerPage />);
  152. await screen.findByText('at-the-root.3mf');
  153. fireEvent.click(screen.getAllByText('Elsewhere')[0]);
  154. await waitFor(() => expect(listedFolders).toContain('4'));
  155. answer();
  156. await new Promise((resolve) => setTimeout(resolve, 300));
  157. expect(listedFolders).not.toContain('3');
  158. expect(screen.queryByText('the-queued-one.3mf')).not.toBeInTheDocument();
  159. });
  160. it('marks nothing without the parameter', async () => {
  161. window.history.pushState({}, '', '/files');
  162. render(<FileManagerPage />);
  163. await screen.findByText('at-the-root.3mf');
  164. expect(card('at-the-root.3mf').style.outline).toBe('');
  165. expect(listedFolders).not.toContain('3');
  166. });
  167. });