|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import * as os from 'node:os'; |
| 3 | +import * as path from 'node:path'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { CommandExecutionError } from '../../../errors.js'; |
| 6 | +import { imagexUpload } from './imagex-upload.js'; |
| 7 | +import type { ImageXUploadInfo } from './imagex-upload.js'; |
| 8 | + |
| 9 | +// ── Helpers ────────────────────────────────────────────────────────────────── |
| 10 | + |
| 11 | +function makeTempImage(ext = '.jpg'): string { |
| 12 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'imagex-test-')); |
| 13 | + const filePath = path.join(dir, `cover${ext}`); |
| 14 | + fs.writeFileSync(filePath, Buffer.from([0xff, 0xd8, 0xff, 0xe0])); // minimal JPEG header bytes |
| 15 | + return filePath; |
| 16 | +} |
| 17 | + |
| 18 | +const FAKE_UPLOAD_INFO: ImageXUploadInfo = { |
| 19 | + upload_url: 'https://imagex.bytedance.com/upload/presigned/fake', |
| 20 | + store_uri: 'tos-cn-i-alisg.example.com/cover/abc123', |
| 21 | +}; |
| 22 | + |
| 23 | +// ── Tests ───────────────────────────────────────────────────────────────────── |
| 24 | + |
| 25 | +describe('imagexUpload', () => { |
| 26 | + let imagePath: string; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + imagePath = makeTempImage('.jpg'); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + // Clean up temp files |
| 34 | + try { |
| 35 | + fs.unlinkSync(imagePath); |
| 36 | + fs.rmdirSync(path.dirname(imagePath)); |
| 37 | + } catch { |
| 38 | + // ignore cleanup errors |
| 39 | + } |
| 40 | + vi.restoreAllMocks(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('throws CommandExecutionError when image file does not exist', async () => { |
| 44 | + await expect( |
| 45 | + imagexUpload('/nonexistent/path/cover.jpg', FAKE_UPLOAD_INFO), |
| 46 | + ).rejects.toThrow(CommandExecutionError); |
| 47 | + |
| 48 | + await expect( |
| 49 | + imagexUpload('/nonexistent/path/cover.jpg', FAKE_UPLOAD_INFO), |
| 50 | + ).rejects.toThrow('Cover image file not found'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('PUTs the image and returns store_uri on success', async () => { |
| 54 | + const mockFetch = vi.fn().mockResolvedValue({ |
| 55 | + ok: true, |
| 56 | + status: 200, |
| 57 | + text: vi.fn().mockResolvedValue(''), |
| 58 | + }); |
| 59 | + vi.stubGlobal('fetch', mockFetch); |
| 60 | + |
| 61 | + const result = await imagexUpload(imagePath, FAKE_UPLOAD_INFO); |
| 62 | + |
| 63 | + expect(result).toBe(FAKE_UPLOAD_INFO.store_uri); |
| 64 | + expect(mockFetch).toHaveBeenCalledOnce(); |
| 65 | + const [url, init] = mockFetch.mock.calls[0] as [string, RequestInit]; |
| 66 | + expect(url).toBe(FAKE_UPLOAD_INFO.upload_url); |
| 67 | + expect(init.method).toBe('PUT'); |
| 68 | + expect((init.headers as Record<string, string>)['Content-Type']).toBe( |
| 69 | + 'image/jpeg', |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it('uses image/png Content-Type for .png files', async () => { |
| 74 | + const pngPath = makeTempImage('.png'); |
| 75 | + const mockFetch = vi.fn().mockResolvedValue({ |
| 76 | + ok: true, |
| 77 | + status: 200, |
| 78 | + text: vi.fn().mockResolvedValue(''), |
| 79 | + }); |
| 80 | + vi.stubGlobal('fetch', mockFetch); |
| 81 | + |
| 82 | + try { |
| 83 | + await imagexUpload(pngPath, FAKE_UPLOAD_INFO); |
| 84 | + const [, init] = mockFetch.mock.calls[0] as [string, RequestInit]; |
| 85 | + expect((init.headers as Record<string, string>)['Content-Type']).toBe( |
| 86 | + 'image/png', |
| 87 | + ); |
| 88 | + } finally { |
| 89 | + try { |
| 90 | + fs.unlinkSync(pngPath); |
| 91 | + fs.rmdirSync(path.dirname(pngPath)); |
| 92 | + } catch { |
| 93 | + // ignore |
| 94 | + } |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + it('throws CommandExecutionError on non-2xx PUT response', async () => { |
| 99 | + const mockFetch = vi.fn().mockResolvedValue({ |
| 100 | + ok: false, |
| 101 | + status: 403, |
| 102 | + text: vi.fn().mockResolvedValue('Forbidden'), |
| 103 | + }); |
| 104 | + vi.stubGlobal('fetch', mockFetch); |
| 105 | + |
| 106 | + await expect(imagexUpload(imagePath, FAKE_UPLOAD_INFO)).rejects.toThrow( |
| 107 | + CommandExecutionError, |
| 108 | + ); |
| 109 | + await expect(imagexUpload(imagePath, FAKE_UPLOAD_INFO)).rejects.toThrow( |
| 110 | + 'ImageX upload failed with status 403', |
| 111 | + ); |
| 112 | + }); |
| 113 | +}); |
0 commit comments