Files

110 lines
4.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict'
// e2e 覆盖率归因(2026-10-28)
//
// 数据来源:`tests/_pw-coverage.js` 在每次 e2e 测试结束时落到 test-results/cov/*.jsonl 的
// V8 coverage ranges。
// 归因逻辑:与 `scripts/coverage-browser.js` **共用** scripts/lib/coverage-bundle.js
// (同一份 bundle 复算 + 偏移单位检测 + byte→char 映射),避免两处算法静默不一致。
//
// ⚠️ 合并语义(容易搞错):
// 同一段代码只要**任一**页面覆盖过就算覆盖 ⇒
// 「未覆盖」= 所有页面未覆盖区间的**交集**。
// 实现上用等价做法:把每页的「已覆盖区间」并集起来,再取补集。
const fs = require('fs')
const path = require('path')
const {
buildBundle, uncoveredIntervals, complement, unionCovered, attributeUncovered,
} = require('./lib/coverage-bundle')
const root = path.resolve(__dirname, '..')
const covDir = path.join(root, 'test-results', 'cov')
// 每行 jsonl = 一个页面的 stopJSCoverage 结果(可能是**多个** app.js 条目)。
// ⚠️ 实测踩坑:同一个页面可能返回 2 个条目 —— 其中一个是**残缺快照**
//(只有 299 个函数 vs 完整 2018 个,多半是加载中途/预取实例)。
// 若把它们一起并进"已覆盖"集合,覆盖率会虚高到 98%(实测)。
// 处置:**每页只取函数数最多的那一条**(与探针脚本 cov.find 的做法一致)。
const appJsRe = /\/app\.js(\?|$)/
function readAllPages() {
if (!fs.existsSync(covDir)) return []
const pages = []
for (const f of fs.readdirSync(covDir).filter((n) => n.endsWith('.jsonl'))) {
const lines = fs.readFileSync(path.join(covDir, f), 'utf8').split('\n').filter(Boolean)
for (const line of lines) {
let arr
try { arr = JSON.parse(line) } catch (e) { continue }
const apps = (arr || []).filter((e) => e && appJsRe.test(e.url || ''))
if (!apps.length) continue
let best = apps[0]
for (const e of apps) if ((e.functions || []).length > (best.functions || []).length) best = e
pages.push(best)
}
}
return pages
}
function main() {
const { bundle, spans, version } = buildBundle(root)
const total = bundle.length
const pages = readAllPages()
if (pages.length) {
const fnCounts = pages.map((p) => (p.functions || []).length)
const lo = Math.min.apply(null, fnCounts), hi = Math.max.apply(null, fnCounts)
if (lo < hi * 0.5) {
console.log('[coverage:e2e] ⚠ 各页函数数差异过大(' + lo + ' … ' + hi + '):可能有页面只采到残缺快照,数字会偏高。')
}
}
if (!pages.length) {
console.log('[coverage:e2e] 没找到采集数据。')
console.log(' 先跑一次:npm run test:e2e (会自动写到 test-results/cov/)')
process.exit(1)
}
let unit = 'char'
const perPageCovered = []
for (const entry of pages) {
const r = uncoveredIntervals(entry, bundle)
if (r.unit === 'byte') unit = 'byte'
else if (r.unit === 'unknown') { console.log('[coverage:e2e] 偏移单位无法判定,放弃(可能有页面加载了不同的 bundle)'); process.exit(1) }
perPageCovered.push(complement(r.uncovered, total))
}
const uncovered = unionCovered(perPageCovered, total) // 交集语义 → 等价实现
const stats = attributeUncovered(spans, uncovered)
let srcTotal = 0, srcCovered = 0, appTotal = 0, appCovered = 0
const rows = []
for (const [file, st] of stats) {
const covered = st.total - st.dead
rows.push({ file, total: st.total, covered, pct: st.total ? covered / st.total * 100 : 0 })
if (file === 'app.js') { appTotal = st.total; appCovered = covered }
else { srcTotal += st.total; srcCovered += covered }
}
const srcCount = rows.length - 1
console.log('')
console.log(' ===== e2e 覆盖率(' + version + ',' + pages.length + ' 个页面快照)=====')
console.log(' ' + String(srcCount).padStart(3) + ' 个 src 模块 : ' + srcTotal + ' 字符, 覆盖 ' + srcCovered
+ ' (' + (srcTotal ? (srcCovered / srcTotal * 100).toFixed(1) : '0.0') + '%)')
console.log(' app.js : ' + appTotal + ' 字符, 覆盖 ' + appCovered
+ ' (' + (appTotal ? (appCovered / appTotal * 100).toFixed(1) : '0.0') + '%)')
console.log(' 偏移单位: ' + unit)
const srcRows = rows.filter((r) => r.file !== 'app.js').sort((a, b) => b.pct - a.pct)
console.log('')
console.log(' src 模块覆盖率 前 5 / 后 5:')
for (const r of srcRows.slice(0, 5)) console.log(' ' + r.pct.toFixed(0).padStart(3) + '% ' + r.file)
console.log(' ...')
for (const r of srcRows.slice(-5)) console.log(' ' + r.pct.toFixed(0).padStart(3) + '% ' + r.file)
if (process.argv.includes('--json')) {
console.log(JSON.stringify({ version, pages: pages.length, unit, app: { total: appTotal, covered: appCovered }, src: { modules: srcCount, total: srcTotal, covered: srcCovered } }))
}
}
main()