import assert from 'node:assert/strict' import test from 'node:test' import { planInternalSkuChannels, type InternalSkuChannelRow, } from './internal-sku-channel-service.js' function channel(patch: Partial): 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?.channel, 'manual') assert.deepEqual( plan.attempts.map((item) => item.channel), ['manual'], ) }) 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, []) })