abnormalFlowReport.helpers.test.mjs 10 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'

describe('abnormalFlowReport.helpers', async () => {
  const {
    ABNORMAL_TYPE_OPTIONS,
    SUCCESS_MODAL,
    getApprovalStatusStyle,
    getRmApprovalStatusStyle,
    getBgApprovalStatusStyle,
    getBgApprovalStatusText,
    getAbnormalTypeLabel,
    formatTradeTime,
    formatTradeDate,
    normalizeVoucherUrls,
    validateReportForm,
    fenToYuan,
    mapUnionCount,
    mapPendingListItem,
    mapReportedListItem,
    mapDetailFromApi,
    unwrapRouterQuery
  } = await import('../tabWorkOrder/services/abnormalFlowReport.helpers.js')

  it('has 7 abnormal type options aligned with swagger', () => {
    assert.equal(ABNORMAL_TYPE_OPTIONS.length, 7)
    assert.equal(getAbnormalTypeLabel(4), '对公收款')
    assert.equal(getAbnormalTypeLabel(5), '混合支付')
  })

  it('success modal copy matches design', () => {
    assert.equal(SUCCESS_MODAL.title, '报备提交成功')
    assert.ok(SUCCESS_MODAL.message.includes('审核'))
  })

  it('maps approval status 1-5 colors', () => {
    assert.equal(getApprovalStatusStyle(1).text, '待区域经理审批')
    assert.equal(getApprovalStatusStyle(1).color, '#F7A431')
    assert.equal(getApprovalStatusStyle(2).color, '#F24724')
    assert.equal(getApprovalStatusStyle(5).text, '审批通过')
  })

  it('bg approval status links with rm round', () => {
    assert.equal(getBgApprovalStatusText(3, 3), '待后台审批')
    assert.equal(getBgApprovalStatusText(3, ''), '待后台审批')
    assert.equal(getBgApprovalStatusText(1, 3), '-')
    assert.equal(getBgApprovalStatusText(2, 3), '-')
  })

  it('rm log status 3 means approved; bg log status 5 means approved', () => {
    const rmPending = getRmApprovalStatusStyle(1, 3)
    const bgWhileRmPending = getBgApprovalStatusStyle(1, 3)
    assert.equal(rmPending.text, '待区域经理审批')
    assert.equal(rmPending.color, '#F7A431')
    assert.equal(bgWhileRmPending.text, '-')
    assert.equal(bgWhileRmPending.background, '#F4F5F7')

    const rmApproved = getRmApprovalStatusStyle(3, 3)
    const bgPending = getBgApprovalStatusStyle(3, 3)
    assert.equal(rmApproved.text, '区域经理已通过')
    assert.equal(rmApproved.color, '#35C376')
    assert.equal(bgPending.text, '待后台审批')
    assert.equal(bgPending.color, '#F7A431')

    const bgApproved = getBgApprovalStatusStyle(3, 5)
    assert.equal(bgApproved.text, '审批通过')
    assert.equal(bgApproved.color, '#35C376')

    const rmRejected = getRmApprovalStatusStyle(2, 3)
    const bgWhileRmRejected = getBgApprovalStatusStyle(2, 3)
    assert.equal(rmRejected.text, '区域经理审批驳回')
    assert.equal(rmRejected.color, '#F24724')
    assert.equal(bgWhileRmRejected.text, '-')

    const bgRejected = getBgApprovalStatusStyle(3, 4)
    assert.equal(bgRejected.text, '后台审批驳回')
    assert.equal(bgRejected.color, '#F24724')
  })

  it('validateReportForm requires type and remark', () => {
    assert.equal(validateReportForm({}).valid, false)
    assert.equal(validateReportForm({ abnormalType: 1 }).valid, false)
    assert.equal(
      validateReportForm({ abnormalType: 1, remark: '测试备注' }).valid,
      true
    )
  })

  it('fenToYuan converts cents', () => {
    assert.equal(fenToYuan(152052), 1520.52)
  })

  it('formatTradeTime normalizes timestamp and date string', () => {
    assert.equal(
      formatTradeTime('2020-06-12 15:12:52'),
      '2020-06-12 15:12:52'
    )
    const ts = new Date('2020-06-12 15:12:52').getTime()
    assert.equal(formatTradeTime(ts), '2020-06-12 15:12:52')
    assert.equal(formatTradeTime(''), '')
    assert.equal(formatTradeTime(null), '')
  })

  it('formatTradeDate returns date without time', () => {
    assert.equal(formatTradeDate('2020-06-14 18:00:00'), '2020-06-14')
    assert.equal(formatTradeDate('2020-02-05'), '2020-02-05')
    assert.equal(formatTradeDate(''), '')
    assert.equal(formatTradeDate(null), '')
  })

  it('normalizeVoucherUrls handles array object and comma string', () => {
    assert.deepEqual(normalizeVoucherUrls(null), [])
    assert.deepEqual(normalizeVoucherUrls(''), [])
    assert.deepEqual(normalizeVoucherUrls(['https://a.png', '', 'https://b.png']), [
      'https://a.png',
      'https://b.png'
    ])
    assert.deepEqual(
      normalizeVoucherUrls([
        { url: 'https://c.png' },
        { imageUrl: 'https://d.png' },
        { voucherImageUrl: 'https://e.png' }
      ]),
      ['https://c.png', 'https://d.png', 'https://e.png']
    )
    assert.deepEqual(
      normalizeVoucherUrls('https://f.png, https://g.png'),
      ['https://f.png', 'https://g.png']
    )
  })

  it('mapUnionCount reads tab totals', () => {
    assert.deepEqual(
      mapUnionCount({
        totalCount: 10,
        notDeclaredCount: 4,
        declaredCount: 6
      }),
      { notDeclaredCount: 4, declaredCount: 6, totalCount: 10 }
    )
  })

  it('mapPendingListItem maps union.page fields', () => {
    const item = mapPendingListItem({
      outTradeNo: 'PAY1',
      tradeNo: 'TN1',
      totalFee: 10000,
      accountDate: '2020-06-12 15:12:52',
      shopMerchantName: '门店A'
    })
    assert.equal(item.outTradeNo, 'PAY1')
    assert.equal(item.tradeRefNo, 'TN1')
    assert.equal(item.payAmount, 100)
    assert.equal(item.merchantName, '门店A')
    assert.equal(item.tradeTime, '2020-06-12 15:12:52')
    assert.equal(item.statusStyle.text, '待报备')
  })

  it('mapReportedListItem maps declaration.page fields', () => {
    const item = mapReportedListItem({
      id: 9,
      tradeNo: 'TN2',
      totalPrice: 88.8,
      accountDate: '2020-01-01',
      merchantName: '门店B',
      approvalStatus: 1
    })
    assert.equal(item.id, 9)
    assert.equal(item.statusStyle.text, '待区域经理审批')
  })

  it('unwrapRouterQuery flattens router params wrapper', () => {
    assert.deepEqual(unwrapRouterQuery({ params: { id: 1001 } }), {
      params: { id: 1001 },
      id: 1001
    })
    assert.deepEqual(unwrapRouterQuery({ id: 9 }), { id: 9 })
  })

  it('mapDetailFromApi splits approval logs by source', () => {
    const detail = mapDetailFromApi({
      tradeInfo: {
        tradeNo: 'TN3',
        totalPrice: 12,
        payDate: '2020-02-02',
        merchantName: '店C'
      },
      declarationInfo: {
        abnormalType: 1,
        remark: '备注',
        createdBy: '张三',
        createdDate: '2020-02-03',
        voucherImageUrls: ['https://a.png']
      },
      approvalLogs: [
        {
          approvalSource: 0,
          approvalStatus: 1,
          operatorName: '经理',
          createdDate: '2020-02-04',
          remark: '一审'
        },
        {
          approvalSource: 1,
          approvalStatus: 3,
          operatorName: '',
          createdDate: '',
          remark: ''
        }
      ]
    })
    assert.equal(detail.tradeRefNo, 'TN3')
    assert.equal(detail.abnormalTypeText, '已付款未录单')
    assert.equal(detail.tradeTime, '2020-02-02 08:00:00')
    assert.equal(detail.reportTime, '2020-02-03 08:00:00')
    assert.equal(detail.rmApprover, '经理')
    assert.equal(detail.rmApproveTime, '2020-02-04 08:00:00')
    assert.equal(detail.rmStatusStyle.text, '待区域经理审批')
    assert.equal(detail.bgApproveTime, '')
    assert.equal(detail.bgStatusStyle.text, '-')
    assert.equal(detail.vouchers.length, 1)
  })

  it('mapDetailFromApi maps voucherImageUrls and empty list', () => {
    const withUrls = mapDetailFromApi({
      declarationInfo: {
        abnormalType: 1,
        remark: '备注',
        voucherImageUrls: [{ url: 'https://a.png' }, 'https://b.png']
      }
    })
    assert.deepEqual(withUrls.vouchers, ['https://a.png', 'https://b.png'])

    const empty = mapDetailFromApi({
      declarationInfo: { abnormalType: 1, remark: '无凭证' }
    })
    assert.deepEqual(empty.vouchers, [])
  })

  it('mapDetailFromApi treats rm log status 3 as rm approved', () => {
    const detail = mapDetailFromApi({
      declarationInfo: { abnormalType: 1, remark: 'x', approvalStatus: 5 },
      approvalLogs: [
        {
          approvalSource: 0,
          approvalStatus: 3,
          operatorName: '何穗',
          createdDate: '2020-02-04',
          remark: '同意'
        },
        {
          approvalSource: 1,
          approvalStatus: 5,
          operatorName: '总部',
          createdDate: '2020-02-05',
          remark: '通过'
        }
      ]
    })
    assert.equal(detail.rmStatusStyle.text, '区域经理已通过')
    assert.equal(detail.bgStatusStyle.text, '审批通过')
  })

  it('mapDetailFromApi maps declarationInfo.delayDeadline', () => {
    const withDeadline = mapDetailFromApi({
      declarationInfo: {
        abnormalType: 1,
        remark: 'x',
        delayDeadline: '2020-02-05'
      }
    })
    assert.equal(withDeadline.delayDeadline, '2020-02-05')

    const without = mapDetailFromApi({
      declarationInfo: { abnormalType: 1, remark: 'x' }
    })
    assert.equal(without.delayDeadline, '')
  })

  it('mapDetailFromApi falls back to declarationInfo.approvalStatus when logs missing', () => {
    const rmPending = mapDetailFromApi({
      declarationInfo: { abnormalType: 1, remark: 'x', approvalStatus: 1 }
    })
    assert.equal(rmPending.rmStatusStyle.text, '待区域经理审批')
    assert.equal(rmPending.bgStatusStyle.text, '-')

    const bgPending = mapDetailFromApi({
      declarationInfo: { abnormalType: 1, remark: 'x', approvalStatus: 3 }
    })
    assert.equal(bgPending.rmStatusStyle.text, '区域经理已通过')
    assert.equal(bgPending.bgStatusStyle.text, '待后台审批')
  })

  it('mapDetailFromApi prefers approvalLogs over declarationInfo.approvalStatus', () => {
    const detail = mapDetailFromApi({
      declarationInfo: { abnormalType: 1, remark: 'x', approvalStatus: 1 },
      approvalLogs: [
        {
          approvalSource: 0,
          approvalStatus: 2,
          operatorName: '经理',
          createdDate: '2020-02-04',
          remark: '驳回'
        }
      ]
    })
    assert.equal(detail.rmStatusStyle.text, '区域经理审批驳回')
    assert.equal(detail.rmApprover, '经理')
  })
})