| 1 | import { describe, expect, it, vi } from 'vitest' |
| 2 | import { |
| 3 | clampExportProgress, |
| 4 | startExportProgressToast |
| 5 | } from '../../../src/renderer/src/components/session-detail/hooks/exportProgressToast' |
| 6 | |
| 7 | function createToastApi() { |
| 8 | return { |
| 9 | loading: vi.fn(() => 'toast-id'), |
| 10 | success: vi.fn(() => 'toast-id'), |
| 11 | info: vi.fn(() => 'toast-id'), |
| 12 | error: vi.fn(() => 'toast-id') |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | describe('export progress toast', () => { |
| 17 | it('clamps progress to a displayable percent', () => { |
| 18 | expect(clampExportProgress(-1)).toBe(0) |
| 19 | expect(clampExportProgress(48.6)).toBe(49) |
| 20 | expect(clampExportProgress(200)).toBe(100) |
| 21 | }) |
| 22 | |
| 23 | it('updates the same loading toast only when real progress arrives', () => { |
| 24 | const toast = createToastApi() |
| 25 | const progressToast = startExportProgressToast({ |
| 26 | toast, |
| 27 | title: 'Exporting PDF', |
| 28 | description: 'Please wait', |
| 29 | initialProgress: 8 |
| 30 | }) |
| 31 | |
| 32 | expect(toast.loading).toHaveBeenCalledTimes(1) |
| 33 | |
| 34 | progressToast.update({ |
| 35 | progress: 42, |
| 36 | description: 'Processing page 2/5' |
| 37 | }) |
| 38 | |
| 39 | expect(toast.loading).toHaveBeenCalledTimes(2) |
| 40 | expect(toast.loading.mock.calls[1][1]).toMatchObject({ |
| 41 | id: 'toast-id' |
| 42 | }) |
| 43 | |
| 44 | progressToast.success('Done', { description: 'Saved' }) |
| 45 | |
| 46 | expect(toast.success).toHaveBeenCalledWith('Done', { |
| 47 | id: 'toast-id', |
| 48 | description: 'Saved' |
| 49 | }) |
| 50 | expect(toast.loading).toHaveBeenCalledTimes(2) |
| 51 | }) |
| 52 | |
| 53 | it('stops progress updates after cancel or failure', () => { |
| 54 | const cancelledToast = createToastApi() |
| 55 | const cancelledProgressToast = startExportProgressToast({ |
| 56 | toast: cancelledToast, |
| 57 | title: 'Exporting', |
| 58 | description: 'Please wait' |
| 59 | }) |
| 60 | |
| 61 | cancelledProgressToast.cancel('Cancelled') |
| 62 | cancelledProgressToast.update({ progress: 60 }) |
| 63 | |
| 64 | expect(cancelledToast.info).toHaveBeenCalledWith('Cancelled', { |
| 65 | id: 'toast-id', |
| 66 | description: null, |
| 67 | duration: 3000 |
| 68 | }) |
| 69 | expect(cancelledToast.loading).toHaveBeenCalledTimes(1) |
| 70 | |
| 71 | const failedToast = createToastApi() |
| 72 | const failedProgressToast = startExportProgressToast({ |
| 73 | toast: failedToast, |
| 74 | title: 'Exporting', |
| 75 | description: 'Please wait' |
| 76 | }) |
| 77 | |
| 78 | failedProgressToast.error('Failed') |
| 79 | failedProgressToast.update({ progress: 80 }) |
| 80 | |
| 81 | expect(failedToast.error).toHaveBeenCalledWith('Failed', { |
| 82 | id: 'toast-id', |
| 83 | description: null, |
| 84 | duration: 6000 |
| 85 | }) |
| 86 | expect(failedToast.loading).toHaveBeenCalledTimes(1) |
| 87 | }) |
| 88 | }) |
| 89 |