Files
order_site/apps/backend/src/repositories/order-item-repo.test.ts
T

205 lines
5.9 KiB
TypeScript

import test from 'node:test'
import assert from 'node:assert/strict'
import {
preserveAssignedOrderItemState,
replaceOrderItemsWithExecutor,
resolveOrderItemSyncPlan,
} from './order-item-repo.js'
test('resolveOrderItemSyncPlan preserves matching order item ids across repeated source syncs', () => {
const existingItems = [
{
id: 15,
sku_code: 'sku-a',
sku_name: '礼包 A',
quantity: 1,
},
]
const nextItems = [
{
skuCode: 'sku-a',
skuName: '礼包 A',
quantity: 1,
specJson: '{}',
itemSnapshotJson: '{}',
createdAt: '2026-04-14T12:00:00.000Z',
updatedAt: '2026-04-14T12:00:00.000Z',
},
]
const plan = resolveOrderItemSyncPlan(existingItems, nextItems)
assert.deepEqual(plan.updates, [
{
orderItemId: 15,
item: nextItems[0],
},
])
assert.deepEqual(plan.creates, [])
assert.deepEqual(plan.deletes, [])
})
test('resolveOrderItemSyncPlan matches by identity before falling back to position', () => {
const existingItems = [
{ id: 21, sku_code: 'sku-a', sku_name: '礼包 A', quantity: 1 },
{ id: 22, sku_code: 'sku-b', sku_name: '礼包 B', quantity: 1 },
]
const nextItems = [
{
skuCode: 'sku-b',
skuName: '礼包 B',
quantity: 1,
specJson: '{}',
itemSnapshotJson: '{}',
createdAt: '2026-04-14T12:00:00.000Z',
updatedAt: '2026-04-14T12:00:00.000Z',
},
{
skuCode: 'sku-a',
skuName: '礼包 A',
quantity: 1,
specJson: '{}',
itemSnapshotJson: '{}',
createdAt: '2026-04-14T12:00:00.000Z',
updatedAt: '2026-04-14T12:00:00.000Z',
},
]
const plan = resolveOrderItemSyncPlan(existingItems, nextItems)
assert.deepEqual(
plan.updates.map((item) => item.orderItemId),
[22, 21],
)
assert.deepEqual(plan.creates, [])
assert.deepEqual(plan.deletes, [])
})
test('resolveOrderItemSyncPlan keeps surplus existing ids for conditional cleanup instead of recreating everything', () => {
const existingItems = [
{ id: 31, sku_code: 'sku-a', sku_name: '礼包 A', quantity: 1 },
{ id: 32, sku_code: 'sku-b', sku_name: '礼包 B', quantity: 1 },
]
const nextItems = [
{
skuCode: 'sku-a',
skuName: '礼包 A',
quantity: 1,
specJson: '{}',
itemSnapshotJson: '{}',
createdAt: '2026-04-14T12:00:00.000Z',
updatedAt: '2026-04-14T12:00:00.000Z',
},
]
const plan = resolveOrderItemSyncPlan(existingItems, nextItems)
assert.deepEqual(
plan.updates.map((item) => item.orderItemId),
[31],
)
assert.deepEqual(plan.creates, [])
assert.deepEqual(plan.deletes, [32])
})
test('已分配订单重推时冻结原渠道决策与中文内部 SKU', () => {
const existing = {
id: 41,
fulfillment_status: 'assigned',
fulfillment_channel: 'affiliate_dash',
sku_code: 'lucky_coin_x2',
sku_name: '幸运币2个',
quantity: 1,
spec_json: { skuNick: '幸运币2个', sourceSpec: 'old' },
item_snapshot_json: {
internalSku: '幸运币2个',
internalSkuChannel: 'affiliate_dash',
internalSkuChannelSource: 'operator',
isConfigured: true,
fulfillmentRoute: { selectedExecutorKey: 'affiliate_dash' },
affiliateDash: { sku: 'lucky_coin_x2' },
externalSkuName: 'old title',
},
}
const incoming = {
skuCode: 'platform-new',
skuName: '幸运币2个',
quantity: 1,
specJson: { skuNick: '新映射', sourceSpec: 'new' },
itemSnapshotJson: {
internalSku: '新映射',
internalSkuChannel: 'work_order',
isConfigured: false,
fulfillmentRoute: null,
externalSkuName: 'new title',
},
createdAt: '2026-11-03T00:00:00.000Z',
updatedAt: '2026-11-03T00:00:00.000Z',
}
const stabilized = preserveAssignedOrderItemState(existing as never, incoming)
assert.equal(stabilized.skuCode, 'lucky_coin_x2')
assert.equal(stabilized.skuName, '幸运币2个')
assert.deepEqual(stabilized.specJson, { skuNick: '幸运币2个', sourceSpec: 'new' })
assert.deepEqual(stabilized.itemSnapshotJson, {
externalSkuName: 'new title',
internalSku: '幸运币2个',
internalSkuChannel: 'affiliate_dash',
internalSkuChannelSource: 'operator',
isConfigured: true,
fulfillmentRoute: { selectedExecutorKey: 'affiliate_dash' },
affiliateDash: { sku: 'lucky_coin_x2' },
})
})
test('已分配订单重推写库时不会用平台新 SKU 覆盖冻结值', async () => {
const existing = {
id: '41',
order_id: 7,
fulfillment_status: 'assigned',
fulfillment_channel: 'affiliate_dash',
sku_code: 'lucky_coin_x2',
sku_name: '幸运币2个',
quantity: 1,
spec_json: { skuNick: '幸运币2个' },
item_snapshot_json: {
internalSku: '幸运币2个',
internalSkuChannel: 'affiliate_dash',
isConfigured: true,
fulfillmentRoute: { selectedExecutorKey: 'affiliate_dash' },
affiliateDash: { sku: 'lucky_coin_x2' },
},
}
const incoming = {
skuCode: 'platform-new',
skuName: '平台新名称',
quantity: 1,
specJson: JSON.stringify({ skuNick: '平台新名称' }),
itemSnapshotJson: JSON.stringify({ externalSkuName: '平台新名称' }),
createdAt: '2026-11-03T00:00:00.000Z',
updatedAt: '2026-11-03T00:00:00.000Z',
}
const updateParams: unknown[][] = []
let selectCount = 0
const executor = async (sql: string, params: unknown[] = []) => {
if (sql.includes('SELECT *') && sql.includes('FROM order_items')) {
selectCount += 1
return { rows: selectCount === 1 ? [existing] : [] }
}
if (sql.includes('UPDATE order_items')) {
updateParams.push(params)
return { rows: [], rowCount: 1 }
}
throw new Error(`unexpected SQL: ${sql}`)
}
await replaceOrderItemsWithExecutor(executor as never, 7, [incoming])
assert.equal(updateParams.length, 1)
assert.equal(updateParams[0]?.[0], 'lucky_coin_x2')
assert.equal(updateParams[0]?.[1], '幸运币2个')
})