mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-27 22:10:22 +08:00
从 PR #188(Pluto-Yt,fix/drift-detection)手改取通用部分落地,不含
0.2.1-rc.3 专属适配:
- 补丁失配不再静默成 skipped:文件在、marker 没有、pattern 一条都不中
→ 新增 drifted 状态(filePatchOutcome / patchStatus / rollupPatchStatus)。
以前这种情况一律返回 skipped 并被当软成功,官方一升级插件就静默失效,
状态页却全绿
- isSelfMarkedPatch() 例外:pattern 本身就是本插件写进宿主的痕迹
(#80 撤销型),找不到 = 本来没加过或已撤过,仍算 skipped
- ALWAYS_SOFT_STATUSES 去掉 skipped;skipped 改为只有可选/自标记型才算
软成功;drifted 看得见(状态页标红、不计入 ready)但不阻断 Apply,
免得把 0.1.6 / 0.2.0 / 0.2.1-rc.2 上的用户堵死
- tallyDisplayPatches / rollupDisplayStatus:drifted 压过 applied
- ensureHostClean 不再过滤 pattern_not_found:没打上就是没打上
- 宿主版本门禁:读 package.json 的 dshTarget,比对宿主真实版本,
gatherState 暴露 host_version / host_target / host_supported,
CLI 与状态页在未适配时直接告警
- applyReplacementsToText 支持 { pattern, replace, expect } 命中数契约:
命中数与声明不符就整条拒写,并回传实际命中数
- #77 锚点容错:写死 5 行改成锚首尾 + 非贪婪窗口 (?:(?!isRewindMarker)[\s\S]){0,300}?,
官方在中间插一行也照样命中;窗口排除 isRewindMarker 保证二次应用不叠加
额外修的 bug:
- rollupPatchStatus 的 lastError 提到最前:以前只在 strictFiles 分支里返回,
一个文件写成功就把别的文件 EACCES / read_error 吞成绿
- applyReplacementsToText 的 expect 契约原先只对字面量 pattern 生效,正则
分支直接 continue 掉了 —— 官方把同一段代码复制成两份时正则会把两处一起
改掉,补上正则分支的命中数检查
- patchStatus 的 drifted 计数排在 strictFiles 的 unmatched 之前:严格补丁的
文件宿主一定在,原文对不上只可能是上游漂移,报 drifted 比 unmatched 更准
- 与 PR 的有意偏差:rollupPatchStatus 里 drifted 排在 already 之后。
多文件补丁(#50/#57/#62)有个别文件本来就不含这段代码(barrel 文件),
按 PR 的顺序会把这几条从 already/applied 翻成 drifted,制造假告警
未取(0.2.1-rc.3 专属、与通用逻辑无法拆开):新增 targetFiles 键
(harness-dev-context / skill-badge)、#32/#53 扩到 rc.3 新位置、
新增 #89 SANDBOX_POLICY_BRAND_NEUTRAL 与 #90 SKILL_BADGE_DESCRIPTION_NEUTRAL
新增 test/patch-status.test.js(29 条,全量 74 条全过)
Based-on: PR #188 by Pluto-Yt
Co-Authored-By: Claude <noreply@anthropic.com>
255 lines
11 KiB
JavaScript
255 lines
11 KiB
JavaScript
import test from "node:test";
|
||
import assert from "node:assert/strict";
|
||
import {
|
||
ALL_PATCHES,
|
||
applyReplacementsToText,
|
||
filePatchOutcome,
|
||
isPatchResultSoftOk,
|
||
isSelfMarkedPatch,
|
||
parseHostVersion,
|
||
patchStatusReady,
|
||
rollupDisplayStatus,
|
||
rollupPatchStatus,
|
||
tallyDisplayPatches,
|
||
} from "../lib/core.js";
|
||
|
||
const base = {
|
||
applied: 0,
|
||
already: 0,
|
||
missing: 0,
|
||
notFound: 0,
|
||
drifted: 0,
|
||
lastError: null,
|
||
keyCount: 1,
|
||
layoutSoft: false,
|
||
strictFiles: false,
|
||
};
|
||
|
||
/* ---------------------------------------------------------------------------
|
||
* 判据:上游漂移必须显性,不能落成 skipped(软成功)。
|
||
* 这是"官方升级后插件静默失效、状态页却全绿"的唯一入口。
|
||
* ------------------------------------------------------------------------- */
|
||
|
||
test("rollupPatchStatus: 文件在、原文对不上 → drifted 而不是 skipped", () => {
|
||
assert.equal(rollupPatchStatus({ ...base, drifted: 1 }), "drifted");
|
||
});
|
||
|
||
test("rollupPatchStatus: pattern 还在 → pattern_not_found(可以打)", () => {
|
||
assert.equal(rollupPatchStatus({ ...base, notFound: 1 }), "pattern_not_found");
|
||
});
|
||
|
||
test("rollupPatchStatus: 有文件真的写成功就仍算 applied,不制造半打假告警", () => {
|
||
assert.equal(rollupPatchStatus({ ...base, applied: 1, drifted: 1 }), "applied");
|
||
});
|
||
|
||
test("rollupPatchStatus: 已经落盘过的补丁仍算 already,不因个别文件漂移翻红", () => {
|
||
// 多文件补丁里个别文件本来就不含这段代码(另一个构建产物),
|
||
// 这种"假漂移"不能盖掉已经打上的部分。
|
||
assert.equal(rollupPatchStatus({ ...base, already: 1, drifted: 1 }), "already");
|
||
});
|
||
|
||
test("rollupPatchStatus: 写错误优先于 applied,不能把 EACCES 吞成绿", () => {
|
||
assert.equal(
|
||
rollupPatchStatus({ ...base, applied: 1, lastError: "write_error:EACCES" }),
|
||
"write_error:EACCES",
|
||
);
|
||
});
|
||
|
||
test("rollupPatchStatus: 文件全缺 → missing_file", () => {
|
||
assert.equal(rollupPatchStatus({ ...base, missing: 1 }), "missing_file");
|
||
});
|
||
|
||
test("filePatchOutcome: 文件在、marker 没有、pattern 一条都不中 → drifted", () => {
|
||
const patch = { id: 9801, replacements: [{ pattern: "NEEDLE_NOT_THERE", replace: "x" }] };
|
||
assert.equal(filePatchOutcome("host text", patch, ""), "drifted");
|
||
});
|
||
|
||
test("filePatchOutcome: marker 在 → applied(不算漂移)", () => {
|
||
const patch = {
|
||
id: 9802,
|
||
markers: ["[dsh-purge] marker"],
|
||
replacements: [{ pattern: "NEEDLE_NOT_THERE", replace: "x" }],
|
||
};
|
||
assert.equal(filePatchOutcome("host [dsh-purge] marker text", patch, ""), "applied");
|
||
});
|
||
|
||
test("filePatchOutcome: pattern 还在 → pending", () => {
|
||
const patch = { id: 9803, replacements: [{ pattern: "NEEDLE", replace: "x" }] };
|
||
assert.equal(filePatchOutcome("a NEEDLE b", patch, ""), "pending");
|
||
});
|
||
|
||
test("自标记型补丁:pattern 就是本插件留下的痕迹,找不到 = 本来没加过,不算漂移", () => {
|
||
const patch = {
|
||
id: 9804,
|
||
replacements: [{ pattern: "- id: tool-fs # [dsh-purge] minimal fs tool parity", replace: "x" }],
|
||
};
|
||
assert.equal(isSelfMarkedPatch(patch), true);
|
||
assert.equal(filePatchOutcome("host text", patch, ""), "skipped");
|
||
});
|
||
|
||
test("自标记型判定:普通补丁不算自标记,#80 算", () => {
|
||
assert.equal(isSelfMarkedPatch({ id: 9805, replacements: [{ pattern: "const a = 1;", replace: "x" }] }), false);
|
||
const p80 = ALL_PATCHES.find((p) => p.id === 80);
|
||
assert.ok(p80, "找不到 #80");
|
||
assert.equal(isSelfMarkedPatch(p80), true);
|
||
});
|
||
|
||
/* ---------------------------------------------------------------------------
|
||
* 就绪口径:drifted 必须让"已就绪"变红(可选补丁不阻断 apply,但状态仍在)。
|
||
* ------------------------------------------------------------------------- */
|
||
|
||
test("patchStatusReady: drifted 的非可选补丁不算就绪", () => {
|
||
assert.equal(patchStatusReady("drifted", { optional: false }), false);
|
||
});
|
||
|
||
test("patchStatusReady: drifted 的可选补丁不阻断就绪", () => {
|
||
assert.equal(patchStatusReady("drifted", { optional: true }), true);
|
||
});
|
||
|
||
test("patchStatusReady: applied / already 算就绪", () => {
|
||
assert.equal(patchStatusReady("applied"), true);
|
||
assert.equal(patchStatusReady("already"), true);
|
||
});
|
||
|
||
test("patchStatusReady: pending 不算就绪", () => {
|
||
assert.equal(patchStatusReady("pending"), false);
|
||
});
|
||
|
||
test("patchStatusReady: skipped 不再天然就绪,可选补丁仍然就绪", () => {
|
||
assert.equal(patchStatusReady("skipped", { optional: false }), false);
|
||
assert.equal(patchStatusReady("skipped", { optional: true }), true);
|
||
});
|
||
|
||
/* ---------------------------------------------------------------------------
|
||
* 展示层统计:drifted 不能被算进 ready。
|
||
* ------------------------------------------------------------------------- */
|
||
|
||
test("tallyDisplayPatches: drifted 不计入 ready", () => {
|
||
const out = tallyDisplayPatches([
|
||
{ status: "applied" },
|
||
{ status: "drifted" },
|
||
{ status: "skipped" },
|
||
]);
|
||
assert.equal(out.applied, 1);
|
||
assert.equal(out.drifted, 1);
|
||
assert.equal(out.ready, 2, "只有 applied + skipped 计入 ready");
|
||
});
|
||
|
||
test("rollupDisplayStatus: 一组里只要有一条漂移,这组就不是干净绿", () => {
|
||
assert.equal(rollupDisplayStatus(["applied", "drifted"]), "drifted");
|
||
assert.equal(rollupDisplayStatus(["applied", "applied"]), "applied");
|
||
assert.equal(rollupDisplayStatus(["pending", "drifted"]), "pending");
|
||
});
|
||
|
||
/* ---------------------------------------------------------------------------
|
||
* 软成功口径:skipped 不再天然软成功;drifted 看得见但不阻断 Apply。
|
||
* ------------------------------------------------------------------------- */
|
||
|
||
test("isPatchResultSoftOk: skipped 只有可选/自标记型才算软成功", () => {
|
||
assert.equal(isPatchResultSoftOk({ status: "skipped" }, { id: 1, optional: false }), false);
|
||
assert.equal(isPatchResultSoftOk({ status: "skipped" }, { id: 2, optional: true }), true);
|
||
});
|
||
|
||
test("isPatchResultSoftOk: drifted 不阻断 Apply(兼容 0.1.6 / 0.2.0 / 0.2.1-rc.2)", () => {
|
||
assert.equal(isPatchResultSoftOk({ status: "drifted" }, { id: 3, optional: false }), true);
|
||
});
|
||
|
||
/* ---------------------------------------------------------------------------
|
||
* 幂等:二次应用不得继续增长(历史事故是每次 apply +841 字符、永不收敛)。
|
||
* ------------------------------------------------------------------------- */
|
||
|
||
test("applyReplacementsToText: 全部字面量 pattern 二次应用不叠加", () => {
|
||
const offenders = [];
|
||
for (const p of ALL_PATCHES) {
|
||
const reps = (p.replacements || []).filter((r) => r.pattern && !(r.pattern instanceof RegExp));
|
||
if (reps.length === 0) continue;
|
||
// 用 pattern 本体合成输入:保证第一遍一定命中,从而真正检验收敛性。
|
||
const src = reps.map((r) => String(r.pattern)).join("\n");
|
||
const once = applyReplacementsToText(src, p, "");
|
||
const twice = applyReplacementsToText(once.text, p, "");
|
||
if (twice.text !== once.text) offenders.push(`#${p.id} ${p.name}`);
|
||
}
|
||
assert.deepEqual(offenders, [], `这些补丁二次应用还在变:${offenders.join(", ")}`);
|
||
});
|
||
|
||
test("applyReplacementsToText: 没有残留 pattern 时 changed=false", () => {
|
||
const patch = { id: 9901, replacements: [{ pattern: "NOT_PRESENT_ANYWHERE", replace: "x" }] };
|
||
const r = applyReplacementsToText("hello world", patch, "");
|
||
assert.equal(r.changed, false);
|
||
assert.equal(r.text, "hello world");
|
||
});
|
||
|
||
test("#77 锚点容错:官方在中间插一行也照样命中,且二次应用不叠加", () => {
|
||
const p77 = ALL_PATCHES.find((p) => p.id === 77);
|
||
assert.ok(p77, "找不到 #77");
|
||
// 0.2.1-rc.2 起的官方原文:中间多了一行 revised.clear()
|
||
const host =
|
||
"\t\t\tappend(record) {\n" +
|
||
"\t\t\t\tconst event = record.event;\n" +
|
||
"\t\t\t\tif (this.inputs.has(event.seq)) return \"none\";\n" +
|
||
"\t\t\t\tif (this.revised.size > 0) this.revised.clear();\n" +
|
||
"\t\t\t\tthis.inputs.set(event.seq, record);\n";
|
||
const once = applyReplacementsToText(host, p77, "");
|
||
assert.equal(once.changed, true, "rc.2 的原文应当命中");
|
||
assert.ok(once.text.includes("isRewindMarker(event)"), "应当注入回退判断");
|
||
assert.ok(once.text.includes("if (this.revised.size > 0) this.revised.clear();"), "官方那行必须原样保留");
|
||
const twice = applyReplacementsToText(once.text, p77, "");
|
||
assert.equal(twice.text, once.text, "二次应用不得再插一遍");
|
||
});
|
||
|
||
/* ---------------------------------------------------------------------------
|
||
* 命中数契约:官方同名代码多一处就该拒写,而不是把两处一起改掉。
|
||
* ------------------------------------------------------------------------- */
|
||
|
||
test("applyReplacementsToText: expect 命中数不符时拒写并回传实际命中数", () => {
|
||
const patch = { id: 9902, replacements: [{ pattern: "NEEDLE", replace: "X", expect: 1 }] };
|
||
const r = applyReplacementsToText("NEEDLE NEEDLE", patch, "");
|
||
assert.ok(r.hitMismatch, "应当回传 hitMismatch");
|
||
assert.equal(r.hitMismatch.expected, 1);
|
||
assert.equal(r.hitMismatch.actual, 2);
|
||
assert.equal(r.text, "NEEDLE NEEDLE", "拒写时原文必须原样返回");
|
||
assert.equal(r.changed, false);
|
||
});
|
||
|
||
test("applyReplacementsToText: expect 命中数一致时正常替换", () => {
|
||
const patch = { id: 9903, replacements: [{ pattern: "NEEDLE", replace: "X", expect: 1 }] };
|
||
const r = applyReplacementsToText("a NEEDLE b", patch, "");
|
||
assert.equal(r.hitMismatch, undefined);
|
||
assert.equal(r.text, "a X b");
|
||
});
|
||
|
||
test("applyReplacementsToText: expect=1 但一处都没命中也算不符(不许静默跳过)", () => {
|
||
const patch = { id: 9905, replacements: [{ pattern: "NEEDLE", replace: "X", expect: 1 }] };
|
||
const r = applyReplacementsToText("nothing here", patch, "");
|
||
assert.ok(r.hitMismatch, "0 次命中同样是失配");
|
||
assert.equal(r.hitMismatch.actual, 0);
|
||
assert.equal(r.changed, false);
|
||
});
|
||
|
||
test("applyReplacementsToText: 未声明 expect 时保持旧行为(全量替换)", () => {
|
||
const patch = { id: 9904, replacements: [{ pattern: "N", replace: "X" }] };
|
||
const r = applyReplacementsToText("N N", patch, "");
|
||
assert.equal(r.hitMismatch, undefined);
|
||
assert.equal(r.text, "X X");
|
||
});
|
||
|
||
test("applyReplacementsToText: 正则 pattern 的 expect 同样生效(官方复制成两份就拒写)", () => {
|
||
const patch = { id: 9906, replacements: [{ pattern: /NEEDLE/g, replace: "X", expect: 1 }] };
|
||
const r = applyReplacementsToText("NEEDLE NEEDLE", patch, "");
|
||
assert.ok(r.hitMismatch, "正则也必须遵守命中数契约");
|
||
assert.equal(r.hitMismatch.actual, 2);
|
||
assert.equal(r.text, "NEEDLE NEEDLE", "拒写时原文必须原样返回");
|
||
});
|
||
|
||
/* ---------------------------------------------------------------------------
|
||
* 宿主版本门禁
|
||
* ------------------------------------------------------------------------- */
|
||
|
||
test("parseHostVersion: 拆出主次修订与预发布段", () => {
|
||
assert.deepEqual(parseHostVersion("0.2.1-rc.3"), {
|
||
major: 0, minor: 2, patch: 1, pre: ["rc", "3"],
|
||
});
|
||
assert.equal(parseHostVersion("not-a-version"), null);
|
||
assert.deepEqual(parseHostVersion("v1.2.3"), { major: 1, minor: 2, patch: 3, pre: [] });
|
||
});
|