lowPriceGoodsValidation.integration.test.mjs 5.07 KB
import { readFileSync } from 'node:fs'
import { register } from 'node:module'
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'

register(
  '../../../../tests/work-order-migration/shims/create-order-context-loader.mjs',
  import.meta.url
)

const pageSource = readFileSync(
  new URL('../createOrder.js', import.meta.url),
  'utf8'
)
const pageTemplate = readFileSync(
  new URL('../createOrder.wxml', import.meta.url),
  'utf8'
)
const rowComponentSource = readFileSync(
  new URL('../components/OrderGoodsRow/order-goods-row.js', import.meta.url),
  'utf8'
)
const rowComponentTemplate = readFileSync(
  new URL('../components/OrderGoodsRow/order-goods-row.wxml', import.meta.url),
  'utf8'
)
const chooseGoodsStart = pageSource.indexOf('  chooseGoods(e) {')
const chooseGoodsEnd = pageSource.indexOf('\n  isLowPriceOrder()', chooseGoodsStart)
const chooseGoodsSource = pageSource.slice(chooseGoodsStart, chooseGoodsEnd)

describe('createOrder low-price goods integration', () => {
  it('IT-WO-LPG-008: selection only syncs state while live price input enables the reminder', () => {
    assert.match(
      pageSource,
      /LOW_PRICE_NOT_TRIGGERED_MESSAGE[\s\S]*applyLowPriceGoodsValidation/
    )
    assert.match(
      pageSource,
      /Number\(this\.data\.orderObj\?\.lowPriceFlag\) === 1/
    )
    assert.match(chooseGoodsSource, /applyLowPriceGoodsRules\(selectedGoodsList\)/)
    assert.doesNotMatch(chooseGoodsSource, /notify:\s*true/)
    assert.match(
      pageSource,
      /changeMealPrice\(e\)[\s\S]*applyLowPriceGoodsRules\(goodsList,\s*\{\s*notify:\s*true\s*\}\)/
    )
    assert.match(
      pageSource,
      /changePrice\(e\)[\s\S]*applyLowPriceGoodsRules\(goodsList,\s*\{\s*notify:\s*true\s*\}\)/
    )
    assert.match(
      pageSource,
      /wx\.\$util\.showToast\(LOW_PRICE_NOT_TRIGGERED_MESSAGE\)/
    )
  })

  it('IT-WO-LPG-009: revalidates the full goods list before building the submit payload', () => {
    assert.match(
      pageSource,
      /checkGoods\(save, subOrderList, priceDisabled, goodsList, orderObj\)[\s\S]*Number\(orderObj\?\.lowPriceFlag\) === 1[\s\S]*applyLowPriceGoodsValidation\(goodsList\)[\s\S]*LOW_PRICE_NOT_TRIGGERED_MESSAGE/
    )
  })

  it('IT-WO-LPG-010: passes lowPriceFlag to every selected-goods row and renders the minimum-price echo only for low-price orders', () => {
    const rowTags = [
      ...pageTemplate.matchAll(/<order-goods-row[\s\S]*?\/>/g)
    ].map(match => match[0])

    assert.ok(rowTags.length > 0)
    rowTags.forEach(tag => {
      assert.match(tag, /lowPriceFlag="\{\{ orderObj\.lowPriceFlag \}\}"/)
    })
    assert.match(rowComponentSource, /lowPriceFlag:\s*\{[\s\S]*type:\s*Number/)
    assert.match(
      rowComponentTemplate,
      /class="low-price-minimum"\s+wx:if="\{\{ lowPriceFlag == 1 \}\}">\{\{product\.lowPriceMinPriceText\}\}/
    )
  })

  it('IT-WO-LPG-011: selection stays silent, live input warns, and submit blocks invalid low-price goods', async () => {
    let pageConfig
    const toastMessages = []
    globalThis.getApp = () => ({ globalData: {} })
    globalThis.Page = config => {
      pageConfig = config
    }
    globalThis.wx = {
      getAccountInfoSync: () => ({
        miniProgram: { appId: 'test-app-id', envVersion: 'develop' }
      }),
      getSystemInfoSync: () => ({ platform: 'devtools' }),
      getStorageSync: () => '',
      $util: {
        getUserInfo: () => ({}),
        showToast: message => toastMessages.push(message)
      }
    }

    await import(`../createOrder.js?low-price-goods=${Date.now()}`)

    const lowPriceGoods = {
      validMinSellPrice: 'N',
      minSellPrice: 100,
      sellPrice: 80
    }
    const lowPriceGoodsList = [{ goodsFlag: 1, goods: lowPriceGoods }]
    const lowPricePage = {
      ...pageConfig,
      data: {
        orderObj: { lowPriceFlag: 1 },
        goodsList: lowPriceGoodsList
      },
      setData(patch) {
        Object.assign(this.data, patch)
      }
    }

    lowPricePage.chooseGoods({ detail: lowPriceGoodsList })

    assert.equal(lowPriceGoods.lowPriceMinPriceText, '最低价:未开启')
    assert.deepEqual(toastMessages, [])

    const liveResult = lowPricePage.applyLowPriceGoodsRules(
      lowPriceGoodsList,
      { notify: true }
    )
    const submitMessage = lowPricePage.checkGoods(
      false,
      [],
      true,
      lowPriceGoodsList,
      lowPricePage.data.orderObj
    )

    assert.equal(liveResult.valid, false)
    assert.equal(lowPriceGoods.errorTarget, true)
    assert.deepEqual(toastMessages, [
      '价格未触发最低价,其他正常单请到生意助手侧录单'
    ])
    assert.equal(
      submitMessage,
      '价格未触发最低价,其他正常单请到生意助手侧录单'
    )

    const normalGoods = {
      validMinSellPrice: 'Y',
      minSellPrice: 100,
      sellPrice: 80
    }
    const normalPage = {
      ...pageConfig,
      data: {
        orderObj: { lowPriceFlag: 0 },
        goodsList: [{ goodsFlag: 1, goods: normalGoods }]
      }
    }
    assert.equal(normalPage.applyLowPriceGoodsRules().valid, true)
    assert.equal(normalGoods.lowPriceMinPriceText, undefined)
  })
})