feat(fulfillment): 统一中文内部 SKU 履约路由
This commit is contained in:
@@ -1,11 +1,7 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import {
|
||||
resolveChannelDecision,
|
||||
type ChannelCandidateItem,
|
||||
type ChannelRule,
|
||||
} from './channel-assign-service.js'
|
||||
import { resolveChannelDecision, type ChannelCandidateItem } from './channel-assign-service.js'
|
||||
import type { InternalSkuChannelRow } from './internal-sku-channel-service.js'
|
||||
|
||||
function buildItem(patch: Partial<ChannelCandidateItem> = {}): ChannelCandidateItem {
|
||||
@@ -36,7 +32,7 @@ function buildChannel(patch: Partial<InternalSkuChannelRow> = {}): InternalSkuCh
|
||||
is_fallback: false,
|
||||
enabled: true,
|
||||
canonical_product_id: null,
|
||||
config_json: {},
|
||||
config_json: { manualConfigId: 1 },
|
||||
source: 'test',
|
||||
note: '',
|
||||
created_by_admin_user_id: null,
|
||||
@@ -44,151 +40,79 @@ function buildChannel(patch: Partial<InternalSkuChannelRow> = {}): InternalSkuCh
|
||||
}
|
||||
}
|
||||
|
||||
function buildRule(patch: Partial<ChannelRule> = {}): ChannelRule {
|
||||
return {
|
||||
id: 1,
|
||||
provider: '',
|
||||
shop_id: '',
|
||||
match_key_type: 'sku_code',
|
||||
match_key: 'suit_xxx',
|
||||
channel: 'affiliate_dash',
|
||||
priority: 60,
|
||||
enabled: true,
|
||||
...patch,
|
||||
}
|
||||
}
|
||||
|
||||
test('渠道判定:真实载体优先于冲突的旧规则', () => {
|
||||
const decision = resolveChannelDecision(buildItem({ has_work_order: true }), [
|
||||
buildRule({ channel: 'third_party_voucher', priority: 10 }),
|
||||
])
|
||||
|
||||
test('渠道判定:真实载体优先于统一渠道配置', () => {
|
||||
const decision = resolveChannelDecision(
|
||||
buildItem({
|
||||
has_work_order: true,
|
||||
channels: [buildChannel({ channel: 'third_party_voucher' })],
|
||||
}),
|
||||
)
|
||||
assert.equal(decision.channel, 'work_order')
|
||||
assert.equal(decision.source, 'carrier_evidence')
|
||||
assert.equal(decision.conflict, false)
|
||||
})
|
||||
|
||||
test('渠道判定:无规则时按载体证据判定', () => {
|
||||
const workOrder = resolveChannelDecision(buildItem({ has_work_order: true }), [])
|
||||
test('渠道判定:无配置时按真实载体判定', () => {
|
||||
const workOrder = resolveChannelDecision(buildItem({ has_work_order: true }))
|
||||
assert.equal(workOrder.channel, 'work_order')
|
||||
assert.equal(workOrder.source, 'carrier_evidence')
|
||||
|
||||
const affiliate = resolveChannelDecision(buildItem({ has_affiliate_task: true }), [])
|
||||
const affiliate = resolveChannelDecision(buildItem({ has_affiliate_task: true }))
|
||||
assert.equal(affiliate.channel, 'affiliate_dash')
|
||||
assert.equal(affiliate.source, 'carrier_evidence')
|
||||
|
||||
const manual = resolveChannelDecision(buildItem({ has_manual_task: true }))
|
||||
assert.equal(manual.channel, 'manual')
|
||||
})
|
||||
|
||||
test('渠道判定:既无规则也无载体时保持 pending,不兜底人工', () => {
|
||||
const decision = resolveChannelDecision(buildItem(), [])
|
||||
test('渠道判定:无配置、无载体时保持 pending,不再看平台 SKU 规则', () => {
|
||||
const decision = resolveChannelDecision(buildItem())
|
||||
assert.equal(decision.channel, null)
|
||||
assert.equal(decision.conflict, false)
|
||||
assert.equal(decision.pendingReason, 'no_channel_capability')
|
||||
})
|
||||
|
||||
test('渠道判定:同优先级多渠道判为冲突,不静默选择', () => {
|
||||
const decision = resolveChannelDecision(buildItem(), [
|
||||
buildRule({ id: 1, channel: 'affiliate_dash', priority: 10 }),
|
||||
buildRule({ id: 2, channel: 'manual', priority: 10 }),
|
||||
])
|
||||
|
||||
assert.equal(decision.channel, null)
|
||||
assert.equal(decision.conflict, true)
|
||||
assert.deepEqual(decision.conflictChannels.sort(), ['affiliate_dash', 'manual'])
|
||||
})
|
||||
|
||||
test('渠道判定:不同优先级时取优先级更小的', () => {
|
||||
const decision = resolveChannelDecision(buildItem(), [
|
||||
buildRule({ id: 1, channel: 'third_party_voucher', priority: 60 }),
|
||||
buildRule({ id: 2, channel: 'manual', priority: 70 }),
|
||||
])
|
||||
|
||||
assert.equal(decision.channel, 'third_party_voucher')
|
||||
})
|
||||
|
||||
test('渠道判定:provider / shop 限定生效,空串表示不限', () => {
|
||||
const scoped = buildRule({ provider: 'pdd', shop_id: 'shop-b' })
|
||||
assert.equal(resolveChannelDecision(buildItem(), [scoped]).channel, null)
|
||||
|
||||
const wide = buildRule({ provider: '', shop_id: '', channel: 'third_party_voucher' })
|
||||
assert.equal(resolveChannelDecision(buildItem(), [wide]).channel, 'third_party_voucher')
|
||||
})
|
||||
|
||||
test('渠道判定:内部 SKU 与 SKU 名称两种键都能命中', () => {
|
||||
const byInternal = buildRule({
|
||||
match_key_type: 'internal_sku',
|
||||
match_key: 'nick-001',
|
||||
channel: 'manual',
|
||||
})
|
||||
assert.equal(
|
||||
resolveChannelDecision(buildItem({ manual_sku_key: 'nick-001', internal_sku: 'nick-001' }), [
|
||||
byInternal,
|
||||
]).channel,
|
||||
'manual',
|
||||
)
|
||||
|
||||
const byName = buildRule({
|
||||
match_key_type: 'sku_name',
|
||||
match_key: '套装-测试',
|
||||
channel: 'third_party_voucher',
|
||||
})
|
||||
assert.equal(
|
||||
resolveChannelDecision(buildItem({ normalized_sku_name: '套装-测试' }), [byName]).channel,
|
||||
'third_party_voucher',
|
||||
)
|
||||
})
|
||||
|
||||
test('渠道判定:停用的规则不参与', () => {
|
||||
const decision = resolveChannelDecision(buildItem(), [buildRule({ enabled: false })])
|
||||
assert.equal(decision.channel, null)
|
||||
})
|
||||
|
||||
test('渠道判定:卡券/人工能力可兜底,自动发货/接单必须靠载体', () => {
|
||||
const voucher = resolveChannelDecision(
|
||||
buildItem({ channels: [buildChannel({ channel: 'third_party_voucher' })] }),
|
||||
[],
|
||||
)
|
||||
assert.equal(voucher.channel, 'third_party_voucher')
|
||||
assert.equal(voucher.source, 'auto_mapping')
|
||||
|
||||
const manual = resolveChannelDecision(buildItem({ channels: [buildChannel()] }), [])
|
||||
assert.equal(manual.channel, 'manual')
|
||||
assert.equal(manual.source, 'auto_mapping')
|
||||
|
||||
const affiliate = resolveChannelDecision(
|
||||
buildItem({ channels: [buildChannel({ channel: 'affiliate_dash' })] }),
|
||||
[],
|
||||
)
|
||||
assert.equal(affiliate.channel, null, '自动发货能力没有任务时不能只改财务口径')
|
||||
|
||||
const workOrder = resolveChannelDecision(
|
||||
buildItem({ channels: [buildChannel({ channel: 'work_order' })] }),
|
||||
[],
|
||||
)
|
||||
assert.equal(workOrder.channel, null, '接单能力没有工单时不能只改财务口径')
|
||||
})
|
||||
|
||||
test('渠道判定:内部 SKU 配置优先于旧 sku_code 规则', () => {
|
||||
const decision = resolveChannelDecision(
|
||||
buildItem({ channels: [buildChannel({ channel: 'third_party_voucher' })] }),
|
||||
[buildRule({ channel: 'manual' })],
|
||||
)
|
||||
assert.equal(decision.channel, 'third_party_voucher')
|
||||
assert.equal(decision.source, 'auto_mapping')
|
||||
})
|
||||
|
||||
test('渠道判定:内部 SKU 同级多渠道判为冲突', () => {
|
||||
test('渠道判定:统一配置同优先级多渠道判为冲突', () => {
|
||||
const decision = resolveChannelDecision(
|
||||
buildItem({
|
||||
channels: [buildChannel({ channel: 'work_order' }), buildChannel({ channel: 'manual' })],
|
||||
channels: [
|
||||
buildChannel({ channel: 'work_order' }),
|
||||
buildChannel({ id: 2, channel: 'manual' }),
|
||||
],
|
||||
}),
|
||||
[],
|
||||
)
|
||||
assert.equal(decision.channel, null)
|
||||
assert.equal(decision.conflict, true)
|
||||
assert.deepEqual(decision.conflictChannels.sort(), ['manual', 'work_order'])
|
||||
})
|
||||
|
||||
test('渠道判定:人工任务作为人工载体证据', () => {
|
||||
const decision = resolveChannelDecision(buildItem({ has_manual_task: true }), [])
|
||||
assert.equal(decision.channel, 'manual')
|
||||
assert.equal(decision.source, 'carrier_evidence')
|
||||
test('渠道判定:卡券能力可以直接落到财务渠道', () => {
|
||||
const decision = resolveChannelDecision(
|
||||
buildItem({ channels: [buildChannel({ channel: 'third_party_voucher', config_json: {} })] }),
|
||||
)
|
||||
assert.equal(decision.channel, 'third_party_voucher')
|
||||
assert.equal(decision.source, 'auto_mapping')
|
||||
})
|
||||
|
||||
test('渠道判定:人工能力只有配置存在时才算载体就绪', () => {
|
||||
const ready = resolveChannelDecision(buildItem({ channels: [buildChannel()] }))
|
||||
assert.equal(ready.channel, 'manual')
|
||||
|
||||
const missing = resolveChannelDecision(
|
||||
buildItem({ channels: [buildChannel({ config_json: {} })] }),
|
||||
)
|
||||
assert.equal(missing.channel, null)
|
||||
assert.equal(missing.pendingReason, 'no_carrier')
|
||||
})
|
||||
|
||||
test('渠道判定:自动发货/接单能力没有真实载体时继续保持 pending', () => {
|
||||
const affiliate = resolveChannelDecision(
|
||||
buildItem({ channels: [buildChannel({ channel: 'affiliate_dash', config_json: {} })] }),
|
||||
)
|
||||
assert.equal(affiliate.channel, null)
|
||||
assert.equal(affiliate.pendingReason, 'no_carrier')
|
||||
|
||||
const workOrder = resolveChannelDecision(
|
||||
buildItem({ channels: [buildChannel({ channel: 'work_order', config_json: {} })] }),
|
||||
)
|
||||
assert.equal(workOrder.channel, null)
|
||||
assert.equal(workOrder.pendingReason, 'no_carrier')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user