|
| 1 | +import { captureRegistryHelp } from "e2e/registry-smoke"; |
| 2 | +import { buildIsolatedCliEnv } from "e2e/isolation"; |
| 3 | +import { runInProcess } from "e2e/in-process"; |
| 4 | +import { defineCommand, buildCommandInvocation, maskToken } from "bailian-cli-core"; |
| 5 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 6 | +import { tmpdir } from "node:os"; |
| 7 | +import { join } from "node:path"; |
| 8 | +import { afterAll, expect, expectTypeOf, test } from "vite-plus/test"; |
| 9 | +import { runCli } from "./helpers.ts"; |
| 10 | +import { |
| 11 | + buildCommandSchema, |
| 12 | + CommandRegistry, |
| 13 | + createCli, |
| 14 | + type CliOptions, |
| 15 | +} from "bailian-cli-runtime"; |
| 16 | +import { commands } from "../../src/commands.ts"; |
| 17 | +import { resolveAliyunIntegration } from "../../src/integrations/aliyun.ts"; |
| 18 | + |
| 19 | +const configDirectory = mkdtempSync(join(tmpdir(), "bl-aliyun-compat-")); |
| 20 | +afterAll(() => rmSync(configDirectory, { recursive: true, force: true })); |
| 21 | + |
| 22 | +test("host adapter propagates executable argv through the runtime context", async () => { |
| 23 | + expectTypeOf<CliOptions>().not.toHaveProperty("commandPrefix"); |
| 24 | + const isolated = buildIsolatedCliEnv({ |
| 25 | + overrides: { NO_UPDATE_NOTIFIER: "1", DO_NOT_TRACK: "1" }, |
| 26 | + }); |
| 27 | + const arguments_ = ["probe", "--output", "json"]; |
| 28 | + const probe = defineCommand({ |
| 29 | + auth: "none", |
| 30 | + description: "fixture", |
| 31 | + async run(context) { |
| 32 | + context.output.result({ |
| 33 | + product: context.identity.binName, |
| 34 | + command: buildCommandInvocation( |
| 35 | + context.identity, |
| 36 | + ["knowledge", "delete"], |
| 37 | + ["--index-id", "example"], |
| 38 | + ), |
| 39 | + }); |
| 40 | + }, |
| 41 | + }); |
| 42 | + try { |
| 43 | + const result = await runInProcess( |
| 44 | + () => |
| 45 | + createCli( |
| 46 | + { probe }, |
| 47 | + { |
| 48 | + binName: "bl", |
| 49 | + clientName: "test", |
| 50 | + npmPackage: "test", |
| 51 | + version: "test", |
| 52 | + ...resolveAliyunIntegration({ ALIBABA_CLOUD_BL_COMPAT_MODE: "aliyun bl" }), |
| 53 | + }, |
| 54 | + ).run(arguments_), |
| 55 | + arguments_, |
| 56 | + { env: isolated.env }, |
| 57 | + ); |
| 58 | + expect(result.exitCode, result.stderr).toBe(0); |
| 59 | + expect(JSON.parse(result.stdout)).toEqual({ |
| 60 | + product: "bl", |
| 61 | + command: { |
| 62 | + bin: "aliyun", |
| 63 | + path: ["bl", "knowledge", "delete"], |
| 64 | + args: ["--index-id", "example"], |
| 65 | + }, |
| 66 | + }); |
| 67 | + } finally { |
| 68 | + isolated.dispose(); |
| 69 | + } |
| 70 | +}); |
| 71 | + |
| 72 | +test("all command metadata preserves standalone wording and resolves hosted references", () => { |
| 73 | + const identity = { |
| 74 | + binName: "bl", |
| 75 | + version: "test", |
| 76 | + clientName: "test", |
| 77 | + npmPackage: "bailian-cli", |
| 78 | + }; |
| 79 | + const standalone = buildCommandSchema(new CommandRegistry(commands, "bl"), identity); |
| 80 | + const hosted = buildCommandSchema(new CommandRegistry(commands, "aliyun bl"), { |
| 81 | + ...identity, |
| 82 | + commandInvocation: { bin: "aliyun", path: ["bl"] }, |
| 83 | + }); |
| 84 | + const serialized = JSON.stringify(hosted); |
| 85 | + expect(serialized).not.toContain("{{commandPrefix}}"); |
| 86 | + expect(hosted.bin).toBe("aliyun"); |
| 87 | + expect(hosted.scope).toEqual(["bl"]); |
| 88 | + const qualifiedStandalone = { |
| 89 | + ...standalone, |
| 90 | + bin: "aliyun", |
| 91 | + scope: ["bl"], |
| 92 | + commands: standalone.commands.map((command) => ({ ...command, path: ["bl", ...command.path] })), |
| 93 | + }; |
| 94 | + expect(JSON.parse(serialized.replaceAll("aliyun bl", "bl"))).toEqual(qualifiedStandalone); |
| 95 | + const skill = hosted.commands.find((command) => command.path.join(" ") === "bl skill init")!; |
| 96 | + expect(JSON.stringify(skill.notes)).toContain("Equivalent to: `aliyun bl skill add --all`"); |
| 97 | +}); |
| 98 | + |
| 99 | +// Real subprocesses below verify the product entrypoint reads the inherited host marker. |
| 100 | +function runCompatible(arguments_: string[], language = "en-US", compatibility = "aliyun bl") { |
| 101 | + writeFileSync(join(configDirectory, "config.json"), JSON.stringify({ language })); |
| 102 | + return runCli(arguments_, { |
| 103 | + BAILIAN_CONFIG_DIR: configDirectory, |
| 104 | + ALIBABA_CLOUD_BL_COMPAT_MODE: compatibility, |
| 105 | + DASHSCOPE_API_KEY: "", |
| 106 | + BAILIAN_ACCESS_KEY_ID: "", |
| 107 | + BAILIAN_ACCESS_KEY_SECRET: "", |
| 108 | + BAILIAN_SECURITY_TOKEN: "", |
| 109 | + ALIBABA_CLOUD_ACCESS_KEY_ID: "", |
| 110 | + ALIBABA_CLOUD_ACCESS_KEY_SECRET: "", |
| 111 | + ALIBABA_CLOUD_SECURITY_TOKEN: "", |
| 112 | + NO_UPDATE_NOTIFIER: "1", |
| 113 | + DO_NOT_TRACK: "1", |
| 114 | + }); |
| 115 | +} |
| 116 | + |
| 117 | +test.each(["en-US", "zh-CN"])( |
| 118 | + "host help and onboarding use the invocation prefix (%s)", |
| 119 | + async (language) => { |
| 120 | + const root = await runCompatible(["--help"], language); |
| 121 | + expect(root.exitCode, root.stderr).toBe(0); |
| 122 | + expect(root.stderr).toContain("aliyun bl <resource> <command> [flags]"); |
| 123 | + expect(root.stderr).toContain("aliyun bl auth login --console"); |
| 124 | + }, |
| 125 | +); |
| 126 | + |
| 127 | +test.each( |
| 128 | + ["en-US", "zh-CN"].flatMap((language) => |
| 129 | + ["app", "app list", "skill init", "managed-agent plan"].map((path) => ({ language, path })), |
| 130 | + ), |
| 131 | +)("host nested help uses the invocation prefix ($language, $path)", ({ language, path }) => { |
| 132 | + const registry = new CommandRegistry(commands, "aliyun bl", { |
| 133 | + language: language as "en-US" | "zh-CN", |
| 134 | + localize: (value) => (typeof value === "string" ? value : value[language as "en-US" | "zh-CN"]), |
| 135 | + }); |
| 136 | + const help = captureRegistryHelp(registry, path.split(" ")); |
| 137 | + expect(help).toContain(`aliyun bl ${path}`); |
| 138 | + expect(help).not.toMatch(/(?<!aliyun )\bbl (?:auth|skill|managed-agent) /); |
| 139 | +}); |
| 140 | + |
| 141 | +// Real entrypoint verifies inherited Bailian env names without making authentication requests. |
| 142 | +test("auth status reads Bailian env before config and host env without adding status fields", async () => { |
| 143 | + writeFileSync( |
| 144 | + join(configDirectory, "config.json"), |
| 145 | + JSON.stringify({ |
| 146 | + language: "en-US", |
| 147 | + access_key_id: "config-ak", |
| 148 | + access_key_secret: "config-sk", |
| 149 | + }), |
| 150 | + ); |
| 151 | + const result = await runCli(["auth", "status", "--output", "json"], { |
| 152 | + BAILIAN_CONFIG_DIR: configDirectory, |
| 153 | + ALIBABA_CLOUD_BL_COMPAT_MODE: "aliyun bl", |
| 154 | + BAILIAN_ACCESS_KEY_ID: "bailian-env-access-key", |
| 155 | + BAILIAN_ACCESS_KEY_SECRET: "bailian-env-secret-key", |
| 156 | + BAILIAN_SECURITY_TOKEN: "bailian-env-sts", |
| 157 | + ALIBABA_CLOUD_ACCESS_KEY_ID: "host-ak", |
| 158 | + ALIBABA_CLOUD_ACCESS_KEY_SECRET: "host-sk", |
| 159 | + ALIBABA_CLOUD_SECURITY_TOKEN: "host-sts", |
| 160 | + DASHSCOPE_API_KEY: "", |
| 161 | + NO_UPDATE_NOTIFIER: "1", |
| 162 | + DO_NOT_TRACK: "1", |
| 163 | + }); |
| 164 | + expect(result.exitCode, result.stderr).toBe(0); |
| 165 | + const state = JSON.parse(result.stdout); |
| 166 | + expect(state.openapi).toEqual({ |
| 167 | + source: "env", |
| 168 | + access_key_id: maskToken("bailian-env-access-key"), |
| 169 | + access_key_secret: maskToken("bailian-env-secret-key"), |
| 170 | + }); |
| 171 | + expect(state.console).toBeUndefined(); |
| 172 | + expect(result.stdout + result.stderr).not.toContain("bailian-env-secret-key"); |
| 173 | + expect(result.stdout + result.stderr).not.toContain("bailian-env-sts"); |
| 174 | +}); |
| 175 | + |
| 176 | +test("host introspection exports executable paths and examples from the real entrypoint", async () => { |
| 177 | + const result = await runCompatible(["auth", "status", "--introspect"]); |
| 178 | + expect(result.exitCode, result.stderr).toBe(0); |
| 179 | + const schema = JSON.parse(result.stdout); |
| 180 | + expect(schema.bin).toBe("aliyun"); |
| 181 | + expect(schema.scope).toEqual(["bl", "auth", "status"]); |
| 182 | + expect(schema.commands[0].path).toEqual(["bl", "auth", "status"]); |
| 183 | + expect(schema.commands[0].usage).toMatch(/^aliyun bl auth status/); |
| 184 | + expect(schema.commands[0].examples[0]).toBe("aliyun bl auth status"); |
| 185 | +}); |
| 186 | + |
| 187 | +test("host version is display-only and absent or unknown COMPAT keeps standalone", async () => { |
| 188 | + const hosted = await runCompatible(["--version"]); |
| 189 | + expect(hosted.stdout).toMatch(/^aliyun bl \d+\.\d+\.\d+\n$/); |
| 190 | + for (const compatibility of [""]) { |
| 191 | + const standalone = await runCompatible(["--version"], "en-US", compatibility); |
| 192 | + expect(standalone.stdout).toMatch(/^bl \d+\.\d+\.\d+\n$/); |
| 193 | + } |
| 194 | +}); |
| 195 | + |
| 196 | +test.each(["text", "json"])( |
| 197 | + "missing Console credentials use host instructions (%s)", |
| 198 | + async (output) => { |
| 199 | + const result = await runCompatible(["app", "list", "--output", output]); |
| 200 | + expect(result.exitCode, result.stderr).toBe(3); |
| 201 | + const diagnostic = output === "json" ? JSON.parse(result.stderr).error.hint : result.stderr; |
| 202 | + expect(diagnostic).toContain("aliyun bl auth login --console"); |
| 203 | + expect(diagnostic).not.toMatch(/(?<!aliyun )\bbl auth login/); |
| 204 | + }, |
| 205 | +); |
| 206 | + |
| 207 | +test("auth status operation hints use the host prefix without changing authentication", async () => { |
| 208 | + const result = await runCompatible(["auth", "status", "--output", "json"]); |
| 209 | + expect(result.exitCode, result.stderr).toBe(0); |
| 210 | + const status = JSON.parse(result.stdout); |
| 211 | + expect(status.authenticated).toBe(false); |
| 212 | + expect(status.hint).toContain("aliyun bl auth login --api-key"); |
| 213 | +}); |
| 214 | + |
| 215 | +// Marker decoding is pure; unknown/whitespace cases need no additional CLI processes. |
| 216 | +test.each(["", " ", "unrecognized-host"])( |
| 217 | + "unknown host marker keeps the default invocation (%j)", |
| 218 | + (marker) => { |
| 219 | + expect(resolveAliyunIntegration({ ALIBABA_CLOUD_BL_COMPAT_MODE: marker })).toEqual({}); |
| 220 | + }, |
| 221 | +); |
| 222 | +test("recognized host marker permits surrounding whitespace", () => { |
| 223 | + expect(resolveAliyunIntegration({ ALIBABA_CLOUD_BL_COMPAT_MODE: " aliyun bl " })).toEqual({ |
| 224 | + commandInvocation: { bin: "aliyun", path: ["bl"] }, |
| 225 | + }); |
| 226 | +}); |
0 commit comments