- 文件上传验证: ModelUploader组件增强、上传API错误处理 - GLTF解析器: 增强节点提取和元数据 - 板材编辑: PanelEditor客户端验证 + panels API服务端验证 - 装配步骤编辑: 修复未使用代码,添加预览按钮 - 装配动画播放: AssemblyPlayer步骤导航边界检查优化 - 分享链接: 完整CRUD API(创建、获取、删除),含验证和过期检查 跳过可选测试任务以加快MVP开发
286 lines
7.5 KiB
TypeScript
286 lines
7.5 KiB
TypeScript
/**
|
|
* 产品管理 API 集成测试
|
|
*
|
|
* 测试范围:
|
|
* - 产品 CRUD 操作
|
|
* - 权限过滤
|
|
* - 协作者管理
|
|
* - SKU 交接
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll, afterAll } from '@jest/globals';
|
|
import { PrismaClient } from '@prisma/client';
|
|
import { setupTestEnvironment, cleanDatabase } from '../helpers/test-utils';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
describe('产品管理 API 测试', () => {
|
|
let testEnv: any;
|
|
|
|
beforeAll(async () => {
|
|
await cleanDatabase();
|
|
testEnv = await setupTestEnvironment();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await cleanDatabase();
|
|
await prisma.$disconnect();
|
|
});
|
|
|
|
describe('产品创建', () => {
|
|
it('设计师应该能创建产品', async () => {
|
|
const product = await prisma.product.create({
|
|
data: {
|
|
name: '测试产品',
|
|
modelNumber: `TEST-${Date.now()}`,
|
|
category: 'FURNITURE',
|
|
visibility: 'TEAM',
|
|
createdBy: testEnv.users.designer.id
|
|
}
|
|
});
|
|
|
|
expect(product).toBeDefined();
|
|
expect(product.name).toBe('测试产品');
|
|
expect(product.createdBy).toBe(testEnv.users.designer.id);
|
|
});
|
|
|
|
it('应该记录创建日志', async () => {
|
|
const product = await prisma.product.create({
|
|
data: {
|
|
name: '日志测试产品',
|
|
modelNumber: `TEST-${Date.now()}`,
|
|
category: 'FURNITURE',
|
|
visibility: 'PUBLIC',
|
|
createdBy: testEnv.users.designer.id
|
|
}
|
|
});
|
|
|
|
const log = await prisma.activityLog.create({
|
|
data: {
|
|
userId: testEnv.users.designer.id,
|
|
type: 'PRODUCT_CREATE',
|
|
action: 'create',
|
|
targetType: 'Product',
|
|
targetId: product.id,
|
|
changes: { productName: product.name }
|
|
}
|
|
});
|
|
|
|
expect(log).toBeDefined();
|
|
expect(log.type).toBe('PRODUCT_CREATE');
|
|
});
|
|
});
|
|
|
|
describe('产品列表权限过滤', () => {
|
|
let publicProduct: any;
|
|
let privateProduct: any;
|
|
|
|
beforeAll(async () => {
|
|
publicProduct = await prisma.product.create({
|
|
data: {
|
|
name: '公开产品',
|
|
modelNumber: `TEST-${Date.now()}`,
|
|
category: 'FURNITURE',
|
|
visibility: 'PUBLIC',
|
|
createdBy: testEnv.users.designer.id
|
|
}
|
|
});
|
|
|
|
privateProduct = await prisma.product.create({
|
|
data: {
|
|
name: '私有产品',
|
|
modelNumber: `TEST-${Date.now()}`,
|
|
category: 'FURNITURE',
|
|
visibility: 'PRIVATE',
|
|
createdBy: testEnv.users.designer.id
|
|
}
|
|
});
|
|
});
|
|
|
|
it('超级管理员应该看到所有产品', async () => {
|
|
const products = await prisma.product.findMany({
|
|
where: { deletedAt: null }
|
|
});
|
|
|
|
expect(products.length).toBeGreaterThanOrEqual(2);
|
|
});
|
|
|
|
it('查看者应该只看到公开产品', async () => {
|
|
const products = await prisma.product.findMany({
|
|
where: {
|
|
deletedAt: null,
|
|
visibility: 'PUBLIC'
|
|
}
|
|
});
|
|
|
|
expect(products.length).toBeGreaterThan(0);
|
|
products.forEach(p => {
|
|
expect(p.visibility).toBe('PUBLIC');
|
|
});
|
|
});
|
|
|
|
it('应该支持搜索功能', async () => {
|
|
const products = await prisma.product.findMany({
|
|
where: {
|
|
deletedAt: null,
|
|
name: {
|
|
contains: '公开'
|
|
}
|
|
}
|
|
});
|
|
|
|
expect(products.length).toBeGreaterThan(0);
|
|
products.forEach(p => {
|
|
expect(p.name).toContain('公开');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('产品更新', () => {
|
|
it('创建者应该能更新产品', async () => {
|
|
const product = await prisma.product.create({
|
|
data: {
|
|
name: '待更新产品',
|
|
modelNumber: `TEST-${Date.now()}`,
|
|
category: 'FURNITURE',
|
|
visibility: 'TEAM',
|
|
createdBy: testEnv.users.designer.id
|
|
}
|
|
});
|
|
|
|
const updatedProduct = await prisma.product.update({
|
|
where: { id: product.id },
|
|
data: {
|
|
name: '已更新产品',
|
|
description: '更新后的描述'
|
|
}
|
|
});
|
|
|
|
expect(updatedProduct.name).toBe('已更新产品');
|
|
expect(updatedProduct.description).toBe('更新后的描述');
|
|
});
|
|
});
|
|
|
|
describe('产品删除(软删除)', () => {
|
|
it('应该软删除产品', async () => {
|
|
const product = await prisma.product.create({
|
|
data: {
|
|
name: '待删除产品',
|
|
modelNumber: `TEST-${Date.now()}`,
|
|
category: 'FURNITURE',
|
|
visibility: 'TEAM',
|
|
createdBy: testEnv.users.designer.id
|
|
}
|
|
});
|
|
|
|
const deletedProduct = await prisma.product.update({
|
|
where: { id: product.id },
|
|
data: { deletedAt: new Date() }
|
|
});
|
|
|
|
expect(deletedProduct.deletedAt).not.toBeNull();
|
|
});
|
|
|
|
it('删除后产品不应该出现在列表中', async () => {
|
|
const product = await prisma.product.create({
|
|
data: {
|
|
name: '测试删除',
|
|
modelNumber: `TEST-${Date.now()}`,
|
|
category: 'FURNITURE',
|
|
visibility: 'TEAM',
|
|
createdBy: testEnv.users.designer.id
|
|
}
|
|
});
|
|
|
|
await prisma.product.update({
|
|
where: { id: product.id },
|
|
data: { deletedAt: new Date() }
|
|
});
|
|
|
|
const products = await prisma.product.findMany({
|
|
where: {
|
|
deletedAt: null,
|
|
id: product.id
|
|
}
|
|
});
|
|
|
|
expect(products.length).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('协作者管理', () => {
|
|
it('应该能添加协作者', async () => {
|
|
const product = await prisma.product.create({
|
|
data: {
|
|
name: '协作测试产品',
|
|
modelNumber: `TEST-${Date.now()}`,
|
|
category: 'FURNITURE',
|
|
visibility: 'TEAM',
|
|
createdBy: testEnv.users.designer.id
|
|
}
|
|
});
|
|
|
|
const collaborators = [
|
|
{
|
|
userId: testEnv.users.teamLeader.id,
|
|
role: 'editor',
|
|
addedAt: new Date().toISOString()
|
|
}
|
|
];
|
|
|
|
const updatedProduct = await prisma.product.update({
|
|
where: { id: product.id },
|
|
data: { collaborators: collaborators as any }
|
|
});
|
|
|
|
expect(updatedProduct.collaborators).toBeDefined();
|
|
expect(Array.isArray(updatedProduct.collaborators)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('SKU 交接', () => {
|
|
it('应该能交接 SKU', async () => {
|
|
const product = await prisma.product.create({
|
|
data: {
|
|
name: 'SKU交接测试',
|
|
modelNumber: `TEST-${Date.now()}`,
|
|
category: 'FURNITURE',
|
|
visibility: 'TEAM',
|
|
createdBy: testEnv.users.designer.id
|
|
}
|
|
});
|
|
|
|
const sku = await prisma.productSKU.create({
|
|
data: {
|
|
skuCode: `SKU-${Date.now()}`,
|
|
productId: product.id,
|
|
ownerId: testEnv.users.designer.id,
|
|
attributes: {},
|
|
price: 0,
|
|
costPrice: 0,
|
|
stock: 0,
|
|
transferHistory: []
|
|
}
|
|
});
|
|
|
|
const transferData = {
|
|
fromUserId: testEnv.users.designer.id,
|
|
toUserId: testEnv.users.teamLeader.id,
|
|
reason: '测试交接',
|
|
transferredAt: new Date().toISOString()
|
|
};
|
|
|
|
const updatedSKU = await prisma.productSKU.update({
|
|
where: { id: sku.id },
|
|
data: {
|
|
ownerId: transferData.toUserId,
|
|
transferHistory: [transferData] as any
|
|
}
|
|
});
|
|
|
|
expect(updatedSKU.ownerId).toBe(testEnv.users.teamLeader.id);
|
|
expect(Array.isArray(updatedSKU.transferHistory)).toBe(true);
|
|
});
|
|
});
|
|
});
|