@mrrisega/dsh-remote 0.6.4-beta.1 → 0.6.4-beta.10

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,183 @@
1
+ /**
2
+ * 「patch 热加载激活」契约测试(node:test,零框架)。
3
+ *
4
+ * 背景:harness 的 web profile 是 `patchReload: "live"`,启动时加载 `@deepseek-ai/cordis-plugin-hmr`
5
+ * 并监听 profile 的 `cordis.patch.yml` —— 存盘后约 1 秒重新 compose 并**动态装载**插件,
6
+ * 不需要重启 dsh web(插件市场自己就用这套机制)。而 `dsh.profile.bundles` 只在启动时读、
7
+ * 没有 watcher,所以「bundles 激活」必须重启 —— 这正是用户装完看不到「远程控制」面板的原因。
8
+ *
9
+ * 因此安装器改为:**激活点 = patch 行**(热加载),bundles 那条同时移除(单一激活点,
10
+ * 否则 dsh web 启动即报「重复 ID」崩溃)。
11
+ *
12
+ * 这些用例在**临时 profile**(mkdtemp)里跑真实的 `dsh-setup.mjs plugin` 子进程,
13
+ * 断言生成的文件产物;绝不触碰开发者真实的 ~/.dsh/profiles/**。
14
+ */
15
+
16
+ import { test } from "node:test";
17
+ import assert from "node:assert/strict";
18
+ import { execFileSync, spawnSync } from "node:child_process";
19
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync, mkdirSync, cpSync } from "node:fs";
20
+ import { tmpdir } from "node:os";
21
+ import { dirname, join } from "node:path";
22
+ import { fileURLToPath } from "node:url";
23
+
24
+ const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "..");
25
+ const SETUP = join(ROOT, "dsh-setup.mjs");
26
+
27
+ const PLUGIN_BUNDLES = ["@deepseek-ai/dsh-base", "@deepseek-ai/dsh-web-app"];
28
+
29
+ /** 造一个临时 profile(默认 patch 是新 profile 的空文档占位 `[]`)。 */
30
+ function makeProfile({ patch = "# dsh profile patch\n[]\n", bundles = PLUGIN_BUNDLES } = {}) {
31
+ const dir = mkdtempSync(join(tmpdir(), "dshac-"));
32
+ writeFileSync(join(dir, "package.json"), JSON.stringify({
33
+ name: "dsh-profile-web", private: true, dependencies: {},
34
+ dsh: { profile: { bundles: [...bundles], patchReload: "live" } }
35
+ }, null, 2) + "\n");
36
+ writeFileSync(join(dir, "cordis.patch.yml"), patch);
37
+ return dir;
38
+ }
39
+
40
+ /** 跑真实的 plugin 安装(子进程,指向临时 profile 与临时配置目录)。stdout+stderr 一并返回。 */
41
+ function installPluginWithStderr(profileDir, relayDir) {
42
+ const r = spawnSync(process.execPath, [SETUP, "plugin", "--profile", profileDir], {
43
+ encoding: "utf8",
44
+ env: { ...process.env, DSH_RELAY_DIR: relayDir, DSH_RELAY_SKIP_SERVICE: "1" }
45
+ });
46
+ return { out: String(r.stdout || ""), err: String(r.stderr || ""), code: r.status };
47
+ }
48
+
49
+ /** 只要 stdout(正常路径够用)。 */
50
+ function installPlugin(profileDir, relayDir) {
51
+ const r = installPluginWithStderr(profileDir, relayDir);
52
+ assert.equal(r.code, 0, `安装子进程应成功退出,stderr=${r.err.slice(0, 200)}`);
53
+ return r.out;
54
+ }
55
+
56
+ function cleanup(...dirs) {
57
+ for (const d of dirs) { try { rmSync(d, { recursive: true, force: true }); } catch { /* ignore */ } }
58
+ }
59
+
60
+ const readBundles = (dir) => JSON.parse(readFileSync(join(dir, "package.json"), "utf8")).dsh.profile.bundles;
61
+ const readPatch = (dir) => readFileSync(join(dir, "cordis.patch.yml"), "utf8");
62
+
63
+ test("patch 激活:写入 insert 行(热加载形态),并从 bundles 移除该插件(单一激活点)", () => {
64
+ const profile = makeProfile();
65
+ const relay = join(profile, "relay");
66
+ try {
67
+ installPlugin(profile, relay);
68
+ const patch = readPatch(profile);
69
+ assert.match(patch, /- insert:/, "应写入 insert 块");
70
+ assert.match(patch, /- id: dsh-remote-web/, "应写入本插件的 id 行");
71
+ assert.match(patch, /name: 'dsh-remote-web'/, "name 必须是裸包名(加载器据此 import)");
72
+ assert.ok(!patch.includes("[]"), "空文档占位 `[]` 必须被清掉(否则整份 patch 解析失败)");
73
+ assert.deepEqual(readBundles(profile), PLUGIN_BUNDLES, "bundles 里不能再有本插件(两处激活=重复 ID 崩溃)");
74
+ } finally { cleanup(profile); }
75
+ });
76
+
77
+ test("patch 激活:relayDir 写的是**配置目录**而不是插件安装目录", () => {
78
+ const profile = makeProfile();
79
+ const relay = join(profile, "relay-config");
80
+ mkdirSync(relay, { recursive: true });
81
+ try {
82
+ installPlugin(profile, relay);
83
+ const patch = readPatch(profile);
84
+ const m = /relayDir: '([^']+)'/.exec(patch);
85
+ assert.ok(m, "应带 relayDir 配置");
86
+ // 面板要读的是该目录下的 .dsh-config.json;写成插件目录会让面板显示成"未登录/空配置"
87
+ assert.equal(m[1], relay, "relayDir 必须是配置目录");
88
+ assert.ok(!m[1].includes("dsh-remote-web-plugin"), "relayDir 不得指向插件源码目录");
89
+ } finally { cleanup(profile); }
90
+ });
91
+
92
+ test("patch 激活:幂等 —— 重复安装不会写出第二个 id(否则重复 ID 崩溃)", () => {
93
+ const profile = makeProfile();
94
+ const relay = join(profile, "relay");
95
+ try {
96
+ installPlugin(profile, relay);
97
+ const first = readPatch(profile);
98
+ installPlugin(profile, relay);
99
+ const second = readPatch(profile);
100
+ assert.equal(second, first, "第二次安装不应再改 patch");
101
+ const ids = second.match(/- id: dsh-remote-web/g) || [];
102
+ assert.equal(ids.length, 1, `id 行必须恰好一条,实际 ${ids.length}`);
103
+ } finally { cleanup(profile); }
104
+ });
105
+
106
+ test("patch 激活:既有其它插件条目必须原样保留(不能吃掉别人的配置)", () => {
107
+ const other = [
108
+ "# dsh profile patch",
109
+ "[]",
110
+ "",
111
+ "# --- mba-feishu managed block ---",
112
+ "- insert:",
113
+ " - id: mba-feishu",
114
+ " name: 'mba-feishu'",
115
+ " config:",
116
+ " transport: stdio",
117
+ " args:",
118
+ " - 'mcp'",
119
+ "# --- end mba-feishu managed block ---",
120
+ ""
121
+ ].join("\n");
122
+ const profile = makeProfile({ patch: other });
123
+ const relay = join(profile, "relay");
124
+ try {
125
+ installPlugin(profile, relay);
126
+ const patch = readPatch(profile);
127
+ assert.match(patch, /id: mba-feishu/, "其它插件的条目必须保留");
128
+ assert.match(patch, /transport: stdio/, "其它插件的字段必须保留");
129
+ assert.match(patch, /- 'mcp'/, "其它插件的嵌套列表必须保留");
130
+ assert.match(patch, /# --- end mba-feishu managed block ---/, "注释块必须保留");
131
+ assert.match(patch, /- id: dsh-remote-web/, "同时写入我们的条目");
132
+ assert.equal((patch.match(/^- insert:/gm) || []).length, 2, "应有两个独立的 insert 块");
133
+ } finally { cleanup(profile); }
134
+ });
135
+
136
+ test("安全护栏:patch 形态异常时**拒写**并回退 bundles(绝不把能用的 profile 弄坏)", () => {
137
+ const broken = "# 坏 patch:顶层塞了非列表内容\nthis is not a list ===\n- insert:\n - id: other\n name: 'other'\n";
138
+ const profile = makeProfile({ patch: broken });
139
+ const relay = join(profile, "relay");
140
+ try {
141
+ const { out, err } = installPluginWithStderr(profile, relay);
142
+ const all = out + err; // 警告走 stderr,必须一并检查
143
+ assert.match(all, /无法写入 patch 激活行/, "应明确报告拒写");
144
+ assert.match(all, /bundles 形态/, "应回退 bundles 保证功能可用");
145
+ assert.equal(readPatch(profile), broken, "坏文件必须一字未改(拒写,不是先写再看)");
146
+ assert.ok(readBundles(profile).includes("dsh-remote-web"), "回退时应把插件加进 bundles");
147
+ } finally { cleanup(profile); }
148
+ });
149
+
150
+ test("安全护栏:基座含空文档占位 `[]` 时也要先清掉(否则整份 patch 解析失败)", () => {
151
+ const profile = makeProfile({ patch: "[]\n" });
152
+ const relay = join(profile, "relay");
153
+ try {
154
+ installPlugin(profile, relay);
155
+ const patch = readPatch(profile);
156
+ assert.ok(!/^\[\]$/m.test(patch), "占位 [] 必须被移除");
157
+ assert.match(patch, /- id: dsh-remote-web/, "并写入我们的条目");
158
+ } finally { cleanup(profile); }
159
+ });
160
+
161
+ test("安全护栏:原子写回 —— 不留 .tmp 残留,且末尾换行规范", () => {
162
+ const profile = makeProfile();
163
+ const relay = join(profile, "relay");
164
+ try {
165
+ installPlugin(profile, relay);
166
+ assert.ok(!readPatch(profile).includes(".dsh-remote.tmp"), "不应把临时文件名写进内容");
167
+ const leftovers = execFileSync("ls", ["-a", profile], { encoding: "utf8" })
168
+ .split("\n").filter((f) => f.includes(".tmp"));
169
+ assert.deepEqual(leftovers, [], `不应留下临时文件: ${leftovers.join(",")}`);
170
+ assert.ok(readPatch(profile).endsWith("\n"), "文件应以换行结尾");
171
+ } finally { cleanup(profile); }
172
+ });
173
+
174
+ test("回归护栏:plugin 子命令必须把激活结果 return 给 setup(否则热挂载验证被跳过)", () => {
175
+ // 事故复盘:convergePluginActivation 已经返回 {hotPatch},但 pluginCmd 没把它 return 出去,
176
+ // 于是 setup() 拿不到激活形态 → 跳过热挂载验证 → 明明热挂载成功却仍报「需要重启」。
177
+ const src = readFileSync(SETUP, "utf8");
178
+ const call = src.match(/return convergePluginActivation\(profileDir[^\n]*/);
179
+ assert.ok(call, "pluginCmd 必须 return convergePluginActivation(...) 的结果");
180
+ // setup() 必须真的消费这个结果
181
+ assert.match(src, /pluginResult = await pluginCmd\(\[\]\)/, "setup 应保存 pluginCmd 的返回值");
182
+ assert.match(src, /pluginResult\.hotPatch/, "应据 hotPatch 判断是否走热挂载形态");
183
+ });
@@ -0,0 +1,111 @@
1
+ /**
2
+ * 安装脚本输出契约测试(node:test,零框架)。
3
+ *
4
+ * 背景:安装输出一度太长且重复 —— 同一段「下一步: 打开 dsh web → 设置 → 远程控制」
5
+ * 在插件阶段和结尾汇总各打印一遍,「已创建自启动服务」也打印两遍;更糟的是 dsh web
6
+ * 没开时只甩一句「自启动服务启动失败」,用户以为安装坏了(其实是正常状态:bridge 依赖
7
+ * dsh web,dsh web 没开时它起来即退)。
8
+ *
9
+ * 这里盯三条边界:
10
+ * ① 输出里「下一步」引导只出现一次(去重不靠人眼);
11
+ * ② dsh web 未运行 + bridge 未跑 → 明确说明"打开 dsh web 后会自动启动",且不出现
12
+ * 「启动失败」这种误导措辞;
13
+ * ③ dsh web 在运行但 bridge 没跑 → 是真的异常,必须给出原因与日志路径。
14
+ */
15
+
16
+ import { test } from "node:test";
17
+ import assert from "node:assert/strict";
18
+ import { readFileSync } from "node:fs";
19
+ import { fileURLToPath } from "node:url";
20
+ import { dirname, join } from "node:path";
21
+ import { readdirSync } from "node:fs";
22
+
23
+ const HERE = dirname(fileURLToPath(import.meta.url));
24
+ const SETUP = join(HERE, "..", "..", "..", "dsh-setup.mjs");
25
+
26
+ const src = readFileSync(SETUP, "utf8");
27
+
28
+ test("测试隔离:unset 系统操作开关的用例必须同时伪造 HOME(否则污染真实自启动)", () => {
29
+ // 事故复盘:harness-restart 用例 unset DSH_RELAY_SKIP_SERVICE 去走真实分支,
30
+ // 但没伪造 HOME → 插件写 plist 的路径是 join(homedir(), "Library/LaunchAgents/…"),
31
+ // 于是把**指向测试临时目录**的 plist 覆盖到开发者真实的 ~/Library/LaunchAgents,
32
+ // 临时目录随即被删 → 真实 bridge 自启动彻底失效(本机实测踩到,macOS 26 用户反馈排查时发现)。
33
+ // 静态护栏:凡 unset 该开关的用例,文件里必须出现 process.env.HOME =。
34
+ const rows = readdirSync(HERE).filter((f) => f.endsWith(".test.mjs"));
35
+ const offenders = [];
36
+ for (const f of rows) {
37
+ const text = readFileSync(join(HERE, f), "utf8");
38
+ if (/delete process\.env\.DSH_RELAY_SKIP_SERVICE/.test(text) && !/process\.env\.HOME\s*=/.test(text)) offenders.push(f);
39
+ }
40
+ assert.deepEqual(offenders, [], `这些用例会碰真实 HOME(必须伪造 HOME): ${offenders.join(", ")}`);
41
+ // 反向确认:护栏本身确有保护对象,否则是空断言
42
+ const guarded = rows.filter((f) => /delete process\.env\.DSH_RELAY_SKIP_SERVICE/.test(readFileSync(join(HERE, f), "utf8")));
43
+ assert.ok(guarded.length >= 3, `应至少有 3 个用例走真实分支(实际 ${guarded.length})`);
44
+ });
45
+
46
+ test("安装输出:拼接汇总时「下一步」引导只有一处文案源(不再多处重复打印)", () => {
47
+ // 引导语只由汇总的 L.push 产出:自建 / 热挂载 / 需重启 / 普通 四个**互斥**分支,
48
+ // 运行时只可能走其中一条 → 用户始终只看到一条引导。
49
+ const pushes = src.match(/L\.push\(" 下一步[::]/g) || [];
50
+ assert.equal(pushes.length, 4, `「下一步」应只由汇总的 4 个互斥分支产出(实际 ${pushes.length} 处)`);
51
+ // 源码里不得再有其它直接打印引导的地方
52
+ const direct = src.match(/console\.log\((?!.*L\.join)[^)]*下一步/g) || [];
53
+ assert.equal(direct.length, 0, "除汇总外不得再直接打印引导");
54
+ // 插件阶段不得再打印用户引导
55
+ const converge = src.slice(src.indexOf("function convergePluginActivation"), src.indexOf("async function pluginCmd"));
56
+ assert.ok(!/下一步|打开 dsh web → 设置/.test(converge), "插件阶段不应再重复打印引导");
57
+ // 互斥性:这些分支必须挂在同一条 if/else-if 链上,否则可能同时打出两条引导
58
+ const chain = src.slice(src.indexOf("} else if (hotMounted) {"), src.indexOf('L.push("\\n" + L.join'));
59
+ assert.ok(chain.length > 0, "应能定位到引导分支链");
60
+ assert.ok(/else if \(needRestart && restartResult/.test(chain), "热挂载与需重启必须在同一条 else-if 链上");
61
+ assert.ok(/else if \(needRestart\)/.test(chain), "兜底的手动重启分支也必须在同一条链上");
62
+ });
63
+
64
+ test("安装输出:「已创建自启动服务」路径只在汇总里打印一次", () => {
65
+ const createMsgs = src.match(/已创建自启动服务/g) || [];
66
+ assert.equal(createMsgs.length, 0, "创建路径不再单独打印(汇总里统一展示),避免同一信息两遍");
67
+ assert.ok(/自启动服务: \$\{svc\.path/.test(src), "汇总里应展示自启动服务路径");
68
+ });
69
+
70
+ test("安装输出:dsh web 未运行时的措辞 —— 说明会自动启动,且不出现「启动失败」误导", () => {
71
+ assert.ok(/async function isDshWebUp/.test(src), "应探测 dsh web 是否在运行(127.0.0.1:3080)");
72
+ const block = src.slice(src.indexOf("const webUp = await isDshWebUp();"));
73
+ // 未运行分支必须明说"会自动启动"
74
+ assert.ok(/dsh web 当前没有运行/.test(block), "未运行分支应直说检测结果");
75
+ assert.ok(/自动启动,无需任何命令/.test(block), "未运行分支应说明何时自动启动");
76
+ assert.ok(/不是安装出错/.test(block), "未运行分支应明确否定「安装出错」");
77
+ // 未运行分支的**用户可见文案**里不能出现「失败」(真失败分支才有)
78
+ const notUpBranch = block.slice(block.indexOf("if (!st.ok && !webUp)"), block.indexOf("} else if (!st.ok) {"));
79
+ // 只看 L.push(...) 的实参(注释里的说明文字不算用户可见文案)
80
+ const texts = (notUpBranch.match(/L\.push\((\s*)"([^"]*)"/g) || [])
81
+ .map((m) => (m.match(/"([^"]*)"/) || [])[1]).join("\n");
82
+ assert.ok(texts.length > 0, "该分支应有用户可见文案");
83
+ assert.ok(!/失败/.test(texts), `dsh web 未运行≠bridge 启动失败,该分支文案不得出现「失败」:\n${texts}`);
84
+ });
85
+
86
+ test("安装输出:真失败(dsh web 在运行但 bridge 没起来)必须给原因与日志路径", () => {
87
+ const block = src.slice(src.indexOf("const webUp = await isDshWebUp();"));
88
+ const failBranch = block.slice(block.indexOf("else if (!st.ok)"), block.indexOf("if (selfHosted) {", block.indexOf("else if (!st.ok)")));
89
+ assert.ok(/bridge 未能启动/.test(failBranch), "真失败分支要明说未能启动");
90
+ assert.ok(/\.dsh-bridge\.log/.test(failBranch), "真失败分支要给出日志路径供排查");
91
+ });
92
+
93
+ test("安装输出:跳过/不支持自启动时不得自相矛盾地显示「运行中」", () => {
94
+ const block = src.slice(src.indexOf("const webUp = await isDshWebUp();"));
95
+ // 三态判定必须存在:未安装 与 运行中/未运行 分开
96
+ assert.ok(/const svcSkipped = st\.status === "skipped"/.test(block), "应识别 skipped/unsupported/无路径三态");
97
+ assert.ok(/未安装(本次显式跳过)/.test(block), "显式跳过要说清是跳过");
98
+ assert.ok(/未安装(当前平台不支持自启动)/.test(block), "平台不支持要说清原因");
99
+ // 跳过分支的文案里不能出现「运行中」
100
+ const skippedBranch = block.slice(block.indexOf("if (svcSkipped) {"), block.indexOf("} else if (!st.ok && !webUp) {"));
101
+ const texts = (skippedBranch.match(/L\.push\((`[^`]*`|"[^"]*")/g) || []).join("\n");
102
+ assert.ok(!/运行中/.test(texts), `未安装自启动服务时不得声称「运行中」:\n${texts}`);
103
+ // 未安装时也不该承诺「打开 dsh web 就会自动启动」(那是自启动服务在管)
104
+ assert.ok(!/自动启动/.test(texts), "未安装自启动服务时不得承诺自动启动");
105
+ });
106
+
107
+ test("安装输出:服务状态字符串仍是插件面板可解析的稳定契约", () => {
108
+ // 插件半按 running / 未运行 解析,不能因为排版调整改口径
109
+ assert.ok(/\? `✅ 运行中/.test(src), "running 口径保留(带 pid)");
110
+ assert.ok(/`未运行 \(\$\{st\.detail \|\| st\.status\}\)`/.test(src), "未运行口径保留(带原因)");
111
+ });