51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
import sharp from 'sharp'
|
|
import {
|
|
PERFORMANCE_SURVEY_IMAGE_LIMITS,
|
|
assertPerformanceSurveyImageDimensions,
|
|
processPerformanceSurveyImages,
|
|
} from '@/lib/performance-survey-image-processing'
|
|
|
|
describe('performance survey image processing', () => {
|
|
test('serially converts a long source image into ordered 1500px WebP slices', async () => {
|
|
const source = await sharp({
|
|
create: {
|
|
width: 24,
|
|
height: 3100,
|
|
channels: 4,
|
|
background: { r: 32, g: 96, b: 180, alpha: 1 },
|
|
},
|
|
}).png().toBuffer()
|
|
|
|
const processed = await processPerformanceSurveyImages([{
|
|
buffer: source,
|
|
mimeType: 'image/png',
|
|
sourceName: 'long-image.png',
|
|
}])
|
|
|
|
expect(processed).toHaveLength(3)
|
|
expect(processed.map((item) => item.sliceIndex)).toEqual([0, 1, 2])
|
|
const metadata = await Promise.all(processed.map((item) => sharp(item.buffer).metadata()))
|
|
expect(metadata.map((item) => item.format)).toEqual(['webp', 'webp', 'webp'])
|
|
expect(metadata.map((item) => item.height)).toEqual([1500, 1500, 100])
|
|
})
|
|
|
|
test('rejects excessive pixel and per-image slice budgets before processing', () => {
|
|
expect(() => assertPerformanceSurveyImageDimensions(10_000, 10_000, 'huge.png')).toThrow('像素总量过大')
|
|
expect(() => assertPerformanceSurveyImageDimensions(
|
|
1,
|
|
PERFORMANCE_SURVEY_IMAGE_LIMITS.sliceHeight * (PERFORMANCE_SURVEY_IMAGE_LIMITS.maxSlicesPerImage + 1),
|
|
'too-long.png',
|
|
)).toThrow('最多允许 24 个切片')
|
|
})
|
|
|
|
test('rejects more than 12 source images before decoding', async () => {
|
|
const inputs = Array.from({ length: PERFORMANCE_SURVEY_IMAGE_LIMITS.maxSourceCount + 1 }, (_, index) => ({
|
|
buffer: Buffer.from('not-decoded'),
|
|
mimeType: 'image/png',
|
|
sourceName: `${index}.png`,
|
|
}))
|
|
|
|
await expect(processPerformanceSurveyImages(inputs)).rejects.toThrow('最多上传 12 张图片')
|
|
})
|
|
})
|