36 lines
3.2 KiB
TypeScript
36 lines
3.2 KiB
TypeScript
import { beforeEach, expect, it, vi } from 'vitest'
|
|||
|
|
import { http } from '@/shared/api/http'
|
||
|
|
import { listFleetQuotes, createFleetQuote, updateFleetQuote, importFleetQuotes, fleetCsv, fleetMoney, fleetVersion, type FleetQuoteDraft } from './fleetQuotes'
|
||
|
|
vi.mock('@/shared/api/http', async original => ({ ...(await original<typeof import('@/shared/api/http')>()), http: { get: vi.fn(), post: vi.fn(), put: vi.fn() } }))
|
||
|
|
const details: FleetQuoteDraft = { kind: 'SALE', partyId: '90071992547409931', originCity: 'LA', destinationCode: 'ONT8', currency: 'USD', distanceMiles: 10, selfPickupPrice: 0, fullDeliveryPrice: null, looseDeliveryPrice: 2, validFrom: '2026-01-01', validTo: '2026-12-31', remark: null }
|
||
|
|
const quote = { quoteNo: 'FQ-ONE', details: { ...details, selfPickupPrice: '0', looseDeliveryPrice: '2', distanceMiles: '10' }, partyName: '委托企业', status: 'ACTIVE', recordVersion: '90071992547409931', updatedAt: '2026-10-01T00:00:00Z' }
|
||
|
|
const ok = (data: unknown) => ({ code: '200', data })
|
||
|
|
beforeEach(() => vi.clearAllMocks())
|
||
|
|
it('preserves long IDs and versions, explicit zero and absent prices', async () => {
|
||
|
|
vi.mocked(http.get).mockResolvedValue(ok({ records: [quote], total: '1', size: '20', page: '1', pages: '1' }))
|
||
|
|
const result = await listFleetQuotes({ kind: 'SALE' })
|
||
|
|
expect(result.records[0]).toMatchObject({ details, recordVersion: '90071992547409931' })
|
||
|
|
expect(result.total).toBe(1)
|
||
|
|
vi.mocked(http.put).mockResolvedValue(ok(quote)); await updateFleetQuote(result.records[0]!, details)
|
||
|
|
expect(http.put).toHaveBeenCalledWith('/platform/v1/last-leg/fleet/quotes/FQ-ONE', { recordVersion: '90071992547409931', details })
|
||
|
|
})
|
||
|
|
it('sends stable creation and batch keys without converting business references', async () => {
|
||
|
|
vi.mocked(http.post).mockResolvedValue(ok(quote)); await createFleetQuote('stable-key', details)
|
||
|
|
expect(http.post).toHaveBeenCalledWith('/platform/v1/last-leg/fleet/quotes', { idempotencyKey: 'stable-key', details })
|
||
|
|
vi.mocked(http.post).mockResolvedValue(ok({ importedRows: 1 })); await importFleetQuotes('batch-key', [details])
|
||
|
|
expect(http.post).toHaveBeenLastCalledWith('/platform/v1/last-leg/fleet/quotes/import', { idempotencyKey: 'batch-key', rows: [details] })
|
||
|
|
})
|
||
|
|
it('does not download a business error JSON as a CSV file', async () => {
|
||
|
|
vi.mocked(http.get).mockResolvedValue(new TextEncoder().encode('{"code":"CONFLICT","message":"缩小范围","requestId":"r1"}').buffer)
|
||
|
|
await expect(fleetCsv('/export', {})).rejects.toMatchObject({ message: '缩小范围', requestId: 'r1' })
|
||
|
|
})
|
||
|
|
it('preserves diagnostics from a non-200 export response', async () => {
|
||
|
|
vi.mocked(http.get).mockRejectedValue({ isAxiosError: true, response: { status: 403, data: new TextEncoder().encode('{"code":"403","message":"无导出权限","traceId":"t1"}').buffer } })
|
||
|
|
await expect(fleetCsv('/export', {})).rejects.toMatchObject({ message: '无导出权限', status: 403, traceId: 't1' })
|
||
|
|
})
|
||
|
|
it('rejects lossy money and numeric versions instead of rounding them', () => {
|
||
|
|
expect(() => fleetMoney('99999999999999999.01')).toThrow('精确展示')
|
||
|
|
expect(() => fleetVersion(9007199254740992)).toThrow('版本无效')
|
||
|
|
expect(fleetVersion('90071992547409931')).toBe('90071992547409931')
|
||
|
|
})
|