Files
order_site/apps/backend/src/services/fulfillment/intake-channel-decision-service.test.ts
T

155 lines
4.7 KiB
TypeScript

import assert from 'node:assert/strict'
import test from 'node:test'
import { applyIntakeChannelPlan } from './intake-channel-decision-service.js'
import type {
InternalSkuChannelPlan,
InternalSkuChannelRow,
} from './internal-sku-channel-service.js'
import type { ResolvedFulfillmentItem } from './product-resolution-service.js'
function buildItem(): ResolvedFulfillmentItem {
return {
itemId: '1',
externalItemId: '1',
externalSkuCode: 'platform-code',
externalSkuName: '幸运币2个',
skuCode: 'platform-code',
skuName: '幸运币2个',
spec: { skuNick: '幸运币2个' },
snapshot: { internalSku: '幸运币2个' },
isConfigured: false,
}
}
function channel(patch: Partial<InternalSkuChannelRow> = {}): InternalSkuChannelRow {
return {
id: 1,
internal_sku: '幸运币2个',
channel: 'affiliate_dash',
priority: 10,
is_fallback: false,
enabled: true,
canonical_product_id: null,
config_json: {},
source: 'operator',
note: '',
created_by_admin_user_id: null,
...patch,
}
}
function plan(primary: InternalSkuChannelRow | null): InternalSkuChannelPlan {
return {
primary,
fallbacks: [],
attempts: primary ? [primary] : [],
conflicts: [],
}
}
test('下单决策:自动发货只从统一 SKU 配置生成任务路由', () => {
const item = applyIntakeChannelPlan(
buildItem(),
plan(channel({ config_json: { affiliateSku: 'lucky_coin_x2' } })),
)
assert.equal(item.isConfigured, true)
assert.equal(item.snapshot.isConfigured, true)
assert.equal(item.skuCode, 'lucky_coin_x2')
assert.equal(item.snapshot.internalSkuChannel, 'affiliate_dash')
assert.deepEqual(item.snapshot.fulfillmentRoute, {
selectedExecutorKey: 'affiliate_dash',
selectedRuleId: '',
reason: '内部 SKU 渠道配置指定自动发货',
matchedRule: null,
candidates: [],
skipped: [],
})
assert.deepEqual(item.snapshot.affiliateDash, {
matchMode: 'affiliate_dash_sku_mapping',
productNo: '幸运币2个',
sku: 'lucky_coin_x2',
skuName: '幸运币2个',
})
})
test('下单决策:自动发货缺载体参数时不建任何任务', () => {
const item = applyIntakeChannelPlan(
{
...buildItem(),
skuName: '不存在商品',
snapshot: { internalSku: '不存在商品' },
spec: { skuNick: '不存在商品' },
},
plan(channel({ internal_sku: '不存在商品' })),
)
assert.equal(item.isConfigured, false)
assert.equal(item.snapshot.isConfigured, false)
assert.equal(item.snapshot.internalSkuChannelReason, 'missing_affiliate_dash_mapping')
})
test('下单决策:人工渠道只有配置存在时才建人工任务', () => {
const ready = applyIntakeChannelPlan(
buildItem(),
plan(channel({ channel: 'manual', config_json: { manualConfigId: 9 } })),
)
assert.equal(ready.isConfigured, true)
assert.equal(ready.snapshot.isConfigured, true)
assert.equal(
(ready.snapshot.fulfillmentRoute as Record<string, unknown>).selectedExecutorKey,
'manual_dispatch',
)
const missing = applyIntakeChannelPlan(
buildItem(),
plan(channel({ channel: 'manual', config_json: {} })),
)
assert.equal(missing.isConfigured, false)
assert.equal(missing.snapshot.isConfigured, false)
assert.equal(missing.snapshot.internalSkuChannelReason, 'manual_config_missing')
})
test('下单决策:接单平台和卡券不提前建任务', () => {
for (const targetChannel of ['work_order', 'third_party_voucher'] as const) {
const item = applyIntakeChannelPlan(
buildItem(),
plan(channel({ channel: targetChannel, config_json: {} })),
)
assert.equal(item.isConfigured, false)
assert.equal(item.snapshot.isConfigured, false)
assert.equal(item.snapshot.internalSkuChannel, targetChannel)
assert.equal(
(item.snapshot.fulfillmentRoute as Record<string, unknown>).selectedExecutorKey,
'',
)
assert.deepEqual(item.snapshot.classification, {
channel: targetChannel,
canonicalSku: '幸运币2个',
})
}
})
test('下单决策:冲突或无配置时清空旧路由,不能由旧逻辑抢占', () => {
const base = buildItem()
base.snapshot = {
...base.snapshot,
fulfillmentRoute: { selectedExecutorKey: 'affiliate_dash' },
affiliateDash: { sku: 'legacy' },
}
const conflict = applyIntakeChannelPlan(base, {
primary: null,
fallbacks: [],
attempts: [],
conflicts: [channel()],
})
assert.equal(conflict.isConfigured, false)
assert.equal(conflict.snapshot.isConfigured, false)
assert.equal(conflict.snapshot.internalSkuChannelReason, 'routing_conflict')
assert.equal(
(conflict.snapshot.fulfillmentRoute as Record<string, unknown>).selectedExecutorKey,
'',
)
assert.equal(conflict.snapshot.affiliateDash, null)
})