68 lines
3.0 KiB
TypeScript
68 lines
3.0 KiB
TypeScript
/** @jest-environment node */
|
|
import { queueXiaoyiAutoListingNotification, queueXiaoyiDeliveries } from '@/lib/external-auto-listing'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
$transaction: jest.fn(), $queryRawUnsafe: jest.fn(),
|
|
externalNotificationDelivery: { createMany: jest.fn() },
|
|
},
|
|
}))
|
|
jest.mock('@/lib/public-app-url', () => ({
|
|
getPublicAppOrigin: () => 'https://sc.jiansun.vip',
|
|
}))
|
|
|
|
const data = {
|
|
materialId: 'material-1', materialType: '自动上架-详情', outcomeKind: 'detail' as const, projectId: 'project-1',
|
|
}
|
|
const events = ['1', '2'].map(id => ({
|
|
...data, eventId: 'event-' + id, revisionId: 'revision-' + id, createdAt: new Date('2026-11-30T12:00:00Z'),
|
|
}))
|
|
|
|
beforeEach(() => {
|
|
jest.resetAllMocks()
|
|
jest.mocked(prisma.$queryRawUnsafe).mockResolvedValue([])
|
|
})
|
|
|
|
test('每次完结在原事务追加独立的版本事件', async () => {
|
|
const create = jest.fn()
|
|
const tx = { xiaoyiAutoListingOutbox: { create } } as unknown as Parameters<typeof queueXiaoyiAutoListingNotification>[0]
|
|
for (const { revisionId } of events) await queueXiaoyiAutoListingNotification(tx, { ...data, revisionId })
|
|
expect(create.mock.calls).toEqual(events.map(({ revisionId }) => [{ data: { ...data, revisionId } }]))
|
|
await expect(queueXiaoyiAutoListingNotification(tx, { ...data, revisionId: 'invalid', materialType: '自动上架-视频' }))
|
|
.rejects.toThrow('参数无效')
|
|
})
|
|
|
|
test('同一素材的两个待投事件均展开,通知、拉取和重放键保持对应版本', async () => {
|
|
const tx = {
|
|
$queryRaw: jest.fn().mockResolvedValueOnce(events).mockResolvedValueOnce([{ id: 'target' }]),
|
|
externalNotificationDelivery: { createMany: jest.fn() },
|
|
xiaoyiAutoListingOutbox: { updateMany: jest.fn() },
|
|
}
|
|
jest.mocked(prisma.$transaction).mockImplementation(async action => action(tx as any))
|
|
await expect(queueXiaoyiDeliveries()).resolves.toBe(2)
|
|
expect(tx.externalNotificationDelivery.createMany).toHaveBeenCalledWith({
|
|
skipDuplicates: true,
|
|
data: events.map(({ eventId, revisionId }) => expect.objectContaining({
|
|
targetId: 'target', materialId: data.materialId, dedupeKey: eventId, status: 'pending',
|
|
payload: expect.objectContaining({
|
|
eventId, revisionId,
|
|
pullUrl: 'https://sc.jiansun.vip/api/external/product-assets?materialId=material-1&revisionId=' + revisionId,
|
|
}),
|
|
})),
|
|
})
|
|
expect(tx.xiaoyiAutoListingOutbox.updateMany).toHaveBeenCalledWith({
|
|
where: { id: { in: ['event-1', 'event-2'] } }, data: { expandedAt: expect.any(Date) },
|
|
})
|
|
})
|
|
|
|
test('没有订阅目标时版本事件仍未展开', async () => {
|
|
const tx = {
|
|
$queryRaw: jest.fn().mockResolvedValueOnce(events).mockResolvedValueOnce([]),
|
|
xiaoyiAutoListingOutbox: { updateMany: jest.fn() },
|
|
}
|
|
jest.mocked(prisma.$transaction).mockImplementation(async action => action(tx as any))
|
|
await expect(queueXiaoyiDeliveries()).resolves.toBe(0)
|
|
expect(tx.xiaoyiAutoListingOutbox.updateMany).not.toHaveBeenCalled()
|
|
})
|