Files
order_site/apps/backend/src/services/open-auto-ship-douyin/protocol.test.ts
T

164 lines
5.2 KiB
TypeScript

import test from 'node:test'
import assert from 'node:assert/strict'
import {
buildAutoShipDouyinCallbackSignSource,
buildAutoShipDouyinSignSource,
encryptAutoShipDouyinCards,
signAutoShipDouyinCallbackPayload,
signAutoShipDouyinPayload,
} from './crypto.js'
import {
buildAutoShipDouyinCardItem,
formatAutoShipDouyinExpireTime,
formatAutoShipDouyinOrderCost,
resolveAutoShipDouyinQueryState,
} from './response.js'
import { shouldRejectAutoShipDouyinMaxAmount } from './cost.js'
import { assertAutoShipDouyinCallbackUrl } from './config.js'
const secret = '12345678901234567890123456789012'
const config = {
enabled: true,
secret,
version: '1.0',
shopId: 'auto_ship_douyin',
shopName: '自动发货抖店',
timestampToleranceSeconds: 600,
callbackAllowedOrigins: ['https://callback.example.com'],
callbackTimeoutMs: 5000,
}
test('auto-ship-douyin incoming signature uses only actual request body fields', () => {
const payload = {
orderNo: 'ORDER-1',
productNo: 'PLATFORM-SKU-1',
buyNum: 1,
maxAmount: '',
callbackUrl: 'https://callback.example.com/hook',
timestamp: 1790000000,
version: '1.0',
}
assert.equal(
buildAutoShipDouyinSignSource(payload, config),
`${secret}buyNum=1&callbackUrl=https://callback.example.com/hook&maxAmount=&orderNo=ORDER-1&productNo=PLATFORM-SKU-1&timestamp=1790000000&version=1.0${secret}`,
)
const sign = signAutoShipDouyinPayload(payload, config)
assert.equal(sign.length, 32)
})
test('auto-ship-douyin callback signature excludes cards per reference parameter table', () => {
const payload = {
orderNo: 'ORDER-1',
outTradeNo: 'OS0000000001',
orderStatus: 20,
failCode: 0,
failReason: '',
orderCost: '100',
cards: 'ENCRYPTED-CARDS-MUST-NOT-BE-SIGNED',
timestamp: 1790000000,
}
const source = buildAutoShipDouyinCallbackSignSource(payload, config)
assert.equal(source.includes('cards='), false)
assert.equal(
source,
`${secret}failCode=0&failReason=&orderCost=100&orderNo=ORDER-1&orderStatus=20&outTradeNo=OS0000000001&timestamp=1790000000${secret}`,
)
assert.equal(signAutoShipDouyinCallbackPayload(payload, config).length, 32)
})
test('auto-ship-douyin callback URL must be HTTPS and allowlisted', () => {
assert.equal(
assertAutoShipDouyinCallbackUrl('https://callback.example.com/hook', config),
'https://callback.example.com/hook',
)
assert.throws(
() => assertAutoShipDouyinCallbackUrl('http://callback.example.com/hook', config),
/HTTPS/,
)
assert.throws(
() => assertAutoShipDouyinCallbackUrl('https://evil.example.com/hook', config),
/允许列表/,
)
})
test('auto-ship-douyin cards encryption matches AES-256-ECB stable sample', () => {
const cards = [
{
cardNo: 'https://example.com/h5/ORDER-1',
cardPwd: '',
expireTime: '',
jumpLink: 'https://example.com/h5/ORDER-1',
},
]
const encrypted = encryptAutoShipDouyinCards(cards, secret)
assert.equal(typeof encrypted, 'string')
assert.ok(encrypted.length > 20)
})
test('auto-ship-douyin query state returns 20 only when every task has a delivery link', () => {
assert.deepEqual(
resolveAutoShipDouyinQueryState({
orderStatus: 'paid',
tasks: [
{ id: 1, task_status: 'link_generated' },
{ id: 2, task_status: 'link_generated' },
],
readyTaskIds: [1, 2],
}),
{ orderStatus: 20, failCode: 0, failReason: '' },
)
assert.deepEqual(
resolveAutoShipDouyinQueryState({
orderStatus: 'paid',
tasks: [
{ id: 1, task_status: 'link_generated' },
{ id: 2, task_status: 'link_generated' },
],
readyTaskIds: [1],
}),
{ orderStatus: 10, failCode: 0, failReason: '' },
)
})
test('auto-ship-douyin manual review is recoverable and reports processing', () => {
assert.deepEqual(
resolveAutoShipDouyinQueryState({
orderStatus: 'paid',
tasks: [{ id: 1, task_status: 'manual_review', last_error: '待人工处理' }],
readyTaskIds: [],
}),
{ orderStatus: 10, failCode: 0, failReason: '' },
)
})
test('auto-ship-douyin maxAmount compares whole-order cost and treats empty as no cap', () => {
assert.equal(shouldRejectAutoShipDouyinMaxAmount({ maxAmount: '', totalCostFen: 99999 }), false)
assert.equal(
shouldRejectAutoShipDouyinMaxAmount({ maxAmount: '100.00', totalCostFen: 9999 }),
false,
)
assert.equal(
shouldRejectAutoShipDouyinMaxAmount({ maxAmount: '100.00', totalCostFen: 10001 }),
true,
)
assert.equal(shouldRejectAutoShipDouyinMaxAmount({ maxAmount: '0', totalCostFen: 1 }), true)
})
test('auto-ship-douyin card and money formatting follow the reference contract', () => {
assert.equal(formatAutoShipDouyinOrderCost(10000), '100.0000')
assert.equal(formatAutoShipDouyinOrderCost(10000, { trim: true }), '100')
assert.equal(formatAutoShipDouyinExpireTime('2026-05-05 12:00:00'), '2026-05-05 12:00:00')
assert.deepEqual(
buildAutoShipDouyinCardItem({
claimUrl: 'https://example.com/h5/ORDER-1',
expireTime: '2026-05-05 12:00:00',
}),
{
cardNo: 'https://example.com/h5/ORDER-1',
cardPwd: '',
expireTime: '2026-05-05 12:00:00',
jumpLink: 'https://example.com/h5/ORDER-1',
},
)
})