0.0.8.5
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
/**
|
||||
* 测试成员历史记录功能
|
||||
*
|
||||
* 测试场景:
|
||||
* 1. 创建成员时记录历史
|
||||
* 2. 更新成员时记录历史
|
||||
* 3. 验证历史记录的完整性
|
||||
*/
|
||||
|
||||
import { prisma } from '../lib/prisma'
|
||||
import { getMemberHistory } from '../lib/member-history'
|
||||
|
||||
async function testMemberHistory() {
|
||||
console.log('🧪 开始测试成员历史记录功能...\n')
|
||||
|
||||
try {
|
||||
// 1. 查找测试用的家族树和用户
|
||||
const tree = await prisma.familyTree.findFirst({
|
||||
include: {
|
||||
collaborators: {
|
||||
include: {
|
||||
user: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (!tree) {
|
||||
console.error('❌ 未找到测试用的家族树')
|
||||
return
|
||||
}
|
||||
|
||||
// 使用树的所有者ID或协作者ID
|
||||
const userId = tree.ownerId || tree.collaborators[0]?.userId
|
||||
if (!userId) {
|
||||
console.error('❌ 未找到测试用户')
|
||||
return
|
||||
}
|
||||
|
||||
console.log('✅ 找到测试家族树:', tree.name)
|
||||
console.log('✅ 测试用户ID:', userId)
|
||||
console.log()
|
||||
|
||||
// 2. 创建测试成员
|
||||
console.log('📝 测试场景 1: 创建成员时记录历史')
|
||||
const testMember = await prisma.familyMember.create({
|
||||
data: {
|
||||
treeId: tree.id,
|
||||
surname: '测试',
|
||||
givenName: '成员',
|
||||
fullName: '测试成员',
|
||||
gender: 'MALE',
|
||||
generation: 1,
|
||||
birthDate: '1990-01-01',
|
||||
phone: '13800138000',
|
||||
email: 'test@example.com',
|
||||
}
|
||||
})
|
||||
console.log('✅ 创建成员:', testMember.fullName, `(ID: ${testMember.id})`)
|
||||
|
||||
// 模拟 API 调用:记录创建历史
|
||||
const { recordMemberHistory } = await import('../lib/member-history')
|
||||
await recordMemberHistory({
|
||||
memberId: testMember.id,
|
||||
treeId: tree.id,
|
||||
changedBy: userId,
|
||||
changeType: 'CREATE',
|
||||
newData: testMember
|
||||
})
|
||||
console.log('✅ 记录创建历史')
|
||||
|
||||
// 验证创建历史
|
||||
let history = await getMemberHistory(testMember.id)
|
||||
console.log(`✅ 历史记录数量: ${history.length}`)
|
||||
console.log(`✅ 版本号: ${history[0].version}`)
|
||||
console.log(`✅ 变更类型: ${history[0].changeType}`)
|
||||
console.log()
|
||||
|
||||
// 3. 更新成员信息
|
||||
console.log('📝 测试场景 2: 更新成员时记录历史')
|
||||
const oldData = { ...testMember }
|
||||
|
||||
const updatedMember = await prisma.familyMember.update({
|
||||
where: { id: testMember.id },
|
||||
data: {
|
||||
fullName: '测试成员(已更新)',
|
||||
birthDate: '1990-01-02',
|
||||
phone: '13900139000',
|
||||
bio: '这是一段个人简介',
|
||||
}
|
||||
})
|
||||
console.log('✅ 更新成员信息')
|
||||
|
||||
// 记录更新历史
|
||||
await recordMemberHistory({
|
||||
memberId: testMember.id,
|
||||
treeId: tree.id,
|
||||
changedBy: userId,
|
||||
changeType: 'UPDATE',
|
||||
oldData: oldData,
|
||||
newData: updatedMember
|
||||
})
|
||||
console.log('✅ 记录更新历史')
|
||||
|
||||
// 验证更新历史
|
||||
history = await getMemberHistory(testMember.id)
|
||||
console.log(`✅ 历史记录数量: ${history.length}`)
|
||||
console.log(`✅ 最新版本号: ${history[0].version}`)
|
||||
console.log(`✅ 变更类型: ${history[0].changeType}`)
|
||||
|
||||
if (history[0].changes) {
|
||||
console.log('✅ 变更字段:')
|
||||
const changes = history[0].changes as Record<string, { old: any, new: any }>
|
||||
for (const [field, change] of Object.entries(changes)) {
|
||||
console.log(` - ${field}: "${change.old}" → "${change.new}"`)
|
||||
}
|
||||
}
|
||||
console.log()
|
||||
|
||||
// 4. 再次更新成员(只修改部分字段)
|
||||
console.log('📝 测试场景 3: 部分字段更新')
|
||||
const oldData2 = { ...updatedMember }
|
||||
|
||||
const updatedMember2 = await prisma.familyMember.update({
|
||||
where: { id: testMember.id },
|
||||
data: {
|
||||
email: 'newemail@example.com',
|
||||
address: '北京市朝阳区',
|
||||
}
|
||||
})
|
||||
console.log('✅ 更新成员信息(部分字段)')
|
||||
|
||||
// 记录更新历史
|
||||
await recordMemberHistory({
|
||||
memberId: testMember.id,
|
||||
treeId: tree.id,
|
||||
changedBy: userId,
|
||||
changeType: 'UPDATE',
|
||||
oldData: oldData2,
|
||||
newData: updatedMember2
|
||||
})
|
||||
console.log('✅ 记录更新历史')
|
||||
|
||||
// 验证更新历史
|
||||
history = await getMemberHistory(testMember.id)
|
||||
console.log(`✅ 历史记录数量: ${history.length}`)
|
||||
console.log(`✅ 最新版本号: ${history[0].version}`)
|
||||
|
||||
if (history[0].changes) {
|
||||
console.log('✅ 变更字段:')
|
||||
const changes = history[0].changes as Record<string, { old: any, new: any }>
|
||||
for (const [field, change] of Object.entries(changes)) {
|
||||
console.log(` - ${field}: "${change.old}" → "${change.new}"`)
|
||||
}
|
||||
}
|
||||
console.log()
|
||||
|
||||
// 5. 显示完整历史记录
|
||||
console.log('📋 完整历史记录:')
|
||||
console.log('─'.repeat(80))
|
||||
for (const record of history.reverse()) {
|
||||
console.log(`版本 ${record.version} | ${record.changeType} | ${record.changedAt.toLocaleString('zh-CN')}`)
|
||||
if (record.changes) {
|
||||
const changes = record.changes as Record<string, { old: any, new: any }>
|
||||
for (const [field, change] of Object.entries(changes)) {
|
||||
console.log(` ${field}: "${change.old}" → "${change.new}"`)
|
||||
}
|
||||
}
|
||||
console.log('─'.repeat(80))
|
||||
}
|
||||
console.log()
|
||||
|
||||
// 6. 清理测试数据
|
||||
console.log('🧹 清理测试数据...')
|
||||
await prisma.memberHistory.deleteMany({
|
||||
where: { memberId: testMember.id }
|
||||
})
|
||||
await prisma.familyMember.delete({
|
||||
where: { id: testMember.id }
|
||||
})
|
||||
console.log('✅ 清理完成')
|
||||
console.log()
|
||||
|
||||
console.log('✅ 所有测试通过!')
|
||||
console.log()
|
||||
console.log('📊 测试总结:')
|
||||
console.log(' ✓ 创建成员时正确记录历史')
|
||||
console.log(' ✓ 更新成员时正确记录历史')
|
||||
console.log(' ✓ 只记录实际变更的字段')
|
||||
console.log(' ✓ 版本号正确递增')
|
||||
console.log(' ✓ 历史记录完整可追溯')
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 测试失败:', error)
|
||||
throw error
|
||||
} finally {
|
||||
await prisma.$disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
// 运行测试
|
||||
testMemberHistory()
|
||||
.catch((error) => {
|
||||
console.error('测试执行失败:', error)
|
||||
process.exit(1)
|
||||
})
|
||||
Reference in New Issue
Block a user