252 lines
6.5 KiB
TypeScript
252 lines
6.5 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
|
|
import {
|
|
createDefaultVoucherConsumePolicy,
|
|
normalizeVoucherConsumePolicy,
|
|
resolveVoucherConsumePolicy,
|
|
} from './voucher-consume-policy-service.js'
|
|
|
|
test('默认核销策略:自动发货 5 秒,工单发布或指派后 10 秒,人工任务 10 秒', () => {
|
|
const policy = createDefaultVoucherConsumePolicy()
|
|
|
|
assert.deepEqual(policy.platforms.kuaishou_industry.channels.order_received, {
|
|
enabled: false,
|
|
mode: 'after_order_received',
|
|
delaySeconds: 5,
|
|
})
|
|
assert.deepEqual(policy.platforms.kuaishou_industry.channels.affiliate_dash, {
|
|
enabled: true,
|
|
mode: 'after_link_generated',
|
|
delaySeconds: 5,
|
|
})
|
|
assert.deepEqual(policy.platforms.douyin_mashida.channels.work_order, {
|
|
enabled: true,
|
|
mode: 'after_work_order_published_or_assigned',
|
|
delaySeconds: 10,
|
|
})
|
|
assert.deepEqual(policy.platforms.kuaishou_industry.channels.manual, {
|
|
enabled: true,
|
|
mode: 'after_task_created',
|
|
delaySeconds: 10,
|
|
})
|
|
assert.deepEqual(policy.platforms.kuaishou_industry.channels.third_party_voucher, {
|
|
enabled: true,
|
|
mode: 'after_order_received',
|
|
delaySeconds: 5,
|
|
})
|
|
assert.deepEqual(policy.platforms.kuaishou_industry.channels.unmatched, {
|
|
enabled: false,
|
|
mode: 'after_order_received',
|
|
delaySeconds: 5,
|
|
})
|
|
})
|
|
|
|
test('工单默认策略同时匹配发布和指派,且不匹配链接生成', () => {
|
|
const policy = createDefaultVoucherConsumePolicy()
|
|
|
|
assert.equal(
|
|
resolveVoucherConsumePolicy({
|
|
provider: 'kuaishou_industry',
|
|
channel: 'work_order',
|
|
event: 'work_order_published',
|
|
policy,
|
|
}).enabled,
|
|
true,
|
|
)
|
|
assert.equal(
|
|
resolveVoucherConsumePolicy({
|
|
provider: 'douyin_mashida',
|
|
channel: 'work_order',
|
|
event: 'work_order_assigned',
|
|
policy,
|
|
}).enabled,
|
|
true,
|
|
)
|
|
assert.equal(
|
|
resolveVoucherConsumePolicy({
|
|
provider: 'douyin_mashida',
|
|
channel: 'work_order',
|
|
event: 'link_generated',
|
|
policy,
|
|
}).enabled,
|
|
false,
|
|
)
|
|
})
|
|
|
|
test('收到订单后事件可匹配四个渠道,第三方卡券默认延迟 5 秒', () => {
|
|
const policy = createDefaultVoucherConsumePolicy()
|
|
for (const channel of [
|
|
'affiliate_dash',
|
|
'work_order',
|
|
'manual',
|
|
'third_party_voucher',
|
|
] as const) {
|
|
const configured = structuredClone(policy)
|
|
configured.platforms.kuaishou_industry.channels[channel] = {
|
|
enabled: true,
|
|
mode: 'after_order_received',
|
|
delaySeconds: channel === 'third_party_voucher' ? 5 : 15,
|
|
}
|
|
const resolved = resolveVoucherConsumePolicy({
|
|
provider: 'kuaishou_industry',
|
|
channel,
|
|
event: 'order_received',
|
|
policy: configured,
|
|
})
|
|
assert.equal(resolved.enabled, true)
|
|
assert.equal(resolved.delaySeconds, channel === 'third_party_voucher' ? 5 : 15)
|
|
}
|
|
})
|
|
|
|
test('策略归一化会丢弃非法模式并限制延迟范围', () => {
|
|
const policy = normalizeVoucherConsumePolicy({
|
|
enabled: true,
|
|
platforms: {
|
|
kuaishou_industry: {
|
|
enabled: true,
|
|
channels: {
|
|
affiliate_dash: {
|
|
enabled: true,
|
|
mode: 'after_work_order_assigned',
|
|
delaySeconds: 99999,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
assert.deepEqual(policy.platforms.kuaishou_industry.channels.affiliate_dash, {
|
|
enabled: true,
|
|
mode: 'after_link_generated',
|
|
delaySeconds: 3600,
|
|
})
|
|
assert.equal(policy.platforms.douyin_mashida.enabled, true)
|
|
})
|
|
|
|
test('旧版第三方卡券停用配置自动升级为收到订单后 5 秒', () => {
|
|
const policy = normalizeVoucherConsumePolicy({
|
|
enabled: true,
|
|
platforms: {
|
|
kuaishou_industry: {
|
|
enabled: true,
|
|
channels: {
|
|
third_party_voucher: {
|
|
enabled: false,
|
|
mode: 'manual',
|
|
delaySeconds: 0,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
assert.equal(policy.version, 2)
|
|
assert.deepEqual(policy.platforms.kuaishou_industry.channels.third_party_voucher, {
|
|
enabled: true,
|
|
mode: 'after_order_received',
|
|
delaySeconds: 5,
|
|
})
|
|
})
|
|
|
|
test('新版明确关闭第三方卡券时继续尊重用户配置', () => {
|
|
const policy = normalizeVoucherConsumePolicy({
|
|
version: 2,
|
|
enabled: true,
|
|
platforms: {
|
|
kuaishou_industry: {
|
|
enabled: true,
|
|
channels: {
|
|
third_party_voucher: {
|
|
enabled: false,
|
|
mode: 'manual',
|
|
delaySeconds: 0,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
assert.deepEqual(policy.platforms.kuaishou_industry.channels.third_party_voucher, {
|
|
enabled: false,
|
|
mode: 'manual',
|
|
delaySeconds: 0,
|
|
})
|
|
})
|
|
|
|
test('旧配置自动补齐未命中渠道兜底,并支持手动开启', () => {
|
|
const legacy = normalizeVoucherConsumePolicy({ version: 2, enabled: true })
|
|
assert.equal(legacy.platforms.douyin_mashida.channels.unmatched.enabled, false)
|
|
|
|
const enabled = structuredClone(legacy)
|
|
enabled.platforms.douyin_mashida.channels.unmatched = {
|
|
enabled: true,
|
|
mode: 'after_order_received',
|
|
delaySeconds: 12,
|
|
}
|
|
assert.deepEqual(
|
|
resolveVoucherConsumePolicy({
|
|
provider: 'douyin_mashida',
|
|
channel: 'unmatched',
|
|
event: 'order_received',
|
|
policy: enabled,
|
|
}),
|
|
{ enabled: true, delaySeconds: 12, mode: 'after_order_received' },
|
|
)
|
|
})
|
|
|
|
test('旧配置开启未命中渠道兜底时,自动继承为订单级收单核销', () => {
|
|
const policy = normalizeVoucherConsumePolicy({
|
|
version: 2,
|
|
enabled: true,
|
|
platforms: {
|
|
kuaishou_industry: {
|
|
enabled: true,
|
|
channels: {
|
|
unmatched: {
|
|
enabled: true,
|
|
mode: 'after_order_received',
|
|
delaySeconds: 12,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
assert.deepEqual(policy.platforms.kuaishou_industry.channels.order_received, {
|
|
enabled: true,
|
|
mode: 'after_order_received',
|
|
delaySeconds: 12,
|
|
})
|
|
})
|
|
|
|
test('显式订单级策略优先于旧未命中渠道兜底配置', () => {
|
|
const policy = normalizeVoucherConsumePolicy({
|
|
version: 2,
|
|
enabled: true,
|
|
platforms: {
|
|
kuaishou_industry: {
|
|
enabled: true,
|
|
channels: {
|
|
order_received: {
|
|
enabled: false,
|
|
mode: 'manual',
|
|
delaySeconds: 0,
|
|
},
|
|
unmatched: {
|
|
enabled: true,
|
|
mode: 'after_order_received',
|
|
delaySeconds: 12,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
assert.deepEqual(policy.platforms.kuaishou_industry.channels.order_received, {
|
|
enabled: false,
|
|
mode: 'manual',
|
|
delaySeconds: 0,
|
|
})
|
|
})
|