createOrderRoute.contract.test.mjs 2.47 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../../')
const appConfig = JSON.parse(fs.readFileSync(path.join(repoRoot, 'app.json'), 'utf8'))
const source = fs.readFileSync(
  path.join(repoRoot, 'tabWorkOrder/pages/createOrder/submitStrategy.js'),
  'utf8'
)
const workflowSource = fs.readFileSync(
  path.join(repoRoot, 'tabWorkOrder/pages/createOrder/services/submitWorkflow.js'),
  'utf8'
)
const routeSource = fs.readFileSync(
  path.join(repoRoot, 'tabWorkOrder/pages/createOrder/routeStrategy.js'),
  'utf8'
)

function hasPage(root, page) {
  const subpackage = (appConfig.subPackages || appConfig.subpackages || []).find(
    item => item.root === root
  )
  return Boolean(subpackage?.pages?.includes(page))
}

describe('boutique createOrder route contract', () => {
  it('keeps create and confirmation pages in tabWorkOrder', () => {
    assert.equal(hasPage('tabWorkOrder', 'pages/createOrder/createOrder'), true)
    assert.equal(hasPage('tabWorkOrder', 'pages/confirmOrder/confirmOrder'), true)
  })

  it('keeps the prerecorded success target in the main package', () => {
    assert.equal(
      appConfig.pages.includes('pages/tabDateIndex/pages/tabDateIndex'),
      true
    )
    assert.match(workflowSource, /alias:\s*['"]pages\.tabDateIndex['"]/)
  })

  it('uses an in-package confirmation alias and never embeds query text in aliases', () => {
    assert.match(workflowSource, /alias:\s*['"]confirmOrder['"]/) 
    assert.doesNotMatch(
      source + workflowSource,
      /\$router\.[a-zA-Z]+\(['"][^'"]+\?[^'"]*['"]/
    )
  })

  it('does not import the equity business subpackage', () => {
    assert.doesNotMatch(source, /packageEquity/)
  })

  it('validates and exactly sums supplemental associated fees before mutating page state', () => {
    assert.match(
      routeSource,
      /import \{ sumBoutiqueAssociatedFeeCents \} from ['"]\.\/services\/goodsPriceEdit\.helpers\.js['"]/
    )
    assert.match(
      routeSource,
      /sumBoutiqueAssociatedFeeCents\([\s\S]*selectOutTradeNoArr\.map\([\s\S]*item\.totalFee/
    )
    assert.match(
      routeSource,
      /if \(!totalFeeResult\.ok\) \{[\s\S]*this\.page\.showToast\([\s\S]*return false/
    )
    assert.doesNotMatch(
      routeSource,
      /reduce\(function \(num, item\) \{\s*return num \+ item\.totalFee/
    )
  })
})