feat: add eager image processing functions (generateAllSizes, convertOriginalToWebp)

This commit is contained in:
Kirill
2026-05-15 19:48:15 +05:00
parent 8b79c01985
commit 5de2694a14
2 changed files with 115 additions and 0 deletions
@@ -97,3 +97,65 @@ describe('image-resize', () => {
await fs.promises.unlink(first.path)
})
})
describe('eager image processing', () => {
it('generateAllSizes creates all width+format combinations', async () => {
const { generateAllSizes } = await import('../image-resize.js')
const sharp = (await import('sharp')).default
const uuid = 'test-eager-uuid-1'
const testImagePath = path.join(UPLOADS_DIR, `${uuid}.png`)
await sharp({ create: { width: 2000, height: 1500, channels: 3, background: { r: 255, g: 0, b: 0 } } })
.png()
.toFile(testImagePath)
await generateAllSizes(uuid, '', testImagePath)
const cacheDir = path.join(UPLOADS_DIR, '.cache')
for (const width of [320, 640, 1024, 1600]) {
for (const format of ['avif', 'webp']) {
const cachePath = path.join(cacheDir, `${uuid}_w${width}.${format}`)
const exists = await fs.promises.access(cachePath).then(() => true).catch(() => false)
expect(exists).toBe(true)
}
}
// Cleanup
await fs.promises.unlink(testImagePath)
for (const width of [320, 640, 1024, 1600]) {
for (const format of ['avif', 'webp']) {
const cachePath = path.join(cacheDir, `${uuid}_w${width}.${format}`)
try {
await fs.promises.unlink(cachePath)
} catch {
// ignore
}
}
}
})
it('convertOriginalToWebp converts and deletes original', async () => {
const { convertOriginalToWebp } = await import('../image-resize.js')
const sharp = (await import('sharp')).default
const uuid = 'test-eager-uuid-2'
const testImagePath = path.join(UPLOADS_DIR, `${uuid}.png`)
await sharp({ create: { width: 800, height: 600, channels: 3, background: { r: 0, g: 255, b: 0 } } })
.png()
.toFile(testImagePath)
const result = await convertOriginalToWebp(uuid, '')
expect(result).toBe(`/uploads/${uuid}.webp`)
const pngExists = await fs.promises.access(testImagePath).then(() => true).catch(() => false)
expect(pngExists).toBe(false)
const webpPath = path.join(UPLOADS_DIR, `${uuid}.webp`)
const webpExists = await fs.promises.access(webpPath).then(() => true).catch(() => false)
expect(webpExists).toBe(true)
// Cleanup
try {
await fs.promises.unlink(webpPath)
} catch {
// ignore
}
})
})