|
| 1 | +import { HttpResponse, http } from 'msw' |
| 2 | +import { apiUrl } from '../testUtils/msw-handlers' |
| 3 | +import { server } from '../testUtils/msw-setup' |
| 4 | +import { TEST_API_TOKEN } from '../testUtils/test-defaults' |
| 5 | +import { generateId, isValidUuidV7Base58 } from '../utils/uuidv7' |
| 6 | +import { AttachmentsClient } from './attachments-client' |
| 7 | + |
| 8 | +const UPLOAD_URL = apiUrl('api/v1/attachments/upload') |
| 9 | + |
| 10 | +const mockAttachmentResponse = { |
| 11 | + attachment_id: 'abc-123', |
| 12 | + url_type: 'file', |
| 13 | + file_name: 'diagram.png', |
| 14 | + file_size: 11, |
| 15 | + underlying_type: 'image/png', |
| 16 | + url: 'https://comms.todoist.com/attachments/abc-123/diagram.png', |
| 17 | + upload_state: 'uploaded', |
| 18 | +} |
| 19 | + |
| 20 | +describe('AttachmentsClient', () => { |
| 21 | + let client: AttachmentsClient |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + client = new AttachmentsClient({ apiToken: TEST_API_TOKEN }) |
| 25 | + }) |
| 26 | + |
| 27 | + describe('upload', () => { |
| 28 | + it('uploads a Buffer with the canonical multipart fields', async () => { |
| 29 | + let capturedForm: FormData | undefined |
| 30 | + let capturedAuth: string | null = null |
| 31 | + let capturedContentType: string | null = null |
| 32 | + |
| 33 | + server.use( |
| 34 | + http.post(UPLOAD_URL, async ({ request }) => { |
| 35 | + capturedAuth = request.headers.get('Authorization') |
| 36 | + capturedContentType = request.headers.get('Content-Type') |
| 37 | + capturedForm = await request.formData() |
| 38 | + return HttpResponse.json(mockAttachmentResponse) |
| 39 | + }), |
| 40 | + ) |
| 41 | + |
| 42 | + const result = await client.upload({ |
| 43 | + file: Buffer.from('hello world'), |
| 44 | + fileName: 'diagram.png', |
| 45 | + }) |
| 46 | + |
| 47 | + expect(capturedAuth).toBe(`Bearer ${TEST_API_TOKEN}`) |
| 48 | + // multipart boundary content-type, never application/json |
| 49 | + expect(capturedContentType).toContain('multipart/form-data') |
| 50 | + |
| 51 | + const file = capturedForm?.get('file') |
| 52 | + expect(file).toBeInstanceOf(Blob) |
| 53 | + expect(capturedForm?.get('file_name')).toBe('diagram.png') |
| 54 | + expect(capturedForm?.get('file_size')).toBe('11') |
| 55 | + expect(capturedForm?.get('underlying_type')).toBe('image/png') |
| 56 | + // A valid ID is generated when none is supplied. |
| 57 | + const attachmentId = capturedForm?.get('attachment_id') |
| 58 | + expect(isValidUuidV7Base58(attachmentId)).toBe(true) |
| 59 | + |
| 60 | + // Response is camel-cased and validated. |
| 61 | + expect(result.attachmentId).toBe('abc-123') |
| 62 | + expect(result.fileName).toBe('diagram.png') |
| 63 | + expect(result.url).toBe('https://comms.todoist.com/attachments/abc-123/diagram.png') |
| 64 | + }) |
| 65 | + |
| 66 | + it('uses a caller-supplied attachmentId', async () => { |
| 67 | + let capturedForm: FormData | undefined |
| 68 | + const callerId = generateId() |
| 69 | + |
| 70 | + server.use( |
| 71 | + http.post(UPLOAD_URL, async ({ request }) => { |
| 72 | + capturedForm = await request.formData() |
| 73 | + return HttpResponse.json({ |
| 74 | + ...mockAttachmentResponse, |
| 75 | + attachment_id: callerId, |
| 76 | + }) |
| 77 | + }), |
| 78 | + ) |
| 79 | + |
| 80 | + const result = await client.upload({ |
| 81 | + file: Buffer.from('data'), |
| 82 | + fileName: 'notes.txt', |
| 83 | + attachmentId: callerId, |
| 84 | + }) |
| 85 | + |
| 86 | + expect(capturedForm?.get('attachment_id')).toBe(callerId) |
| 87 | + expect(result.attachmentId).toBe(callerId) |
| 88 | + }) |
| 89 | + |
| 90 | + it('uploads a Blob and infers the file name and type', async () => { |
| 91 | + let capturedForm: FormData | undefined |
| 92 | + |
| 93 | + server.use( |
| 94 | + http.post(UPLOAD_URL, async ({ request }) => { |
| 95 | + capturedForm = await request.formData() |
| 96 | + return HttpResponse.json(mockAttachmentResponse) |
| 97 | + }), |
| 98 | + ) |
| 99 | + |
| 100 | + const blob = new File([new Uint8Array([1, 2, 3, 4])], 'photo.jpg', { |
| 101 | + type: 'image/jpeg', |
| 102 | + }) |
| 103 | + |
| 104 | + await client.upload({ file: blob }) |
| 105 | + |
| 106 | + expect(capturedForm?.get('file_name')).toBe('photo.jpg') |
| 107 | + expect(capturedForm?.get('file_size')).toBe('4') |
| 108 | + expect(capturedForm?.get('underlying_type')).toBe('image/jpeg') |
| 109 | + }) |
| 110 | + |
| 111 | + it('uploads a Uint8Array', async () => { |
| 112 | + let capturedForm: FormData | undefined |
| 113 | + |
| 114 | + server.use( |
| 115 | + http.post(UPLOAD_URL, async ({ request }) => { |
| 116 | + capturedForm = await request.formData() |
| 117 | + return HttpResponse.json(mockAttachmentResponse) |
| 118 | + }), |
| 119 | + ) |
| 120 | + |
| 121 | + await client.upload({ file: new Uint8Array([1, 2, 3]), fileName: 'bytes.bin' }) |
| 122 | + |
| 123 | + expect(capturedForm?.get('file')).toBeInstanceOf(Blob) |
| 124 | + expect(capturedForm?.get('file_name')).toBe('bytes.bin') |
| 125 | + expect(capturedForm?.get('file_size')).toBe('3') |
| 126 | + expect(capturedForm?.get('underlying_type')).toBe('application/octet-stream') |
| 127 | + }) |
| 128 | + |
| 129 | + it('throws when uploading raw bytes without a fileName', async () => { |
| 130 | + await expect(client.upload({ file: Buffer.from('x') })).rejects.toThrow( |
| 131 | + 'fileName is required when uploading raw bytes', |
| 132 | + ) |
| 133 | + }) |
| 134 | + }) |
| 135 | +}) |
0 commit comments