mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 00:20:45 +00:00
* fix(#3501): automated AI fix * fix: address Codex review on PR #3502 --------- Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
78 lines
2.9 KiB
JavaScript
78 lines
2.9 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
|
|
const bridge = require("./mcpServerBridge.cjs");
|
|
|
|
function readDiscovery(discoveryPath) {
|
|
return JSON.parse(fs.readFileSync(discoveryPath, "utf8"));
|
|
}
|
|
|
|
test("self-heals a deleted discovery file while the TCP host stays alive", async (t) => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-discovery-test-"));
|
|
const discoveryPath = path.join(dir, "discovery.json");
|
|
t.after(() => {
|
|
bridge.cleanup();
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
bridge.init({ cliDiscoveryFilePath: discoveryPath });
|
|
|
|
const port = await bridge.getOrCreateHost();
|
|
assert.ok(Number.isInteger(port) && port > 0);
|
|
assert.ok(fs.existsSync(discoveryPath));
|
|
const original = readDiscovery(discoveryPath);
|
|
assert.equal(original.port, port);
|
|
assert.equal(original.pid, process.pid);
|
|
assert.ok(original.token);
|
|
|
|
// Simulate an out-of-band deletion while the host is still listening.
|
|
fs.rmSync(discoveryPath, { force: true });
|
|
assert.ok(!fs.existsSync(discoveryPath));
|
|
|
|
// The host reuse path repairs the file immediately from in-memory state.
|
|
const portAgain = await bridge.getOrCreateHost();
|
|
assert.equal(portAgain, port);
|
|
assert.ok(fs.existsSync(discoveryPath));
|
|
const repaired = readDiscovery(discoveryPath);
|
|
assert.equal(repaired.port, port);
|
|
assert.equal(repaired.token, original.token);
|
|
assert.equal(repaired.pid, process.pid);
|
|
|
|
// A stale file (wrong port/token) is rewritten too.
|
|
fs.writeFileSync(discoveryPath, JSON.stringify({ port: 1, token: "stale", pid: 999 }));
|
|
bridge.ensureCliDiscoveryFile();
|
|
const fixed = readDiscovery(discoveryPath);
|
|
assert.equal(fixed.port, port);
|
|
assert.equal(fixed.token, original.token);
|
|
});
|
|
|
|
test("ensureCliDiscoveryFile does nothing while the host is down", async (t) => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-discovery-test-"));
|
|
const discoveryPath = path.join(dir, "discovery.json");
|
|
t.after(() => {
|
|
bridge.cleanup();
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
bridge.init({ cliDiscoveryFilePath: discoveryPath });
|
|
bridge.ensureCliDiscoveryFile();
|
|
assert.ok(!fs.existsSync(discoveryPath));
|
|
|
|
const port = await bridge.getOrCreateHost();
|
|
bridge.cleanup();
|
|
|
|
// cleanup removes the file and the host is gone; ensure must not revive it.
|
|
bridge.ensureCliDiscoveryFile();
|
|
assert.ok(!fs.existsSync(discoveryPath));
|
|
|
|
// A fresh host can still be started after cleanup and rewrites discovery.
|
|
const newPort = await bridge.getOrCreateHost();
|
|
assert.ok(Number.isInteger(newPort) && newPort > 0);
|
|
// Note: newPort may legitimately equal port (OS can reuse the ephemeral port
|
|
// once the old listener closed), so don't assert they differ.
|
|
assert.equal(readDiscovery(discoveryPath).port, newPort);
|
|
});
|