|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { access, mkdir, mkdtemp, rm } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 6 | +import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"; |
| 7 | + |
| 8 | +const repoRoot = process.cwd(); |
| 9 | +const timeoutMs = Number.parseInt(process.env.PROMPT_REFINER_STDIO_SMOKE_TIMEOUT_MS || "30000", 10); |
| 10 | +const entry = process.env.PROMPT_REFINER_STDIO_ENTRY || join(repoRoot, "dist", "src", "index.js"); |
| 11 | +const tempRoot = await mkdtemp(join(tmpdir(), "prompt-refiner-stdio-smoke-")); |
| 12 | +const stateDir = join(tempRoot, "state"); |
| 13 | +const homeDir = join(tempRoot, "home"); |
| 14 | + |
| 15 | +let client; |
| 16 | +let transport; |
| 17 | +let stderr = ""; |
| 18 | + |
| 19 | +try { |
| 20 | + await access(entry); |
| 21 | + await mkdir(stateDir, { recursive: true }); |
| 22 | + await mkdir(homeDir, { recursive: true }); |
| 23 | + |
| 24 | + transport = new StdioClientTransport({ |
| 25 | + command: process.execPath, |
| 26 | + args: [entry], |
| 27 | + cwd: repoRoot, |
| 28 | + env: { |
| 29 | + ...process.env, |
| 30 | + AZURE_CONFIG_DIR: join(homeDir, ".azure"), |
| 31 | + HOME: homeDir, |
| 32 | + PORT: "0", |
| 33 | + PROMPT_REFINER_BACKGROUND: "false", |
| 34 | + PROMPT_REFINER_GLOBAL_DIR: stateDir, |
| 35 | + USERPROFILE: homeDir, |
| 36 | + }, |
| 37 | + stderr: "pipe", |
| 38 | + }); |
| 39 | + transport.stderr?.setEncoding("utf8"); |
| 40 | + transport.stderr?.on("data", chunk => { |
| 41 | + stderr += chunk; |
| 42 | + }); |
| 43 | + |
| 44 | + client = new Client({ name: "prompt-refiner-stdio-smoke", version: "1.0.0" }); |
| 45 | + await withTimeout(client.connect(transport), "initialize MCP stdio server"); |
| 46 | + |
| 47 | + const listed = await withTimeout(client.listTools(), "list MCP tools"); |
| 48 | + const toolNames = listed.tools.map(tool => tool.name); |
| 49 | + assert.ok(toolNames.includes("lint_prompt"), `lint_prompt not advertised. Tools: ${toolNames.join(", ")}`); |
| 50 | + |
| 51 | + const result = await withTimeout(client.callTool({ |
| 52 | + name: "lint_prompt", |
| 53 | + arguments: { |
| 54 | + prompt: "Implement a focused change and run the relevant tests.", |
| 55 | + semantic: false, |
| 56 | + }, |
| 57 | + }), "call lint_prompt over stdio"); |
| 58 | + |
| 59 | + assert.equal(result.isError, undefined); |
| 60 | + assert.equal(result.content?.[0]?.type, "text"); |
| 61 | + const payload = JSON.parse(result.content[0].text); |
| 62 | + assert.match(payload.promptId, /^prm_[0-9a-f-]{36}$/u); |
| 63 | + assert.ok(Array.isArray(payload.gaps), "lint_prompt gaps must be an array"); |
| 64 | + assert.equal(typeof payload.context, "object"); |
| 65 | + |
| 66 | + console.log(`Stdio MCP smoke passed: ${toolNames.length} tools advertised and lint_prompt returned ${payload.gaps.length} gap(s).`); |
| 67 | +} catch (error) { |
| 68 | + const details = stderr.trim() ? `\nserver stderr:\n${stderr.trim()}` : ""; |
| 69 | + throw new Error(`${error instanceof Error ? error.message : String(error)}${details}`); |
| 70 | +} finally { |
| 71 | + await client?.close().catch(() => undefined); |
| 72 | + await transport?.close().catch(() => undefined); |
| 73 | + await rm(tempRoot, { recursive: true, force: true }); |
| 74 | +} |
| 75 | + |
| 76 | +async function withTimeout(promise, action) { |
| 77 | + let timer; |
| 78 | + try { |
| 79 | + return await Promise.race([ |
| 80 | + promise, |
| 81 | + new Promise((_, reject) => { |
| 82 | + timer = setTimeout(() => reject(new Error(`${action} timed out after ${timeoutMs}ms`)), timeoutMs); |
| 83 | + }), |
| 84 | + ]); |
| 85 | + } finally { |
| 86 | + clearTimeout(timer); |
| 87 | + } |
| 88 | +} |
0 commit comments