76 lines
3.1 KiB
TypeScript
76 lines
3.1 KiB
TypeScript
|
|
/** @jest-environment node */
|
|
import {
|
|
XIAOYI_AUTO_LISTING_EVENT_TYPE,
|
|
queueXiaoyiAutoListingNotification,
|
|
queueXiaoyiDeliveries,
|
|
} from '@/lib/external-auto-listing'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: { $queryRawUnsafe: jest.fn(), $executeRawUnsafe: jest.fn() },
|
|
}))
|
|
jest.mock('@/lib/public-app-url', () => ({
|
|
getPublicAppOrigin: () => 'https://sc.jiansun.vip',
|
|
}))
|
|
|
|
const event = {
|
|
materialId: 'material-1',
|
|
materialType: '自动上架-详情',
|
|
outcomeKind: 'detail' as const,
|
|
projectId: 'project-1',
|
|
}
|
|
|
|
describe('小壹自动上架通知', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
jest.useFakeTimers()
|
|
;(prisma.$queryRawUnsafe as jest.Mock).mockResolvedValue([])
|
|
})
|
|
afterEach(() => jest.useRealTimers())
|
|
|
|
test('与素材创建共用事务并按素材编号防止重复登记', async () => {
|
|
const execute = jest.fn().mockResolvedValue(1)
|
|
await queueXiaoyiAutoListingNotification({ $executeRawUnsafe: execute }, event)
|
|
const [sql, id, materialId, materialType, outcomeKind, projectId] = execute.mock.calls[0]
|
|
expect(sql).toContain('ON CONFLICT (material_id) DO NOTHING')
|
|
expect(id).toMatch(/^[0-9a-f-]{36}$/)
|
|
expect({ materialId, materialType, outcomeKind, projectId }).toEqual(event)
|
|
await expect(queueXiaoyiAutoListingNotification({ $executeRawUnsafe: execute }, {
|
|
...event, materialType: '自动上架-视频',
|
|
})).rejects.toThrow('参数无效')
|
|
expect(execute).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
test('配置接收方后按同一事件和素材编号生成可重试投递', async () => {
|
|
;(prisma.$queryRawUnsafe as jest.Mock)
|
|
.mockResolvedValueOnce([{
|
|
eventId: 'event-1', targetId: 'target-1', ...event, createdAt: new Date('2026-11-30T12:00:00Z'),
|
|
}])
|
|
.mockResolvedValueOnce([{ id: 'delivery-1' }])
|
|
await queueXiaoyiDeliveries()
|
|
const [selectSql, eventType] = (prisma.$queryRawUnsafe as jest.Mock).mock.calls[0]
|
|
expect(selectSql).toContain('t.event_types ? $1')
|
|
expect(eventType).toBe(XIAOYI_AUTO_LISTING_EVENT_TYPE)
|
|
const [insertSql, , targetId, insertedEventType, materialId, , status, rawPayload] =
|
|
(prisma.$queryRawUnsafe as jest.Mock).mock.calls[1]
|
|
expect(insertSql).toContain('ON CONFLICT ("target_id","event_type","dedupe_key") DO NOTHING')
|
|
expect({ targetId, insertedEventType, materialId, status }).toEqual({
|
|
targetId: 'target-1', insertedEventType: XIAOYI_AUTO_LISTING_EVENT_TYPE, materialId: 'material-1', status: 'pending',
|
|
})
|
|
expect(JSON.parse(rawPayload)).toEqual({
|
|
eventId: 'event-1', eventType: XIAOYI_AUTO_LISTING_EVENT_TYPE, ...event,
|
|
occurredAt: '2026-11-30T12:00:00.000Z',
|
|
pullUrl: 'https://sc.jiansun.vip/api/external/product-assets?materialId=material-1',
|
|
})
|
|
})
|
|
|
|
test('没有订阅目标时不发出请求,事件仍可在以后匹配目标', async () => {
|
|
const fetch = jest.spyOn(global, 'fetch')
|
|
await queueXiaoyiDeliveries()
|
|
expect(prisma.$queryRawUnsafe).toHaveBeenCalledTimes(1)
|
|
expect(fetch).not.toHaveBeenCalled()
|
|
fetch.mockRestore()
|
|
})
|
|
})
|