|
| 1 | +/** |
| 2 | + * Packaged **CLI** integration tests: the `bin` field and `npx` behavior for a |
| 3 | + * tarball shaped like a publish (not the programmatic `exports` entry; see |
| 4 | + * `package-published-esm.integration.test.ts` for `import "@currents/mcp"`). |
| 5 | + * |
| 6 | +
|
| 7 | + * 1. Prerequisite: `build/index.js` exists (`npm run test:run` runs `build` |
| 8 | + * first). If missing, the suite is skipped so `vitest` without a prior |
| 9 | + * build does not fail noisily. |
| 10 | + * 2. `packTarball`: `npm pack` from the package root → one `.tgz` under a |
| 11 | + * temp dir. Contents follow `package.json` `files` and npm’s pack rules |
| 12 | + * (same artifact shape as registry install, minus release-only publish.cjs |
| 13 | + * mutations). |
| 14 | + */ |
| 15 | +import { execFileSync, spawnSync } from "node:child_process"; |
| 16 | +import { existsSync, mkdtempSync, readdirSync } from "node:fs"; |
| 17 | +import { tmpdir } from "node:os"; |
| 18 | +import path from "node:path"; |
| 19 | +import { fileURLToPath } from "node:url"; |
| 20 | +import { describe, expect, it } from "vitest"; |
| 21 | + |
| 22 | +const root = fileURLToPath(new URL("..", import.meta.url)); |
| 23 | +const buildIndex = path.join(root, "build", "index.js"); |
| 24 | + |
| 25 | +function packTarball(packDest: string): string { |
| 26 | + // Respect `files` and standard pack rules; do not mutate package.json (unlike release `publish.cjs`). |
| 27 | + execFileSync("npm", ["pack", "--pack-destination", packDest], { |
| 28 | + cwd: root, |
| 29 | + stdio: ["ignore", "pipe", "pipe"], |
| 30 | + }); |
| 31 | + const tgz = readdirSync(packDest).filter((f) => f.endsWith(".tgz")); |
| 32 | + if (tgz.length !== 1) { |
| 33 | + throw new Error(`expected one .tgz in ${packDest}, got: ${tgz.join(", ")}`); |
| 34 | + } |
| 35 | + return path.join(packDest, tgz[0]); |
| 36 | +} |
| 37 | + |
| 38 | +describe.skipIf(!existsSync(buildIndex))( |
| 39 | + "packaged CLI (npx / bin)", |
| 40 | + { timeout: 60_000 }, |
| 41 | + () => { |
| 42 | + /** |
| 43 | + * `npx -y --package <abs-path-to.tgz> mcp`: npm treats the tarball as the |
| 44 | + * package to install transiently; `mcp` is the bin name from that package’s |
| 45 | + * `package.json` `bin` map (not the scoped package name). The child should |
| 46 | + * start the MCP server and log the “live” line (stdio MCP servers run until |
| 47 | + * stdin closes; we cap wall time with `spawnSync` timeout). Accept either |
| 48 | + * that log line or process timeout as success so slow CI still passes. |
| 49 | + * */ |
| 50 | + it("starts via npx --package tarball mcp", () => { |
| 51 | + const packDir = mkdtempSync(path.join(tmpdir(), "mcp-pack-")); |
| 52 | + const tarball = packTarball(packDir); |
| 53 | + const r = spawnSync("npx", ["-y", "--package", tarball, "mcp"], { |
| 54 | + cwd: packDir, |
| 55 | + timeout: 45_000, |
| 56 | + encoding: "utf-8", |
| 57 | + stdio: ["pipe", "pipe", "pipe"], |
| 58 | + env: { |
| 59 | + ...process.env, |
| 60 | + // Required for server startup; value is unused in this smoke test. |
| 61 | + CURRENTS_API_KEY: "vitest-cli-pack-smoke", |
| 62 | + }, |
| 63 | + }); |
| 64 | + const combined = `${r.stdout ?? ""}${r.stderr ?? ""}`; |
| 65 | + const timedOut = |
| 66 | + r.error != null && "code" in r.error && r.error.code === "ETIMEDOUT"; |
| 67 | + expect(combined.includes("Currents MCP Server is live") || timedOut).toBe( |
| 68 | + true |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + /* |
| 73 | + * Second `it` — consumer project + `node_modules/.bin`: |
| 74 | + * - `npm init -y` and `npm install <tgz>` in a fresh temp project. npm links |
| 75 | + * `node_modules/.bin/mcp` (or `mcp.cmd` on Windows) to the packed CLI. |
| 76 | + * - Assert the shim exists. This catches broken `bin`, wrong `files` (missing |
| 77 | + * `build/index.js`), or install layout issues without spawning the server. |
| 78 | + * */ |
| 79 | + it("exposes mcp bin after npm install from tarball", () => { |
| 80 | + const packDir = mkdtempSync(path.join(tmpdir(), "mcp-pack-")); |
| 81 | + const installDir = mkdtempSync(path.join(tmpdir(), "mcp-install-")); |
| 82 | + const tarball = packTarball(packDir); |
| 83 | + execFileSync("npm", ["init", "-y"], { |
| 84 | + cwd: installDir, |
| 85 | + stdio: "ignore", |
| 86 | + }); |
| 87 | + execFileSync("npm", ["install", tarball], { |
| 88 | + cwd: installDir, |
| 89 | + stdio: "ignore", |
| 90 | + }); |
| 91 | + const binDir = path.join(installDir, "node_modules", ".bin"); |
| 92 | + const hasMcp = |
| 93 | + existsSync(path.join(binDir, "mcp")) || |
| 94 | + existsSync(path.join(binDir, "mcp.cmd")); |
| 95 | + expect(hasMcp).toBe(true); |
| 96 | + }); |
| 97 | + } |
| 98 | +); |
0 commit comments