|
1 | 1 | import { describe, test, expect } from "vitest"; |
2 | 2 | import { readFile } from "fs/promises"; |
3 | 3 | import { join } from "path"; |
| 4 | +import { execFile } from "child_process"; |
| 5 | +import { promisify } from "util"; |
4 | 6 |
|
5 | 7 | const REPO_ROOT = join(import.meta.dirname, "..", "..", "..", ".."); |
| 8 | +const execFileAsync = promisify(execFile); |
6 | 9 |
|
7 | 10 | describe("plugin MCP manifests", () => { |
8 | | - // Deliberately the raw key, not `Bearer <key>` as the CLI writes. Both plugins |
9 | | - // document that an unset key still works over the anonymous tier, and this is |
10 | | - // the only form that survives both states: the server rejects `Bearer` with an |
11 | | - // empty token but treats an empty Authorization as anonymous. |
12 | | - test.each(["plugins/claude/context7/.mcp.json", "plugins/copilot/context7/.mcp.json"])( |
13 | | - "%s passes the raw key via Authorization", |
14 | | - async (relPath) => { |
15 | | - const raw = await readFile(join(REPO_ROOT, relPath), "utf-8"); |
16 | | - const config = JSON.parse(raw) as { |
17 | | - mcpServers: { context7: { headers: Record<string, string> } }; |
18 | | - }; |
19 | | - expect(config.mcpServers.context7.headers).toEqual({ |
20 | | - Authorization: "${CONTEXT7_API_KEY:-}", |
21 | | - }); |
22 | | - } |
23 | | - ); |
| 11 | + test("Claude uses an API key only when one is set", async () => { |
| 12 | + const relPath = "plugins/claude/context7/.mcp.json"; |
| 13 | + const raw = await readFile(join(REPO_ROOT, relPath), "utf-8"); |
| 14 | + const config = JSON.parse(raw) as { |
| 15 | + mcpServers: { context7: { headers?: Record<string, string>; headersHelper: string } }; |
| 16 | + }; |
| 17 | + expect(config.mcpServers.context7.headers).toBeUndefined(); |
| 18 | + expect(config.mcpServers.context7.headersHelper).toBe( |
| 19 | + 'node "${CLAUDE_PLUGIN_ROOT}/scripts/headers.mjs"' |
| 20 | + ); |
| 21 | + |
| 22 | + const helper = join(REPO_ROOT, "plugins/claude/context7/scripts/headers.mjs"); |
| 23 | + const withoutKey = await execFileAsync(process.execPath, [helper], { |
| 24 | + env: { ...process.env, CONTEXT7_API_KEY: "" }, |
| 25 | + }); |
| 26 | + expect(JSON.parse(withoutKey.stdout)).toEqual({}); |
| 27 | + |
| 28 | + const withKey = await execFileAsync(process.execPath, [helper], { |
| 29 | + env: { ...process.env, CONTEXT7_API_KEY: "ctx7sk-test" }, |
| 30 | + }); |
| 31 | + expect(JSON.parse(withKey.stdout)).toEqual({ Authorization: "ctx7sk-test" }); |
| 32 | + }); |
| 33 | + |
| 34 | + test("Copilot passes the raw API key via Authorization", async () => { |
| 35 | + const raw = await readFile(join(REPO_ROOT, "plugins/copilot/context7/.mcp.json"), "utf-8"); |
| 36 | + const config = JSON.parse(raw) as { |
| 37 | + mcpServers: { context7: { headers: Record<string, string> } }; |
| 38 | + }; |
| 39 | + expect(config.mcpServers.context7.headers).toEqual({ |
| 40 | + Authorization: "${CONTEXT7_API_KEY:-}", |
| 41 | + }); |
| 42 | + }); |
24 | 43 | }); |
0 commit comments