import { describe, expect, it } from 'vitest' import type { OverseasFeeRetryRecord } from './api/overseasFees' import { retryFailureReason } from './overseasFeeRetryPresentation' const failed = (errorMessage: string | null, action = '重试推送'): OverseasFeeRetryRecord => ({ requestId: 'retry-1', requestedAt: '', operatorName: '操作员', action, result: 'FAILED', errorMessage, }) describe('费用重试失败原因', () => { it('指出财务接收失败,但不把通用内部错误猜成字段故障', () => { const text = retryFailureReason(failed('EventDeliveryFailure: Consumer rejected event: code=S0500, message=Internal server error, requestId=abc, traceId=def')) expect(text).toBe('推送结算失败:财务系统处理异常,请联系管理员排查后重试。') expect(text).not.toMatch(/字段|EventDeliveryFailure|requestId|traceId/) }) it('只有明确的字段错误才说明字段长度不足', () => { expect(retryFailureReason(failed("Data truncation: Data too long for column 'payload_hash' at row 1"))) .toBe('推送结算失败:财务系统的摘要字段长度不足,请联系管理员更新数据库后重试。') }) it('扣费与余额不足明确指出客户账户及处理方式', () => { expect(retryFailureReason(failed('可用额度不足', '重试扣费'))) .toBe('扣费失败:客户余额及可用授信不足,请充值或调整授信后重试。') }) it('超时不能当作未扣款,提示先核查实际费用状态', () => { expect(retryFailureReason(failed('Read timed out', '重试资金指令'))) .toBe('发送资金指令失败:未及时收到财务处理结果,请先核对费用状态再重试。') }) it.each([ ['Connection refused', '无法连接财务系统'], ["Table 'baiyue_finance.fin_inbox_event' doesn't exist", '缺少必要的表或字段'], [null, '未返回具体原因'], ['UnknownFailure: unrecognized', '根据技术详情排查'], ['Consumer rejected event: code=FIN-EVT-017, message=费用来源与成功扣款的客户、业务或金额不匹配, requestId=abc', '费用来源与成功扣款的客户、业务或金额不匹配'], ])('显示已知原因,未知异常保留排查入口:%s', (raw, expected) => { expect(retryFailureReason(failed(raw))).toContain(expected) }) it('成功及已提交记录不展示过期失败原因', () => { for (const result of ['SUCCESS', 'SUBMITTED'] as const) { expect(retryFailureReason({ ...failed('旧失败原因'), result })).toBe('—') } }) })