gongxiangp2/lib/__tests__/material-edit-routes.test.ts
chenpei 4712175cb0 feat(素材链路): 发布白底图双轨与AI生图回传能力
- 完成白底图 WebP 双轨接口、上传链路与预览缓存改造
- 新增 AI 生图平台成品回传接口、任务状态与表单规则
- 补齐 AI 生成标签、任务详情补全、相关测试与对接文档
2026-03-25 10:28:02 +08:00

136 lines
3.7 KiB
TypeScript

/** @jest-environment node */
import { NextRequest } from 'next/server'
import { PUT as putMaterialById } from '@/app/api/materials/[id]/route'
import { PUT as putMaterialUpdate } from '@/app/api/materials/update/route'
import { getServerSession } from 'next-auth'
import { updateMaterialWithSharedRules } from '@/lib/material-update'
jest.mock('next-auth', () => ({
getServerSession: jest.fn(),
}))
jest.mock('@/lib/auth', () => ({
authOptions: {},
}))
jest.mock('@/lib/prisma', () => ({
prisma: {},
}))
jest.mock('@/lib/department-permission', () => ({
canUserViewMaterial: jest.fn(),
isShareLinkAccess: jest.fn(),
}))
jest.mock('@/lib/task-detail-full-set', () => ({
deriveTaskDetailIsFullSet: jest.fn(),
}))
jest.mock('@/lib/material-update', () => ({
updateMaterialWithSharedRules: jest.fn(),
}))
describe('material edit routes', () => {
const originalFile = globalThis.File
beforeEach(() => {
jest.clearAllMocks()
;(getServerSession as jest.Mock).mockResolvedValue({
user: {
id: 'user-1',
role: 'USER',
},
})
;(updateMaterialWithSharedRules as jest.Mock).mockResolvedValue({
material: {
id: 'material-1',
productModel: 'SPU-001',
categoryName: '店铺A',
sceneName: '场景A',
platformName: 'Amazon',
productCategoryName: '椅子',
materialType: '标准模型',
renderer: 'CR',
authorName: '测试作者',
taskNo: null,
taskStatus: null,
taskFetchedAt: null,
isFullSet: false,
zipFileSize: 0,
},
})
Object.defineProperty(globalThis, 'File', {
configurable: true,
writable: true,
value: undefined,
})
})
afterAll(() => {
Object.defineProperty(globalThis, 'File', {
configurable: true,
writable: true,
value: originalFile,
})
})
function createFormData() {
const formData = new FormData()
formData.append('productModel', 'SPU-001')
formData.append('categoryName', '店铺A')
formData.append('sceneName', '场景A')
formData.append('platformName', 'Amazon')
formData.append('productCategoryName', '椅子')
formData.append('materialType', '标准模型')
formData.append('renderer', 'CR')
formData.append('authorName', '测试作者')
formData.append('userId', 'user-1')
formData.append('taskNo', '')
return formData
}
test('PUT /api/materials/[id] 在没有全局 File 时仍可正常处理表单更新', async () => {
const request = new NextRequest('http://localhost/api/materials/material-1', {
method: 'PUT',
body: createFormData(),
})
const response = await putMaterialById(request, {
params: { id: 'material-1' },
})
const body = await response.json()
expect(response.status).toBe(200)
expect(body.success).toBe(true)
expect(updateMaterialWithSharedRules).toHaveBeenCalledWith(
expect.objectContaining({
materialId: 'material-1',
previewImage: null,
}),
)
})
test('PUT /api/materials/update 在没有全局 File 时仍可正常处理表单更新', async () => {
const formData = createFormData()
formData.append('id', 'material-1')
const request = new NextRequest('http://localhost/api/materials/update', {
method: 'PUT',
body: formData,
})
const response = await putMaterialUpdate(request)
const body = await response.json()
expect(response.status).toBe(200)
expect(body.message).toBe('更新成功')
expect(updateMaterialWithSharedRules).toHaveBeenCalledWith(
expect.objectContaining({
materialId: 'material-1',
previewImage: null,
}),
)
})
})