export {} jest.mock('@/lib/prisma', () => ({ prisma: { $executeRawUnsafe: jest.fn(), materialLog: { create: jest.fn(), }, }, })) const { prisma } = require('@/lib/prisma') const { ensureMaterialLogsTable, createMaterialLog } = require('../material-log') describe('material log persistence safety', () => { beforeEach(() => { jest.resetAllMocks() }) it('兜底建表会移除素材级联删除外键', async () => { await ensureMaterialLogsTable() const executedSql = prisma.$executeRawUnsafe.mock.calls.map(([sql]: [string]) => sql).join('\n') expect(executedSql).toContain('ADD COLUMN IF NOT EXISTS "materialSnapshot" JSONB') expect(executedSql).toContain('DROP CONSTRAINT IF EXISTS "material_logs_materialId_fkey"') expect(executedSql).not.toContain( 'FOREIGN KEY ("materialId") REFERENCES "materials"("id") ON DELETE CASCADE' ) }) it('创建日志时会保存素材快照', async () => { await createMaterialLog({ materialId: 'material-1', userId: 'user-1', action: 'DELETE', materialSnapshot: { productModel: '测试素材', }, }) expect(prisma.materialLog.create).toHaveBeenCalledWith({ data: expect.objectContaining({ materialId: 'material-1', action: 'DELETE', materialSnapshot: { productModel: '测试素材', }, }), }) }) })