mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 04:40:22 +08:00
- restart-web.js 把后台 server 的 pid/url 记到 \/dsh-purge/web-server-<port>.json - killOldServer 增加按端口兜底:清掉旧版本留下、没有 pid 记录的孤儿 - 新增 lib/bin-preflight.js:把回收钩子注入宿主 bin.js 顶部,早于 Cordis 激活与 webserver 绑端口 - 重启时打印『后台运行、关窗不会停』,避免把提示符返回误判成程序已退出 - 新增 test/web-reclaim.test.js(7 例),全量 node --test 91/91 通过
85 lines
3.2 KiB
JavaScript
85 lines
3.2 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import {
|
|
clearRecord,
|
|
dshHomeDir,
|
|
looksLikeDshWeb,
|
|
parseLsofPids,
|
|
parseNetstatListeners,
|
|
readRecord,
|
|
recordPath,
|
|
writeRecord,
|
|
} from "../lib/web-reclaim.js";
|
|
|
|
test("recordPath 落在 $DSH_HOME/dsh-purge 下,按端口区分", () => {
|
|
const home = path.join(os.tmpdir(), "dsh-home-x");
|
|
assert.equal(recordPath(home, 3080), path.join(home, "dsh-purge", "web-server-3080.json"));
|
|
assert.equal(recordPath(home, 4000), path.join(home, "dsh-purge", "web-server-4000.json"));
|
|
});
|
|
|
|
test("write/read/clear 记录往返", () => {
|
|
const home = fs.mkdtempSync(path.join(os.tmpdir(), "dsh-purge-rec-"));
|
|
try {
|
|
writeRecord(home, 3080, 4321, { url: "http://127.0.0.1:3080/?token=x" });
|
|
const rec = readRecord(home, 3080);
|
|
assert.equal(rec.pid, 4321);
|
|
assert.equal(rec.port, 3080);
|
|
assert.ok(rec.at);
|
|
assert.ok(clearRecord(home, 3080));
|
|
assert.equal(readRecord(home, 3080), null);
|
|
} finally {
|
|
fs.rmSync(home, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("readRecord 对坏文件返回 null 而不抛", () => {
|
|
const home = fs.mkdtempSync(path.join(os.tmpdir(), "dsh-purge-bad-"));
|
|
try {
|
|
fs.mkdirSync(path.join(home, "dsh-purge"), { recursive: true });
|
|
fs.writeFileSync(path.join(home, "dsh-purge", "web-server-3080.json"), "{not json", "utf8");
|
|
assert.equal(readRecord(home, 3080), null);
|
|
} finally {
|
|
fs.rmSync(home, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("dshHomeDir 优先 DSH_HOME,其次 ~/.dsh", () => {
|
|
assert.equal(dshHomeDir({ DSH_HOME: " D:\\dsh-home " }), "D:\\dsh-home");
|
|
assert.equal(dshHomeDir({}), path.join(os.homedir(), ".dsh"));
|
|
});
|
|
|
|
test("looksLikeDshWeb 只认 dsh bin.js + web 子命令", () => {
|
|
const win =
|
|
'"node" "C:\\Users\\X\\AppData\\Roaming\\npm\\\\node_modules\\@deepseek-ai\\dsh\\lib\\bin.js" web';
|
|
const posix = "/usr/bin/node /usr/lib/node_modules/@deepseek-ai/dsh/lib/bin.js web --port 3080";
|
|
assert.equal(looksLikeDshWeb(win), true);
|
|
assert.equal(looksLikeDshWeb(posix), true);
|
|
|
|
// 同机其它 node 进程不能被误杀
|
|
assert.equal(looksLikeDshWeb('"node" "C:\\app\\server.js" web'), false);
|
|
assert.equal(looksLikeDshWeb('"node" "…\\@deepseek-ai\\dsh\\lib\\bin.js" desktop'), false);
|
|
assert.equal(looksLikeDshWeb(""), false);
|
|
assert.equal(looksLikeDshWeb(undefined), false);
|
|
});
|
|
|
|
test("parseNetstatListeners 抽出 LISTENING 行", () => {
|
|
const out = [
|
|
" TCP 127.0.0.1:3080 0.0.0.0:0 LISTENING 28276",
|
|
" TCP 127.0.0.1:3080 127.0.0.1:50511 TIME_WAIT 0",
|
|
" TCP [::1]:4000 [::]:0 LISTENING 999",
|
|
].join("\r\n");
|
|
const rows = parseNetstatListeners(out);
|
|
assert.equal(rows.length, 2);
|
|
assert.deepEqual(rows[0], { address: "127.0.0.1", port: 3080, pid: 28276 });
|
|
assert.equal(rows[1].pid, 999);
|
|
});
|
|
|
|
test("parseLsofPids 处理多行/空输出", () => {
|
|
assert.deepEqual(parseLsofPids("1234\n5678\n"), [1234, 5678]);
|
|
assert.deepEqual(parseLsofPids(""), []);
|
|
assert.deepEqual(parseLsofPids(" \n"), []);
|
|
});
|