|
3 | 3 | API_MAX_FILES_ALLOWED_PER_MESSAGE, |
4 | 4 | DEFAULT_UPLOAD_SIZE_LIMIT_BYTES, |
5 | 5 | } from '../../../src/constants'; |
| 6 | +import type { LocalUploadAttachment } from '../../../src'; |
6 | 7 | import { |
7 | 8 | AttachmentManagerConfig, |
8 | 9 | DraftResponse, |
@@ -1309,6 +1310,54 @@ describe('AttachmentManager', () => { |
1309 | 1310 | ); |
1310 | 1311 | expect(mockChannel.sendImage).not.toHaveBeenCalled(); |
1311 | 1312 | }); |
| 1313 | + |
| 1314 | + it('on retry from failed state, onProgress keeps updating uploadProgress while uploading', async () => { |
| 1315 | + const { |
| 1316 | + messageComposer: { attachmentManager }, |
| 1317 | + mockChannel, |
| 1318 | + } = setup(); |
| 1319 | + |
| 1320 | + const file = new File([''], 'test.jpg', { type: 'image/jpeg' }); |
| 1321 | + mockChannel.sendImage.mockRejectedValueOnce(new Error('Upload failed')); |
| 1322 | + |
| 1323 | + const local = await attachmentManager.fileToLocalUploadAttachment(file); |
| 1324 | + await attachmentManager.uploadAttachment(local); |
| 1325 | + |
| 1326 | + const failedAttachment = attachmentManager.attachments[0]; |
| 1327 | + expect(failedAttachment.localMetadata.uploadState).toBe('failed'); |
| 1328 | + |
| 1329 | + let releaseUpload!: () => void; |
| 1330 | + const uploadGate = new Promise<void>((resolve) => { |
| 1331 | + releaseUpload = resolve; |
| 1332 | + }); |
| 1333 | + |
| 1334 | + const customUploadFn = vi.fn(async (_file, options) => { |
| 1335 | + options?.onProgress?.(33); |
| 1336 | + await uploadGate; |
| 1337 | + return { file: 'retry-url' }; |
| 1338 | + }); |
| 1339 | + attachmentManager.setCustomUploadFn(customUploadFn); |
| 1340 | + |
| 1341 | + const uploadPromise = attachmentManager.uploadAttachment( |
| 1342 | + failedAttachment as LocalUploadAttachment, |
| 1343 | + ); |
| 1344 | + |
| 1345 | + await vi.waitFor(() => { |
| 1346 | + expect(attachmentManager.attachments[0].localMetadata.uploadProgress).toBe(33); |
| 1347 | + expect(attachmentManager.attachments[0].localMetadata.uploadState).toBe( |
| 1348 | + 'uploading', |
| 1349 | + ); |
| 1350 | + }); |
| 1351 | + |
| 1352 | + releaseUpload(); |
| 1353 | + await uploadPromise; |
| 1354 | + |
| 1355 | + expect(customUploadFn).toHaveBeenCalledWith( |
| 1356 | + file, |
| 1357 | + expect.objectContaining({ onProgress: expect.any(Function) }), |
| 1358 | + ); |
| 1359 | + expect(attachmentManager.attachments[0].localMetadata.uploadState).toBe('finished'); |
| 1360 | + }); |
1312 | 1361 | }); |
1313 | 1362 |
|
1314 | 1363 | describe('doDefaultUploadRequest', () => { |
|
0 commit comments