import { afterEach, describe, expect, it, vi } from 'vitest' import { ApiError } from './api' import { createPaymentPlan, createPurchaseCredential, listPaymentPlans, listPurchaseCredentials, purchaseConfigError, setPurchaseCredentialEnabled, updatePaymentPlan } from './purchase-config-api' vi.mock('./api',()=>({api:{get:vi.fn(),post:vi.fn(),put:vi.fn()},ApiError:class ApiError extends Error{code?:string;status?:number;constructor(message:string,code?:string,status?:number){super(message);this.code=code;this.status=status}}})) import { api } from './api' describe('purchase config API',()=>{ afterEach(()=>vi.clearAllMocks()) it('uses admin API endpoints and exact write payloads',async()=>{ vi.mocked(api.get).mockResolvedValue({data:{data:[]}} as never);vi.mocked(api.post).mockResolvedValue({data:{data:{}}} as never);vi.mocked(api.put).mockResolvedValue({data:{data:{}}} as never) const input={product_id:3,code:'p30',name:'30天',amount_cents:5000,currency:'CNY' as const,license_days:30,enabled:false} const update={...input,version:2} await listPaymentPlans();await createPaymentPlan(input);await updatePaymentPlan(8,update);await listPurchaseCredentials();await createPurchaseCredential({customer_id:4,product_id:3,label:'桌面端'});await setPurchaseCredentialEnabled(9,false) expect(api.get).toHaveBeenNthCalledWith(1,'/payment-plans');expect(api.post).toHaveBeenCalledWith('/payment-plans',input);expect(api.put).toHaveBeenNthCalledWith(1,'/payment-plans/8',update);expect(api.get).toHaveBeenNthCalledWith(2,'/purchase-credentials');expect(api.post).toHaveBeenCalledWith('/purchase-credentials',{customer_id:4,product_id:3,label:'桌面端'});expect(api.put).toHaveBeenNthCalledWith(2,'/purchase-credentials/9',{enabled:false}) }) it('returns fixed allowlisted errors and never returns raw server messages',()=>{ expect(purchaseConfigError(new ApiError('secret backend detail','invalid_request',400),'fallback')).toBe('填写内容无效,请检查金额、固定天数与客户产品范围') expect(purchaseConfigError(new ApiError('secret backend detail','unknown_error',500),'安全的默认提示')).toBe('安全的默认提示') expect(purchaseConfigError(new ApiError('x','x',401),'fallback')).toBe('登录已失效,请重新登录') }) })