Add Vitest test suite covering contracts, tools, and runtime

This commit is contained in:
morluto
2026-07-12 03:25:29 +08:00
parent c047fcbd5c
commit 567351c491
11 changed files with 831 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
import { createServer } from "node:net";
const [socketPath, token] = process.argv.slice(2);
const server = createServer((socket) => {
socket.on("error", () => undefined);
socket.setEncoding("utf8");
let buffer = "";
const send = (message, fragmented = false) => {
const line = `${JSON.stringify(message)}\n`;
if (!fragmented) return socket.write(line);
const midpoint = Math.floor(line.length / 2);
socket.write(line.slice(0, midpoint));
setTimeout(() => socket.write(line.slice(midpoint)), 2);
};
socket.on("data", (chunk) => {
buffer += chunk;
let newline = buffer.indexOf("\n");
while (newline >= 0) {
const request = JSON.parse(buffer.slice(0, newline));
buffer = buffer.slice(newline + 1);
if (request.token !== token) {
send({ id: request.id, error: { code: -32001, message: "bad token" } });
} else if (request.method === "health") {
send(
{
id: request.id,
result: { name: "betterBinaryMCP Hopper bridge", version: "1.0.0" },
},
true,
);
} else if (request.method === "shutdown") {
send({ id: request.id, result: { shutdown: true } });
setTimeout(() => server.close(), 2);
} else if (request.method === "hang") {
// Deliberately leave the request pending.
} else if (request.method === "exit") {
process.exit(7);
} else if (request.method === "malformed") {
socket.write("{not-json}\n");
} else if (request.method === "wrong_id") {
send({ id: request.id + 100, result: {} });
} else if (request.method === "remote_error") {
send({
id: request.id,
error: { code: -32001, message: "safe fake failure" },
});
} else {
const delay = request.params?.delay ?? 0;
setTimeout(
() => send({ id: request.id, result: request.params ?? {} }),
delay,
);
}
newline = buffer.indexOf("\n");
}
});
});
server.listen(socketPath);
process.on("SIGTERM", () => server.close());
+27
View File
@@ -0,0 +1,27 @@
import { spawn } from "node:child_process";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
const bootstrapIndex = process.argv.indexOf("--python") + 1;
const actionIndex = Math.max(
process.argv.indexOf("--executable"),
process.argv.indexOf("--database"),
);
if (
bootstrapIndex === 0 ||
actionIndex === -1 ||
bootstrapIndex > actionIndex
) {
process.stderr.write("Python bootstrap must precede the Hopper action\n");
process.exit(2);
}
const source = readFileSync(process.argv[bootstrapIndex], "utf8");
const [socketLine, tokenLine] = source.split("\n");
const socketPath = JSON.parse(socketLine.slice(socketLine.indexOf("=") + 1));
const token = JSON.parse(tokenLine.slice(tokenLine.indexOf("=") + 1));
const bridge = fileURLToPath(new URL("./fakeHopper.mjs", import.meta.url));
const child = spawn(process.execPath, [bridge, socketPath, token], {
stdio: "ignore",
});
child.on("exit", (code) => process.exit(code ?? 0));
process.on("SIGTERM", () => child.kill("SIGTERM"));