// Task 265 — CC's plugin-system enumerates tools/list by spawning this
// server with empty env. Module-init must not throw `MissingPortEnvError`;
// any throw empties plugin registry #1 and breaks /agents resolution
// (see learnings_mcp_module_init_never_throws_on_per_account_env.md).
//
// This test spawns the compiled dist/index.js with env: {} and asserts:
//   1. The process accepts JSON-RPC initialize without exiting with the
//      module-init throw.
//   2. tools/list returns the tool descriptor list.
//
// The MCP stdio transport exits cleanly when stdin EOFs, so success here
// is "got the initialize + tools/list responses on stdout, never saw
// `[whatsapp:module-init] missing-port-env` on stderr."
import { describe, it, expect } from "vitest";
import { spawn } from "node:child_process";
import { resolve } from "node:path";
const SERVER_BIN = resolve(__dirname, "..", "..", "dist", "index.js");
describe("whatsapp MCP boot without env", () => {
    it("initialize + tools/list succeed with env: {}", async () => {
        const child = spawn(process.execPath, [SERVER_BIN], {
            env: {},
            stdio: ["pipe", "pipe", "pipe"],
        });
        let stdout = "";
        let stderr = "";
        child.stdout.on("data", (b) => { stdout += String(b); });
        child.stderr.on("data", (b) => { stderr += String(b); });
        child.stdin.write(JSON.stringify({
            jsonrpc: "2.0", id: 1, method: "initialize",
            params: {
                protocolVersion: "2024-11-05",
                capabilities: {},
                clientInfo: { name: "boot-test", version: "0" },
            },
        }) + "\n");
        child.stdin.write(JSON.stringify({
            jsonrpc: "2.0", method: "notifications/initialized",
        }) + "\n");
        child.stdin.write(JSON.stringify({
            jsonrpc: "2.0", id: 2, method: "tools/list",
        }) + "\n");
        // Wait for both responses, then close stdin to let the server exit.
        await new Promise((res) => setTimeout(res, 500));
        child.stdin.end();
        const exitCode = await new Promise((res) => {
            child.on("exit", (code) => res(code ?? -1));
        });
        expect(stderr).not.toMatch(/missing-port-env reason=missing-env/);
        expect(exitCode).toBe(0);
        const lines = stdout.trim().split("\n").map((l) => JSON.parse(l));
        const initResp = lines.find((m) => m.id === 1);
        expect(initResp?.result?.serverInfo?.name).toBe("maxy-whatsapp");
        const toolsResp = lines.find((m) => m.id === 2);
        expect(Array.isArray(toolsResp?.result?.tools)).toBe(true);
        expect(toolsResp.result.tools.length).toBeGreaterThan(0);
    }, 10_000);
});
//# sourceMappingURL=boot-without-env.test.js.map