mainPackageBudget.test.mjs 5.27 KB
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import test from 'node:test'
import { fileURLToPath } from 'node:url'

import { areaList } from '../../packageWorkspace/utils/area-data.js'
import { analyzeMainPackage } from '../../scripts/main-package-size.mjs'

const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
const analyzerPath = path.join(projectRoot, 'scripts/main-package-size.mjs')
const budgetBytes = 2_000_000
const migrationHeadroomBytes = 1_500_000

test('physical main package stays below the guarded upload budget', () => {
  assert.equal(fs.existsSync(analyzerPath), true, 'main package analyzer must exist')
  const first = analyzeMainPackage({ rootDir: projectRoot })
  const second = analyzeMainPackage({ rootDir: projectRoot })

  assert.equal(first.totalBytes <= budgetBytes, true, JSON.stringify(first))
  assert.deepEqual(first, second)
  assert.ok(first.totalFiles > 0)
  assert.equal(first.budgetBytes, budgetBytes)
})

test('work-order subpackage migration preserves the 1.5 MB main-package headroom target', () => {
  const report = analyzeMainPackage({ rootDir: projectRoot })

  assert.equal(report.totalBytes <= migrationHeadroomBytes, true, JSON.stringify(report))
})

test('heavy libraries and single-subpackage components are physically isolated', () => {
  const movedPaths = [
    ['utils/wxcharts.js', 'packageStudy/utils/wxcharts.js'],
    ['utils/lib/wxqrcode.js', 'tabWorkOrder/utils/wxqrcode.js'],
    ['components/GLCustForm', 'packageWorkspace/components/GLCustForm'],
    ['components/FilterBar', 'packageWorkspace/components/FilterBar'],
    ['components/TopToolbarPopup', 'packageWorkspace/components/TopToolbarPopup'],
    ['components/CarBrands', 'tabWorkOrder/components/CarBrands'],
    ['components/CustomerInfo', 'tabWorkOrder/components/CustomerInfo'],
    ['components/DateSelect', 'tabWorkOrder/components/DateSelect'],
    ['components/Goods', 'tabWorkOrder/components/Goods'],
    ['components/InventoryInfo', 'tabWorkOrder/components/InventoryInfo'],
    ['components/Qrbox', 'tabWorkOrder/components/Qrbox'],
    ['components/RemarkPopup', 'tabWorkOrder/components/RemarkPopup'],
    ['components/SkuGoods/Goods.js', 'tabWorkOrder/components/SkuGoods'],
    ['components/TechnicianSelectPopup', 'tabWorkOrder/components/TechnicianSelectPopup'],
    ['components/tipsBottom', 'tabWorkOrder/components/tipsBottom'],
    ['components/areaOperationsModal', 'tabWorkOrder/components/areaOperationsModal'],
  ]

  for (const [oldPath, newPath] of movedPaths) {
    const oldEntry = fs.statSync(path.join(projectRoot, oldPath), { throwIfNoEntry: false })
    if (oldEntry?.isDirectory()) {
      assert.equal(
        fs.readdirSync(path.join(projectRoot, oldPath)).length,
        0,
        `${oldPath} must not contain main-package files`,
      )
    } else {
      assert.equal(oldEntry, undefined, oldPath)
    }
    assert.equal(fs.existsSync(path.join(projectRoot, newPath)), true, newPath)
  }
})

test('root entries no longer pull workspace-only form and area data into main', () => {
  const appSource = fs.readFileSync(path.join(projectRoot, 'app.js'), 'utf8')
  const appConfig = JSON.parse(fs.readFileSync(path.join(projectRoot, 'app.json'), 'utf8'))
  const packageJson = JSON.parse(fs.readFileSync(path.join(projectRoot, 'package.json'), 'utf8'))
  const projectConfig = JSON.parse(
    fs.readFileSync(path.join(projectRoot, 'project.config.json'), 'utf8'),
  )
  const supervisorConfig = JSON.parse(
    fs.readFileSync(
      path.join(projectRoot, 'packageWorkspace/pages/supervisorDaily/supervisorDaily.json'),
      'utf8',
    ),
  )

  assert.doesNotMatch(appSource, /@vant\/area-data|\$areaList/)
  assert.equal(appConfig.usingComponents['gl-cust-form'], undefined)
  assert.equal(packageJson.dependencies['@vant/area-data'], undefined)
  assert.equal(
    projectConfig.packOptions.ignore.some(
      item =>
        item.type === 'folder' &&
        item.value === 'miniprogram_npm/@vant/area-data',
    ),
    true,
  )
  assert.equal(
    supervisorConfig.usingComponents['gl-cust-form'],
    '../../components/GLCustForm/GLCustForm',
  )
})

test('workspace-local area data preserves the Vant area payload contract', () => {
  const licensePath = path.join(projectRoot, 'packageWorkspace/utils/area-data.LICENSE')

  assert.deepEqual(Object.keys(areaList).sort(), [
    'city_list',
    'county_list',
    'province_list',
  ])
  assert.equal(areaList.province_list[110000], '北京市')
  assert.equal(areaList.city_list[440300], '深圳市')
  assert.equal(areaList.county_list[440305], '南山区')
  assert.match(fs.readFileSync(licensePath, 'utf8'), /MIT License/)
})

test('subpackage consumers no longer import the former main-package paths', () => {
  const studyResult = fs.readFileSync(
    path.join(projectRoot, 'packageStudy/pages/aiCheck/result/result.js'),
    'utf8',
  )
  const orderPopup = fs.readFileSync(
    path.join(projectRoot, 'tabWorkOrder/components/OrderPopup/OrderPopup.js'),
    'utf8',
  )
  const rootWxUtils = fs.readFileSync(path.join(projectRoot, 'utils/utils_wx.js'), 'utf8')

  assert.match(studyResult, /require\(["']\.\.\/\.\.\/\.\.\/utils\/wxcharts["']\)/)
  assert.match(orderPopup, /from ["']\.\.\/\.\.\/utils\/wxqrcode["']/)
  assert.doesNotMatch(rootWxUtils, /wxqrcode|handleCopy/)
})