import assert from 'node:assert/strict' import test from 'node:test' import { planFulfillmentTaskForOrderItem } from './planner.js' import type { OrderItemRow, OrderRow } from '../../types/repository/rows.js' test('planFulfillmentTaskForOrderItem 为 affiliate-dash 商品生成履约计划', async () => { const plan = await planFulfillmentTaskForOrderItem({ order: createOrder(), item: createOrderItem({ sku_code: 'AFF-1001', sku_name: '幸运币90个', item_snapshot_json: { fulfillmentRoute: { selectedExecutorKey: 'affiliate_dash', }, affiliateDash: { sku: 'AFF-1001', skuName: '幸运币90个', matchMode: 'affiliate_dash_sku_mapping', }, }, }), getProfileByKey: async (profileKey) => ({ id: 11, profile_key: profileKey, name: 'affiliate-dash 履约', executor_key: profileKey, requires_claim: false, auto_dispatch: true, }), }) assert.ok(plan) assert.equal(plan.executorKey, 'affiliate_dash') assert.equal(plan.requiresClaim, true) assert.equal(plan.autoDispatch, false) assert.equal(plan.context.profileKey, 'affiliate_dash') assert.equal((plan.context.affiliateDash as { sku?: string }).sku, 'AFF-1001') assert.equal((plan.context.affiliateDash as { productName?: string }).productName, '幸运币90个') }) test('planFulfillmentTaskForOrderItem 支持履约路由转人工', async () => { const plan = await planFulfillmentTaskForOrderItem({ order: createOrder(), item: createOrderItem({ sku_code: 'MANUAL-1', sku_name: '人工商品', item_snapshot_json: { fulfillmentRoute: { selectedExecutorKey: 'manual_dispatch', }, }, }), getProfileByKey: async (profileKey) => ({ id: 1, profile_key: profileKey, name: '人工发货', executor_key: 'manual_dispatch', requires_claim: true, auto_dispatch: true, }), }) assert.ok(plan) assert.equal(plan.executorKey, 'manual_dispatch') assert.equal(plan.requiresClaim, false) assert.equal(plan.autoDispatch, false) assert.equal(plan.context.affiliateDash, null) }) test('planFulfillmentTaskForOrderItem 未命中平台配置时返回 null', async () => { const plan = await planFulfillmentTaskForOrderItem({ order: createOrder(), item: createOrderItem({ item_snapshot_json: { affiliateDash: null, }, }), getProfileByKey: async () => null, }) assert.equal(plan, null) }) test('planFulfillmentTaskForOrderItem affiliate 缺 sku 时返回 null', async () => { const plan = await planFulfillmentTaskForOrderItem({ order: createOrder(), item: createOrderItem({ item_snapshot_json: { fulfillmentRoute: { selectedExecutorKey: 'affiliate_dash', }, affiliateDash: { skuName: '无 sku 商品', }, }, }), getProfileByKey: async (profileKey) => ({ id: 11, profile_key: profileKey, name: 'affiliate-dash 履约', executor_key: profileKey, requires_claim: true, auto_dispatch: false, }), }) assert.equal(plan, null) }) test('planFulfillmentTaskForOrderItem 为抖音命中商品生成 affiliate_dash 履约计划', async () => { const plan = await planFulfillmentTaskForOrderItem({ order: createOrder({ provider: 'douyin_mashida', platform: 'doudian' }), item: createOrderItem({ sku_code: '幸运币2个', sku_name: '【测试专用】和平精英皮肤小店入口', item_snapshot_json: { fulfillmentRoute: { selectedExecutorKey: 'affiliate_dash' }, affiliateDash: { matchMode: 'affiliate_dash_sku_mapping', productNo: '幸运币2个', sku: 'lucky_coin_x2', skuName: '幸运币2个', }, }, }), getProfileByKey: async (profileKey) => ({ id: 21, profile_key: profileKey, name: 'affiliate-dash 履约', executor_key: profileKey, requires_claim: false, auto_dispatch: false, }), }) assert.ok(plan) assert.equal(plan.executorKey, 'affiliate_dash') assert.equal(plan.profileKey, 'affiliate_dash') assert.equal(plan.requiresClaim, true) assert.equal(plan.autoDispatch, false) const affiliateDash = plan.context.affiliateDash as Record | null assert.ok(affiliateDash) assert.equal(affiliateDash.sku, 'lucky_coin_x2') assert.equal(affiliateDash.productName, '幸运币2个') }) test('planFulfillmentTaskForOrderItem 抖音订单缺 affiliate_dash sku 时返回 null', async () => { const plan = await planFulfillmentTaskForOrderItem({ order: createOrder({ provider: 'douyin_mashida', platform: 'doudian' }), item: createOrderItem({ sku_code: '', sku_name: '无内部 SKU', item_snapshot_json: { fulfillmentRoute: { selectedExecutorKey: 'affiliate_dash' }, }, }), getProfileByKey: async (profileKey) => ({ id: 21, profile_key: profileKey, name: 'affiliate-dash 履约', executor_key: profileKey, }), }) assert.equal(plan, null) }) function createOrder(patch: Partial = {}): OrderRow { return { id: 1, provider: '91kaquan', platform: 'kuaishou', shop_id: '91kaquan', shop_name: '91卡券', platform_order_id: '2614900602069169', order_status: 'paid', pay_status: 'paid', buyer_id: '', buyer_name: '', receiver_contact: '', total_amount: 0, currency: 'CNY', raw_payload_json: '{}', paid_at: '2026-07-08T00:00:00.000Z', created_at: '2026-07-08T00:00:00.000Z', updated_at: '2026-07-08T00:00:00.000Z', ...patch, } } function createOrderItem(patch: Partial = {}): OrderItemRow { return { id: 10, order_id: 1, sku_code: 'SKU-1', sku_name: '测试商品', quantity: 1, spec_json: '{}', item_snapshot_json: {}, created_at: '2026-07-08T00:00:00.000Z', updated_at: '2026-07-08T00:00:00.000Z', ...patch, } }