Files
order_site/apps/backend/src/services/cost-center/cost-snapshot-service.test.ts
T

386 lines
10 KiB
TypeScript

import assert from 'node:assert/strict'
import test from 'node:test'
import {
resolveCost,
shouldSupersedeSnapshot,
resolveGenericCosts,
selectCostVersion,
type CostItemRow,
type CostVersionRow,
} from './cost-snapshot-service.js'
const costItems = new Map<string, CostItemRow>([
['sys_affiliate_purchase', { id: 3, code: 'sys_affiliate_purchase', pricing_mode: 'per_item' }],
['sys_work_order_reward', { id: 1, code: 'sys_work_order_reward', pricing_mode: 'per_order' }],
[
'sys_third_party_voucher_cost',
{ id: 4, code: 'sys_third_party_voucher_cost', pricing_mode: 'per_item' },
],
])
const pricingModes = new Map([
[1, 'per_order'],
[3, 'per_item'],
[4, 'per_item'],
])
const versions: CostVersionRow[] = [
{
id: 100,
cost_item_id: 3,
scope_type: 'product',
canonical_product_id: 11,
work_product_rule_id: null,
amount_fen: 1200,
effective_from: '2026-09-29T00:00:00+08:00',
},
{
id: 101,
cost_item_id: 1,
scope_type: 'rule',
canonical_product_id: null,
work_product_rule_id: 9,
amount_fen: 840,
effective_from: '2026-09-29T00:00:00+08:00',
},
{
id: 102,
cost_item_id: 4,
scope_type: 'product',
canonical_product_id: 33,
work_product_rule_id: null,
amount_fen: 5000,
effective_from: '2026-10-20T00:00:00+08:00',
},
{
id: 103,
cost_item_id: 4,
scope_type: 'product',
canonical_product_id: 33,
work_product_rule_id: null,
amount_fen: 6000,
effective_from: '2026-11-01T00:00:00+08:00',
},
]
function item(patch: Record<string, unknown> = {}) {
return {
id: 1,
order_id: 1,
fulfillment_channel: 'affiliate_dash',
canonical_product_id: 11,
quantity: 2,
basis_at: '2026-10-31T10:00:00+08:00',
work_order_id: null as number | null,
work_product_rule_id: null as number | null,
work_order_status: null as string | null,
reward_amount: null as number | null,
revenue_fen: 10000,
refund_fen: 0,
basis_date: '2026-10-31',
...patch,
}
}
test('成本解析:自动发货按件计,单价乘数量', () => {
const resolution = resolveCost(item(), costItems, versions, pricingModes)
assert.ok(resolution)
assert.equal(resolution.costVersionId, 100)
assert.equal(resolution.totalAmountFen, 2400)
assert.equal(resolution.stage, 'estimated')
})
test('成本解析:卡券渠道按件计,取业务日期前的最新版本', () => {
const resolution = resolveCost(
item({ fulfillment_channel: 'third_party_voucher', canonical_product_id: 33, quantity: 1 }),
costItems,
versions,
pricingModes,
)
assert.ok(resolution)
assert.equal(resolution.costVersionId, 102)
assert.equal(resolution.unitAmountFen, 5000)
})
test('成本解析:接单模板按 SKU 计件时,预估成本按数量乘每件金额', () => {
const resolution = resolveCost(
item({
fulfillment_channel: 'work_order',
canonical_product_id: null,
quantity: 2,
work_product_rule_id: 9,
work_product_rule_pricing_mode: 'unit',
}),
costItems,
versions,
pricingModes,
)
assert.ok(resolution)
assert.equal(resolution.costVersionId, 101)
assert.equal(resolution.unitAmountFen, 840)
assert.equal(resolution.totalAmountFen, 1680)
})
test('成本解析:生效日期晚于业务日期的版本不参与', () => {
const version = selectCostVersion(versions, {
costItemId: 4,
productId: 33,
ruleId: null,
basisAt: '2026-10-31T10:00:00+08:00',
})
assert.equal(version?.id, 102)
const future = selectCostVersion(versions, {
costItemId: 4,
productId: 33,
ruleId: null,
basisAt: '2026-11-02T10:00:00+08:00',
})
assert.equal(future?.id, 103)
})
test('成本解析:接单平台优先按规则版本', () => {
const resolution = resolveCost(
item({
fulfillment_channel: 'work_order',
canonical_product_id: 55,
work_product_rule_id: 9,
quantity: 1,
}),
costItems,
versions,
pricingModes,
)
assert.ok(resolution)
assert.equal(resolution.costVersionId, 101)
assert.equal(resolution.totalAmountFen, 840)
})
test('成本解析:工单已验收时用实际奖励金额,阶段为 actual', () => {
const resolution = resolveCost(
item({
fulfillment_channel: 'work_order',
canonical_product_id: 55,
work_product_rule_id: 9,
work_order_id: 77,
work_order_status: 'accepted',
reward_amount: 3300,
quantity: 1,
}),
costItems,
versions,
pricingModes,
)
assert.ok(resolution)
assert.equal(resolution.totalAmountFen, 3300)
assert.equal(resolution.stage, 'actual')
})
test('成本解析:没有可用版本时返回 null(进成本缺失队列,不记 0)', () => {
const resolution = resolveCost(
item({ canonical_product_id: 999 }),
costItems,
versions,
pricingModes,
)
assert.equal(resolution, null)
})
test('成本解析:人工渠道不在这里算成本', () => {
const resolution = resolveCost(
item({ fulfillment_channel: 'manual' }),
costItems,
versions,
pricingModes,
)
assert.equal(resolution, null)
})
test('全局 0 元占位版本不能当有效成本', () => {
const zeroVersions: CostVersionRow[] = [
{
id: 900,
cost_item_id: 1,
scope_type: 'global',
canonical_product_id: null,
work_product_rule_id: null,
amount_fen: 0,
rate_bps: 0,
effective_from: '2026-09-01T00:00:00+08:00',
},
]
assert.equal(
resolveCost(
item({ fulfillment_channel: 'work_order', canonical_product_id: null }),
costItems,
zeroVersions,
pricingModes,
),
null,
)
})
test('比例成本按费率计算,费率为 0 视为未配置', () => {
const percentItems = new Map<string, CostItemRow>([
[
'sys_work_order_reward',
{
id: 1,
code: 'sys_work_order_reward',
pricing_mode: 'percent_of_revenue',
cost_model: 'work_order_reward',
},
],
])
const percentVersions: CostVersionRow[] = [
{
id: 901,
cost_item_id: 1,
scope_type: 'rule',
canonical_product_id: null,
work_product_rule_id: 9,
amount_fen: 0,
rate_bps: 500,
effective_from: '2026-09-01T00:00:00+08:00',
},
]
const resolution = resolveCost(
item({ fulfillment_channel: 'work_order', work_product_rule_id: 9, revenue_fen: 10000 }),
percentItems,
percentVersions,
new Map([[1, 'percent_of_revenue']]),
)
assert.equal(resolution?.totalAmountFen, 500)
const zeroRate = percentVersions.map((row) => ({ ...row, rate_bps: 0 }))
assert.equal(
resolveCost(
item({ fulfillment_channel: 'work_order', work_product_rule_id: 9 }),
percentItems,
zeroRate,
new Map([[1, 'percent_of_revenue']]),
),
null,
)
})
test('通用成本模型按商品/全局版本解析且能与渠道成本共存', () => {
const genericItems: CostItemRow[] = [
{
id: 20,
code: 'sys_platform_fee',
pricing_mode: 'percent_of_revenue',
cost_model: 'platform_fee',
},
]
const versionsWithGeneric: CostVersionRow[] = [
{
id: 902,
cost_item_id: 20,
scope_type: 'global',
canonical_product_id: null,
work_product_rule_id: null,
amount_fen: 0,
rate_bps: 300,
effective_from: '2026-09-01T00:00:00+08:00',
},
]
const generic = resolveGenericCosts(
item({ revenue_fen: 20000 }),
genericItems,
versionsWithGeneric,
new Map([[20, 'percent_of_revenue']]),
)
assert.equal(generic.length, 1)
assert.equal(generic[0]?.totalAmountFen, 600)
})
test('售后损失/其他成本只有商品级版本才自动入账', () => {
const items: CostItemRow[] = [
{
id: 30,
code: 'sys_after_sales_loss',
pricing_mode: 'per_order',
cost_model: 'after_sales_loss',
},
]
const globalVersion: CostVersionRow[] = [
{
id: 903,
cost_item_id: 30,
scope_type: 'global',
canonical_product_id: null,
work_product_rule_id: null,
amount_fen: 500,
rate_bps: 0,
effective_from: '2026-09-01T00:00:00+08:00',
},
]
assert.equal(
resolveGenericCosts(item(), items, globalVersion, new Map([[30, 'per_order']])).length,
0,
'全局售后损失不能套到所有订单',
)
const productVersion: CostVersionRow[] = globalVersion.map((row) => ({
...row,
scope_type: 'product',
canonical_product_id: 11,
}))
assert.equal(
resolveGenericCosts(item(), items, productVersion, new Map([[30, 'per_order']])).length,
1,
'商品级售后损失可以入账',
)
})
test('比例费用按退款后的净收入计算', () => {
const feeItems = new Map<string, CostItemRow>([
[
'sys_platform_fee',
{
id: 20,
code: 'sys_platform_fee',
pricing_mode: 'percent_of_revenue',
cost_model: 'platform_fee',
},
],
])
const feeVersions: CostVersionRow[] = [
{
id: 904,
cost_item_id: 20,
scope_type: 'global',
canonical_product_id: null,
work_product_rule_id: null,
amount_fen: 0,
rate_bps: 300,
effective_from: '2026-09-01T00:00:00+08:00',
},
]
const noRefund = resolveGenericCosts(
item({ revenue_fen: 10000, refund_fen: 0 }),
[...feeItems.values()],
feeVersions,
new Map([[20, 'percent_of_revenue']]),
)
const withRefund = resolveGenericCosts(
item({ revenue_fen: 10000, refund_fen: 4000 }),
[...feeItems.values()],
feeVersions,
new Map([[20, 'percent_of_revenue']]),
)
assert.equal(noRefund[0]?.totalAmountFen, 300)
assert.equal(withRefund[0]?.totalAmountFen, 180)
})
test('历史订单成本固化:只有预估→实际与派生费用允许替换', () => {
assert.equal(shouldSupersedeSnapshot(null, 'estimated'), true)
assert.equal(shouldSupersedeSnapshot('estimated', 'estimated'), false)
assert.equal(shouldSupersedeSnapshot('estimated', 'estimated', true), true)
assert.equal(shouldSupersedeSnapshot('estimated', 'actual'), true)
assert.equal(shouldSupersedeSnapshot('actual', 'estimated'), false)
assert.equal(shouldSupersedeSnapshot('actual', 'actual'), false)
})