|
| 1 | +import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, test } from "vite-plus/test"; |
| 5 | +import { parseStdoutJson, runCommandE2e, runCommandHelp } from "./helpers.ts"; |
| 6 | +import { CONFIG_ROUTES, MCP_ROUTES } from "./topic-routes.ts"; |
| 7 | + |
| 8 | +const PLACEHOLDER_KEY = "sk-pi-e2e-placeholder-secret"; |
| 9 | +let probeRoot: string; |
| 10 | +let agentDir: string; |
| 11 | +let probeEnv: NodeJS.ProcessEnv; |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + probeRoot = mkdtempSync(join(tmpdir(), "bl-pi-e2e-")); |
| 15 | + agentDir = join(probeRoot, "pi-agent"); |
| 16 | + probeEnv = { PI_CODING_AGENT_DIR: agentDir, BAILIAN_CONFIG_DIR: join(probeRoot, "bailian") }; |
| 17 | +}); |
| 18 | + |
| 19 | +afterEach(() => rmSync(probeRoot, { recursive: true, force: true })); |
| 20 | + |
| 21 | +const modelArgs = [ |
| 22 | + "config", |
| 23 | + "agent", |
| 24 | + "--agent", |
| 25 | + "pi", |
| 26 | + "--region", |
| 27 | + "ap-southeast-1", |
| 28 | + "--api-key", |
| 29 | + PLACEHOLDER_KEY, |
| 30 | + "--model", |
| 31 | + "qwen3-coder-plus", |
| 32 | +]; |
| 33 | +const mcpArgs = [ |
| 34 | + "mcp", |
| 35 | + "connect", |
| 36 | + "--agent", |
| 37 | + "pi", |
| 38 | + "--server", |
| 39 | + "TextGenerateImage", |
| 40 | + "--transport", |
| 41 | + "streamable-http", |
| 42 | + "--api-key", |
| 43 | + PLACEHOLDER_KEY, |
| 44 | + "--output", |
| 45 | + "json", |
| 46 | +]; |
| 47 | + |
| 48 | +describe("e2e: Pi Agent integration", () => { |
| 49 | + test("config agent and mcp connect/disconnect help expose Pi", async () => { |
| 50 | + for (const [routes, args] of [ |
| 51 | + [CONFIG_ROUTES, ["config", "agent", "--help"]], |
| 52 | + [MCP_ROUTES, ["mcp", "connect", "--help"]], |
| 53 | + [MCP_ROUTES, ["mcp", "disconnect", "--help"]], |
| 54 | + ] as const) { |
| 55 | + const result = await runCommandHelp(routes, [...args]); |
| 56 | + expect(result.exitCode, result.stderr).toBe(0); |
| 57 | + expect(result.stderr).toMatch(/\bpi\b/); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + test("model dry-run accepts Pi, redacts its key, and leaves its directory absent", async () => { |
| 62 | + const result = await runCommandE2e( |
| 63 | + CONFIG_ROUTES, |
| 64 | + [...modelArgs, "--dry-run", "--output", "json"], |
| 65 | + probeEnv, |
| 66 | + ); |
| 67 | + expect(result.exitCode, result.stderr).toBe(0); |
| 68 | + const plan = parseStdoutJson<{ |
| 69 | + agent: string; |
| 70 | + base_url: string; |
| 71 | + model: string; |
| 72 | + api_key: string; |
| 73 | + }>(result.stdout); |
| 74 | + expect(plan.agent).toBe("pi"); |
| 75 | + expect(plan.base_url).toBe( |
| 76 | + "https://token-plan.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1", |
| 77 | + ); |
| 78 | + expect(plan.model).toBe("qwen3-coder-plus"); |
| 79 | + expect(plan.api_key).not.toBe(PLACEHOLDER_KEY); |
| 80 | + expect(result.stdout + result.stderr).not.toContain(PLACEHOLDER_KEY); |
| 81 | + expect(existsSync(agentDir)).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + test("config agent writes Pi's native settings and private auth without disclosing credentials", async () => { |
| 85 | + const result = await runCommandE2e(CONFIG_ROUTES, modelArgs, probeEnv); |
| 86 | + expect(result.exitCode, result.stderr).toBe(0); |
| 87 | + expect(result.stdout + result.stderr).not.toContain(PLACEHOLDER_KEY); |
| 88 | + const settings = JSON.parse(readFileSync(join(agentDir, "settings.json"), "utf8")); |
| 89 | + expect(settings).toMatchObject({ |
| 90 | + defaultProvider: "bailian-cli", |
| 91 | + defaultModel: "qwen3-coder-plus", |
| 92 | + }); |
| 93 | + const auth = JSON.parse(readFileSync(join(agentDir, "auth.json"), "utf8")); |
| 94 | + expect(auth["bailian-cli"]).toEqual({ type: "api_key", key: PLACEHOLDER_KEY }); |
| 95 | + expect(readFileSync(join(agentDir, "models.json"), "utf8")).not.toContain(PLACEHOLDER_KEY); |
| 96 | + }); |
| 97 | + |
| 98 | + test("MCP dry-run reports only non-secret metadata without writing configuration", async () => { |
| 99 | + const result = await runCommandE2e(MCP_ROUTES, [...mcpArgs, "--dry-run"], probeEnv); |
| 100 | + expect(result.exitCode, result.stderr).toBe(0); |
| 101 | + const plan = parseStdoutJson<{ agents: string[]; endpoint: string; header_names: string[] }>( |
| 102 | + result.stdout, |
| 103 | + ); |
| 104 | + expect(plan.agents).toEqual(["pi"]); |
| 105 | + expect(plan.endpoint).toBe("https://dashscope.aliyuncs.com/api/v1/mcps/TextGenerateImage/mcp"); |
| 106 | + expect(plan.header_names).toContain("Authorization"); |
| 107 | + expect(result.stdout + result.stderr).not.toContain(PLACEHOLDER_KEY); |
| 108 | + expect(existsSync(agentDir)).toBe(false); |
| 109 | + }); |
| 110 | + |
| 111 | + test("MCP connect is idempotent and disconnect removes the managed entry without disclosing credentials", async () => { |
| 112 | + const first = await runCommandE2e(MCP_ROUTES, mcpArgs, probeEnv); |
| 113 | + expect(first.exitCode, first.stderr).toBe(0); |
| 114 | + expect(first.stdout + first.stderr).not.toContain(PLACEHOLDER_KEY); |
| 115 | + expect(parseStdoutJson<{ results: { status: string }[] }>(first.stdout).results[0].status).toBe( |
| 116 | + "added", |
| 117 | + ); |
| 118 | + const repeated = await runCommandE2e(MCP_ROUTES, mcpArgs, probeEnv); |
| 119 | + expect(repeated.exitCode, repeated.stderr).toBe(0); |
| 120 | + expect( |
| 121 | + parseStdoutJson<{ results: { status: string }[] }>(repeated.stdout).results[0].status, |
| 122 | + ).toBe("unchanged"); |
| 123 | + const disconnected = await runCommandE2e( |
| 124 | + MCP_ROUTES, |
| 125 | + ["mcp", "disconnect", "--agent", "pi", "--server", "TextGenerateImage", "--output", "json"], |
| 126 | + probeEnv, |
| 127 | + ); |
| 128 | + expect(disconnected.exitCode, disconnected.stderr).toBe(0); |
| 129 | + expect( |
| 130 | + parseStdoutJson<{ results: { status: string }[] }>(disconnected.stdout).results[0].status, |
| 131 | + ).toBe("removed"); |
| 132 | + expect(JSON.parse(readFileSync(join(agentDir, "mcp.json"), "utf8")).mcpServers).toEqual({}); |
| 133 | + }); |
| 134 | + |
| 135 | + test("MCP SSE uses the shared adapter rejection before any Pi file is created", async () => { |
| 136 | + const sseArgs = mcpArgs.map((argument) => (argument === "streamable-http" ? "sse" : argument)); |
| 137 | + const result = await runCommandE2e(MCP_ROUTES, sseArgs, probeEnv); |
| 138 | + expect(result.exitCode).toBe(2); |
| 139 | + expect(result.stderr).toMatch(/pi.*SSE/i); |
| 140 | + expect(result.stdout + result.stderr).not.toContain(PLACEHOLDER_KEY); |
| 141 | + expect(existsSync(agentDir)).toBe(false); |
| 142 | + }); |
| 143 | +}); |
0 commit comments