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

describe('abnormalFlowLock.helpers', async () => {
  const {
    ABNORMAL_FLOW_LOCK_BYPASS,
    shouldRestrictNewOrderToDjQrOnly,
    shouldShowAbnormalFlowTip,
    markAbnormalFlowTipDismissed,
    isAbnormalFlowLockActive,
    hasUnprocessedAbnormalFlow,
    resolveAbnormalFlowTipCount,
    shouldBypassAbnormalFlowLock,
    isAbnormalFlowWhitelistRoute,
    buildAbnormalFlowTipText,
    isAdditionRecordOrderSnapshot,
    buildAdditionRecordLockBypassContext
  } = await import('../utils/abnormalFlowLock.helpers.js')

  it('tip dismiss for entire login session', () => {
    assert.equal(shouldShowAbnormalFlowTip(null, 's1'), true)
    const storage = markAbnormalFlowTipDismissed(null, 's1')
    assert.equal(shouldShowAbnormalFlowTip(storage, 's1'), false)
    assert.equal(shouldShowAbnormalFlowTip(storage, 's2'), true)
    assert.equal(shouldShowAbnormalFlowTip(storage, 's1'), false)
  })

  it('lock active for boolean true and legacy flags', () => {
    assert.equal(isAbnormalFlowLockActive(true), true)
    assert.equal(isAbnormalFlowLockActive(false), false)
    assert.equal(
      isAbnormalFlowLockActive({ lockControlEnabled: 'Y', lockFlowFlag: 'Y' }),
      true
    )
  })

  it('unprocessed prefers totalCount for home tip', () => {
    assert.equal(
      hasUnprocessedAbnormalFlow({ totalCount: 3, notDeclaredCount: 0 }),
      true
    )
    assert.equal(
      hasUnprocessedAbnormalFlow({ totalCount: 0, notDeclaredCount: 2 }),
      false
    )
    assert.equal(hasUnprocessedAbnormalFlow({ totalCount: 0 }), false)
    assert.equal(hasUnprocessedAbnormalFlow({ abnormalFlowNum: 2 }), true)
    assert.equal(
      resolveAbnormalFlowTipCount({
        totalCount: 3,
        notDeclaredCount: 9,
        abnormalFlowNum: 1
      }),
      3
    )
    assert.equal(
      resolveAbnormalFlowTipCount({ notDeclaredCount: 4, abnormalFlowNum: 9 }),
      4
    )
  })

  it('bypass whitelist and addition/dj qr', () => {
    assert.equal(
      shouldBypassAbnormalFlowLock({
        routeName: 'tabWorkOrder.abnormalFlowList'
      }),
      true
    )
    assert.equal(isAbnormalFlowWhitelistRoute('abnormalFlowReport'), true)
    assert.equal(
      shouldBypassAbnormalFlowLock({
        orderCreateType: 'additionRecordOrder'
      }),
      true
    )
    assert.equal(shouldBypassAbnormalFlowLock({ paymentMethod: 5 }), true)
    assert.equal(shouldBypassAbnormalFlowLock({ paymentMethod: 1 }), false)
  })

  it('tip text includes count', () => {
    assert.match(buildAbnormalFlowTipText(3), /3笔异常流水/)
  })

  it('shouldRestrictNewOrderToDjQrOnly uses checkShopLock only', () => {
    const lockOn = { lockControlEnabled: 'Y', lockFlowFlag: 'Y' }

    assert.equal(shouldRestrictNewOrderToDjQrOnly(true), true)
    assert.equal(shouldRestrictNewOrderToDjQrOnly(lockOn), true)
    assert.equal(shouldRestrictNewOrderToDjQrOnly(false), false)
    assert.equal(shouldRestrictNewOrderToDjQrOnly(null), false)
    assert.equal(
      shouldRestrictNewOrderToDjQrOnly({
        lockControlEnabled: 'N',
        lockFlowFlag: 'Y'
      }),
      false
    )
  })

  it('exports bypass constants', () => {
    assert.equal(ABNORMAL_FLOW_LOCK_BYPASS.ADDITION_RECORD, 'additionRecord')
  })

  it('isAdditionRecordOrderSnapshot detects flow selection', () => {
    assert.equal(isAdditionRecordOrderSnapshot({}), false)
    assert.equal(
      isAdditionRecordOrderSnapshot({ orderCreateType: 'createdOrder' }),
      false
    )
    assert.equal(
      isAdditionRecordOrderSnapshot({ orderCreateType: 'additionRecordOrder' }),
      true
    )
    assert.equal(
      isAdditionRecordOrderSnapshot({ outTradeNo: 'T001' }),
      true
    )
    assert.equal(
      isAdditionRecordOrderSnapshot({
        selectOutTradeNoArr: [{ outTradeNo: 'T001' }]
      }),
      true
    )
    assert.equal(
      isAdditionRecordOrderSnapshot({
        orderInfo: { selectOutTradeNoArr: [{ outTradeNo: 'T002' }] }
      }),
      true
    )
  })

  it('buildAdditionRecordLockBypassContext returns bypass for 补录', () => {
    assert.deepEqual(
      buildAdditionRecordLockBypassContext({
        orderCreateType: 'createdOrder',
        paymentMethod: 3
      }),
      { orderCreateType: 'createdOrder', paymentMethod: 3 }
    )
    assert.deepEqual(
      buildAdditionRecordLockBypassContext({
        selectOutTradeNoArr: [{ outTradeNo: 'T001' }],
        paymentMethod: 1
      }),
      {
        orderCreateType: 'additionRecordOrder',
        paymentMethod: 1,
        bypassReason: ABNORMAL_FLOW_LOCK_BYPASS.ADDITION_RECORD
      }
    )
    assert.equal(
      shouldBypassAbnormalFlowLock(
        buildAdditionRecordLockBypassContext({
          outTradeNo: 'T002',
          paymentMethod: 1
        })
      ),
      true
    )
  })
})