- 文件上传验证: ModelUploader组件增强、上传API错误处理 - GLTF解析器: 增强节点提取和元数据 - 板材编辑: PanelEditor客户端验证 + panels API服务端验证 - 装配步骤编辑: 修复未使用代码,添加预览按钮 - 装配动画播放: AssemblyPlayer步骤导航边界检查优化 - 分享链接: 完整CRUD API(创建、获取、删除),含验证和过期检查 跳过可选测试任务以加快MVP开发
274 lines
5.8 KiB
TypeScript
274 lines
5.8 KiB
TypeScript
/**
|
|
* 测试工具函数
|
|
* 提供测试中常用的辅助函数
|
|
*/
|
|
|
|
import { PrismaClient } from '@prisma/client';
|
|
import { hash } from 'bcryptjs';
|
|
import jwt from 'jsonwebtoken';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
/**
|
|
* 清理测试数据库
|
|
*/
|
|
export async function cleanDatabase() {
|
|
// 按照依赖关系的逆序删除
|
|
await prisma.activityLog.deleteMany();
|
|
await prisma.feedback.deleteMany();
|
|
await prisma.productSKU.deleteMany();
|
|
await prisma.product.deleteMany();
|
|
await prisma.user.deleteMany();
|
|
await prisma.team.deleteMany();
|
|
await prisma.department.deleteMany();
|
|
}
|
|
|
|
/**
|
|
* 创建测试用户
|
|
*/
|
|
export async function createTestUser(data: {
|
|
email: string;
|
|
name: string;
|
|
password?: string;
|
|
role?: any;
|
|
departmentId?: string;
|
|
teamId?: string;
|
|
isFirstLogin?: boolean;
|
|
}) {
|
|
const hashedPassword = await hash(data.password || 'Test123456!', 10);
|
|
|
|
return await prisma.user.create({
|
|
data: {
|
|
email: data.email,
|
|
name: data.name,
|
|
password: hashedPassword,
|
|
role: data.role || 'DESIGNER',
|
|
departmentId: data.departmentId,
|
|
teamId: data.teamId,
|
|
isFirstLogin: data.isFirstLogin ?? false,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 创建测试部门
|
|
*/
|
|
export async function createTestDepartment(data: {
|
|
name: string;
|
|
description?: string;
|
|
parentId?: string;
|
|
}) {
|
|
const createData: any = {
|
|
name: data.name,
|
|
fullPath: data.name, // 简化处理,直接使用名称作为路径
|
|
};
|
|
if (data.parentId) {
|
|
createData.parentId = data.parentId;
|
|
}
|
|
return await prisma.department.create({
|
|
data: createData,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 创建测试小组
|
|
*/
|
|
export async function createTestTeam(data: {
|
|
name: string;
|
|
departmentId: string;
|
|
leaderId?: string;
|
|
}) {
|
|
return await prisma.team.create({
|
|
data: {
|
|
name: data.name,
|
|
departmentId: data.departmentId,
|
|
leaderId: data.leaderId,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 创建测试产品
|
|
*/
|
|
export async function createTestProduct(data: {
|
|
name: string;
|
|
category: string;
|
|
createdBy: string;
|
|
visibility?: string;
|
|
collaborators?: any;
|
|
}) {
|
|
return await prisma.product.create({
|
|
data: {
|
|
name: data.name,
|
|
modelNumber: `TEST-${Date.now()}`,
|
|
category: data.category,
|
|
createdBy: data.createdBy,
|
|
visibility: data.visibility || 'TEAM',
|
|
collaborators: data.collaborators || [],
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 创建测试 SKU
|
|
*/
|
|
export async function createTestSKU(data: {
|
|
name: string;
|
|
productId: string;
|
|
ownerId: string;
|
|
specifications?: any;
|
|
}) {
|
|
return await prisma.productSKU.create({
|
|
data: {
|
|
skuCode: `SKU-${Date.now()}`,
|
|
productId: data.productId,
|
|
ownerId: data.ownerId,
|
|
attributes: data.specifications || {},
|
|
price: 0,
|
|
costPrice: 0,
|
|
stock: 0,
|
|
transferHistory: [],
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 生成测试 Token
|
|
*/
|
|
export function generateTestToken(userId: string, role: string = 'DESIGNER') {
|
|
const secret = process.env.JWT_SECRET || 'test-secret';
|
|
return jwt.sign(
|
|
{ userId, role },
|
|
secret,
|
|
{ expiresIn: '1h' }
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 创建完整的测试环境
|
|
* 包括部门、小组、用户、产品等
|
|
*/
|
|
export async function setupTestEnvironment() {
|
|
// 使用时间戳避免名称冲突
|
|
const timestamp = Date.now();
|
|
|
|
// 创建部门
|
|
const department = await createTestDepartment({
|
|
name: `测试部门-${timestamp}`,
|
|
description: '用于测试的部门',
|
|
});
|
|
|
|
// 创建小组
|
|
const team = await createTestTeam({
|
|
name: `测试小组-${timestamp}`,
|
|
departmentId: department.id,
|
|
});
|
|
|
|
// 创建超级管理员
|
|
const superAdmin = await createTestUser({
|
|
email: `superadmin-${timestamp}@test.com`,
|
|
name: `超级管理员-${timestamp}`,
|
|
password: 'Test123456!',
|
|
role: 'SUPER_ADMIN',
|
|
});
|
|
|
|
// 创建管理员
|
|
const admin = await createTestUser({
|
|
email: `admin-${timestamp}@test.com`,
|
|
name: `管理员-${timestamp}`,
|
|
password: 'Test123456!',
|
|
role: 'ADMIN',
|
|
departmentId: department.id,
|
|
});
|
|
|
|
// 创建部门管理员
|
|
const deptManager = await createTestUser({
|
|
email: `deptmanager-${timestamp}@test.com`,
|
|
name: `部门管理员-${timestamp}`,
|
|
password: 'Test123456!',
|
|
role: 'DEPARTMENT_MANAGER',
|
|
departmentId: department.id,
|
|
});
|
|
|
|
// 创建组长
|
|
const teamLeader = await createTestUser({
|
|
email: `teamleader-${timestamp}@test.com`,
|
|
name: `组长-${timestamp}`,
|
|
password: 'Test123456!',
|
|
role: 'TEAM_LEADER',
|
|
departmentId: department.id,
|
|
teamId: team.id,
|
|
});
|
|
|
|
// 更新小组的组长
|
|
await prisma.team.update({
|
|
where: { id: team.id },
|
|
data: { leaderId: teamLeader.id },
|
|
});
|
|
|
|
// 创建设计师
|
|
const designer = await createTestUser({
|
|
email: `designer-${timestamp}@test.com`,
|
|
name: `设计师-${timestamp}`,
|
|
password: 'Test123456!',
|
|
role: 'DESIGNER',
|
|
departmentId: department.id,
|
|
teamId: team.id,
|
|
});
|
|
|
|
// 创建查看者
|
|
const viewer = await createTestUser({
|
|
email: `viewer-${timestamp}@test.com`,
|
|
name: `查看者-${timestamp}`,
|
|
password: 'Test123456!',
|
|
role: 'VIEWER',
|
|
departmentId: department.id,
|
|
teamId: team.id,
|
|
});
|
|
|
|
// 创建测试产品
|
|
const product = await createTestProduct({
|
|
name: `测试产品-${timestamp}`,
|
|
category: 'FURNITURE',
|
|
createdBy: designer.id,
|
|
visibility: 'TEAM',
|
|
});
|
|
|
|
// 创建测试 SKU
|
|
const sku = await createTestSKU({
|
|
name: `测试SKU-${timestamp}`,
|
|
productId: product.id,
|
|
ownerId: designer.id,
|
|
specifications: { color: '红色', size: '三人座' },
|
|
});
|
|
|
|
return {
|
|
department,
|
|
team,
|
|
users: {
|
|
superAdmin,
|
|
admin,
|
|
deptManager,
|
|
teamLeader,
|
|
designer,
|
|
viewer,
|
|
},
|
|
product,
|
|
sku,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 清理测试数据
|
|
*/
|
|
export async function cleanupTestData() {
|
|
await cleanDatabase();
|
|
}
|
|
|
|
/**
|
|
* 关闭数据库连接
|
|
*/
|
|
export async function disconnectDatabase() {
|
|
await prisma.$disconnect();
|
|
}
|