68 lines
2.9 KiB
TypeScript
68 lines
2.9 KiB
TypeScript
/** @jest-environment node */
|
|
import { prisma } from '@/lib/prisma'
|
|
import { applyListingReceipt, recordListingStatus, type ListingStatus } from '@/lib/xiaoyi-listing-status'
|
|
|
|
jest.mock('@/lib/prisma', () => ({ prisma: { $transaction: jest.fn() } }))
|
|
|
|
const listed: ListingStatus = {
|
|
eventId: 'event-1', sequence: 2, state: 'listed', listingId: 'listing-1', message: null,
|
|
updatedAt: '2026-12-04T10:00:00.000Z',
|
|
}
|
|
const now = '2026-12-05T10:00:00.000Z'
|
|
|
|
test('同一成功回执重复提交保持幂等,新序号也可回传成功', () => {
|
|
const { updatedAt, ...update } = listed
|
|
expect(applyListingReceipt(listed, update, now)).toEqual({
|
|
status: 200, applied: false, listingStatus: listed,
|
|
})
|
|
expect(applyListingReceipt(listed, { ...update, sequence: 3 }, now)).toEqual({
|
|
status: 200, applied: true, listingStatus: { ...update, sequence: 3, updatedAt: now },
|
|
})
|
|
})
|
|
|
|
test.each(['listing', 'failed'] as const)('同一事件上架成功后拒绝用更大序号改为 %s', state => {
|
|
expect(applyListingReceipt(listed, { ...listed, sequence: 3, state }, now)).toMatchObject({
|
|
status: 409, code: 'ALREADY_LISTED',
|
|
})
|
|
})
|
|
|
|
test('修订后的事件可以从上架中转为成功,期间保留已上架状态', async () => {
|
|
const identity = { materialId: 'material-1', channel: 'auto-listing', store: '' }
|
|
const receipts = { [listed.eventId]: listed }
|
|
const tx = {
|
|
$queryRaw: jest.fn().mockResolvedValue([{
|
|
eventId: 'event-2', legacyEventId: null, deletedAt: null, listingStatus: listed,
|
|
}]),
|
|
channelListing: {
|
|
findUnique: jest.fn().mockResolvedValue({ state: 'listed', listingId: listed.listingId, receipts }),
|
|
upsert: jest.fn(),
|
|
},
|
|
$executeRaw: jest.fn(),
|
|
}
|
|
jest.mocked(prisma.$transaction).mockImplementation(async action => action(tx as any))
|
|
const update = { eventId: 'event-2', sequence: 1, state: 'listing', listingId: null, message: null } as const
|
|
|
|
await expect(recordListingStatus(identity.materialId, update)).resolves.toMatchObject({
|
|
status: 200, applied: true, listingStatus: update,
|
|
})
|
|
expect(tx.channelListing.upsert).toHaveBeenLastCalledWith(expect.objectContaining({
|
|
update: { state: 'listed', listingId: listed.listingId, receipts: {
|
|
'event-1': listed,
|
|
'event-2': { ...update, updatedAt: expect.any(String) },
|
|
} },
|
|
}))
|
|
expect(tx.$executeRaw).not.toHaveBeenCalled()
|
|
|
|
const success = { ...update, sequence: 2, state: 'listed', listingId: 'listing-2' } as const
|
|
await expect(recordListingStatus(identity.materialId, success)).resolves.toMatchObject({
|
|
status: 200, applied: true, listingStatus: success,
|
|
})
|
|
expect(tx.channelListing.upsert).toHaveBeenLastCalledWith(expect.objectContaining({
|
|
update: { state: 'listed', listingId: success.listingId, receipts: {
|
|
'event-1': listed,
|
|
'event-2': { ...success, updatedAt: expect.any(String) },
|
|
} },
|
|
}))
|
|
expect(tx.$executeRaw).toHaveBeenCalledTimes(1)
|
|
})
|