Files
admin-web/src/modules/fba-pricing/carrier-rates/api/platformPriceBooks.test.ts
T
2026-08-07 14:47:11 +08:00

152 lines
4.9 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
const http = vi.hoisted(() => ({
get: vi.fn(),
post: vi.fn(),
}))
vi.mock('@/shared/api/http', () => ({
http,
unwrapApiResponse: <T>(response: { data: T }) => response.data,
}))
import {
downloadPlatformPriceBookSource,
getPlatformPriceBook,
listPlatformPriceBooks,
publishPlatformPriceBook,
uploadPlatformPriceBook,
} from './platformPriceBooks'
const version = {
id: '9007199254740995',
versionNo: '2',
originalFileName: '2026-08-price-book.xlsx',
sourceFileId: '9007199254740996',
sourceSha256: 'a'.repeat(64),
status: 'PENDING_CONFIRMATION' as const,
channelCount: '15',
rateCount: '42',
blockingErrorCount: '2',
warningCount: 1,
parseIssuesJson: null,
createdBy: '10001',
createdAt: '2026-08-07T10:00:00',
publishedBy: null,
publishedAt: null,
supersededAt: null,
}
const detail = {
version,
channels: [{
channelCode: 'MASON_TRUCK',
channelName: '美森正班海卡',
priceModel: 'FBA_WAREHOUSE_RATE' as const,
sourceSheetName: '美森正班海卡',
estimatedDepartureDate: '2026-08-12',
estimatedDepartureText: '8月12日',
latestWarehouseEntry: null,
vesselName: null,
voyageNo: null,
referenceTransit: null,
surchargeDescription: null,
remark: null,
}],
rates: [{
id: '9007199254740997',
channelCode: 'MASON_TRUCK',
priceModel: 'FBA_WAREHOUSE_RATE' as const,
originWarehouseId: null,
originWarehouseCode: 'CN-YIWU-01',
originWarehouseName: '义乌仓',
destinationType: 'OVERSEAS' as const,
destinationCode: 'US-LAX-01',
destinationWarehouseId: null,
destinationName: '洛杉矶仓',
regionCode: null,
pricingTaxMode: 'TAX_INCLUDED' as const,
chargeUnit: 'KG' as const,
weightFromKg: null,
unitPrice: '12.5',
currency: 'CNY',
sourceSheetName: '美森正班海卡',
sourceRowNumber: '8',
sourceDestinationText: '洛杉矶仓',
remark: null,
}],
issues: [{
severity: 'BLOCKING',
issueCode: 'ORIGIN_MAPPING_REQUIRED',
message: '起运仓需要映射到平台国内仓',
sourceSheetName: '美森正班海卡',
sourceRowNumber: '8',
sourceCell: null,
}],
}
describe('platform price-book API', () => {
beforeEach(() => vi.clearAllMocks())
it('uses the platform price-book list endpoint without customer scope and preserves long ids', async () => {
http.get.mockResolvedValue({ data: [version] })
const result = await listPlatformPriceBooks({ status: ' PENDING_CONFIRMATION ' })
expect(http.get).toHaveBeenCalledWith('/platform/v1/fba-pricing/carrier-rates/price-books', {
params: { status: 'PENDING_CONFIRMATION' },
})
expect(result[0]).toMatchObject({
id: '9007199254740995',
sourceFileId: '9007199254740996',
createdBy: '10001',
versionNo: 2,
rateCount: 42,
})
})
it('omits an empty status filter for the complete version history', async () => {
http.get.mockResolvedValue({ data: [] })
await listPlatformPriceBooks({ status: ' ' })
expect(http.get).toHaveBeenCalledWith('/platform/v1/fba-pricing/carrier-rates/price-books', { params: {} })
})
it('uploads one workbook, loads its parsed detail, and normalizes parsed rows', async () => {
http.post.mockResolvedValue({ data: detail })
http.get.mockResolvedValue({ data: detail })
const file = new File(['book'], 'price-book.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
const uploadResult = await uploadPlatformPriceBook(file)
const detailResult = await getPlatformPriceBook('9007199254740995')
expect(http.post).toHaveBeenCalledWith('/platform/v1/fba-pricing/carrier-rates/price-books', expect.any(FormData))
const form = http.post.mock.calls[0][1] as FormData
expect(form.get('file')).toBe(file)
expect(http.get).toHaveBeenCalledWith('/platform/v1/fba-pricing/carrier-rates/price-books/9007199254740995')
expect(uploadResult.rates[0]).toMatchObject({ id: '9007199254740997', unitPrice: 12.5, sourceRowNumber: 8 })
expect(detailResult.issues[0].sourceRowNumber).toBe(8)
})
it('publishes only confirmed warehouse mappings and downloads the original workbook', async () => {
http.post.mockResolvedValue({ data: detail })
const request = {
originMappings: [{ originWarehouseCode: 'CN-YIWU-01', warehouseId: '20001' }],
overseasMappings: [{ sourceDestinationName: '洛杉矶仓', warehouseId: '20011' }],
}
await publishPlatformPriceBook('9007199254740995', request)
await downloadPlatformPriceBookSource('9007199254740995')
expect(http.post).toHaveBeenCalledWith(
'/platform/v1/fba-pricing/carrier-rates/price-books/9007199254740995/publish',
request,
)
expect(http.get).toHaveBeenCalledWith(
'/platform/v1/fba-pricing/carrier-rates/price-books/9007199254740995/source',
{ responseType: 'blob' },
)
})
})