/** @jest-environment node */ import { prisma } from '@/lib/prisma' import { saveProductPackageAssets } from '@/lib/task-material-processor' jest.mock('@/lib/prisma', () => ({ prisma: { $transaction: jest.fn(), }, })) jest.mock('@/lib/s3', () => ({ deleteFromS3: jest.fn() })) jest.mock('@/lib/public-display', () => ({ buildMaterialPublicDisplayPath: jest.fn(), uploadPublicDisplayToS3: jest.fn(), })) jest.mock('@/lib/image-processor', () => ({ compressImage: jest.fn() })) jest.mock('@/lib/remote-upload-sync', () => ({ deleteStoredUploadFileFromRemote: jest.fn(), syncStoredUploadFileToRemote: jest.fn(), })) describe('task material database transaction', () => { test('任务详情完成事务允许等待短暂的连接池高峰', async () => { const tx = { materialAsset: { createMany: jest.fn().mockResolvedValue({ count: 2 }), create: jest.fn(), }, materialAssetBarcode: { createMany: jest.fn(), }, material: { update: jest.fn().mockResolvedValue({ id: 'material-1' }), }, } ;(prisma.$transaction as jest.Mock).mockImplementation( async (callback: (client: typeof tx) => Promise) => callback(tx), ) await saveProductPackageAssets( 'material-1', { main: [{ fileUrl: 'https://s3.example/main.webp', fileName: 'main.webp' }], sku: [], detail: [], }, { all: { filePath: 'materials/all.zip', fileName: 'all.zip', fileSize: 1024, }, }, ) expect(prisma.$transaction).toHaveBeenCalledWith( expect.any(Function), { maxWait: 15000, timeout: 30000, }, ) }) })