import assert from 'node:assert/strict'; import { mkdtemp, readFile, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import sharp from 'sharp'; import { MediaStorage, MediaValidationError } from '../dist/content/media-storage.js'; import { ContentError, ContentRepository } from '../dist/content/content-repository.js'; const root = await mkdtemp(join(tmpdir(), 'qipai-media-')); try { const png = await sharp({ create: { width: 2400, height: 1200, channels: 3, background: '#336699' } }).png().toBuffer(); const storage = new MediaStorage(root); const image = await storage.storeImage({ tenantId: '7', storeId: '11', originalName: 'banner.png', contentType: 'image/png', body: png }); assert.equal(image.mimeType, 'image/webp'); assert.ok(image.width <= 1920); assert.match(image.storagePath, /^tenants\/7\/stores\/11\/.+\.webp$/); assert.ok((await readFile(join(root, ...image.storagePath.split('/')))).length > 0); await assert.rejects( () => storage.storeImage({ tenantId: '7', originalName: 'bad.txt', contentType: 'text/plain', body: Buffer.from('not an image') }), (error) => error instanceof MediaValidationError && error.code === 'IMAGE_TYPE_INVALID' ); } finally { await rm(root, { recursive: true, force: true }); } const storeActor = { tenantId: '7', userId: '21', access: { roles: ['STORE_ADMIN'], capabilities: ['store.operation.write'], storeIds: ['11'] }, traceId: 'trace', ip: '127.0.0.1', userAgent: 'test' }; const repository = new ContentRepository({ async execute() { return [[], []]; } }); await assert.rejects( () => repository.saveAdvertisement(storeActor, { scopeType: 'PLATFORM', title: 'x', imageAssetId: '1', targetType: 'NONE', targetValue: '', status: 'DRAFT', sortOrder: 0 }), (error) => error instanceof ContentError && error.code === 'PLATFORM_AD_FORBIDDEN' ); console.log('PASS: M03-B image compression, tenant paths and advertisement scope validation are present.');