Commit e15db776 by 张立顺

test: enforce package budget before upload

1 parent e1697b67
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
## 规模 ## 规模
- 节点:4231 - 节点:4240
- 关系:8107 - 关系:8117
- 未解析:88 - 未解析:88
## 节点类型 ## 节点类型
...@@ -18,10 +18,10 @@ ...@@ -18,10 +18,10 @@
| domain | 6 | | domain | 6 |
| endpoint | 234 | | endpoint | 234 |
| globalState | 33 | | globalState | 33 |
| method | 2921 | | method | 2928 |
| module | 365 | | module | 366 |
| page | 71 | | page | 71 |
| test | 77 | | test | 78 |
## 关系类型 ## 关系类型
...@@ -32,11 +32,11 @@ ...@@ -32,11 +32,11 @@
| callsApi | 221 | | callsApi | 221 |
| callsEndpoint | 332 | | callsEndpoint | 332 |
| contains | 710 | | contains | 710 |
| coveredBy | 62 | | coveredBy | 63 |
| declares | 2922 | | declares | 2929 |
| definesApi | 332 | | definesApi | 332 |
| documentedBy | 53 | | documentedBy | 53 |
| imports | 438 | | imports | 440 |
| navigatesTo | 71 | | navigatesTo | 71 |
| readsState | 777 | | readsState | 777 |
| usesComponent | 314 | | usesComponent | 314 |
......
...@@ -11,3 +11,4 @@ ...@@ -11,3 +11,4 @@
| workspace 筛选 | 根组件 | `packageWorkspace/components` | | workspace 筛选 | 根组件 | `packageWorkspace/components` |
| 工单独占组件 | 根组件 | `tabWorkOrder/components` | | 工单独占组件 | 根组件 | `tabWorkOrder/components` |
| 主包预算 | 无 | `npm run size:main`,最大 2,000,000 字节 | | 主包预算 | 无 | `npm run size:main`,最大 2,000,000 字节 |
| 跨包边界 | 仅检查文件存在 | `npm run check:packageBoundaries`,拒绝主包到分包及跨分包依赖 |
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
], ],
"interfaces": [ "interfaces": [
"npm run size:main", "npm run size:main",
"npm run check:packageBoundaries",
"npm run knowledge:build", "npm run knowledge:build",
"npm test", "npm test",
"node scripts/wx-upload-guard.js" "node scripts/wx-upload-guard.js"
...@@ -58,6 +59,10 @@ ...@@ -58,6 +59,10 @@
"status": "pass" "status": "pass"
}, },
{ {
"command": "npm run check:packageBoundaries",
"status": "pass"
},
{
"command": "npm run test:workOrder", "command": "npm run test:workOrder",
"status": "pass" "status": "pass"
}, },
......
...@@ -14,9 +14,10 @@ ...@@ -14,9 +14,10 @@
## 最终证据(2026-08-21) ## 最终证据(2026-08-21)
- 保守物理主包从 2,360,562 字节降到 1,981,498 字节,减少 379,064 字节;低于内部 2,000,000 字节预算 18,502 字节,低于微信 2 MiB 上限 115,654 字节。 - 保守物理主包从 2,360,562 字节降到 1,981,498 字节,减少 379,064 字节;低于内部 2,000,000 字节预算 18,502 字节,低于微信 2 MiB 上限 115,654 字节。
- 主包预算与分包边界合同 5 项通过,覆盖地区数据结构、许可文件、旧路径清理和消费者新路径。 - 主包预算与分包边界合同 7 项通过,覆盖地区数据结构、许可文件、旧路径清理、消费者新路径和非法跨包边。
- 精品工单迁移、创建页、生命周期和闭包命令全部通过;生命周期回归为 82 项通过,4 个 Live 用例默认跳过。 - 精品工单迁移、创建页、生命周期和闭包命令全部通过;生命周期回归为 82 项通过,4 个 Live 用例默认跳过。
- 知识图谱回归 29 项通过;12 个生成产物保持最新,最终图谱包含 4,231 个节点、8,107 条边、71 个页面和 88 个已知未解析项。 - 知识图谱回归 29 项通过;12 个生成产物保持最新,最终图谱包含 4,240 个节点、8,117 条边、71 个页面和 88 个已知未解析项。
- `npm test` 退出码为 0;Node 24.19 上传 guard 扫描 235 个工单文件,依赖、路由和 API 闭包均无缺口。 - `npm test` 退出码为 0;Node 24.19 上传 guard 扫描 235 个工单文件,依赖、路由和 API 闭包均无缺口。
- 上传 guard 依次执行主包预算、跨包依赖和工单闭包;跨包审计检查 660 条运行时导入/组件边,未发现主包到分包或跨分包非法依赖。
- `@vant/area-data` 已从依赖清单移除;workspace 内保留 1.5.2 的地区数据与 MIT 许可,并通过 `project.config.json` 排除可能残留的旧 npm 构建产物。 - `@vant/area-data` 已从依赖清单移除;workspace 内保留 1.5.2 的地区数据与 MIT 许可,并通过 `project.config.json` 排除可能残留的旧 npm 构建产物。
- 登录及写入型 Live 用例继续依赖运行时环境变量与显式开关,本次验证未运行 Live 写入,也未在仓库写入测试账号。 - 登录及写入型 Live 用例继续依赖运行时环境变量与显式开关,本次验证未运行 Live 写入,也未在仓库写入测试账号。
...@@ -226,9 +226,9 @@ ...@@ -226,9 +226,9 @@
| 查询可选商品目录 | `getSelectOrderGoodsList` | POST | `dj.api.smartcarlife.selectOrderGoods.list` | `merchantId`、`sceneType`、`baseSceneCode` | 商品/套餐目录、营销标识、价格及分类字段 | 场景和销售顾问就绪后;补录场景可不依赖销售顾问;常规 | `tabWorkOrder/pages/createOrder/createOrder.js`、`tabWorkOrder/api.js` | | 查询可选商品目录 | `getSelectOrderGoodsList` | POST | `dj.api.smartcarlife.selectOrderGoods.list` | `merchantId`、`sceneType`、`baseSceneCode` | 商品/套餐目录、营销标识、价格及分类字段 | 场景和销售顾问就绪后;补录场景可不依赖销售顾问;常规 | `tabWorkOrder/pages/createOrder/createOrder.js`、`tabWorkOrder/api.js` |
| 加载营销元数据 | `loadMarketingMetaMaps` 内再次调用 `getSelectOrderGoodsList` | POST | `dj.api.smartcarlife.selectOrderGoods.list` | 同上 | 营销标识映射 | 商品加载辅助流程;可能与页面直接请求形成重复请求;条件 | `tabWorkOrder/utils/loadMarketingFlagMap.js` | | 加载营销元数据 | `loadMarketingMetaMaps` 内再次调用 `getSelectOrderGoodsList` | POST | `dj.api.smartcarlife.selectOrderGoods.list` | 同上 | 营销标识映射 | 商品加载辅助流程;可能与页面直接请求形成重复请求;条件 | `tabWorkOrder/utils/loadMarketingFlagMap.js` |
| 查询升级基础商品 | `getOrderBaseGoodsList` | POST | `dj.api.smartcarlife.orderUpgradeGoods.listByMerchantId` | `merchantId`、`sceneType`、`baseSceneCode`、`upradeFlag` | 基础商品列表、子商品及价格字段 | 特定商品加载/回显流程;条件 | `tabWorkOrder/pages/createOrder/createOrder.js`、`tabWorkOrder/api.js` | | 查询升级基础商品 | `getOrderBaseGoodsList` | POST | `dj.api.smartcarlife.orderUpgradeGoods.listByMerchantId` | `merchantId`、`sceneType`、`baseSceneCode`、`upradeFlag` | 基础商品列表、子商品及价格字段 | 特定商品加载/回显流程;条件 | `tabWorkOrder/pages/createOrder/createOrder.js`、`tabWorkOrder/api.js` |
| 查询车辆品牌 | `getBrandList` | POST | `dj.api.get.car.brandList` | `merchantId`、`needCarSeries=true` | 品牌、车系、车型层级数据 | 打开车辆品牌组件;组件本地缓存命中时不请求 | `components/CarBrands/CarBrands.js`、`api/api.js` | | 查询车辆品牌 | `getBrandList` | POST | `dj.api.get.car.brandList` | `merchantId`、`needCarSeries=true` | 品牌、车系、车型层级数据 | 打开车辆品牌组件;组件本地缓存命中时不请求 | `tabWorkOrder/components/CarBrands/CarBrands.js`、`api/api.js` |
| 按工单客户查询客户列表 | `getWorkPackageGoodsList` | POST | `dj.api.smartcarlife.order.search.containPackageGoods` | `merchantId=所选4S店ID`、`keyword`、`pageNum`、`pageSize=20`、`statusList=[1]`;封装补充 `sceneType=1` | `data.list`、`data.total`,转换为客户及车型列表 | 打开客户选择弹窗、搜索、分页;常规 | `components/CustomerInfo/CustomerInfo.js`、`api/api.js` | | 按工单客户查询客户列表 | `getWorkPackageGoodsList` | POST | `dj.api.smartcarlife.order.search.containPackageGoods` | `merchantId=所选4S店ID`、`keyword`、`pageNum`、`pageSize=20`、`statusList=[1]`;封装补充 `sceneType=1` | `data.list`、`data.total`,转换为客户及车型列表 | 打开客户选择弹窗、搜索、分页;常规 | `tabWorkOrder/components/CustomerInfo/CustomerInfo.js`、`api/api.js` |
| 查询客户档案列表 | `getCustomerFileList` | POST | `dj.api.smartcarlife.vehicle.damage.upgrade.quotation.customer.page` | `currentPage=1`、`pageSize=999999`、`merchantId=所选4S店ID`、`search` | `data.list` 中客户和车辆字段 | 客户档案模式搜索;条件 | `components/CustomerInfo/CustomerInfo.js`、`api/api.js` | | 查询客户档案列表 | `getCustomerFileList` | POST | `dj.api.smartcarlife.vehicle.damage.upgrade.quotation.customer.page` | `currentPage=1`、`pageSize=999999`、`merchantId=所选4S店ID`、`search` | `data.list` 中客户和车辆字段 | 客户档案模式搜索;条件 | `tabWorkOrder/components/CustomerInfo/CustomerInfo.js`、`api/api.js` |
| 按手机号查询客户 | `getQueryCouponInfoByPhone` | POST | `dj.api.smartcarlife.customer.queryCouponInfoByPhone` | `phone`、`merchantId` | 客户 ID、车架号、品牌、车系、车型、车牌等 | 优惠券组件中手机号合法时;条件 | `tabWorkOrder/components/Coupon/Coupon.js`、`tabWorkOrder/api.js` | | 按手机号查询客户 | `getQueryCouponInfoByPhone` | POST | `dj.api.smartcarlife.customer.queryCouponInfoByPhone` | `phone`、`merchantId` | 客户 ID、车架号、品牌、车系、车型、车牌等 | 优惠券组件中手机号合法时;条件 | `tabWorkOrder/components/Coupon/Coupon.js`、`tabWorkOrder/api.js` |
| 查询客户优惠券 | `getCustomerCouponList` | POST | `dj.api.customer.coupon.list` | `customerPhone`、`statusList=[0,1]`、`usedFlag='N'` | 客户券列表及状态 | 手机号存在时;条件 | `tabWorkOrder/components/Coupon/Coupon.js`、`api/api.js` | | 查询客户优惠券 | `getCustomerCouponList` | POST | `dj.api.customer.coupon.list` | `customerPhone`、`statusList=[0,1]`、`usedFlag='N'` | 客户券列表及状态 | 手机号存在时;条件 | `tabWorkOrder/components/Coupon/Coupon.js`、`api/api.js` |
| 查询门店礼券 | `getCouponList` | POST | `dj.api.smartCarLife.merchant.coupon.list` | `merchantId`、`yqMerchantId`、`orderByType=9` | `couponInfo`、`couponType` 等礼券列表字段 | 门店、园区店或手机号变化时;条件 | `tabWorkOrder/components/Coupon/Coupon.js`、`api/api.js` | | 查询门店礼券 | `getCouponList` | POST | `dj.api.smartCarLife.merchant.coupon.list` | `merchantId`、`yqMerchantId`、`orderByType=9` | `couponInfo`、`couponType` 等礼券列表字段 | 门店、园区店或手机号变化时;条件 | `tabWorkOrder/components/Coupon/Coupon.js`、`api/api.js` |
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
"test:affected": "node scripts/knowledge-graph/affected-tests.mjs", "test:affected": "node scripts/knowledge-graph/affected-tests.mjs",
"test:knowledge": "node scripts/run-node-tests.mjs tests/knowledge-graph", "test:knowledge": "node scripts/run-node-tests.mjs tests/knowledge-graph",
"size:main": "node scripts/main-package-size.mjs", "size:main": "node scripts/main-package-size.mjs",
"check:packageBoundaries": "node scripts/check-package-boundaries.mjs",
"knowledge:build": "node scripts/knowledge-graph/knowledge-build.mjs", "knowledge:build": "node scripts/knowledge-graph/knowledge-build.mjs",
"knowledge:check": "node scripts/knowledge-graph/knowledge-check.mjs", "knowledge:check": "node scripts/knowledge-graph/knowledge-check.mjs",
"impact": "node scripts/knowledge-graph/query-knowledge-graph.mjs", "impact": "node scripts/knowledge-graph/query-knowledge-graph.mjs",
......
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { buildKnowledgeGraph } from './knowledge-graph/build-knowledge-graph.mjs'
const NON_RUNTIME_PREFIXES = [
'.agents/',
'.superpowers/',
'docs/',
'node_modules/',
'outputs/',
'scripts/',
'test/',
'tests/',
'typings/',
]
function normalizePath(value) {
return value?.replaceAll('\\', '/').replace(/^\.\//, '')
}
function packageOwner(filePath, subpackageRoots) {
const normalized = normalizePath(filePath)
return (
subpackageRoots.find(
root => normalized === root || normalized?.startsWith(`${root}/`),
) ?? 'main'
)
}
function isRuntimeNode(node) {
const filePath = normalizePath(node?.path)
return (
Boolean(filePath) &&
node.type !== 'test' &&
!NON_RUNTIME_PREFIXES.some(prefix => filePath.startsWith(prefix))
)
}
function packageEdges({ graph, subpackageRoots }) {
const nodeById = new Map(graph.nodes.map(node => [node.id, node]))
return graph.edges
.filter(edge => edge.type === 'imports' || edge.type === 'usesComponent')
.map(edge => ({
edge,
fromNode: nodeById.get(edge.from),
toNode: nodeById.get(edge.to),
}))
.filter(({ fromNode, toNode }) => isRuntimeNode(fromNode) && isRuntimeNode(toNode))
.map(({ edge, fromNode, toNode }) => ({
type: edge.type,
from: edge.from,
to: edge.to,
fromPath: normalizePath(fromNode.path),
toPath: normalizePath(toNode.path),
fromOwner: packageOwner(fromNode.path, subpackageRoots),
toOwner: packageOwner(toNode.path, subpackageRoots),
evidence: edge.evidence,
}))
.sort((left, right) =>
`${left.fromPath}:${left.toPath}:${left.type}`.localeCompare(
`${right.fromPath}:${right.toPath}:${right.type}`,
'en',
),
)
}
export function findIllegalPackageEdges({ graph, subpackageRoots }) {
return packageEdges({ graph, subpackageRoots }).filter(
edge =>
edge.toOwner !== 'main' &&
edge.fromOwner !== edge.toOwner,
)
}
export async function checkPackageBoundaries({ rootDir = process.cwd() } = {}) {
const absoluteRoot = path.resolve(rootDir)
const appConfig = JSON.parse(
fs.readFileSync(path.join(absoluteRoot, 'app.json'), 'utf8'),
)
const subpackageRoots = (appConfig.subpackages ?? appConfig.subPackages ?? [])
.map(item => normalizePath(item.root))
.filter(Boolean)
.sort()
const graph = await buildKnowledgeGraph({ rootDir: absoluteRoot })
const checkedEdges = packageEdges({ graph, subpackageRoots })
const illegalEdges = checkedEdges.filter(
edge => edge.toOwner !== 'main' && edge.fromOwner !== edge.toOwner,
)
return {
status: illegalEdges.length ? 'error' : 'ok',
subpackageRoots,
checkedEdges: checkedEdges.length,
illegalEdges,
}
}
const currentFile = fileURLToPath(import.meta.url)
if (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(currentFile)) {
const report = await checkPackageBoundaries()
console.log(JSON.stringify(report, null, 2))
if (report.illegalEdges.length) process.exitCode = 1
}
...@@ -17,20 +17,22 @@ console.log( ...@@ -17,20 +17,22 @@ console.log(
`[wx-upload-guard] npm artifacts pruned; nested=${pruneResult.removedNestedNpm}; sourceMaps=${pruneResult.removedSourceMaps}` `[wx-upload-guard] npm artifacts pruned; nested=${pruneResult.removedNestedNpm}; sourceMaps=${pruneResult.removedSourceMaps}`
) )
const result = spawnSync( function runGuard(scriptName, label) {
process.execPath, const result = spawnSync(process.execPath, [path.join(__dirname, scriptName)], {
[path.join(__dirname, 'check-work-order-migration.mjs')],
{
cwd: projectRoot, cwd: projectRoot,
stdio: 'inherit', stdio: 'inherit',
shell: false shell: false
})
if (result.error) {
console.error(`[wx-upload-guard] unable to run ${label}`)
console.error(result.error.message)
process.exit(1)
} }
)
if (result.error) { if (result.status !== 0) process.exit(result.status ?? 1)
console.error('[wx-upload-guard] unable to run migration audit')
console.error(result.error.message)
process.exit(1)
} }
process.exit(result.status ?? 1) runGuard('main-package-size.mjs', 'main package size audit')
runGuard('check-package-boundaries.mjs', 'package boundary audit')
runGuard('check-work-order-migration.mjs', 'migration audit')
import assert from 'node:assert/strict'
import path from 'node:path'
import test from 'node:test'
import { fileURLToPath } from 'node:url'
import {
checkPackageBoundaries,
findIllegalPackageEdges,
} from '../../scripts/check-package-boundaries.mjs'
const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
const subpackageRoots = ['packageStudy', 'packageWorkspace', 'tabWorkOrder']
function graphWithEdges(edges) {
return {
nodes: [
{ id: 'main', type: 'module', path: 'utils/main.js' },
{ id: 'study', type: 'module', path: 'packageStudy/utils/study.js' },
{ id: 'workspace', type: 'component', path: 'packageWorkspace/components/card/card' },
{ id: 'work-order', type: 'page', path: 'tabWorkOrder/pages/orderDetail/orderDetail' },
{ id: 'test', type: 'test', path: 'tests/package-size/example.test.mjs' },
],
edges,
}
}
test('package boundary audit rejects main-to-subpackage and cross-subpackage edges', () => {
const graph = graphWithEdges([
{ from: 'main', to: 'study', type: 'imports', evidence: { path: 'utils/main.js' } },
{
from: 'study',
to: 'workspace',
type: 'usesComponent',
evidence: { path: 'packageStudy/pages/example/example.json' },
},
{ from: 'work-order', to: 'main', type: 'imports', evidence: {} },
{ from: 'test', to: 'study', type: 'imports', evidence: {} },
])
assert.deepEqual(
findIllegalPackageEdges({ graph, subpackageRoots }).map(edge => [
edge.fromOwner,
edge.toOwner,
edge.type,
]),
[
['packageStudy', 'packageWorkspace', 'usesComponent'],
['main', 'packageStudy', 'imports'],
],
)
})
test('current runtime graph contains no illegal package dependency edges', async () => {
const report = await checkPackageBoundaries({ rootDir: projectRoot })
assert.deepEqual(report.illegalEdges, [])
assert.ok(report.checkedEdges > 0)
assert.deepEqual(report.subpackageRoots, subpackageRoots)
})
...@@ -91,10 +91,12 @@ describe('小程序上传边界', () => { ...@@ -91,10 +91,12 @@ describe('小程序上传边界', () => {
} }
}) })
it('上传前守卫脚本存在并执行静态闭包检查', async () => { it('上传前守卫脚本执行主包预算、跨包边界和静态闭包检查', async () => {
const guardPath = path.join(projectRoot, 'scripts/wx-upload-guard.js') const guardPath = path.join(projectRoot, 'scripts/wx-upload-guard.js')
assert.ok((await stat(guardPath)).isFile()) assert.ok((await stat(guardPath)).isFile())
const source = await readFile(guardPath, 'utf8') const source = await readFile(guardPath, 'utf8')
assert.match(source, /main-package-size\.mjs/)
assert.match(source, /check-package-boundaries\.mjs/)
assert.match(source, /check-work-order-migration\.mjs/) assert.match(source, /check-work-order-migration\.mjs/)
}) })
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!