Files
order_site/apps/backend/src/services/fulfillment/internal-sku-channel-service.test.ts
T

86 lines
2.9 KiB
TypeScript

import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import test from 'node:test'
import {
planInternalSkuChannels,
type InternalSkuChannelRow,
} from './internal-sku-channel-service.js'
function channel(patch: Partial<InternalSkuChannelRow>): InternalSkuChannelRow {
return {
id: 1,
internal_sku: '内部SKU',
channel: 'manual',
priority: 10,
is_fallback: false,
enabled: true,
canonical_product_id: null,
config_json: {},
source: 'test',
note: '',
created_by_admin_user_id: null,
...patch,
}
}
test('渠道计划:主渠道失败后按优先级使用显式兜底', () => {
const plan = planInternalSkuChannels([
channel({ channel: 'manual', priority: 90, is_fallback: true }),
channel({ channel: 'affiliate_dash', priority: 10 }),
])
assert.equal(plan.primary?.channel, 'affiliate_dash')
assert.deepEqual(
plan.attempts.map((item) => item.channel),
['affiliate_dash', 'manual'],
)
})
test('渠道计划:同级多主渠道是冲突,不能静默按字母选一个', () => {
const plan = planInternalSkuChannels([
channel({ channel: 'manual', priority: 10 }),
channel({ channel: 'work_order', priority: 10 }),
])
assert.equal(plan.primary, null)
assert.deepEqual(plan.conflicts.map((item) => item.channel).sort(), ['manual', 'work_order'])
})
test('渠道计划:非兜底渠道即使优先级数值更大也压过兜底渠道', () => {
const plan = planInternalSkuChannels([
channel({ channel: 'manual', priority: 1, is_fallback: true }),
channel({ channel: 'work_order', priority: 50 }),
])
assert.equal(plan.primary?.channel, 'work_order')
assert.equal(plan.fallbacks[0]?.channel, 'manual')
})
test('渠道计划:只有兜底渠道时保持未路由,必须人工补主渠道', () => {
const plan = planInternalSkuChannels([
channel({ channel: 'manual', priority: 90, is_fallback: true }),
])
assert.equal(plan.primary, null)
assert.deepEqual(plan.fallbacks, [])
assert.deepEqual(plan.attempts, [])
})
test('渠道计划:历史规范化重复的同一渠道会去重,不误判为冲突', () => {
const plan = planInternalSkuChannels([
channel({ id: 1, channel: 'manual', priority: 90, is_fallback: true }),
channel({ id: 2, channel: 'manual', priority: 10 }),
])
assert.equal(plan.primary?.channel, 'manual')
assert.equal(plan.primary?.id, 2)
assert.deepEqual(plan.conflicts, [])
})
test('运行时渠道服务只读 sku_channel_routes,不再读写旧表', async () => {
const source = await readFile(
new URL('./internal-sku-channel-service.ts', import.meta.url),
'utf8',
)
assert.ok(source.includes('FROM sku_channel_routes'))
assert.ok(source.includes('INSERT INTO sku_channel_routes'))
assert.ok(source.includes('DELETE FROM sku_channel_routes'))
assert.ok(!source.includes('internal_sku_channels'))
})