|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { readFileSync } from "node:fs"; |
| 4 | +import { dirname, join } from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | +import { applyTestCount, readTestCounts } from "../scripts/test-count-sync.mjs"; |
| 7 | + |
| 8 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 9 | +const PKG_ROOT = join(HERE, ".."); |
| 10 | +const REPO_ROOT = join(PKG_ROOT, ".."); |
| 11 | +const READMES = [join(REPO_ROOT, "README.md"), join(PKG_ROOT, "README.md")]; |
| 12 | + |
| 13 | +// 双 README 是发布门面,同一个数字在里面重复出现三到五次(一个 shields.io 徽章 + |
| 14 | +// 每条开发命令注释一处,root 是双语所以中英各一条)。它们必须永远同数——否则 |
| 15 | +// 「这个项目有多少测试」取决于读者恰好看了哪一行。 |
| 16 | +// |
| 17 | +// v0.8.11 实际漂移过:徽章 1437、四条注释留在 1431。本用例就是那次漂移的回归锁, |
| 18 | +// 跑在真实 README 上(不是 fixture),所以它能在发版前拦住同类漂移。 |
| 19 | +test("双 README 的测试数处处一致(徽章与所有注释同数)", () => { |
| 20 | + for (const f of READMES) { |
| 21 | + const counts = readTestCounts(readFileSync(f, "utf8")); |
| 22 | + const shapes = new Set(counts.map((c) => c.shape)); |
| 23 | + assert.ok(counts.length >= 2, `${f} 抽到的测试数出现处太少:${JSON.stringify(counts)}`); |
| 24 | + assert.ok(shapes.has("badge"), `${f} 没抽到徽章:${JSON.stringify(counts)}`); |
| 25 | + const values = [...new Set(counts.map((c) => c.value))]; |
| 26 | + assert.equal(values.length, 1, `${f} 测试数不一致:${JSON.stringify(counts)}`); |
| 27 | + } |
| 28 | +}); |
| 29 | + |
| 30 | +// v0.8.11 的真实漂移样本(徽章已 1437、注释仍 1431)。锁住「徽章 + 四种注释形状」 |
| 31 | +// 全覆盖:少任何一种,就会有一处静默落后——release-prep.mjs 原先只覆盖徽章 URL, |
| 32 | +// 自动发版路径因此反倒成了漂移来源。 |
| 33 | +test("applyTestCount 覆盖徽章与全部四种注释形状(真实漂移样本)", () => { |
| 34 | + const drifted = [ |
| 35 | + '<img src="https://img.shields.io/badge/tests-1431%20passed-3E63DD?style=flat-square" alt="tests">', |
| 36 | + "[](https://github.com/slow-stack/mneme)", |
| 37 | + "npm test # 1431 个测试", |
| 38 | + "npm test # 运行 1431 个测试", |
| 39 | + "test/ # 1431 个 node:test 测试(审计与三轴线压测不变量)", |
| 40 | + "npm test # 1431 tests" |
| 41 | + ].join("\n"); |
| 42 | + const fixed = applyTestCount(drifted, 1437); |
| 43 | + assert.ok(!/\b1431\b/.test(fixed), "仍有 1431 未被替换:\n" + fixed); |
| 44 | + const counts = readTestCounts(fixed); |
| 45 | + assert.equal(counts.length, 6, "六处出现都该被认出来:" + JSON.stringify(counts)); |
| 46 | + assert.deepEqual([...new Set(counts.map((c) => c.value))], [1437]); |
| 47 | +}); |
| 48 | + |
| 49 | +test("applyTestCount 幂等:同一个数重复施加不再产生变化", () => { |
| 50 | + const once = applyTestCount("tests-9%20passed-3E63DD\n# 9 个测试\n# 9 tests", 12); |
| 51 | + assert.equal(applyTestCount(once, 12), once); |
| 52 | +}); |
| 53 | + |
| 54 | +// 历史版本表里的「745 测试全绿」「790 通过」是各版本当时的真实记录(sync-test-badge |
| 55 | +// 的注释也写明了刻意不改它们)。替换规则只认徽章 URL 与 `#` 前缀注释两种形状, |
| 56 | +// 这条用例锁住那个边界,免得将来放宽正则把历史改写掉。 |
| 57 | +test("applyTestCount 不碰历史版本表里的「N 测试全绿」", () => { |
| 58 | + const history = "| **v0.8.0** | 745 测试全绿 |\n| **v0.7.9** | 790 通过 |\n| v0.2.7 | 263 |"; |
| 59 | + assert.equal(applyTestCount(history, 1437), history); |
| 60 | +}); |
0 commit comments