105 lines
2.8 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-require-imports */
export {}
const mockHandleRequest = jest.fn()
const mockJsonError = jest.fn()
const mockJsonSuccess = jest.fn()
const mockGetCase = jest.fn()
jest.mock('@/server/core/response', () => ({
handleRequest: (...args: unknown[]) => mockHandleRequest(...args),
jsonError: (...args: unknown[]) => mockJsonError(...args),
jsonSuccess: (...args: unknown[]) => mockJsonSuccess(...args),
}))
jest.mock('@/server/services/bom-costing/bom-costing.service', () => ({
bomCostingService: {
getCase: (...args: unknown[]) => mockGetCase(...args),
},
}))
jest.mock('@/server/services/bom-costing/bom-costing-scenario.service', () => ({
bomCostingScenarioService: {},
}))
const { AppError, ErrorCode } = require('@/server/core/errors')
const { POST } = require('./route')
describe('POST /api/bom-costing/cases/:id/scenarios', () => {
const user = {
id: 'user-1',
role: 'USER',
businessRoles: ['OPERATIONS'],
}
beforeEach(() => {
jest.clearAllMocks()
mockHandleRequest.mockImplementation(async (_request, handler) =>
handler({ requestId: 'req-scenario', user }),
)
mockJsonError.mockImplementation((code, message, status, requestId) => ({
success: false,
error: { code, message },
status,
requestId,
}))
mockJsonSuccess.mockImplementation((data, status, requestId) => ({
success: true,
data,
status,
requestId,
}))
})
it('关闭换材质计算,但仍验证当前用户可以访问目标案例', async () => {
mockGetCase.mockResolvedValue({ id: 'case-ready' })
const request = {
json: async () => ({
skuId: 'sku-1',
targetMaterialId: 'material-1',
}),
}
const response = await POST(request, {
params: Promise.resolve({ id: 'case-ready' }),
})
expect(mockGetCase).toHaveBeenCalledWith(
'case-ready',
user,
)
expect(response).toMatchObject({
success: false,
status: 409,
error: { code: ErrorCode.VALIDATION_FAILED, message: expect.stringContaining('换材质预估已暂时关闭') },
})
})
it('没有案例读取权限时维持原有鉴权错误', async () => {
mockGetCase.mockRejectedValue(
new AppError(
ErrorCode.VALIDATION_FAILED,
'当前核算尚未完成,请等待本次处理结束后再调整成本',
),
)
const request = {
json: async () => ({
skuId: 'sku-1',
targetMaterialId: 'material-1',
}),
}
const response = await POST(request, {
params: Promise.resolve({ id: 'case-processing' }),
})
expect(response).toMatchObject({
success: false,
status: 422,
error: { code: ErrorCode.VALIDATION_FAILED },
})
expect(mockGetCase).toHaveBeenCalledWith('case-processing', user)
})
})