101 lines
3.2 KiB
TypeScript
101 lines
3.2 KiB
TypeScript
/** @jest-environment node */
|
|
|
|
import path from 'path'
|
|
import { promises as fs } from 'fs'
|
|
import sharp from 'sharp'
|
|
import {
|
|
buildWhiteBackgroundWebpVariants,
|
|
resolveWhiteBackgroundPreviewFileUrl,
|
|
WHITE_BACKGROUND_PREVIEW_MAX_DIMENSION,
|
|
} from '@/lib/white-background-preview'
|
|
|
|
describe('white-background-preview helpers', () => {
|
|
const tempRoot = path.join(process.cwd(), 'uploads', '__white-background-preview-tests__')
|
|
const originalEnv = { ...process.env }
|
|
|
|
beforeEach(() => {
|
|
process.env.S3_PUBLIC_DOMAIN = 'https://s3.jiansun.vip/gongxiangpan'
|
|
})
|
|
|
|
afterEach(async () => {
|
|
process.env = { ...originalEnv }
|
|
await fs.rm(tempRoot, { recursive: true, force: true })
|
|
})
|
|
|
|
test('builds original and preview webp variants for large transparent images', async () => {
|
|
const source = await sharp({
|
|
create: {
|
|
width: 2200,
|
|
height: 1100,
|
|
channels: 4,
|
|
background: { r: 255, g: 255, b: 255, alpha: 0 },
|
|
},
|
|
})
|
|
.png()
|
|
.toBuffer()
|
|
|
|
const { original, preview } = await buildWhiteBackgroundWebpVariants(source)
|
|
const originalMetadata = await sharp(original).metadata()
|
|
const previewMetadata = await sharp(preview).metadata()
|
|
|
|
expect(originalMetadata.format).toBe('webp')
|
|
expect(originalMetadata.width).toBe(2200)
|
|
expect(originalMetadata.height).toBe(1100)
|
|
expect(previewMetadata.format).toBe('webp')
|
|
expect(previewMetadata.width).toBe(WHITE_BACKGROUND_PREVIEW_MAX_DIMENSION)
|
|
expect(previewMetadata.height).toBe(750)
|
|
expect(original.length).toBeGreaterThan(preview.length)
|
|
})
|
|
|
|
test('preview variant does not enlarge small images', async () => {
|
|
const source = await sharp({
|
|
create: {
|
|
width: 400,
|
|
height: 300,
|
|
channels: 4,
|
|
background: { r: 255, g: 255, b: 255, alpha: 0 },
|
|
},
|
|
})
|
|
.png()
|
|
.toBuffer()
|
|
|
|
const { preview } = await buildWhiteBackgroundWebpVariants(source)
|
|
const previewMetadata = await sharp(preview).metadata()
|
|
|
|
expect(previewMetadata.width).toBe(400)
|
|
expect(previewMetadata.height).toBe(300)
|
|
})
|
|
|
|
test('优先使用本地 preview 文件,否则回退到 public-display 链接', async () => {
|
|
const originalAbsolutePath = path.join(
|
|
tempRoot,
|
|
'material-1',
|
|
'white-background',
|
|
'sku-1',
|
|
'front.webp',
|
|
)
|
|
const previewAbsolutePath = path.join(
|
|
tempRoot,
|
|
'material-1',
|
|
'white-background',
|
|
'sku-1',
|
|
'preview',
|
|
'front.webp',
|
|
)
|
|
const originalRelativePath = path.relative(process.cwd(), originalAbsolutePath).replace(/\\/g, '/')
|
|
const previewRelativePath = path.relative(process.cwd(), previewAbsolutePath).replace(/\\/g, '/')
|
|
|
|
await fs.mkdir(path.dirname(originalAbsolutePath), { recursive: true })
|
|
await fs.writeFile(originalAbsolutePath, Buffer.from('original'))
|
|
|
|
expect(resolveWhiteBackgroundPreviewFileUrl(originalRelativePath)).toBe(
|
|
'https://s3.jiansun.vip/gongxiangpan/public-display/white-background/material-1/sku-1/front.webp',
|
|
)
|
|
|
|
await fs.mkdir(path.dirname(previewAbsolutePath), { recursive: true })
|
|
await fs.writeFile(previewAbsolutePath, Buffer.from('preview'))
|
|
|
|
expect(resolveWhiteBackgroundPreviewFileUrl(originalRelativePath)).toBe(`/${previewRelativePath}`)
|
|
})
|
|
})
|