createOrderPage.contract.test.mjs 4.4 KB
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import { fileURLToPath } from 'node:url'

const pageSource = readFileSync(
  fileURLToPath(new URL('../createOrder.js', import.meta.url)),
  'utf8'
)
const submitSource = readFileSync(
  fileURLToPath(new URL('../submitStrategy.js', import.meta.url)),
  'utf8'
)
const routeSource = readFileSync(
  fileURLToPath(new URL('../routeStrategy.js', import.meta.url)),
  'utf8'
)

const secondStageServiceSources = [
  './services/orderValidation.helpers.js',
  './services/paymentSelection.helpers.js',
  './services/orderHydration.helpers.js',
  './services/goodsChooseEcho.workflow.js'
].map(relativePath =>
  readFileSync(
    fileURLToPath(new URL(`../${relativePath}`, import.meta.url)),
    'utf8'
  )
)

test('boutique create page contains no temporary console logging', () => {
  assert.doesNotMatch(pageSource, /console\.log\s*\(/)
})

test('boutique create page removes the disabled wait-order polling path', () => {
  assert.doesNotMatch(pageSource, /\bgetWaitOrder\s*\(/)
  assert.doesNotMatch(pageSource, /\bwaitinputorder\s*\(/)
  assert.doesNotMatch(pageSource, /\bwaitInputCount\b/)
})

test('boutique create page does not retain empty lifecycle handlers', () => {
  assert.doesNotMatch(
    pageSource,
    /onPullDownRefresh\s*:\s*function\s*\(\)\s*\{\s*\}/
  )
  assert.doesNotMatch(
    pageSource,
    /onReachBottom\s*:\s*function\s*\(\)\s*\{\s*\}/
  )
})

test('boutique create page delegates second-stage decisions while retaining compatibility methods', () => {
  const contracts = [
    ['checkRequired', 'validateBoutiqueRequiredFields'],
    ['checkPhoneUseConfig', 'resolveConfiguredPhone'],
    ['setOrderObj', 'buildBoutiqueOrderHydration'],
    ['getChooseList', 'buildBoutiqueChooseList'],
    ['settingPayType', 'buildBoutiquePaymentSelectionPatch']
  ]

  for (const [methodName, serviceName] of contracts) {
    assert.match(pageSource, new RegExp(`\\b${methodName}\\s*\\(`))
    assert.match(pageSource, new RegExp(`\\b${serviceName}\\s*\\(`))
  }
  assert.equal((pageSource.match(/\bcloseTips\s*\(/g) || []).length, 1)
  assert.doesNotMatch(pageSource, /\bhandleSpecificationsEcho\s*\(/)
  assert.doesNotMatch(pageSource, /\bresolveEchoMarketingMeta\s*\(/)
})

test('boutique second-stage modules keep wx effects and cross-package imports outside services', () => {
  for (const source of secondStageServiceSources) {
    assert.doesNotMatch(source, /\bsetData\s*\(/)
    assert.doesNotMatch(source, /\bgetPageInstance\s*\(/)
    assert.doesNotMatch(source, /\bwx\.\$router\b/)
    assert.doesNotMatch(source, /packageEquity/)
  }
  assert.doesNotMatch(pageSource + submitSource + routeSource, /packageEquity/)
})

test('boutique create page uses ESM imports and route or submit strategies contain no temporary logs', () => {
  assert.match(
    pageSource,
    /import timeUtil from '~\/utils\/timeFormat\/time-utils\.js'/
  )
  assert.doesNotMatch(pageSource, /\brequire\s*\(/)
  assert.doesNotMatch(submitSource + routeSource, /console\.log\s*\(/)
})

test('boutique validation strategy preserves toast presentation while other messages stay modal', () => {
  assert.match(
    submitSource,
    /function ValidationResult\(isValid, message, errorKey, presentation\)/
  )
  assert.match(
    submitSource,
    /new ValidationResult\(\s*false,\s*result\.message,\s*result\.errorKey,\s*result\.presentation\s*\)/
  )
  assert.match(
    submitSource,
    /validation\.presentation === ['"]toast['"][\s\S]*this\.page\.showToast\(validation\.message\)/
  )
  assert.match(
    submitSource,
    /else\s*\{[\s\S]*wx\.showModal\(\{[\s\S]*content:\s*validation\.message/
  )
  assert.match(
    submitSource,
    /if \(validation\.errorKey\)[\s\S]*this\.page\.handleRequiredError\(validation\.errorKey\)/
  )
})

test('sales staff confirmation resets Coupon in setData callback while delivery does not', () => {
  const start = pageSource.indexOf('onStaffPickerConfirm(e)')
  const end = pageSource.indexOf('/**', start + 1)
  const methodSource = pageSource.slice(start, end)

  assert.notEqual(start, -1)
  assert.match(
    methodSource,
    /this\.setData\([\s\S]*\(\) => \{[\s\S]*if \(mode === STAFF_MODE\.SALES\) \{[\s\S]*resetCouponComponent\(this\)/
  )
  assert.equal((methodSource.match(/resetCouponComponent\(this\)/g) || []).length, 1)
  assert.doesNotMatch(
    methodSource,
    /mode === STAFF_MODE\.DELIVERY[\s\S]*resetCouponComponent\(this\)/
  )
})