124 lines
3.3 KiB
TypeScript
124 lines
3.3 KiB
TypeScript
import { beforeEach, describe, expect, it, jest } from '@jest/globals'
|
|
|
|
jest.mock('@/server/services', () => ({
|
|
panelService: {
|
|
findAssemblySemanticsForUser: jest.fn(),
|
|
bulkSaveAssemblySemanticsForUser: jest.fn(),
|
|
},
|
|
}))
|
|
|
|
jest.mock('@/server/core/response', () => ({
|
|
handleRequest: jest.fn(async (request: unknown, handler: any) =>
|
|
handler({
|
|
request,
|
|
requestId: 'req-test',
|
|
startTime: Date.now(),
|
|
user: {
|
|
id: 'user-1',
|
|
email: 'qa@test.com',
|
|
name: 'QA',
|
|
role: 'ADMIN',
|
|
status: 'ACTIVE',
|
|
},
|
|
}),
|
|
),
|
|
jsonSuccess: jest.fn((data, status = 200, requestId) => ({
|
|
status,
|
|
payload: {
|
|
success: true,
|
|
data,
|
|
meta: { requestId },
|
|
},
|
|
})),
|
|
jsonError: jest.fn((code, message, status = 400, requestId) => ({
|
|
status,
|
|
payload: {
|
|
success: false,
|
|
error: { code, message },
|
|
meta: { requestId },
|
|
},
|
|
})),
|
|
}))
|
|
|
|
const { GET, PUT } = require('@/app/api/products/[id]/assembly/semantics/route')
|
|
const { panelService } = require('@/server/services')
|
|
|
|
function createGetRequest(url: string) {
|
|
return { url } as any
|
|
}
|
|
|
|
function createPutRequest(url: string, body: unknown) {
|
|
return {
|
|
url,
|
|
json: jest.fn(async () => body),
|
|
} as any
|
|
}
|
|
|
|
describe('GET/PUT /api/products/[id]/assembly/semantics', () => {
|
|
const findAssemblySemanticsForUser = jest.mocked(panelService.findAssemblySemanticsForUser)
|
|
const bulkSaveAssemblySemanticsForUser = jest.mocked(panelService.bulkSaveAssemblySemanticsForUser)
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
findAssemblySemanticsForUser.mockResolvedValue({
|
|
panels: [],
|
|
} as any)
|
|
bulkSaveAssemblySemanticsForUser.mockResolvedValue({
|
|
updatedCount: 1,
|
|
panels: [],
|
|
} as any)
|
|
})
|
|
|
|
it('GET 应按 skuId 读取当前 SKU 的装配语义', async () => {
|
|
await GET(
|
|
createGetRequest('http://localhost/api/products/product-1/assembly/semantics?skuId=sku-a'),
|
|
{ params: Promise.resolve({ id: 'product-1' }) },
|
|
)
|
|
|
|
expect(findAssemblySemanticsForUser).toHaveBeenCalledWith(
|
|
'product-1',
|
|
expect.objectContaining({ id: 'user-1' }),
|
|
expect.objectContaining({ skuId: 'sku-a' }),
|
|
)
|
|
})
|
|
|
|
it('PUT 应走批量保存接口,并保留多板件依赖关系', async () => {
|
|
const updates = [
|
|
{
|
|
panelId: 'panel-a',
|
|
assemblySemantic: {
|
|
version: '1.0',
|
|
mountMode: 'slide_in',
|
|
assemblyAxis: [1, 0, 0],
|
|
assemblyDirection: 'right',
|
|
preAssembleOffset: [120, 0, 0],
|
|
travelDistance: 120,
|
|
dependsOn: ['panel-b'],
|
|
primarySupportPanelId: 'panel-b',
|
|
targetPanelIds: ['panel-b'],
|
|
mates: { left: ['panel-b'] },
|
|
confidence: 1,
|
|
warnings: [],
|
|
autoGenerated: false,
|
|
},
|
|
},
|
|
]
|
|
|
|
await PUT(
|
|
createPutRequest(
|
|
'http://localhost/api/products/product-1/assembly/semantics?skuId=sku-a',
|
|
{ updates },
|
|
),
|
|
{ params: Promise.resolve({ id: 'product-1' }) },
|
|
)
|
|
|
|
expect(bulkSaveAssemblySemanticsForUser).toHaveBeenCalledWith(
|
|
'product-1',
|
|
updates,
|
|
expect.objectContaining({ id: 'user-1' }),
|
|
expect.objectContaining({ requestId: 'req-test' }),
|
|
expect.objectContaining({ skuId: 'sku-a' }),
|
|
)
|
|
})
|
|
})
|