574 lines
21 KiB
TypeScript
574 lines
21 KiB
TypeScript
/** @jest-environment node */
|
||
|
||
import {
|
||
AUTO_LISTING_DELIVERY_STATUS,
|
||
AUTO_LISTING_EVENT_TYPE,
|
||
createExternalNotificationTarget,
|
||
listExternalNotificationDeliveries,
|
||
listExternalNotificationTargets,
|
||
queueTaskDetailBarcodeImagesUpdatedNotification,
|
||
queueTaskDetailFullSetNotification,
|
||
processExternalNotificationDelivery,
|
||
TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE,
|
||
retryExternalNotificationDelivery,
|
||
setExternalNotificationTargetActive,
|
||
testExternalNotificationTarget,
|
||
updateExternalNotificationTarget,
|
||
} from '@/lib/external-auto-listing'
|
||
import { prisma } from '@/lib/prisma'
|
||
import { postAutoListingCallback } from '@/lib/external-auto-listing-callback'
|
||
import { decryptCallbackSecret, encryptCallbackSecret } from '@/lib/external-auto-listing-crypto'
|
||
|
||
jest.mock('@/lib/external-auto-listing-callback', () => ({
|
||
...jest.requireActual('@/lib/external-auto-listing-callback'),
|
||
postAutoListingCallback: jest.fn(),
|
||
validateAutoListingCallbackUrl: jest.fn(async (url: string) => new URL(url).href),
|
||
}))
|
||
|
||
jest.mock('@/lib/prisma', () => ({
|
||
prisma: {
|
||
$executeRawUnsafe: jest.fn(),
|
||
$queryRawUnsafe: jest.fn(),
|
||
$transaction: jest.fn(),
|
||
material: {
|
||
findFirst: jest.fn(),
|
||
},
|
||
materialAssetBarcode: {
|
||
findMany: jest.fn(),
|
||
},
|
||
},
|
||
}))
|
||
|
||
jest.mock('@/lib/public-app-url', () => ({
|
||
getPublicAppOrigin: jest.fn(() => 'https://sc.jiansun.vip'),
|
||
}))
|
||
|
||
describe('external auto listing', () => {
|
||
const encryptionKey = process.env.API_KEY_ENCRYPTION_KEY
|
||
|
||
beforeAll(() => { process.env.API_KEY_ENCRYPTION_KEY = 'external-auto-listing-test-key-for-fixtures' })
|
||
|
||
afterAll(() => {
|
||
if (encryptionKey === undefined) delete process.env.API_KEY_ENCRYPTION_KEY
|
||
else process.env.API_KEY_ENCRYPTION_KEY = encryptionKey
|
||
})
|
||
|
||
beforeEach(() => {
|
||
jest.clearAllMocks()
|
||
jest.useFakeTimers()
|
||
;(prisma.$executeRawUnsafe as jest.Mock).mockResolvedValue(undefined)
|
||
;(prisma.$queryRawUnsafe as jest.Mock).mockResolvedValue([])
|
||
;(prisma.$transaction as jest.Mock).mockImplementation(async (callback: any) =>
|
||
callback({
|
||
$executeRawUnsafe: jest.fn().mockResolvedValue(undefined),
|
||
$queryRawUnsafe: jest.fn().mockResolvedValue([]),
|
||
}),
|
||
)
|
||
;(prisma.materialAssetBarcode.findMany as jest.Mock).mockResolvedValue([])
|
||
})
|
||
|
||
afterEach(() => {
|
||
jest.useRealTimers()
|
||
})
|
||
|
||
test('应能创建并更新自动上架通知目标', async () => {
|
||
;(prisma.$queryRawUnsafe as jest.Mock)
|
||
.mockResolvedValueOnce([
|
||
{
|
||
id: 'target-1',
|
||
name: '自动上架平台',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
isActive: true,
|
||
authType: 'bearer',
|
||
authConfig: { token: 'secret-token' },
|
||
eventTypes: [AUTO_LISTING_EVENT_TYPE, TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE],
|
||
createdAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
updatedAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
},
|
||
])
|
||
.mockResolvedValueOnce([
|
||
{
|
||
id: 'target-1',
|
||
name: '自动上架平台',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
isActive: true,
|
||
authType: 'bearer',
|
||
authConfig: { token: 'secret-token' },
|
||
eventTypes: [AUTO_LISTING_EVENT_TYPE, TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE],
|
||
createdAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
updatedAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
},
|
||
])
|
||
.mockResolvedValueOnce([
|
||
{
|
||
id: 'target-1',
|
||
name: '自动上架平台-新版',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
isActive: false,
|
||
authType: 'header',
|
||
authConfig: { headerName: 'X-Signature', headerValue: 'abc123' },
|
||
eventTypes: [AUTO_LISTING_EVENT_TYPE, TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE],
|
||
createdAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
updatedAt: new Date('2026-03-25T10:30:00.000Z'),
|
||
},
|
||
])
|
||
.mockResolvedValueOnce([
|
||
{
|
||
id: 'target-1',
|
||
name: '自动上架平台-新版',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
isActive: false,
|
||
authType: 'header',
|
||
authConfig: { headerName: 'X-Signature', headerValue: 'abc123' },
|
||
eventTypes: [AUTO_LISTING_EVENT_TYPE, TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE],
|
||
createdAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
updatedAt: new Date('2026-03-25T10:30:00.000Z'),
|
||
},
|
||
])
|
||
|
||
const created = await createExternalNotificationTarget({
|
||
name: '自动上架平台',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
authType: 'bearer',
|
||
authConfig: { token: 'secret-token' },
|
||
})
|
||
|
||
const insert = jest.mocked(prisma.$executeRawUnsafe).mock.calls.find(([sql]) =>
|
||
sql.includes('INSERT INTO "external_notification_targets"'))
|
||
const savedConfig = JSON.parse(String(insert?.[6]))
|
||
expect(savedConfig).not.toHaveProperty('token')
|
||
expect(JSON.stringify(savedConfig)).not.toContain('secret-token')
|
||
expect(decryptCallbackSecret(savedConfig)).toBe('secret-token')
|
||
|
||
expect(created).toEqual({
|
||
id: 'target-1',
|
||
name: '自动上架平台',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
isActive: true,
|
||
authType: 'bearer',
|
||
authConfig: { token: 'secret-token' },
|
||
eventTypes: [AUTO_LISTING_EVENT_TYPE, TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE],
|
||
createdAt: '2026-03-25T10:00:00.000Z',
|
||
updatedAt: '2026-03-25T10:00:00.000Z',
|
||
})
|
||
|
||
const listed = await listExternalNotificationTargets()
|
||
expect(listed).toHaveLength(1)
|
||
|
||
const updated = await updateExternalNotificationTarget('target-1', {
|
||
name: '自动上架平台-新版',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
isActive: false,
|
||
authType: 'header',
|
||
authConfig: {
|
||
headerName: 'X-Signature',
|
||
headerValue: 'abc123',
|
||
},
|
||
})
|
||
|
||
expect(updated).toEqual({
|
||
id: 'target-1',
|
||
name: '自动上架平台-新版',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
isActive: false,
|
||
authType: 'header',
|
||
authConfig: { headerName: 'X-Signature', headerValue: 'abc123' },
|
||
eventTypes: [AUTO_LISTING_EVENT_TYPE, TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE],
|
||
createdAt: '2026-03-25T10:00:00.000Z',
|
||
updatedAt: '2026-03-25T10:30:00.000Z',
|
||
})
|
||
})
|
||
|
||
test('保留已加密的凭据,改回调地址时要求填写新凭据', async () => {
|
||
const target = {
|
||
id: 'target-1',
|
||
name: '上架系统',
|
||
callbackUrl: 'https://receiver.example.com/callback',
|
||
authType: 'header',
|
||
authConfig: { headerName: 'X-Key', ...encryptCallbackSecret('fixture-secret') },
|
||
isActive: false,
|
||
eventTypes: [AUTO_LISTING_EVENT_TYPE],
|
||
}
|
||
jest.mocked(prisma.$queryRawUnsafe).mockResolvedValue([target])
|
||
await updateExternalNotificationTarget(target.id, { name: '改名' })
|
||
const update = jest.mocked(prisma.$executeRawUnsafe).mock.calls.find(([sql]) =>
|
||
sql.includes('UPDATE "external_notification_targets"'))
|
||
expect(update?.[4]).toBeNull()
|
||
expect(JSON.parse(String(update?.[6]))).toEqual(target.authConfig)
|
||
|
||
await expect(updateExternalNotificationTarget(target.id, {
|
||
callbackUrl: 'https://different.example.com/callback',
|
||
})).rejects.toThrow('重新填写')
|
||
await expect(updateExternalNotificationTarget(target.id, {
|
||
authConfig: { headerName: 'X-New-Key' },
|
||
})).rejects.toThrow('重新填写')
|
||
})
|
||
|
||
test('启停只修改状态,不重写凭据或地址', async () => {
|
||
jest.mocked(prisma.$executeRawUnsafe).mockResolvedValueOnce(1)
|
||
jest.mocked(prisma.$queryRawUnsafe).mockResolvedValueOnce([{ id: 'target-1', isActive: false }])
|
||
await expect(setExternalNotificationTargetActive('target-1', false)).resolves.toMatchObject({ isActive: false })
|
||
expect(prisma.$executeRawUnsafe).toHaveBeenCalledWith(
|
||
expect.stringContaining('SET is_active = $2, updated_at = NOW()'),
|
||
'target-1', false,
|
||
)
|
||
})
|
||
|
||
test('测试使用已保存凭据,停用目标也只发一次无素材的探针', async () => {
|
||
const callbackUrl = 'https://receiver.example.com/callback'
|
||
jest.mocked(prisma.$queryRawUnsafe).mockResolvedValueOnce([{
|
||
id: 'target-1', callbackUrl, authType: 'bearer', authConfig: encryptCallbackSecret('fixture-token'),
|
||
isActive: false, eventTypes: [AUTO_LISTING_EVENT_TYPE],
|
||
}])
|
||
jest.mocked(postAutoListingCallback).mockResolvedValueOnce(204)
|
||
await expect(testExternalNotificationTarget('target-1')).resolves.toEqual({ statusCode: 204, error: null })
|
||
expect(postAutoListingCallback).toHaveBeenCalledTimes(1)
|
||
expect(postAutoListingCallback).toHaveBeenCalledWith(callbackUrl, {
|
||
'Content-Type': 'application/json', Authorization: 'Bearer fixture-token',
|
||
}, {
|
||
eventId: expect.any(String), eventType: 'auto-listing.callback.test', test: true, occurredAt: expect.any(String),
|
||
})
|
||
expect(prisma.$executeRawUnsafe).not.toHaveBeenCalled()
|
||
})
|
||
|
||
test('投递成功后应标记为 succeeded', async () => {
|
||
;(prisma.$transaction as jest.Mock).mockImplementation(async (callback: any) =>
|
||
callback({
|
||
$executeRawUnsafe: jest.fn().mockResolvedValue(undefined),
|
||
$queryRawUnsafe: jest.fn().mockResolvedValue([
|
||
{
|
||
id: 'delivery-1',
|
||
status: 'pending',
|
||
payload: { eventId: 'evt-1' },
|
||
attemptCount: 0,
|
||
maxAttempts: 5,
|
||
updatedAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
targetId: 'target-1',
|
||
name: '自动上架平台',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
isActive: true,
|
||
authType: 'bearer',
|
||
authConfig: { token: 'secret-token' },
|
||
eventTypes: [AUTO_LISTING_EVENT_TYPE],
|
||
createdAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
targetUpdatedAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
},
|
||
]),
|
||
}),
|
||
)
|
||
jest.mocked(postAutoListingCallback).mockResolvedValueOnce(200)
|
||
|
||
const result = await processExternalNotificationDelivery('delivery-1')
|
||
|
||
expect(result).toEqual({
|
||
success: true,
|
||
status: AUTO_LISTING_DELIVERY_STATUS.SUCCEEDED,
|
||
})
|
||
expect(prisma.$executeRawUnsafe).toHaveBeenCalledWith(
|
||
expect.stringContaining(`UPDATE "${'external_notification_deliveries'}"`),
|
||
'delivery-1',
|
||
AUTO_LISTING_DELIVERY_STATUS.SUCCEEDED,
|
||
200,
|
||
null,
|
||
)
|
||
})
|
||
|
||
test('创建通知任务时应把预览图和拉取地址写入 payload', async () => {
|
||
;(prisma.material.findFirst as jest.Mock).mockResolvedValue({
|
||
id: 'material-1',
|
||
taskNo: null,
|
||
productModel: 'XG025503',
|
||
categoryName: '旗舰店',
|
||
sceneName: '客厅',
|
||
materialType: '任务-详情',
|
||
previewUrl: 'https://s3.jiansun.vip/gongxiangpan/public-display/task-detail/material-1/cover.webp',
|
||
isFullSet: true,
|
||
createdAt: new Date('2026-03-25T08:00:00.000Z'),
|
||
assets: [
|
||
{
|
||
id: 'asset-main-1',
|
||
assetType: 'main-preview',
|
||
fileUrl: 'https://s3.jiansun.vip/gongxiangpan/public-display/task-detail/material-1/main/1.webp',
|
||
fileName: 'main-1.webp',
|
||
sortOrder: 0,
|
||
},
|
||
{
|
||
id: 'asset-sku-1',
|
||
assetType: 'sku-preview',
|
||
fileUrl: 'https://s3.jiansun.vip/gongxiangpan/public-display/task-detail/material-1/sku/1.webp',
|
||
fileName: 'sku-1.webp',
|
||
sortOrder: 1,
|
||
},
|
||
{
|
||
id: 'asset-detail-1',
|
||
assetType: 'detail-preview',
|
||
fileUrl: 'https://s3.jiansun.vip/gongxiangpan/public-display/task-detail/material-1/detail/1.webp',
|
||
fileName: 'detail-1.webp',
|
||
sortOrder: 2,
|
||
},
|
||
],
|
||
})
|
||
;(prisma.$queryRawUnsafe as jest.Mock)
|
||
.mockResolvedValueOnce([
|
||
{
|
||
id: 'target-1',
|
||
name: '自动上架平台',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
isActive: true,
|
||
authType: 'none',
|
||
authConfig: {},
|
||
eventTypes: [AUTO_LISTING_EVENT_TYPE],
|
||
createdAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
updatedAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
},
|
||
])
|
||
.mockResolvedValueOnce([{ id: 'delivery-1' }])
|
||
|
||
const createdIds = await queueTaskDetailFullSetNotification('material-1')
|
||
const insertCall = (prisma.$queryRawUnsafe as jest.Mock).mock.calls[1]
|
||
const payload = JSON.parse(insertCall[9])
|
||
|
||
expect(createdIds).toEqual(['delivery-1'])
|
||
expect(payload.pullUrl).toBe(
|
||
'https://sc.jiansun.vip/api/external/product-assets?materialId=material-1',
|
||
)
|
||
expect(payload.coverPreviewUrl).toBe(
|
||
'https://s3.jiansun.vip/gongxiangpan/public-display/task-detail/material-1/cover.webp',
|
||
)
|
||
expect(payload.previewAssets).toEqual({
|
||
main: [
|
||
{
|
||
id: 'asset-main-1',
|
||
url: 'https://s3.jiansun.vip/gongxiangpan/public-display/task-detail/material-1/main/1.webp',
|
||
fileName: 'main-1.webp',
|
||
sortOrder: 0,
|
||
},
|
||
],
|
||
sku: [
|
||
{
|
||
id: 'asset-sku-1',
|
||
url: 'https://s3.jiansun.vip/gongxiangpan/public-display/task-detail/material-1/sku/1.webp',
|
||
fileName: 'sku-1.webp',
|
||
sortOrder: 1,
|
||
},
|
||
],
|
||
detail: [
|
||
{
|
||
id: 'asset-detail-1',
|
||
url: 'https://s3.jiansun.vip/gongxiangpan/public-display/task-detail/material-1/detail/1.webp',
|
||
fileName: 'detail-1.webp',
|
||
sortOrder: 2,
|
||
},
|
||
],
|
||
})
|
||
})
|
||
|
||
test('投递失败后应进入 retrying,并支持手工补发查询', async () => {
|
||
;(prisma.$transaction as jest.Mock).mockImplementation(async (callback: any) =>
|
||
callback({
|
||
$executeRawUnsafe: jest.fn().mockResolvedValue(undefined),
|
||
$queryRawUnsafe: jest.fn().mockResolvedValue([
|
||
{
|
||
id: 'delivery-2',
|
||
status: 'pending',
|
||
payload: { eventId: 'evt-2' },
|
||
attemptCount: 0,
|
||
maxAttempts: 5,
|
||
updatedAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
targetId: 'target-1',
|
||
name: '自动上架平台',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
isActive: true,
|
||
authType: 'none',
|
||
authConfig: {},
|
||
eventTypes: [AUTO_LISTING_EVENT_TYPE],
|
||
createdAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
targetUpdatedAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
},
|
||
]),
|
||
}),
|
||
)
|
||
;(prisma.$queryRawUnsafe as jest.Mock)
|
||
.mockResolvedValueOnce([
|
||
{
|
||
id: 'delivery-2',
|
||
targetId: 'target-1',
|
||
targetName: '自动上架平台',
|
||
eventType: AUTO_LISTING_EVENT_TYPE,
|
||
materialId: 'material-1',
|
||
materialType: '任务-详情',
|
||
taskNo: null,
|
||
status: AUTO_LISTING_DELIVERY_STATUS.RETRYING,
|
||
attemptCount: 1,
|
||
maxAttempts: 5,
|
||
nextRetryAt: new Date('2026-03-25T10:01:00.000Z'),
|
||
lastStatusCode: 500,
|
||
lastResponseBody: 'server error',
|
||
lastError: 'HTTP 500',
|
||
createdAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
updatedAt: new Date('2026-03-25T10:00:01.000Z'),
|
||
completedAt: null,
|
||
},
|
||
])
|
||
jest.mocked(postAutoListingCallback).mockResolvedValueOnce(500)
|
||
|
||
const result = await processExternalNotificationDelivery('delivery-2')
|
||
const deliveries = await listExternalNotificationDeliveries()
|
||
await retryExternalNotificationDelivery('delivery-2')
|
||
|
||
expect(result).toEqual({
|
||
success: false,
|
||
status: AUTO_LISTING_DELIVERY_STATUS.RETRYING,
|
||
})
|
||
expect(deliveries).toHaveLength(1)
|
||
expect(deliveries[0].status).toBe(AUTO_LISTING_DELIVERY_STATUS.RETRYING)
|
||
expect(prisma.$executeRawUnsafe).toHaveBeenCalledWith(
|
||
expect.stringContaining(`UPDATE "${'external_notification_deliveries'}"`),
|
||
'delivery-2',
|
||
AUTO_LISTING_DELIVERY_STATUS.PENDING,
|
||
)
|
||
})
|
||
|
||
test('条码图片更新通知应展开成一条码一记录并附带 lookupUrl', async () => {
|
||
;(prisma.material.findFirst as jest.Mock).mockResolvedValue({
|
||
id: 'material-1',
|
||
taskNo: 'TASK202604220001',
|
||
productModel: 'XG025503',
|
||
categoryName: '旗舰店',
|
||
sceneName: '客厅',
|
||
materialType: '任务-详情',
|
||
authorName: '旧作者',
|
||
createdAt: new Date('2026-04-22T09:00:00.000Z'),
|
||
user: {
|
||
realName: '张三',
|
||
username: 'zhangsan',
|
||
},
|
||
assets: [
|
||
{
|
||
id: 'asset-sku-1',
|
||
fileUrl: 'https://s3.jiansun.vip/gongxiangpan/public-display/task-detail/material-1/sku/1.webp',
|
||
fileName: 'sku-1.webp',
|
||
sortOrder: 0,
|
||
skuCode: 'SKU-001',
|
||
createdAt: new Date('2026-04-22T09:00:00.000Z'),
|
||
updatedAt: new Date('2026-04-22T09:05:00.000Z'),
|
||
barcodes: [{ barcode: '690000000001' }, { barcode: '690000000002' }],
|
||
},
|
||
],
|
||
})
|
||
;(prisma.materialAssetBarcode.findMany as jest.Mock).mockResolvedValue([
|
||
{
|
||
barcode: '690000000001',
|
||
assetId: 'asset-sku-1',
|
||
asset: {
|
||
createdAt: new Date('2026-04-22T09:00:00.000Z'),
|
||
sortOrder: 0,
|
||
},
|
||
},
|
||
{
|
||
barcode: '690000000002',
|
||
assetId: 'asset-sku-1',
|
||
asset: {
|
||
createdAt: new Date('2026-04-22T09:00:00.000Z'),
|
||
sortOrder: 0,
|
||
},
|
||
},
|
||
])
|
||
;(prisma.$queryRawUnsafe as jest.Mock)
|
||
.mockResolvedValueOnce([
|
||
{
|
||
id: 'target-1',
|
||
name: '自动上架平台',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
isActive: true,
|
||
authType: 'none',
|
||
authConfig: {},
|
||
eventTypes: [TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE],
|
||
createdAt: new Date('2026-04-22T10:00:00.000Z'),
|
||
updatedAt: new Date('2026-04-22T10:00:00.000Z'),
|
||
},
|
||
])
|
||
.mockResolvedValueOnce([{ id: 'delivery-barcode-1' }])
|
||
|
||
const createdIds = await queueTaskDetailBarcodeImagesUpdatedNotification('material-1', ['asset-sku-1'])
|
||
const insertCall = (prisma.$queryRawUnsafe as jest.Mock).mock.calls[1]
|
||
const payload = JSON.parse(insertCall[9])
|
||
|
||
expect(createdIds).toEqual(['delivery-barcode-1'])
|
||
expect(insertCall[3]).toBe(TASK_DETAIL_BARCODE_IMAGES_UPDATED_EVENT_TYPE)
|
||
expect(typeof insertCall[7]).toBe('string')
|
||
expect(insertCall[7]).toBe(payload.eventId)
|
||
expect(payload.items).toEqual([
|
||
{
|
||
materialId: 'material-1',
|
||
assetId: 'asset-sku-1',
|
||
skuCode: 'SKU-001',
|
||
barcode: '690000000001',
|
||
previewUrl: 'https://s3.jiansun.vip/gongxiangpan/public-display/task-detail/material-1/sku/1.webp',
|
||
fileName: 'sku-1.webp',
|
||
sortOrder: 0,
|
||
uploadedAt: '2026-04-22T09:00:00.000Z',
|
||
updatedAt: '2026-04-22T09:05:00.000Z',
|
||
uploaderName: '张三',
|
||
isLatest: true,
|
||
lookupUrl:
|
||
'https://sc.jiansun.vip/api/external/product-barcode-images?barcode=690000000001',
|
||
},
|
||
{
|
||
materialId: 'material-1',
|
||
assetId: 'asset-sku-1',
|
||
skuCode: 'SKU-001',
|
||
barcode: '690000000002',
|
||
previewUrl: 'https://s3.jiansun.vip/gongxiangpan/public-display/task-detail/material-1/sku/1.webp',
|
||
fileName: 'sku-1.webp',
|
||
sortOrder: 0,
|
||
uploadedAt: '2026-04-22T09:00:00.000Z',
|
||
updatedAt: '2026-04-22T09:05:00.000Z',
|
||
uploaderName: '张三',
|
||
isLatest: true,
|
||
lookupUrl:
|
||
'https://sc.jiansun.vip/api/external/product-barcode-images?barcode=690000000002',
|
||
},
|
||
])
|
||
})
|
||
|
||
test('重试退避时间应基于数据库 NOW() 计算间隔,避免 ::timestamp 时区偏差', async () => {
|
||
;(prisma.$transaction as jest.Mock).mockImplementationOnce(async (callback) =>
|
||
callback({
|
||
$executeRawUnsafe: jest.fn().mockResolvedValue(1),
|
||
$queryRawUnsafe: jest.fn().mockResolvedValue([
|
||
{
|
||
id: 'delivery-tz-1',
|
||
status: 'retrying',
|
||
payload: { eventId: 'evt-1' },
|
||
attemptCount: 1,
|
||
maxAttempts: 5,
|
||
updatedAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
targetId: 'target-1',
|
||
name: '自动上架平台',
|
||
callbackUrl: 'https://auto.example.com/callback',
|
||
isActive: true,
|
||
authType: 'none',
|
||
authConfig: {},
|
||
eventTypes: [AUTO_LISTING_EVENT_TYPE],
|
||
createdAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
targetUpdatedAt: new Date('2026-03-25T10:00:00.000Z'),
|
||
},
|
||
]),
|
||
}),
|
||
)
|
||
jest.mocked(postAutoListingCallback).mockResolvedValueOnce(502)
|
||
|
||
const result = await processExternalNotificationDelivery('delivery-tz-1')
|
||
expect(result).toEqual({
|
||
success: false,
|
||
status: AUTO_LISTING_DELIVERY_STATUS.RETRYING,
|
||
})
|
||
const updateCall = (prisma.$executeRawUnsafe as jest.Mock).mock.calls.at(-1)!
|
||
expect(updateCall[0]).toContain("NOW() + ($6::bigint * INTERVAL '1 millisecond')")
|
||
expect(updateCall[0]).not.toContain('::timestamp')
|
||
expect(updateCall[6]).toBe(5 * 60 * 1000)
|
||
expect(updateCall[7]).toBe(false)
|
||
})
|
||
})
|