|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 3 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 4 | +import useFileQuery from '../../queries/fileQueries'; |
| 5 | +import * as utils from '@/utils'; |
| 6 | + |
| 7 | +// Mock the utils module |
| 8 | +vi.mock('@/utils', async () => { |
| 9 | + const actual = await vi.importActual('@/utils'); |
| 10 | + return { |
| 11 | + ...actual, |
| 12 | + sendFetchRequest: vi.fn(), |
| 13 | + buildUrl: (path: string) => path, // Simple mock for buildUrl |
| 14 | + makeMapKey: (type: string, name: string) => `${type}_${name}` |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +// Mock the context |
| 19 | +const mockZonesAndFspData = { |
| 20 | + 'fsp_test-fsp': { |
| 21 | + name: 'test-fsp', |
| 22 | + path: '/test/path', |
| 23 | + type: 'posix' |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +vi.mock('@/contexts/ZonesAndFspMapContext', () => ({ |
| 28 | + useZoneAndFspMapContext: () => ({ |
| 29 | + zonesAndFspQuery: { |
| 30 | + data: mockZonesAndFspData |
| 31 | + } |
| 32 | + }) |
| 33 | +})); |
| 34 | + |
| 35 | +describe('useFileQuery 500 Error Reproduction', () => { |
| 36 | + let queryClient: QueryClient; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + queryClient = new QueryClient({ |
| 40 | + defaultOptions: { |
| 41 | + queries: { |
| 42 | + retry: false // Disable retries for faster tests |
| 43 | + } |
| 44 | + } |
| 45 | + }); |
| 46 | + vi.clearAllMocks(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should handle 500 Internal Server Error correctly', async () => { |
| 50 | + // Arrange |
| 51 | + const fspName = 'test-fsp'; |
| 52 | + const folderName = ''; |
| 53 | + |
| 54 | + // Mock sendFetchRequest to return a 500 response with HTML body |
| 55 | + // and a json() method that fails (as it tries to parse HTML) |
| 56 | + const mockResponse = { |
| 57 | + ok: false, |
| 58 | + status: 500, |
| 59 | + statusText: 'Internal Server Error', |
| 60 | + json: async () => { |
| 61 | + throw new SyntaxError('Unexpected token < in JSON at position 0'); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + vi.mocked(utils.sendFetchRequest).mockResolvedValue( |
| 66 | + mockResponse as unknown as Response |
| 67 | + ); |
| 68 | + |
| 69 | + // Act |
| 70 | + const { result } = renderHook(() => useFileQuery(fspName, folderName), { |
| 71 | + wrapper: ({ children }) => ( |
| 72 | + <QueryClientProvider client={queryClient}> |
| 73 | + {children} |
| 74 | + </QueryClientProvider> |
| 75 | + ) |
| 76 | + }); |
| 77 | + |
| 78 | + // Assert |
| 79 | + // We expect the query to eventually fail |
| 80 | + await waitFor(() => expect(result.current.isError).toBe(true), { |
| 81 | + timeout: 10000 |
| 82 | + }); |
| 83 | + |
| 84 | + // The bug is that it throws the SyntaxError from JSON parsing |
| 85 | + // instead of the "Server returned 500" error. |
| 86 | + // So this assertion confirms the BUG (on this branch) |
| 87 | + // or confirms the FIX (if the message is clean). |
| 88 | + |
| 89 | + // Since we want to REPRODUCE the failure, we check what it actually is. |
| 90 | + // If the bug is present, the error message will be the SyntaxError. |
| 91 | + // If the fix is present, it will be "Server returned 500 Internal Server Error". |
| 92 | + |
| 93 | + const error = result.current.error; |
| 94 | + console.log('Test caught error:', error?.message); |
| 95 | + |
| 96 | + // We want to verify that the code *should* be handling this, |
| 97 | + // so we Assert what we WANT (the fix). |
| 98 | + // This test definition asserts the correct behavior. |
| 99 | + // Running it on the buggy branch should FAIL this assertion. |
| 100 | + expect(error?.message).toBe('Server returned 500 Internal Server Error'); |
| 101 | + expect(error?.message).not.toContain('Unexpected token'); |
| 102 | + }); |
| 103 | +}, 15000); |
0 commit comments