gongxiangp2/lib/__tests__/xiaoyi-auto-listing-notification.test.ts

95 lines
4.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',
statusUrl: '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()
})
test('原位修订通知可更新事件并按新 eventId 去重投递,不被旧事件吞掉', async () => {
const execute = jest.fn().mockResolvedValue(1)
await queueXiaoyiAutoListingNotification({ $executeRawUnsafe: execute }, { ...event, replaceExisting: true })
const [sql] = execute.mock.calls[0]
expect(sql).toContain('ON CONFLICT (material_id) DO UPDATE')
;(prisma.$queryRawUnsafe as jest.Mock)
.mockResolvedValueOnce([{
eventId: 'event-rev-2', targetId: 'target-1', ...event, createdAt: new Date('2026-12-01T12:00:00Z'),
}])
.mockResolvedValueOnce([{ id: 'delivery-rev-2' }])
await queueXiaoyiDeliveries()
const [selectSql] = (prisma.$queryRawUnsafe as jest.Mock).mock.calls[0]
expect(selectSql).toContain('d.dedupe_key=e.id OR (d.dedupe_key=e.material_id AND d.created_at>=e.created_at)')
const insertCall = (prisma.$queryRawUnsafe as jest.Mock).mock.calls[1]
expect(insertCall.at(-1)).toBe('event-rev-2')
})
})