diff --git a/CHANGELOG.md b/CHANGELOG.md index 6780d89..1bcaa3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ All notable changes to cloudpin are listed here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow [Semantic Versioning](https://semver.org/). -Versions 0.1.0 to 0.4.2 (2026-10-08 to 2026-10-10) are in [docs/changelog-0.x.md](docs/changelog-0.x.md). +Versions 1.0.0 to 1.1.2 are in [docs/changelog-1.0-1.1.md](docs/changelog-1.0-1.1.md), and 0.1.0 to 0.4.2 in +[docs/changelog-0.x.md](docs/changelog-0.x.md). ## [Unreleased] @@ -20,10 +21,10 @@ Versions 0.1.0 to 0.4.2 (2026-10-08 to 2026-10-10) are in [docs/changelog-0.x.md - winget manifests for `paureis.cloudpin` (#96) in `packaging/winget/`: a portable zip installing `cloudpin.exe` as the `cloudpin` command. `node scripts/bump-installers.mjs X.Y.Z --winget
0 ? parts.join("; ") : undefined;
}
-function planJson(step: Step): PlanJson {
+export function planJson(step: Step): PlanJson {
const reason = reasonOf(step);
const candidates = step.candidates?.map((c) => c.name) ?? step.listed;
return {
@@ -145,6 +145,19 @@ function describe(c: Candidate): string {
return parts.length > 0 ? `: ${parts.join(", then ")}` : "";
}
+/** The object `--json` prints; with no `ran` and no `after`, the plan alone (and cloudpin_switch_plan's answer). */
+export function switchJson(report: CheckReport, plan: PlanJson[], ran: RanJson[], after: CheckReport | null): SwitchJson {
+ return {
+ result: (after ?? report).result,
+ path: report.path,
+ environment: report.environment,
+ ...(report.error !== undefined ? { error: report.error } : {}),
+ plan,
+ ran,
+ after,
+ };
+}
+
/**
* What `cloudpin switch` would do, running no switch command and writing nothing: the check it starts from and one
* step per provider, using only the CLIs' read-only listings. On Windows the environment comes back with its names
@@ -194,16 +207,7 @@ export async function runSwitch(cwd: string, typed: NodeJS.ProcessEnv, deps: Gua
const only = opts.only ? { only: opts.only } : {};
const { report, steps, env } = await planSwitch(cwd, typed, deps, only);
const emit = (plan: PlanJson[], ran: RanJson[], after: CheckReport | null) => {
- const json: SwitchJson = {
- result: (after ?? report).result,
- path: report.path,
- environment: report.environment,
- ...(report.error !== undefined ? { error: report.error } : {}),
- plan,
- ran,
- after,
- };
- io.out(JSON.stringify(json, null, 2));
+ io.out(JSON.stringify(switchJson(report, plan, ran, after), null, 2));
};
if (report.error !== undefined || report.path === null) {
diff --git a/test/mcp-switch-plan.test.ts b/test/mcp-switch-plan.test.ts
new file mode 100644
index 0000000..e5cc568
--- /dev/null
+++ b/test/mcp-switch-plan.test.ts
@@ -0,0 +1,379 @@
+import { readdirSync, statSync, writeFileSync } from "node:fs";
+import { join } from "node:path";
+import { describe, expect, it } from "vitest";
+import { findConfig } from "../src/config.js";
+import type { GuardDeps } from "../src/guard.js";
+import { CLOUDPIN_INSTRUCTIONS, cloudpinHandler, cloudpinTools, type ToolContext } from "../src/mcp-tools.js";
+import { providers } from "../src/providers/index.js";
+import { runSwitch, type SwitchIO } from "../src/switch.js";
+import type { Exec, ProviderDef } from "../src/types.js";
+import { fakeDoctorHost } from "./fake-doctor-host.js";
+import { tempDir } from "./tmp.js";
+
+// #112: the plan `cloudpin switch --json` prints without --yes, as an MCP tool. Every ID and name here is made up.
+
+const SUB = "11111111-1111-4111-8111-111111111111";
+const OTHER_SUB = "22222222-2222-4222-8222-222222222222";
+const TENANT = "33333333-3333-4333-8333-333333333333";
+const OTHER_TENANT = "44444444-4444-4444-8444-444444444444";
+const SERVER = "https://kube.example.test";
+/** What a credential variable holds in these tests; it must never reach an answer. */
+const FAKE_VALUE = "example-value-of-a-variable";
+
+/** Every command `cloudpin switch` may run. The fake CLIs answer them, so a tool that ran one is seen. */
+const SWITCHES = /auth switch|teams switch|config set|account set|use-context|set-context/;
+/** The identity and listing reads a plan may use. */
+const READS = /^(gh api user|gh auth status|az account (show|list)|aws sts get-caller-identity|gcloud config list|gcloud auth list)\b/;
+
+interface Logins {
+ ghUser?: string;
+ ghAccounts?: unknown;
+ azSub?: string;
+ azTenant?: string;
+ azList?: { id: string; tenantId: string }[];
+ gcloudLogins?: string[];
+}
+
+/** Fake CLIs that record every call. */
+function machine(logins: Logins = {}) {
+ const calls: string[] = [];
+ const exec: Exec = async (bin, args) => {
+ const line = [bin, ...args].join(" ");
+ calls.push(line);
+ const ok = (value: unknown) => ({ code: 0, stdout: typeof value === "string" ? value : JSON.stringify(value), stderr: "" });
+ if (SWITCHES.test(line)) return ok("");
+ if (line.startsWith("gh api user")) return ok(`${logins.ghUser ?? "someone-else"}\n`);
+ if (line.startsWith("gh auth status")) return ok(logins.ghAccounts ?? []);
+ if (line.startsWith("az account show")) return ok({ id: logins.azSub ?? OTHER_SUB, tenantId: logins.azTenant ?? TENANT, name: "Other Subscription" });
+ if (line.startsWith("az account list")) return ok(logins.azList ?? []);
+ if (line.startsWith("gcloud config list")) return ok({ core: { account: "other@example.test", project: "other-project" } });
+ if (line.startsWith("gcloud auth list")) return ok((logins.gcloudLogins ?? []).map((account) => ({ account })));
+ if (line.startsWith("aws sts")) return ok({ Account: "999999999999", Arn: "arn:aws:iam::999999999999:user/x" });
+ throw new Error(`unscripted: ${line}`);
+ };
+ return { calls, exec };
+}
+
+interface Line {
+ provider: string;
+ status: string;
+ commands?: string[][];
+ candidates?: string[];
+ reason?: string;
+}
+interface Answer {
+ note: string;
+ result: string;
+ path: string | null;
+ environment: unknown;
+ error?: string;
+ plan: Line[];
+ ran: unknown[];
+ after: unknown;
+}
+
+/** A real folder with this pin file, the real providers and config reader, and fake CLIs. */
+function project(pinFile: string | null, logins: Logins = {}, extraEnv: NodeJS.ProcessEnv = {}, platform: NodeJS.Platform = "linux", list = providers) {
+ const dir = tempDir("cloudpin-mcp-plan-");
+ if (pinFile !== null) writeFileSync(join(dir, ".cloudpin.yml"), pinFile);
+ const env: NodeJS.ProcessEnv = { HOME: dir, USERPROFILE: dir, ...extraEnv };
+ const m = machine(logins);
+ const deps: GuardDeps = { providers: list, exec: m.exec, platform, findConfig };
+ const ctx: ToolContext = {
+ cwd: dir,
+ env,
+ read: () => null,
+ isDir: (p) => statSync(p, { throwIfNoEntry: false })?.isDirectory() ?? false,
+ doctor: fakeDoctorHost,
+ };
+ const tool = cloudpinTools(deps, ctx).find((t) => t.name === "cloudpin_switch_plan");
+ const call = async (args: Record