gongxiangp2/lib/__tests__/desktop-delivery-barcode.test.ts

62 lines
2.9 KiB
TypeScript

/** @jest-environment node */
import { createDesktopDelivery } from '@/lib/desktop-client/deliveries'
import { findDetailMaterialMatches } from '@/lib/detail-generator/materials'
import { prisma } from '@/lib/prisma'
jest.mock('@/lib/prisma', () => ({ prisma: { $queryRawUnsafe: jest.fn() } }))
jest.mock('@/lib/detail-generator/materials', () => ({ findDetailMaterialMatches: jest.fn() }))
jest.mock('node:fs/promises', () => ({ ...jest.requireActual('node:fs/promises'), mkdir: jest.fn().mockRejectedValue(new Error('upload-stage')) }))
jest.mock('@/lib/material-facets-cache', () => ({ invalidateMaterialFacetsCache: jest.fn() }))
jest.mock('@/lib/remote-upload-sync', () => ({ syncStoredUploadFileToRemote: jest.fn() }))
jest.mock('@/lib/upload-sessions', () => ({ getOwnedUploadSession: jest.fn() }))
jest.mock('@/lib/upload-paths', () => ({
getUploadRootDir: () => '/test',
buildStoredUploadPath: jest.fn(),
getUploadStorageExtension: jest.fn(),
inspectUploadOutput: jest.fn(),
}))
jest.mock('@/lib/path-security', () => ({ resolveStrictChildPath: jest.fn() }))
jest.mock('@/lib/detail-generator/repository', () => ({ detailTransactionLock: jest.fn() }))
jest.mock('@/lib/external-auto-listing', () => ({
ensureExternalNotificationTables: jest.fn(),
queueXiaoyiAutoListingNotification: jest.fn(),
wakeXiaoyiAutoListingNotifications: jest.fn(),
}))
const file = (uploadId: string, fileName: string) => ({
uploadId, fileName, fileSize: 1, sha256: 'a'.repeat(64),
})
const input = {
projectId: 'project-1', requestId: 'request-1', kind: 'detail' as const, productModel: 'SPU-1',
assets: {
main: [file('upload-main', 'main.jpg')],
detail: [file('upload-detail', 'detail.jpg')],
sku: [{ ...file('upload-sku', '00123.jpg'), barcode: '00123' }],
},
}
beforeEach(() => {
jest.clearAllMocks()
;(prisma.$queryRawUnsafe as jest.Mock).mockResolvedValueOnce([{ id: 'project-1' }]).mockResolvedValueOnce([])
})
test('未登记的条码可按用户填写入库', async () => {
;(findDetailMaterialMatches as jest.Mock).mockResolvedValue([])
await expect(createDesktopDelivery('owner', input, async () => {})).rejects.toThrow('upload-stage')
expect(findDetailMaterialMatches).toHaveBeenCalledWith('owner', 'barcode', '00123', true)
})
test('条码已属于另一个 SPU 时终止入库', async () => {
;(findDetailMaterialMatches as jest.Mock).mockResolvedValueOnce([])
.mockResolvedValueOnce([{ productModel: 'SPU-2', skuCode: 'SKU', barcodes: ['00123'] }])
await expect(createDesktopDelivery('owner', input, async () => {})).rejects.toThrow('条码 00123 已登记在其他 SPU')
})
test('该 SPU 的登记条码可继续入库', async () => {
;(findDetailMaterialMatches as jest.Mock).mockResolvedValueOnce([
{ productModel: 'SPU-1', skuCode: 'SKU', barcodes: ['00123'] },
])
await expect(createDesktopDelivery('owner', input, async () => {})).rejects.toThrow('upload-stage')
expect(findDetailMaterialMatches).toHaveBeenCalledTimes(1)
})