dsh-plugin-mesh 0.5.6 → 0.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,276 @@
1
+ /**
2
+ * 「检查更新」的自测:版本比较 + registry 查询失败时的语义。
3
+ *
4
+ * 这一块的**核心断言不是"能查出新版本",而是"查不到时必须说查不到"**。
5
+ * 本项目最贵的教训(AGENTS.md 硬约束 1):探测失败被当成否定事实 ——
6
+ * 这里如果写成「没问到 → 当作已是最新」,用户会永远升不了级,而且看不出来。
7
+ *
8
+ * 全部无网络:fetch 是注入的假实现。
9
+ *
10
+ * node lib/_selftest-update.mjs
11
+ */
12
+ import { strict as assert } from "node:assert";
13
+ import {
14
+ buildUpdateReport,
15
+ buildUpgradeCommand,
16
+ compareVersions,
17
+ fetchLatestVersion,
18
+ parseVersion,
19
+ pluginDirFromUrl,
20
+ updateKind,
21
+ DEFAULT_REGISTRIES,
22
+ } from "./update.js";
23
+
24
+ let passed = 0;
25
+ let failed = 0;
26
+ function test(name, fn) {
27
+ try {
28
+ fn();
29
+ passed += 1;
30
+ console.log(` ✓ ${name}`);
31
+ } catch (err) {
32
+ failed += 1;
33
+ console.log(` ✗ ${name}`);
34
+ console.log(` ${err?.message ?? err}`);
35
+ }
36
+ }
37
+
38
+ async function testAsync(name, fn) {
39
+ try {
40
+ await fn();
41
+ passed += 1;
42
+ console.log(` ✓ ${name}`);
43
+ } catch (err) {
44
+ failed += 1;
45
+ console.log(` ✗ ${name}`);
46
+ console.log(` ${err?.message ?? err}`);
47
+ }
48
+ }
49
+
50
+ const jsonResponse = (body, status = 200) => ({
51
+ ok: status >= 200 && status < 300,
52
+ status,
53
+ json: async () => body,
54
+ });
55
+
56
+ console.log("\n── 版本解析与比较 ──");
57
+
58
+ test("解析 x.y.z / v 前缀 / 预发布 / build 元数据", () => {
59
+ assert.deepEqual(parseVersion("0.5.6"), { major: 0, minor: 5, patch: 6, pre: [] });
60
+ assert.deepEqual(parseVersion("v1.2.3"), { major: 1, minor: 2, patch: 3, pre: [] });
61
+ assert.deepEqual(parseVersion(" 1.2.3-beta.2 "), { major: 1, minor: 2, patch: 3, pre: ["beta", "2"] });
62
+ assert.deepEqual(parseVersion("1.2.3+build.9"), { major: 1, minor: 2, patch: 3, pre: [] });
63
+ });
64
+
65
+ test("不是语义化版本 → null(不能假装能比)", () => {
66
+ for (const bad of ["", "latest", "1.2", "1.2.3.4", "x.y.z", null, undefined, "1.2.x"]) {
67
+ assert.equal(parseVersion(bad), null, JSON.stringify(bad));
68
+ }
69
+ });
70
+
71
+ test("主/次/修订逐段比较", () => {
72
+ assert.equal(compareVersions("0.5.6", "0.5.7"), -1);
73
+ assert.equal(compareVersions("0.6.0", "0.5.9"), 1);
74
+ assert.equal(compareVersions("1.0.0", "1.0.0"), 0);
75
+ assert.equal(compareVersions("v1.0.0", "1.0.0"), 0, "v 前缀不影响比较");
76
+ });
77
+
78
+ test("预发布小于同号正式版;预发布之间按 semver 规则", () => {
79
+ assert.equal(compareVersions("1.0.0-beta", "1.0.0"), -1);
80
+ assert.equal(compareVersions("1.0.0", "1.0.0-rc.1"), 1);
81
+ assert.equal(compareVersions("1.0.0-alpha", "1.0.0-beta"), -1);
82
+ assert.equal(compareVersions("1.0.0-beta.2", "1.0.0-beta.10"), -1, "数字段按数值比");
83
+ assert.equal(compareVersions("1.0.0-beta", "1.0.0-beta.1"), -1, "少的段更小");
84
+ });
85
+
86
+ test("任一侧无法解析 → null,不是 0", () => {
87
+ assert.equal(compareVersions("0.5.6", "latest"), null);
88
+ assert.equal(compareVersions("", "0.5.6"), null);
89
+ });
90
+
91
+ test("更新量级:major / minor / patch / prerelease / none", () => {
92
+ assert.equal(updateKind("0.5.6", "1.0.0"), "major");
93
+ assert.equal(updateKind("0.5.6", "0.6.0"), "minor");
94
+ assert.equal(updateKind("0.5.6", "0.5.7"), "patch");
95
+ assert.equal(updateKind("0.6.0-beta.1", "0.6.0"), "prerelease");
96
+ assert.equal(updateKind("0.5.6", "0.5.6-rc.1"), "none", "预发布比正式版旧");
97
+ assert.equal(updateKind("0.5.6", "0.5.6"), "none");
98
+ assert.equal(updateKind("0.5.7", "0.5.6"), "none", "更旧不叫更新");
99
+ assert.equal(updateKind("0.5.6", "latest"), "unknown");
100
+ });
101
+
102
+ console.log("\n── 升级命令 / 安装目录 ──");
103
+
104
+ test("插件安装目录从 import.meta.url 推出(lib/ 的上一级,带结尾斜杠)", () => {
105
+ const dir = pluginDirFromUrl("file:///Users/peng/.dsh/profiles/desktop/node_modules/dsh-plugin-mesh/lib/index.js");
106
+ assert.equal(dir, "/Users/peng/.dsh/profiles/desktop/node_modules/dsh-plugin-mesh/");
107
+ });
108
+
109
+ test("升级命令用的是 release 真实资产名与 tag(与 scripts/release-plugin.sh 一致)", () => {
110
+ const cmd = buildUpgradeCommand({ version: "0.6.0", pluginDir: "/tmp/plugin/" });
111
+ assert.ok(cmd.includes("plugin-v0.6.0"), cmd);
112
+ assert.ok(cmd.includes("dsh-plugin-mesh-0.6.0.tar.gz"), cmd);
113
+ assert.ok(cmd.includes('tar xz -C "/tmp/plugin/"'), cmd);
114
+ assert.ok(cmd.startsWith("curl -L https://ghfast.top/https://github.com/peng456/dsh-plugin-mesh/releases/download/"), cmd);
115
+ });
116
+
117
+ test("没有版本就不编一条假命令", () => {
118
+ assert.equal(buildUpgradeCommand({ version: "", pluginDir: "/x/" }), "");
119
+ });
120
+
121
+ console.log("\n── 报告组装:失败绝不能说成「已是最新」──");
122
+
123
+ const okLatest = { ok: true, version: "0.6.0", registry: DEFAULT_REGISTRIES[0] };
124
+
125
+ test("有新版本 → hasUpdate / kind / 链接 / 可复制命令都在", () => {
126
+ const r = buildUpdateReport({
127
+ current: "0.5.6",
128
+ diskVersion: "0.5.6",
129
+ latest: okLatest,
130
+ pluginDir: "/p/",
131
+ now: Date.parse("2026-10-29T00:00:00Z"),
132
+ });
133
+ assert.equal(r.ok, true);
134
+ assert.equal(r.current, "0.5.6");
135
+ assert.equal(r.latest, "0.6.0");
136
+ assert.equal(r.comparable, true);
137
+ assert.equal(r.hasUpdate, true);
138
+ assert.equal(r.kind, "minor");
139
+ assert.equal(r.versionDrift, false);
140
+ assert.equal(r.checkedAt, "2026-10-29T00:00:00.000Z");
141
+ assert.ok(r.releaseUrl.endsWith("/releases/tag/plugin-v0.6.0"), r.releaseUrl);
142
+ assert.ok(r.npmUrl.endsWith("/v/0.6.0"), r.npmUrl);
143
+ assert.ok(r.upgradeCommand.includes("0.6.0"));
144
+ assert.ok(r.upgradeCommand.includes('"/p/"'));
145
+ });
146
+
147
+ test("同版本 → 已是最新(hasUpdate=false, kind=none)", () => {
148
+ const r = buildUpdateReport({ current: "0.6.0", latest: okLatest, pluginDir: "/p/" });
149
+ assert.equal(r.ok, true);
150
+ assert.equal(r.hasUpdate, false);
151
+ assert.equal(r.kind, "none");
152
+ assert.equal(r.comparable, true);
153
+ });
154
+
155
+ test("版本号无法比较 → comparable=false,且 hasUpdate 不能是 true", () => {
156
+ const r = buildUpdateReport({
157
+ current: "0.5.6",
158
+ latest: { ok: true, version: "2026.1", registry: "x" },
159
+ });
160
+ assert.equal(r.ok, true);
161
+ assert.equal(r.comparable, false);
162
+ assert.equal(r.hasUpdate, false);
163
+ assert.equal(r.kind, "unknown");
164
+ });
165
+
166
+ test("registry 查询失败 → ok:false 且带原因;不能有 latest,也不能说没有更新", () => {
167
+ const r = buildUpdateReport({
168
+ current: "0.5.6",
169
+ latest: { ok: false, error: "检查更新失败:registry.npmjs.org 超时(5000ms)", failures: ["a 超时", "b HTTP 502"] },
170
+ });
171
+ assert.equal(r.ok, false);
172
+ assert.ok(r.error.includes("超时"), r.error);
173
+ assert.deepEqual(r.failures, ["a 超时", "b HTTP 502"]);
174
+ assert.equal("latest" in r, false, "没问到最新版本就不能回报 latest");
175
+ assert.equal("hasUpdate" in r, false, "没问到就不能声称有/没有更新");
176
+ });
177
+
178
+ test("磁盘版本与进程版本不一致 → versionDrift=true(升级了没重启)", () => {
179
+ const r = buildUpdateReport({ current: "0.5.6", diskVersion: "0.6.0", latest: okLatest });
180
+ assert.equal(r.versionDrift, true);
181
+ const same = buildUpdateReport({ current: "0.6.0", diskVersion: "0.6.0", latest: okLatest });
182
+ assert.equal(same.versionDrift, false);
183
+ });
184
+
185
+ console.log("\n── registry 查询:逐源尝试,失败原因全留 ──");
186
+
187
+ await testAsync("官方源成功 → 直接用,不再问镜像", async () => {
188
+ const calls = [];
189
+ const r = await fetchLatestVersion({
190
+ registries: ["https://a.test/latest", "https://b.test/latest"],
191
+ fetchImpl: async (url) => {
192
+ calls.push(url);
193
+ return jsonResponse({ version: "0.6.0" });
194
+ },
195
+ });
196
+ assert.equal(r.ok, true);
197
+ assert.equal(r.version, "0.6.0");
198
+ assert.equal(r.registry, "https://a.test/latest");
199
+ assert.deepEqual(calls, ["https://a.test/latest"]);
200
+ });
201
+
202
+ await testAsync("官方源 HTTP 500 → 回退镜像成功", async () => {
203
+ const r = await fetchLatestVersion({
204
+ registries: ["https://a.test/latest", "https://b.test/latest"],
205
+ fetchImpl: async (url) =>
206
+ url.startsWith("https://a.test") ? jsonResponse({ error: "boom" }, 500) : jsonResponse({ version: "0.7.0" }),
207
+ });
208
+ assert.equal(r.ok, true);
209
+ assert.equal(r.version, "0.7.0");
210
+ assert.equal(r.registry, "https://b.test/latest");
211
+ });
212
+
213
+ await testAsync("两个源都挂 → ok:false,且每个原因都保留(能看出是谁挂了)", async () => {
214
+ const r = await fetchLatestVersion({
215
+ registries: ["https://a.test/latest", "https://b.test/latest"],
216
+ fetchImpl: async (url) => {
217
+ if (url.startsWith("https://a.test")) throw new Error("ENOTFOUND a.test");
218
+ return jsonResponse({}, 502);
219
+ },
220
+ });
221
+ assert.equal(r.ok, false);
222
+ assert.equal(r.failures.length, 2, JSON.stringify(r.failures));
223
+ assert.ok(r.failures[0].includes("ENOTFOUND"), r.failures[0]);
224
+ assert.ok(r.failures[1].includes("HTTP 502"), r.failures[1]);
225
+ assert.ok(r.error.includes("检查更新失败"), r.error);
226
+ });
227
+
228
+ await testAsync("超时要写成「超时」,不能和 HTTP 错误混为一谈", async () => {
229
+ const timeout = Object.assign(new Error("The operation was aborted due to timeout"), { name: "TimeoutError" });
230
+ const r = await fetchLatestVersion({
231
+ registries: ["https://a.test/latest"],
232
+ timeoutMs: 1234,
233
+ fetchImpl: async () => {
234
+ throw timeout;
235
+ },
236
+ });
237
+ assert.equal(r.ok, false);
238
+ assert.ok(r.failures[0].includes("超时(1234ms)"), r.failures[0]);
239
+ });
240
+
241
+ await testAsync("响应里没有 version → 该源不算成功,继续下一个", async () => {
242
+ const r = await fetchLatestVersion({
243
+ registries: ["https://a.test/latest", "https://b.test/latest"],
244
+ fetchImpl: async (url) =>
245
+ url.startsWith("https://a.test") ? jsonResponse({ name: "dsh-plugin-mesh" }) : jsonResponse({ version: "0.6.0" }),
246
+ });
247
+ assert.equal(r.ok, true);
248
+ assert.equal(r.version, "0.6.0");
249
+ });
250
+
251
+ await testAsync("registry 返回无法比较的版本 → 该源不算成功,最终报失败", async () => {
252
+ const r = await fetchLatestVersion({
253
+ registries: ["https://a.test/latest"],
254
+ fetchImpl: async () => jsonResponse({ version: "2026.1" }),
255
+ });
256
+ assert.equal(r.ok, false);
257
+ assert.ok(r.failures[0].includes("2026.1"), r.failures[0]);
258
+ });
259
+
260
+ await testAsync("没有 fetch 的运行环境 → 说清楚无法检查,而不是静默", async () => {
261
+ const r = await fetchLatestVersion({ fetchImpl: null, registries: [] });
262
+ assert.equal(r.ok, false);
263
+ assert.ok(r.error.includes("fetch"), r.error);
264
+ });
265
+
266
+ test("pendingUpgrade 原样带出:磁盘装好了但进程没重启,界面靠它给重启/重载", () => {
267
+ const pending = { version: "0.6.0", previous: "0.5.6", restartRequired: true, reloadRequired: false, inferred: false };
268
+ const r = buildUpdateReport({ current: "0.5.6", diskVersion: "0.6.0", latest: okLatest, pendingUpgrade: pending });
269
+ assert.equal(r.pendingUpgrade.restartRequired, true);
270
+ assert.equal(r.pendingUpgrade.version, "0.6.0");
271
+ const none = buildUpdateReport({ current: "0.6.0", diskVersion: "0.6.0", latest: okLatest });
272
+ assert.equal(none.pendingUpgrade, null);
273
+ });
274
+
275
+ console.log(`\n${failed === 0 ? "全部通过" : "有失败"}:${passed} 通过 / ${failed} 失败`);
276
+ process.exit(failed === 0 ? 0 : 1);