GuDingZiChan/backend/src/utils/__tests__/assetValue.recursion.test.ts

77 lines
2.0 KiB
TypeScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { refreshParentTotalValueWithChildren, syncMountedChildrenState } from '../assetValue'
test('syncMountedChildrenState 发现父子环时在写入前中止', async () => {
let updateCount = 0
const client = {
equipment: {
findMany: async (args: any) => {
const parentId = args.where.parent_id.in[0]
if (parentId === 1) {
return [{ id: 2, unit_price: 100, total_quantity: 1 }]
}
return [{ id: 1, unit_price: 1000, total_quantity: 1 }]
},
update: async () => {
updateCount += 1
}
}
}
await assert.rejects(
syncMountedChildrenState(client as any, 1, '待报废'),
/循环父子关系/
)
assert.equal(updateCount, 0)
})
test('syncMountedChildrenState 超过 100 层时在写入前中止', async () => {
let findCount = 0
let updateCount = 0
const client = {
equipment: {
findMany: async (args: any) => {
findCount += 1
const parentId = args.where.parent_id.in[0]
return [{ id: parentId + 1, unit_price: 10, total_quantity: 1 }]
},
update: async () => {
updateCount += 1
}
}
}
await assert.rejects(
syncMountedChildrenState(client as any, 1, '维护中'),
/超过 100 层/
)
assert.equal(findCount, 101)
assert.equal(updateCount, 0)
})
test('挂载零价配件时仍显式更新父节点以刷新 updated_at', async () => {
const updates: any[] = []
const client = {
equipment: {
findUnique: async () => ({
id: 1,
unit_price: 1000,
total_value_with_children: 1000,
children: [{ unit_price: 0 }]
}),
update: async (args: any) => {
updates.push(args)
return { id: 1 }
}
}
}
await refreshParentTotalValueWithChildren(client as any, 1)
assert.equal(updates.length, 1)
assert.equal(updates[0].where.id, 1)
assert.equal(updates[0].data.total_value_with_children, 1000)
})