// Shared test helpers extracted from cli.test.ts / server.test.ts / e2e.test.ts // / snapshots.test.ts to remove copy-paste duplication. Not a test file itself // — the runner pattern `tsx --test tests/*.test.ts` (see package.json) excludes // this file by name. import assert from "node:assert/strict"; import { spawn } from "node:child_process"; import { readFileSync } from "node:fs"; import { createServer, type IncomingMessage, type ServerResponse, } from "node:http"; import { resolve } from "node:path"; import { pathToFileURL } from "node:url"; import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"; // Absolute file:// URL for tsx's loader. Spawn the CLI via // `node --import src/index.ts` to bypass the `./node_modules/.bin/tsx` // shim (a .cmd on Windows that child_process.spawn can't launch without // shell:true). Absolute so tests that override the child cwd still work. // spawnClient stays on `command: "tsx"` — StdioClientTransport handles // cross-platform spawn itself. export const TSX_LOADER_URL = pathToFileURL( resolve("./node_modules/tsx/dist/loader.mjs"), ).href; // Expected version for handshake / --version assertions. Read from package.json // so the tests track the real version and stay in lockstep with the single // source both adapters read — no literal to bump (or forget) on release. export const PKG_VERSION: string = JSON.parse( readFileSync(new URL("../package.json", import.meta.url), "utf8"), ).version; export async function startMock( handler: (req: IncomingMessage, res: ServerResponse) => void, ): Promise<{ url: string; close: () => Promise }> { const httpServer = createServer(handler); await new Promise((resolve) => httpServer.listen(0, "127.0.0.1", () => resolve()), ); const address = httpServer.address(); if (!address || typeof address !== "object") { throw new Error("mock server address unavailable"); } return { url: `http://127.0.0.1:${address.port}`, // closeAllConnections() drops keep-alive sockets so close() actually // resolves; without it the server lingers past the test boundary. close: () => new Promise((resolve, reject) => { httpServer.closeAllConnections(); httpServer.close((err) => (err ? reject(err) : resolve())); }), }; } export async function spawnClient(opts?: { name?: string; env?: Record; }): Promise { const transport = new StdioClientTransport({ command: "tsx", args: ["src/index.ts"], env: { ...process.env, ...opts?.env } as Record, }); const client = new Client({ name: opts?.name ?? "markfetch-test", version: "0.0.0", }); await client.connect(transport); return client; } export function textOf(result: { content: unknown }): string { const content = result.content as Array<{ type: string; text?: string }>; return content[0]?.text ?? ""; } // Matches any of the eight [code] error prefixes the tool emits. Used by // schema-rejection assertions to prove the handler did NOT run — a [code] // prefix would mean the call escaped Zod and reached core. export const ERROR_CODE_PREFIX_RE = /^\[(network_error|http_error|timeout|unsupported_content_type|extraction_failed|too_large|save_failed|save_forbidden)\]/; // Asserts that a tool call is rejected at the Zod schema boundary, not by the // handler. The SDK either throws (some versions) or returns isError:true with // schema-error text — both are valid rejections. What's NOT valid is a // [code]-prefixed reply, which would prove the handler ran. export async function assertSchemaRejection( client: Client, args: Record, failureMessage: string, ): Promise { let caught = false; let result: { isError?: boolean; content?: unknown } | undefined; try { result = (await client.callTool({ name: "fetch_markdown", arguments: args, })) as { isError?: boolean; content?: unknown }; } catch { caught = true; } if (!caught) { assert.equal( result?.isError, true, "schema rejection must surface as isError", ); const text = textOf(result as { content: unknown }); assert.ok( !ERROR_CODE_PREFIX_RE.test(text), `${failureMessage}: ${text}`, ); } } // One-shot subprocess spawn that returns exit code + stderr. Used by // startup-failure tests that expect a misconfigured env var to fail fast. export async function spawnAndCaptureExit( args: string[], env: Record, ): Promise<{ exitCode: number; stderr: string }> { const child = spawn( process.execPath, ["--import", TSX_LOADER_URL, ...args], { env: { ...process.env, ...env } as Record, stdio: ["pipe", "pipe", "pipe"], }, ); let stderr = ""; child.stderr.on("data", (d: Buffer) => { stderr += d.toString(); }); const exitCode = await new Promise((resolve) => child.on("exit", (code) => resolve(code ?? -1)), ); return { exitCode, stderr }; } // Deterministic Readability-friendly fixture with three

sections so // server-side tests that assert on multiple sub-headings have material; // CLI tests assert on a subset and still pass. export const HAPPY_FIXTURE = ` Test Article

Test Article Title

First substantive paragraph with enough content to pass Readability's heuristics. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. The article contains real prose for the extractor to score positively.

Section heading

Second paragraph with continuing content. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. More words to give Readability adequate signal.

Another section

Third paragraph: Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

`;