|
| 1 | +import { mkdtemp, rm } from "node:fs/promises"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | +import { afterAll, beforeAll, describe, expect, test } from "vite-plus/test"; |
| 6 | +import { parseStdoutJson, runCli } from "./helpers.ts"; |
| 7 | + |
| 8 | +const fixtureRoot = join(fileURLToPath(import.meta.url), "..", "..", "fixtures", "command-pack"); |
| 9 | +let configDir: string; |
| 10 | + |
| 11 | +function env(): NodeJS.ProcessEnv { |
| 12 | + return { BAILIAN_CONFIG_DIR: configDir, DO_NOT_TRACK: "1" }; |
| 13 | +} |
| 14 | + |
| 15 | +describe("e2e: Command Pack", () => { |
| 16 | + beforeAll(async () => { |
| 17 | + configDir = await mkdtemp(join(tmpdir(), "bl-command-pack-e2e-")); |
| 18 | + }); |
| 19 | + |
| 20 | + afterAll(async () => { |
| 21 | + await rm(configDir, { recursive: true, force: true }); |
| 22 | + }); |
| 23 | + |
| 24 | + test("plugin 分组和管理命令 help 正常", async () => { |
| 25 | + const group = await runCli(["plugin"], env()); |
| 26 | + expect(group.exitCode, group.stderr).toBe(0); |
| 27 | + expect(group.stderr).toContain("plugin install"); |
| 28 | + expect(group.stderr).toContain("plugin link"); |
| 29 | + expect(group.stderr).toContain("plugin list"); |
| 30 | + expect(group.stderr).toContain("plugin remove"); |
| 31 | + |
| 32 | + const install = await runCli(["plugin", "install", "--help"], env()); |
| 33 | + expect(install.exitCode, install.stderr).toBe(0); |
| 34 | + expect(install.stderr).toContain("--package"); |
| 35 | + }); |
| 36 | + |
| 37 | + test("未 link 时插件命令不存在", async () => { |
| 38 | + const result = await runCli(["agent", "ping", "--message", "before"], env()); |
| 39 | + expect(result.exitCode).not.toBe(0); |
| 40 | + expect(result.stderr).toMatch(/Unknown command/i); |
| 41 | + }); |
| 42 | + |
| 43 | + test("link 后命令进入原生 registry/help/执行链路", async () => { |
| 44 | + const linked = await runCli( |
| 45 | + ["plugin", "link", "--path", fixtureRoot, "--output", "json"], |
| 46 | + env(), |
| 47 | + ); |
| 48 | + expect(linked.exitCode, linked.stderr).toBe(0); |
| 49 | + const linkedJson = parseStdoutJson<{ linked: { name: string; commands: string[] } }>( |
| 50 | + linked.stdout, |
| 51 | + ); |
| 52 | + expect(linkedJson.linked.name).toBe("@ali/bailian-plugin-agent"); |
| 53 | + expect(linkedJson.linked.commands).toEqual([ |
| 54 | + "agent credential", |
| 55 | + "agent credential-denied", |
| 56 | + "agent fail", |
| 57 | + "agent output", |
| 58 | + "agent ping", |
| 59 | + ]); |
| 60 | + |
| 61 | + const rootHelp = await runCli(["--help"], env()); |
| 62 | + expect(rootHelp.exitCode, rootHelp.stderr).toBe(0); |
| 63 | + expect(rootHelp.stderr).toContain("agent ping"); |
| 64 | + |
| 65 | + const commandHelp = await runCli(["agent", "ping", "--help"], env()); |
| 66 | + expect(commandHelp.exitCode, commandHelp.stderr).toBe(0); |
| 67 | + expect(commandHelp.stderr).toContain("Ping the Command Pack fixture"); |
| 68 | + expect(commandHelp.stderr).toContain("--message"); |
| 69 | + |
| 70 | + const executed = await runCli(["agent", "ping", "--message", "hello"], env()); |
| 71 | + expect(executed.exitCode, executed.stderr).toBe(0); |
| 72 | + expect(executed.stdout).toContain("command-pack:hello"); |
| 73 | + |
| 74 | + const credential = await runCli(["agent", "credential", "--api-key", "fixture-key"], env()); |
| 75 | + expect(credential.exitCode, credential.stderr).toBe(0); |
| 76 | + expect(credential.stdout).toContain("credential-source:flag"); |
| 77 | + |
| 78 | + const denied = await runCli(["agent", "credential-denied"], { |
| 79 | + ...env(), |
| 80 | + DASHSCOPE_API_KEY: "fixture-key", |
| 81 | + }); |
| 82 | + expect(denied.exitCode).toBe(1); |
| 83 | + expect(denied.stderr).toContain('must declare auth="apiKey"'); |
| 84 | + |
| 85 | + const outputText = await runCli(["agent", "output"], env()); |
| 86 | + expect(outputText.exitCode, outputText.stderr).toBe(0); |
| 87 | + expect(outputText.stdout).toBe("command-pack-output\n"); |
| 88 | + |
| 89 | + const outputJson = await runCli(["agent", "output", "--output", "json"], env()); |
| 90 | + expect(outputJson.exitCode, outputJson.stderr).toBe(0); |
| 91 | + expect(parseStdoutJson(outputJson.stdout)).toEqual({ source: "command-pack", ok: true }); |
| 92 | + |
| 93 | + const failed = await runCli(["agent", "fail", "--output", "text"], env()); |
| 94 | + expect(failed.exitCode).toBe(2); |
| 95 | + expect(failed.stderr).toContain("Command Pack fixture usage error."); |
| 96 | + expect(failed.stderr).toContain("Use agent fail only in tests."); |
| 97 | + }); |
| 98 | + |
| 99 | + test("plugin list 输出加载状态", async () => { |
| 100 | + const result = await runCli(["plugin", "list", "--output", "json"], env()); |
| 101 | + expect(result.exitCode, result.stderr).toBe(0); |
| 102 | + const json = parseStdoutJson<{ |
| 103 | + command_packs: Array<{ name: string; status: string; commands: string[] }>; |
| 104 | + }>(result.stdout); |
| 105 | + expect(json.command_packs).toEqual([ |
| 106 | + expect.objectContaining({ |
| 107 | + name: "@ali/bailian-plugin-agent", |
| 108 | + status: "loaded", |
| 109 | + commands: [ |
| 110 | + "agent credential", |
| 111 | + "agent credential-denied", |
| 112 | + "agent fail", |
| 113 | + "agent output", |
| 114 | + "agent ping", |
| 115 | + ], |
| 116 | + }), |
| 117 | + ]); |
| 118 | + }); |
| 119 | + |
| 120 | + test("remove 后命令从下一进程消失", async () => { |
| 121 | + const removed = await runCli( |
| 122 | + ["plugin", "remove", "--name", "@ali/bailian-plugin-agent", "--output", "json"], |
| 123 | + env(), |
| 124 | + ); |
| 125 | + expect(removed.exitCode, removed.stderr).toBe(0); |
| 126 | + expect(parseStdoutJson<{ removed: string }>(removed.stdout).removed).toBe( |
| 127 | + "@ali/bailian-plugin-agent", |
| 128 | + ); |
| 129 | + |
| 130 | + const result = await runCli(["agent", "ping", "--message", "after"], env()); |
| 131 | + expect(result.exitCode).not.toBe(0); |
| 132 | + expect(result.stderr).toMatch(/Unknown command/i); |
| 133 | + }); |
| 134 | +}); |
0 commit comments