Files
flyer-crawler.projectium.com/src/features/flyer/FlyerUploader.test.tsx
Torben Sorensen b392b82c25
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 15m20s
fix unit tests
2025-12-30 00:09:57 -08:00

518 lines
23 KiB
TypeScript

// src/features/flyer/FlyerUploader.test.tsx
import React from 'react';
import { render, screen, fireEvent, waitFor, act } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach, afterEach, type Mock } from 'vitest';
import { FlyerUploader } from './FlyerUploader';
import * as aiApiClientModule from '../../services/aiApiClient';
import * as checksumModule from '../../utils/checksum';
import { useNavigate, MemoryRouter } from 'react-router-dom';
import { QueryClient, QueryClientProvider, onlineManager } from '@tanstack/react-query';
// Mock dependencies
vi.mock('../../services/aiApiClient', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../services/aiApiClient')>();
return {
...actual,
uploadAndProcessFlyer: vi.fn(),
getJobStatus: vi.fn(),
};
});
vi.mock('../../services/logger.client', () => ({
// Keep the original logger.info/error but also spy on it for test assertions if needed
logger: {
info: vi.fn((...args) => console.log('[LOGGER.INFO]', ...args)),
error: vi.fn((...args) => console.error('[LOGGER.ERROR]', ...args)),
warn: vi.fn((...args) => console.warn('[LOGGER.WARN]', ...args)),
debug: vi.fn((...args) => console.debug('[LOGGER.DEBUG]', ...args)),
},
}));
vi.mock('../../utils/checksum', () => ({
generateFileChecksum: vi.fn(),
}));
// Mock react-router-dom
vi.mock('react-router-dom', async () => {
const actual = await vi.importActual<typeof import('react-router-dom')>('react-router-dom');
return {
...actual,
useNavigate: vi.fn(),
};
});
const mockedAiApiClient = aiApiClientModule as unknown as {
uploadAndProcessFlyer: Mock;
getJobStatus: Mock;
};
const mockedChecksumModule = checksumModule as unknown as {
generateFileChecksum: Mock;
};
const renderComponent = (onProcessingComplete = vi.fn()) => {
console.log('--- [TEST LOG] ---: Rendering component inside MemoryRouter.');
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
});
return render(
<QueryClientProvider client={queryClient}>
<MemoryRouter>
<FlyerUploader onProcessingComplete={onProcessingComplete} />
</MemoryRouter>
</QueryClientProvider>,
);
};
describe('FlyerUploader', () => {
const navigateSpy = vi.fn();
beforeEach(() => {
// Disable react-query's online manager to prevent it from interfering with fake timers
onlineManager.setEventListener((setOnline) => {
return () => {};
});
console.log(`\n--- [TEST LOG] ---: Starting test: "${expect.getState().currentTestName}"`);
vi.resetAllMocks(); // Resets mock implementations AND call history.
console.log('--- [TEST LOG] ---: Mocks reset.');
mockedChecksumModule.generateFileChecksum.mockResolvedValue('mock-checksum');
(useNavigate as Mock).mockReturnValue(navigateSpy);
});
afterEach(() => {
console.log(`--- [TEST LOG] ---: Finished test: "${expect.getState().currentTestName}"\n`);
});
it('should render the initial state correctly', () => {
renderComponent();
expect(screen.getByText('Upload New Flyer')).toBeInTheDocument();
expect(screen.getByText(/click to select a file/i)).toBeInTheDocument();
});
it('should handle file upload and start polling', async () => {
console.log('--- [TEST LOG] ---: 1. Setting up mocks for upload and polling.');
mockedAiApiClient.uploadAndProcessFlyer.mockResolvedValue({ jobId: 'job-123' });
mockedAiApiClient.getJobStatus.mockResolvedValue({
state: 'active',
progress: { message: 'Checking...' },
});
console.log('--- [TEST LOG] ---: 2. Rendering component and preparing file.');
renderComponent();
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
const input = screen.getByLabelText(/click to select a file/i);
console.log('--- [TEST LOG] ---: 3. Firing file change event.');
fireEvent.change(input, { target: { files: [file] } });
console.log('--- [TEST LOG] ---: 4. File change event fired. Now AWAITING UI update.');
try {
console.log('--- [TEST LOG] ---: 5a. Awaiting screen.findByText("Checking...")');
await screen.findByText('Checking...');
console.log('--- [TEST LOG] ---: 5b. SUCCESS: UI updated to polling state.');
} catch (error) {
console.error('--- [TEST LOG] ---: 5c. ERROR: findByText("Checking...") timed out.');
console.log('--- [DEBUG] ---: DOM at time of failure:');
screen.debug(); // Print the DOM when the error occurs
throw error; // Re-throw the error to fail the test
}
console.log('--- [TEST LOG] ---: 6. Asserting mock calls after UI update.');
expect(mockedAiApiClient.uploadAndProcessFlyer).toHaveBeenCalledWith(file, 'mock-checksum');
expect(mockedAiApiClient.getJobStatus).toHaveBeenCalledTimes(1);
console.log('--- [TEST LOG] ---: 7. Mocks verified. Advancing timers now...');
// With real timers, we now wait for the polling interval to elapse.
console.log(
`--- [TEST LOG] ---: 9. Act block finished. Now checking if getJobStatus was called again.`,
);
try {
// The polling interval is 3s, so we wait for a bit longer.
await waitFor(() => {
const calls = mockedAiApiClient.getJobStatus.mock.calls.length;
console.log(`--- [TEST LOG] ---: 10. waitFor check: getJobStatus calls = ${calls}`);
expect(mockedAiApiClient.getJobStatus).toHaveBeenCalledTimes(2);
}, { timeout: 4000 });
console.log('--- [TEST LOG] ---: 11. SUCCESS: Second poll confirmed.');
} catch (error) {
console.error('--- [TEST LOG] ---: 11. ERROR: waitFor for second poll timed out.');
console.log('--- [DEBUG] ---: DOM at time of failure:');
screen.debug();
throw error;
}
});
it('should handle file upload via drag and drop', async () => {
console.log('--- [TEST LOG] ---: 1. Setting up mocks for drag and drop.');
mockedAiApiClient.uploadAndProcessFlyer.mockResolvedValue({ jobId: 'job-dnd' });
mockedAiApiClient.getJobStatus.mockResolvedValue({
state: 'active',
progress: { message: 'Dropped...' },
});
console.log('--- [TEST LOG] ---: 2. Rendering component and preparing file for drop.');
renderComponent();
const file = new File(['dnd-content'], 'dnd-flyer.pdf', { type: 'application/pdf' });
// The dropzone is the label element
const dropzone = screen.getByText(/click to select a file/i).closest('label')!;
console.log('--- [TEST LOG] ---: 3. Firing drop event.');
// Simulate the drop event
fireEvent.drop(dropzone, {
dataTransfer: { files: [file] },
});
console.log('--- [TEST LOG] ---: 4. Awaiting UI update to "Dropped...".');
await screen.findByText('Dropped...');
console.log('--- [TEST LOG] ---: 5. Asserting upload was called.');
expect(mockedAiApiClient.uploadAndProcessFlyer).toHaveBeenCalledWith(file, 'mock-checksum');
});
it('should poll for status, complete successfully, and redirect', async () => {
const onProcessingComplete = vi.fn();
console.log('--- [TEST LOG] ---: 1. Setting up mock sequence for polling.');
mockedAiApiClient.uploadAndProcessFlyer.mockResolvedValue({ jobId: 'job-123' });
mockedAiApiClient.getJobStatus
.mockResolvedValueOnce({ state: 'active', progress: { message: 'Analyzing...' } })
.mockResolvedValueOnce({ state: 'completed', returnValue: { flyerId: 42 } });
console.log('--- [TEST LOG] ---: 2. Rendering component and uploading file.');
renderComponent(onProcessingComplete);
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
const input = screen.getByLabelText(/click to select a file/i);
fireEvent.change(input, { target: { files: [file] } });
console.log('--- [TEST LOG] ---: 3. Fired event. Now AWAITING UI update to "Analyzing...".');
try {
await screen.findByText('Analyzing...');
console.log('--- [TEST LOG] ---: 4. SUCCESS: UI is showing "Analyzing...".');
} catch (error) {
console.error('--- [TEST LOG] ---: 4. ERROR: findByText("Analyzing...") timed out.');
console.log('--- [DEBUG] ---: DOM at time of failure:');
screen.debug();
throw error;
}
expect(mockedAiApiClient.getJobStatus).toHaveBeenCalledTimes(1);
console.log('--- [TEST LOG] ---: 5. First poll confirmed. Now AWAITING timer advancement.');
try {
console.log(
'--- [TEST LOG] ---: 8a. waitFor check: Waiting for completion text and job status count.',
);
// Wait for the second poll to occur and the UI to update.
await waitFor(() => {
console.log(
`--- [TEST LOG] ---: 8b. waitFor interval: calls=${
mockedAiApiClient.getJobStatus.mock.calls.length
}`,
);
expect(
screen.getByText('Processing complete! Redirecting to flyer 42...'),
).toBeInTheDocument();
}, { timeout: 4000 });
console.log('--- [TEST LOG] ---: 9. SUCCESS: Completion message found.');
} catch (error) {
console.error('--- [TEST LOG] ---: 9. ERROR: waitFor for completion message timed out.');
console.log('--- [DEBUG] ---: DOM at time of failure:');
screen.debug();
throw error;
}
expect(mockedAiApiClient.getJobStatus).toHaveBeenCalledTimes(2);
// Wait for the redirect timer (1.5s in component) to fire.
await act(() => new Promise((r) => setTimeout(r, 2000)));
console.log(`--- [TEST LOG] ---: 11. Timers advanced. Now asserting navigation.`);
expect(onProcessingComplete).toHaveBeenCalled();
expect(navigateSpy).toHaveBeenCalledWith('/flyers/42');
console.log('--- [TEST LOG] ---: 12. Callback and navigation confirmed.');
});
it('should handle a failed job', async () => {
console.log('--- [TEST LOG] ---: 1. Setting up mocks for a failed job.');
mockedAiApiClient.uploadAndProcessFlyer.mockResolvedValue({ jobId: 'job-fail' });
// The getJobStatus function throws a specific error when the job fails,
// which is then caught by react-query and placed in the `error` state.
const jobFailedError = new aiApiClientModule.JobFailedError('AI model exploded', 'UNKNOWN_ERROR');
mockedAiApiClient.getJobStatus.mockRejectedValue(jobFailedError);
console.log('--- [TEST LOG] ---: 2. Rendering and uploading.');
renderComponent();
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
const input = screen.getByLabelText(/click to select a file/i);
console.log('--- [TEST LOG] ---: 3. Firing file change event.');
fireEvent.change(input, { target: { files: [file] } });
console.log('--- [TEST LOG] ---: 3. File upload triggered.');
try {
console.log('--- [TEST LOG] ---: 4. AWAITING failure message...');
// The UI should now display the error from the `pollError` state, which includes the "Polling failed" prefix.
expect(await screen.findByText(/Polling failed: AI model exploded/i)).toBeInTheDocument();
console.log('--- [TEST LOG] ---: 5. SUCCESS: Failure message found.');
} catch (error) {
console.error('--- [TEST LOG] ---: 5. ERROR: findByText for failure message timed out.');
console.log('--- [DEBUG] ---: DOM at time of failure:');
screen.debug();
throw error;
}
expect(screen.getByText('Upload Another Flyer')).toBeInTheDocument();
console.log('--- [TEST LOG] ---: 6. "Upload Another" button confirmed.');
});
it('should clear the polling timeout when a job fails', async () => {
const clearTimeoutSpy = vi.spyOn(global, 'clearTimeout');
console.log('--- [TEST LOG] ---: 1. Setting up mocks for failed job timeout clearance.');
mockedAiApiClient.uploadAndProcessFlyer.mockResolvedValue({ jobId: 'job-fail-timeout' });
// We need at least one 'active' response to establish a timeout loop so we have something to clear
// The second call should be a rejection, as this is how getJobStatus signals a failure.
mockedAiApiClient.getJobStatus
.mockResolvedValueOnce({ state: 'active', progress: { message: 'Working...' } })
.mockRejectedValueOnce(new aiApiClientModule.JobFailedError('Fatal Error', 'UNKNOWN_ERROR'));
renderComponent();
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
const input = screen.getByLabelText(/click to select a file/i);
fireEvent.change(input, { target: { files: [file] } });
// Wait for the first poll to complete and UI to update to "Working..."
await screen.findByText('Working...');
// Wait for the failure UI
await waitFor(() => expect(screen.getByText(/Polling failed: Fatal Error/i)).toBeInTheDocument(), { timeout: 4000 });
// Verify clearTimeout was called
expect(clearTimeoutSpy).toHaveBeenCalled();
// Verify no further polling occurs
const callsBefore = mockedAiApiClient.getJobStatus.mock.calls.length;
// Wait for a duration longer than the polling interval
await act(() => new Promise((r) => setTimeout(r, 4000)));
expect(mockedAiApiClient.getJobStatus).toHaveBeenCalledTimes(callsBefore);
clearTimeoutSpy.mockRestore();
});
it('should clear the polling timeout when the component unmounts', async () => {
const clearTimeoutSpy = vi.spyOn(global, 'clearTimeout');
console.log('--- [TEST LOG] ---: 1. Setting up mocks for unmount timeout clearance.');
mockedAiApiClient.uploadAndProcessFlyer.mockResolvedValue({ jobId: 'job-unmount' });
mockedAiApiClient.getJobStatus.mockResolvedValue({
state: 'active',
progress: { message: 'Polling...' },
});
const { unmount } = renderComponent();
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
const input = screen.getByLabelText(/click to select a file/i);
fireEvent.change(input, { target: { files: [file] } });
// Wait for the first poll to complete and the UI to show the polling state
await screen.findByText('Polling...');
// Now that we are in a polling state (and a timeout is set), unmount the component
console.log('--- [TEST LOG] ---: 2. Unmounting component to trigger cleanup effect.');
unmount();
// Verify that the cleanup function in the useEffect hook was called
expect(clearTimeoutSpy).toHaveBeenCalled();
console.log('--- [TEST LOG] ---: 3. clearTimeout confirmed.');
clearTimeoutSpy.mockRestore();
});
it('should handle a duplicate flyer error (409)', async () => {
console.log('--- [TEST LOG] ---: 1. Setting up mock for 409 duplicate error.');
// The API client now throws a structured error for non-2xx responses.
mockedAiApiClient.uploadAndProcessFlyer.mockRejectedValue({
status: 409,
body: { flyerId: 99, message: 'Duplicate' },
});
console.log('--- [TEST LOG] ---: 2. Rendering and uploading.');
renderComponent();
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
const input = screen.getByLabelText(/click to select a file/i);
console.log('--- [TEST LOG] ---: 3. Firing file change event.');
fireEvent.change(input, { target: { files: [file] } });
console.log('--- [TEST LOG] ---: 3. File upload triggered.');
try {
console.log('--- [TEST LOG] ---: 4. AWAITING duplicate flyer message...');
expect(
await screen.findByText(/This flyer has already been processed/i),
).toBeInTheDocument();
console.log('--- [TEST LOG] ---: 5. SUCCESS: Duplicate message found.');
} catch (error) {
console.error('--- [TEST LOG] ---: 5. ERROR: findByText for duplicate message timed out.');
console.log('--- [DEBUG] ---: DOM at time of failure:');
screen.debug();
throw error;
}
const link = screen.getByRole('link', { name: /Flyer #99/i });
expect(link).toHaveAttribute('href', '/flyers/99');
console.log('--- [TEST LOG] ---: 6. Duplicate link confirmed.');
});
it('should allow the user to stop watching progress', async () => {
console.log('--- [TEST LOG] ---: 1. Setting up mocks for infinite polling.');
mockedAiApiClient.uploadAndProcessFlyer.mockResolvedValue({ jobId: 'job-stop' });
mockedAiApiClient.getJobStatus.mockResolvedValue({
state: 'active',
progress: { message: 'Analyzing...' },
} as any);
console.log('--- [TEST LOG] ---: 2. Rendering and uploading.');
renderComponent();
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
const input = screen.getByLabelText(/click to select a file/i);
console.log('--- [TEST LOG] ---: 3. Firing file change event.');
fireEvent.change(input, { target: { files: [file] } });
console.log('--- [TEST LOG] ---: 3. File upload triggered.');
let stopButton;
try {
console.log('--- [TEST LOG] ---: 4. AWAITING polling UI...');
stopButton = await screen.findByRole('button', { name: 'Stop Watching Progress' });
console.log('--- [TEST LOG] ---: 5. SUCCESS: Polling UI is visible.');
} catch (error) {
console.error('--- [TEST LOG] ---: 5. ERROR: findByRole for stop button timed out.');
console.log('--- [DEBUG] ---: DOM at time of failure:');
screen.debug();
throw error;
}
console.log('--- [TEST LOG] ---: 6. Clicking "Stop Watching Progress" button.');
fireEvent.click(stopButton);
console.log('--- [TEST LOG] ---: 7. Click event fired.');
try {
console.log('--- [TEST LOG] ---: 8. AWAITING UI reset to idle state...');
expect(await screen.findByText(/click to select a file/i)).toBeInTheDocument();
// Fix typo: queryText -> queryByText
expect(screen.queryByText('Analyzing...')).not.toBeInTheDocument();
console.log('--- [TEST LOG] ---: 9. SUCCESS: UI has reset and message removed.');
} catch (error) {
console.error('--- [TEST LOG] ---: 9. ERROR: findByText for idle state timed out.');
console.log('--- [DEBUG] ---: DOM at time of failure:');
screen.debug();
throw error;
}
});
describe('Error Handling and Edge Cases', () => {
it('should handle checksum generation failure', async () => {
console.log('--- [TEST LOG] ---: 1. Setting up mock for checksum failure.');
mockedChecksumModule.generateFileChecksum.mockRejectedValue(
new Error('Checksum generation failed'),
);
renderComponent();
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
const input = screen.getByLabelText(/click to select a file/i);
console.log('--- [TEST LOG] ---: 2. Firing file change event.');
fireEvent.change(input, { target: { files: [file] } });
console.log('--- [TEST LOG] ---: 3. Awaiting error message.');
expect(await screen.findByText(/Checksum generation failed/i)).toBeInTheDocument();
expect(mockedAiApiClient.uploadAndProcessFlyer).not.toHaveBeenCalled();
console.log('--- [TEST LOG] ---: 4. Assertions passed.');
});
it('should handle a generic network error during upload', async () => {
console.log('--- [TEST LOG] ---: 1. Setting up mock for generic upload error.');
// Simulate a structured error from the API client
mockedAiApiClient.uploadAndProcessFlyer.mockRejectedValue({
status: 500,
body: { message: 'Network Error During Upload' },
});
renderComponent();
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
const input = screen.getByLabelText(/click to select a file/i);
console.log('--- [TEST LOG] ---: 2. Firing file change event.');
fireEvent.change(input, { target: { files: [file] } });
console.log('--- [TEST LOG] ---: 3. Awaiting error message.');
expect(await screen.findByText(/Network Error During Upload/i)).toBeInTheDocument();
console.log('--- [TEST LOG] ---: 4. Assertions passed.');
});
it('should handle a generic network error during polling', async () => {
console.log('--- [TEST LOG] ---: 1. Setting up mock for polling error.');
mockedAiApiClient.uploadAndProcessFlyer.mockResolvedValue({ jobId: 'job-poll-fail' });
mockedAiApiClient.getJobStatus.mockRejectedValue(new Error('Polling Network Error'));
renderComponent();
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
const input = screen.getByLabelText(/click to select a file/i);
console.log('--- [TEST LOG] ---: 2. Firing file change event.');
fireEvent.change(input, { target: { files: [file] } });
console.log('--- [TEST LOG] ---: 3. Awaiting error message.');
expect(await screen.findByText(/Polling failed: Polling Network Error/i)).toBeInTheDocument();
console.log('--- [TEST LOG] ---: 4. Assertions passed.');
});
it('should handle a completed job with a missing flyerId', async () => {
console.log('--- [TEST LOG] ---: 1. Setting up mock for malformed completion payload.');
mockedAiApiClient.uploadAndProcessFlyer.mockResolvedValue({ jobId: 'job-no-flyerid' });
mockedAiApiClient.getJobStatus.mockResolvedValue(
{ state: 'completed', returnValue: {} }, // No flyerId
);
renderComponent();
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
const input = screen.getByLabelText(/click to select a file/i);
console.log('--- [TEST LOG] ---: 2. Firing file change event.');
fireEvent.change(input, { target: { files: [file] } });
console.log('--- [TEST LOG] ---: 3. Awaiting error message.');
expect(
await screen.findByText(/Job completed but did not return a flyer ID/i),
).toBeInTheDocument();
console.log('--- [TEST LOG] ---: 4. Assertions passed.');
});
it('should handle a non-JSON response during polling', async () => {
console.log('--- [TEST LOG] ---: 1. Setting up mock for non-JSON response.');
// The actual function would throw, so we mock the rejection.
// The new getJobStatus would throw an error like "Failed to parse JSON..."
mockedAiApiClient.uploadAndProcessFlyer.mockResolvedValue({ jobId: 'job-bad-json' });
mockedAiApiClient.getJobStatus.mockRejectedValue(
new Error('Failed to parse JSON response from server. Body: <html>502 Bad Gateway</html>'),
);
renderComponent();
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
const input = screen.getByLabelText(/click to select a file/i);
console.log('--- [TEST LOG] ---: 2. Firing file change event.');
fireEvent.change(input, { target: { files: [file] } });
console.log('--- [TEST LOG] ---: 3. Awaiting error message.');
expect(
await screen.findByText(/Polling failed: Failed to parse JSON response from server/i),
).toBeInTheDocument();
console.log('--- [TEST LOG] ---: 4. Assertions passed.');
});
it('should do nothing if the file input is cancelled', () => {
renderComponent();
const input = screen.getByLabelText(/click to select a file/i);
fireEvent.change(input, { target: { files: [] } }); // Empty file list
expect(mockedAiApiClient.uploadAndProcessFlyer).not.toHaveBeenCalled();
});
});
});