177 lines
6.2 KiB
TypeScript
177 lines
6.2 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import { readFile } from 'node:fs/promises'
|
|
import test from 'node:test'
|
|
|
|
import { listOrdersWithExecutor, type OrderQueryExecutor } from './order-repo.js'
|
|
|
|
test('订单更新永不覆盖首次写入的平台原始报文', async () => {
|
|
const source = await readFile(new URL('./order-repo.ts', import.meta.url), 'utf8')
|
|
const updateSource = source.slice(
|
|
source.indexOf('export async function updateOrderWithExecutor'),
|
|
source.indexOf('export async function getOrderById'),
|
|
)
|
|
assert.doesNotMatch(updateSource, /raw_payload_json\s*=/)
|
|
})
|
|
|
|
test('订单列表综合筛选下推到总数和分页查询', async () => {
|
|
const calls: Array<{ sql: string; params: unknown[] }> = []
|
|
const executor = createExecutor(calls, 3)
|
|
|
|
const result = await listOrdersWithExecutor(executor, {
|
|
page: 2,
|
|
pageSize: 10,
|
|
keyword: '皮肤店',
|
|
provider: '91kaquan',
|
|
executorKey: 'affiliate_dash',
|
|
payStatus: 'paid',
|
|
taskStatus: 'manual_review',
|
|
dateFrom: '2026-09-01',
|
|
dateTo: '2026-09-02',
|
|
})
|
|
|
|
assert.equal(result.total, 3)
|
|
assert.equal(calls.length, 2)
|
|
assert.match(calls[0]?.sql || '', /o\.platform_order_id.*ILIKE \$1/s)
|
|
assert.match(calls[0]?.sql || '', /oi\.sku_code.*oi\.sku_name.*ILIKE \$1/s)
|
|
assert.match(calls[0]?.sql || '', /kuaishouSendCode'.*itemTitle.*kuaishouSendCode'.*skuNick/s)
|
|
assert.match(calls[0]?.sql || '', /o\.provider IN \('91kaquan', 'open_91', '91'\)/)
|
|
assert.match(calls[0]?.sql || '', /o\.pay_status = \$2/)
|
|
assert.match(calls[0]?.sql || '', /ft\.task_status = \$3/)
|
|
assert.match(calls[0]?.sql || '', /ft\.executor_key = \$4/)
|
|
// 有关键词时不应用日期过滤
|
|
assert.doesNotMatch(calls[0]?.sql || '', /paid_at/i)
|
|
assert.deepEqual(calls[0]?.params, ['%皮肤店%', 'paid', 'manual_review', 'affiliate_dash'])
|
|
assert.match(calls[1]?.sql || '', /LIMIT \$5 OFFSET \$6/)
|
|
assert.deepEqual(calls[1]?.params, [...(calls[0]?.params || []), 10, 10])
|
|
})
|
|
|
|
test('订单列表无关键词时按支付时间与中国时区日期筛选', async () => {
|
|
const calls: Array<{ sql: string; params: unknown[] }> = []
|
|
|
|
await listOrdersWithExecutor(createExecutor(calls, 0), {
|
|
dateFrom: '2026-09-01',
|
|
dateTo: '2026-09-07',
|
|
})
|
|
|
|
assert.match(
|
|
calls[0]?.sql || '',
|
|
/COALESCE\(o\.paid_at, o\.created_at\) >= \(\$1::date\)::timestamp AT TIME ZONE 'Asia\/Shanghai'/,
|
|
)
|
|
assert.match(
|
|
calls[0]?.sql || '',
|
|
/COALESCE\(o\.paid_at, o\.created_at\) < \(\(\$2::date \+ 1\)::timestamp AT TIME ZONE 'Asia\/Shanghai'\)/,
|
|
)
|
|
assert.deepEqual(calls[0]?.params, ['2026-09-01', '2026-09-07'])
|
|
assert.match(calls[1]?.sql || '', /ORDER BY COALESCE\(o\.paid_at, o\.created_at\) DESC/)
|
|
})
|
|
|
|
test('订单列表保留订单号和精确 SKU 旧筛选参数兼容', async () => {
|
|
const calls: Array<{ sql: string; params: unknown[] }> = []
|
|
|
|
await listOrdersWithExecutor(createExecutor(calls, 0), {
|
|
platformOrderId: '26245',
|
|
skuCode: 'SKU-1',
|
|
})
|
|
|
|
assert.match(calls[0]?.sql || '', /o\.platform_order_id ILIKE \$1/)
|
|
assert.match(calls[0]?.sql || '', /oi\.sku_code = \$2/)
|
|
assert.deepEqual(calls[0]?.params, ['%26245%', 'SKU-1'])
|
|
})
|
|
|
|
test('订单列表关键词为拼多多 order_sn 时走平台订单号索引快路径', async () => {
|
|
const calls: Array<{ sql: string; params: unknown[] }> = []
|
|
const orderSn = '260915-091435834962130'
|
|
|
|
await listOrdersWithExecutor(createExecutor(calls, 0), {
|
|
keyword: orderSn,
|
|
})
|
|
|
|
assert.match(
|
|
calls[0]?.sql || '',
|
|
/o\.platform_order_id = \$1 OR o\.platform_order_id LIKE \$1 \|\| '%'/s,
|
|
)
|
|
assert.doesNotMatch(calls[0]?.sql || '', /oi\.sku_code/i)
|
|
assert.doesNotMatch(calls[0]?.sql || '', /item_snapshot_json/i)
|
|
assert.deepEqual(calls[0]?.params, [orderSn])
|
|
})
|
|
|
|
test('订单列表 platformOrderId 为拼多多 order_sn 时走索引快路径', async () => {
|
|
const calls: Array<{ sql: string; params: unknown[] }> = []
|
|
const orderSn = '260915-091435834962130'
|
|
|
|
await listOrdersWithExecutor(createExecutor(calls, 0), {
|
|
platformOrderId: orderSn,
|
|
})
|
|
|
|
assert.match(
|
|
calls[0]?.sql || '',
|
|
/o\.platform_order_id = \$1 OR o\.platform_order_id LIKE \$1 \|\| '%'/s,
|
|
)
|
|
assert.deepEqual(calls[0]?.params, [orderSn])
|
|
})
|
|
|
|
test('订单列表按当前履约模式筛选,不再依赖任务执行器', async () => {
|
|
const calls: Array<{ sql: string; params: unknown[] }> = []
|
|
|
|
await listOrdersWithExecutor(createExecutor(calls, 0), {
|
|
channel: 'manual',
|
|
})
|
|
|
|
assert.match(calls[0]?.sql || '', /fulfillment\.channel = \$1/)
|
|
assert.match(calls[0]?.sql || '', /fulfillment\.channel IS DISTINCT FROM \$1/)
|
|
assert.doesNotMatch(calls[0]?.sql || '', /COUNT\(DISTINCT/)
|
|
assert.doesNotMatch(calls[0]?.sql || '', /ft\.executor_key/)
|
|
assert.deepEqual(calls[0]?.params, ['manual'])
|
|
})
|
|
|
|
test('订单列表待判定筛选不扫描任务执行器', async () => {
|
|
const calls: Array<{ sql: string; params: unknown[] }> = []
|
|
|
|
await listOrdersWithExecutor(createExecutor(calls, 0), {
|
|
channel: 'pending',
|
|
})
|
|
|
|
assert.match(calls[0]?.sql || '', /EXISTS/)
|
|
assert.match(calls[0]?.sql || '', /fulfillment\.status = 'pending'/)
|
|
assert.doesNotMatch(calls[0]?.sql || '', /COUNT\(DISTINCT/)
|
|
assert.deepEqual(calls[0]?.params, [])
|
|
})
|
|
|
|
test('订单列表历史平台筛选映射为 archived', async () => {
|
|
const calls: Array<{ sql: string; params: unknown[] }> = []
|
|
|
|
await listOrdersWithExecutor(createExecutor(calls, 0), {
|
|
channel: 'legacy_platform',
|
|
})
|
|
|
|
assert.match(calls[0]?.sql || '', /fulfillment\.status = 'archived'/)
|
|
assert.deepEqual(calls[0]?.params, [])
|
|
})
|
|
|
|
test('订单列表混合履约筛选按订单项聚合判定', async () => {
|
|
const calls: Array<{ sql: string; params: unknown[] }> = []
|
|
|
|
await listOrdersWithExecutor(createExecutor(calls, 0), {
|
|
channel: 'mixed',
|
|
})
|
|
|
|
assert.match(calls[0]?.sql || '', /HAVING COUNT\(DISTINCT fulfillment\.channel\) > 1/)
|
|
assert.deepEqual(calls[0]?.params, [])
|
|
})
|
|
|
|
function createExecutor(
|
|
calls: Array<{ sql: string; params: unknown[] }>,
|
|
total: number,
|
|
): OrderQueryExecutor {
|
|
return (async (sql: string, params: unknown[] = []) => {
|
|
calls.push({ sql, params: [...params] })
|
|
return {
|
|
command: 'SELECT',
|
|
rowCount: calls.length === 1 ? 1 : 0,
|
|
oid: 0,
|
|
fields: [],
|
|
rows: calls.length === 1 ? [{ total }] : [],
|
|
}
|
|
}) as OrderQueryExecutor
|
|
}
|