feat: add multipart image uploads
Register Fastify multipart support, validate JPEG/PNG/WebP binary signatures, store upload bytes through ImageStorage, persist metadata after storage succeeds, clean up bytes on metadata failure, and document the upload boundary. Verification: npm test passed 68 tests and integration passed 14 tests. No build script is configured.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { createInventoryImageService } from '../../src/modules/inventory/services/inventory-image-service.js';
|
||||
|
||||
const input = {
|
||||
inventoryItemId: 'inventory-item-id',
|
||||
sellerId: 'seller-id',
|
||||
storageKey: 'inventory/item/front.jpg',
|
||||
originalFilename: 'front.jpg',
|
||||
mimeType: 'image/jpeg',
|
||||
fileSize: 4,
|
||||
width: 1200,
|
||||
height: 900,
|
||||
data: Buffer.from([0xff, 0xd8, 0xff, 0x00]),
|
||||
};
|
||||
|
||||
test('stores validated upload bytes before persisting image metadata', async () => {
|
||||
const stored = [];
|
||||
const service = createInventoryImageService({
|
||||
imageStorage: {
|
||||
async put(value) { stored.push(value); },
|
||||
async delete() {},
|
||||
},
|
||||
inventoryImageRepository: {
|
||||
async createForSeller(image) {
|
||||
return { id: 'image-1', ...image };
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const image = await service.upload(input);
|
||||
|
||||
assert.equal(stored.length, 1);
|
||||
assert.deepEqual(stored[0], { key: input.storageKey, data: input.data });
|
||||
assert.equal(image.id, 'image-1');
|
||||
assert.equal(image.processingStatus, 'PENDING');
|
||||
assert.equal('data' in image, false);
|
||||
});
|
||||
|
||||
test('removes stored bytes when metadata persistence fails', async () => {
|
||||
const deleted = [];
|
||||
const service = createInventoryImageService({
|
||||
imageStorage: {
|
||||
async put() {},
|
||||
async delete(key) { deleted.push(key); },
|
||||
},
|
||||
inventoryImageRepository: {
|
||||
async createForSeller() { return null; },
|
||||
},
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
() => service.upload(input),
|
||||
(error) => error.code === 'INVENTORY_ITEM_NOT_FOUND',
|
||||
);
|
||||
assert.deepEqual(deleted, [input.storageKey]);
|
||||
});
|
||||
Reference in New Issue
Block a user