|
| 1 | +package cmd |
| 2 | + |
| 3 | +// cli_mcp_gaps_test.go — regression tests for the BugBash QA round-2 |
| 4 | +// CLI-MCP gaps closed in the `fix/cli-hygiene-env-passthrough` PR. |
| 5 | +// |
| 6 | +// CLI-MCP-8 — `--env` flag is parsed, forwarded in the request body, |
| 7 | +// and the resolved env is surfaced in the human output. |
| 8 | +// CLI-MCP-9 — `instant deploy` parent Short text is explicitly labeled |
| 9 | +// as a stub so the root help row carries the pointer. |
| 10 | +// CLI-MCP-11 — `instant resource <token>` and `instant resource delete |
| 11 | +// <token>` exit 3 (ExitAuthRequired) when the caller is |
| 12 | +// unauthenticated, BEFORE any side effects, matching the |
| 13 | +// contract `instant resources` (list) already honors. |
| 14 | +// |
| 15 | +// Each test pins the fix — reverting it would re-introduce a documented |
| 16 | +// QA-found gap. |
| 17 | + |
| 18 | +import ( |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | +) |
| 22 | + |
| 23 | +// ── CLI-MCP-8: --env flag plumbing ─────────────────────────────────────────── |
| 24 | + |
| 25 | +// TestCLI_MCP_8_EnvFlagForwarded asserts that `instant db new --name X --env Y` |
| 26 | +// includes "env":"Y" in the request body — the mock echoes the resolved env |
| 27 | +// back so we can assert on the wire shape via the mock's recorded resource. |
| 28 | +// |
| 29 | +// Why this matters: until CLI-MCP-8 the CLI dropped --env entirely, forcing |
| 30 | +// agents that needed a non-default environment to fall back to curl |
| 31 | +// (CLAUDE.md rule 11 — empty `env` lands in "development"). |
| 32 | +func TestCLI_MCP_8_EnvFlagForwarded(t *testing.T) { |
| 33 | + c := newITContext(t) |
| 34 | + resetProvisionFlags() |
| 35 | + |
| 36 | + stdout, _ := captureStdout(t, func() { |
| 37 | + _, _, err := run("db", "new", "--name", "env-fwd-db", "--env", "production") |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("db new --env: %v", err) |
| 40 | + } |
| 41 | + }) |
| 42 | + // Resolved env line surfaces in the human output (CLI-MCP-8 acceptance). |
| 43 | + if !strings.Contains(stdout, "env production") { |
| 44 | + t.Errorf("expected `env production` line in output, got %q", stdout) |
| 45 | + } |
| 46 | + |
| 47 | + // The mock parses and echoes body.Env back as the resource's env. List |
| 48 | + // it to confirm the value reached the server. |
| 49 | + tok := lastSavedToken(t) |
| 50 | + if tok == "" { |
| 51 | + t.Fatalf("no token persisted after provision") |
| 52 | + } |
| 53 | + t.Cleanup(func() { c.deleteResource(tok) }) |
| 54 | + c.mock.mu.Lock() |
| 55 | + defer c.mock.mu.Unlock() |
| 56 | + res, ok := c.mock.resources[tok] |
| 57 | + if !ok { |
| 58 | + t.Fatalf("mock has no record of token %s", tok) |
| 59 | + } |
| 60 | + if res.Env != "production" { |
| 61 | + t.Errorf("server received env=%q, want %q", res.Env, "production") |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// TestCLI_MCP_8_EnvFlagOmittedKeepsServerDefault asserts that omitting --env |
| 66 | +// does NOT inject an env field — the server sees an absent key and applies |
| 67 | +// its documented default (development). The mock mirrors this: empty body.Env |
| 68 | +// → resolves to "development". |
| 69 | +func TestCLI_MCP_8_EnvFlagOmittedKeepsServerDefault(t *testing.T) { |
| 70 | + c := newITContext(t) |
| 71 | + resetProvisionFlags() |
| 72 | + |
| 73 | + stdout, _ := captureStdout(t, func() { |
| 74 | + _, _, err := run("cache", "new", "--name", "env-default-cache") |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("cache new (no --env): %v", err) |
| 77 | + } |
| 78 | + }) |
| 79 | + if !strings.Contains(stdout, "env development") { |
| 80 | + t.Errorf("expected resolved env=development line, got %q", stdout) |
| 81 | + } |
| 82 | + tok := lastSavedToken(t) |
| 83 | + if tok == "" { |
| 84 | + t.Fatalf("no token persisted") |
| 85 | + } |
| 86 | + t.Cleanup(func() { c.deleteResource(tok) }) |
| 87 | +} |
| 88 | + |
| 89 | +// TestCLI_MCP_8_EnvFallbackOnLegacyAPI asserts the CLI surfaces |
| 90 | +// `env development` (not the empty string) when the server response omits |
| 91 | +// the `env` field entirely — the documented behavior against an API build |
| 92 | +// that predates migration 026. |
| 93 | +func TestCLI_MCP_8_EnvFallbackOnLegacyAPI(t *testing.T) { |
| 94 | + c := newITContext(t) |
| 95 | + resetProvisionFlags() |
| 96 | + c.mock.mu.Lock() |
| 97 | + c.mock.omitEnvInProvision = true |
| 98 | + c.mock.mu.Unlock() |
| 99 | + |
| 100 | + stdout, _ := captureStdout(t, func() { |
| 101 | + _, _, err := run("db", "new", "--name", "env-legacy-db") |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("db new (legacy API): %v", err) |
| 104 | + } |
| 105 | + }) |
| 106 | + if !strings.Contains(stdout, "env development") { |
| 107 | + t.Errorf("expected legacy fallback `env development`, got %q", stdout) |
| 108 | + } |
| 109 | + tok := lastSavedToken(t) |
| 110 | + t.Cleanup(func() { c.deleteResource(tok) }) |
| 111 | +} |
| 112 | + |
| 113 | +// TestCLI_MCP_8_EnvOverrideReasonSurfaced asserts that when the server |
| 114 | +// returns env_override_reason (e.g. anon caller asked for production and |
| 115 | +// got demoted), the CLI prints that reason on its own line. |
| 116 | +func TestCLI_MCP_8_EnvOverrideReasonSurfaced(t *testing.T) { |
| 117 | + c := newITContext(t) |
| 118 | + resetProvisionFlags() |
| 119 | + c.mock.mu.Lock() |
| 120 | + c.mock.envOverrideReason = "anonymous tier cannot target production; downgraded to development" |
| 121 | + c.mock.mu.Unlock() |
| 122 | + |
| 123 | + stdout, _ := captureStdout(t, func() { |
| 124 | + _, _, err := run("db", "new", "--name", "env-override-db", "--env", "production") |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("db new --env production: %v", err) |
| 127 | + } |
| 128 | + }) |
| 129 | + if !strings.Contains(stdout, "env_override_reason") { |
| 130 | + t.Errorf("expected env_override_reason line in output, got %q", stdout) |
| 131 | + } |
| 132 | + if !strings.Contains(stdout, "anonymous tier cannot target production") { |
| 133 | + t.Errorf("expected reason text in output, got %q", stdout) |
| 134 | + } |
| 135 | + tok := lastSavedToken(t) |
| 136 | + t.Cleanup(func() { c.deleteResource(tok) }) |
| 137 | +} |
| 138 | + |
| 139 | +// TestCLI_MCP_8_EnvFlagAllProvisioningVerbs asserts every provisioning verb |
| 140 | +// (db, cache, nosql, queue, storage, webhook, vector) accepts --env. A typo |
| 141 | +// in init() that forgot to bind --env on one group would be caught here. |
| 142 | +func TestCLI_MCP_8_EnvFlagAllProvisioningVerbs(t *testing.T) { |
| 143 | + c := newITContext(t) |
| 144 | + for _, tc := range []struct{ group, name string }{ |
| 145 | + {"db", "env-all-db"}, |
| 146 | + {"cache", "env-all-cache"}, |
| 147 | + {"nosql", "env-all-nosql"}, |
| 148 | + {"queue", "env-all-queue"}, |
| 149 | + {"storage", "env-all-storage"}, |
| 150 | + {"webhook", "env-all-webhook"}, |
| 151 | + {"vector", "env-all-vector"}, |
| 152 | + } { |
| 153 | + t.Run(tc.group, func(t *testing.T) { |
| 154 | + resetProvisionFlags() |
| 155 | + _, _ = captureStdout(t, func() { |
| 156 | + _, _, err := run(tc.group, "new", "--name", tc.name, "--env", "staging") |
| 157 | + if err != nil { |
| 158 | + t.Fatalf("%s new --env: %v", tc.group, err) |
| 159 | + } |
| 160 | + }) |
| 161 | + tok := lastSavedToken(t) |
| 162 | + if tok == "" { |
| 163 | + t.Fatalf("%s: no token persisted", tc.group) |
| 164 | + } |
| 165 | + c.mock.mu.Lock() |
| 166 | + res, ok := c.mock.resources[tok] |
| 167 | + c.mock.mu.Unlock() |
| 168 | + if !ok { |
| 169 | + t.Fatalf("%s: mock missing token", tc.group) |
| 170 | + } |
| 171 | + if res.Env != "staging" { |
| 172 | + t.Errorf("%s: server saw env=%q, want %q", tc.group, res.Env, "staging") |
| 173 | + } |
| 174 | + t.Cleanup(func() { c.deleteResource(tok) }) |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +// ── CLI-MCP-9: deploy parent help text labels as stub ──────────────────────── |
| 180 | + |
| 181 | +// TestCLI_MCP_9_DeployShortLabelsStub asserts the cobra Short for the |
| 182 | +// `instant deploy` parent contains the literal "[stub" marker AND points at |
| 183 | +// the canonical alternative path (MCP create_deploy / POST /deploy/new). |
| 184 | +// The Short string is what surfaces in `instant --help` one-liners — an |
| 185 | +// agent's first contact with this command MUST carry the pointer. |
| 186 | +func TestCLI_MCP_9_DeployShortLabelsStub(t *testing.T) { |
| 187 | + if deployCmd.Short == "" { |
| 188 | + t.Fatal("deploy command has no Short text") |
| 189 | + } |
| 190 | + if !strings.Contains(deployCmd.Short, "[stub") { |
| 191 | + t.Errorf("deploy.Short missing `[stub` marker: %q", deployCmd.Short) |
| 192 | + } |
| 193 | + if !strings.Contains(deployCmd.Short, "create_deploy") && |
| 194 | + !strings.Contains(deployCmd.Short, "/deploy/new") { |
| 195 | + t.Errorf("deploy.Short must point at MCP `create_deploy` or `POST /deploy/new`: %q", |
| 196 | + deployCmd.Short) |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +// ── CLI-MCP-11: resource detail/delete unauth → exit 3 ────────────────────── |
| 201 | + |
| 202 | +// TestCLI_MCP_11_ResourceDetail_Unauth_ExitsAuthRequired asserts that |
| 203 | +// calling `instant resource <token>` without auth exits with |
| 204 | +// ExitAuthRequired (3), BEFORE any API call. Reverting the haveAuth() |
| 205 | +// short-circuit would let an anonymous caller reach the API and either |
| 206 | +// succeed (token-as-bearer pattern) or 404 (exit 1) — neither matches the |
| 207 | +// documented contract that read commands require auth. |
| 208 | +func TestCLI_MCP_11_ResourceDetail_Unauth_ExitsAuthRequired(t *testing.T) { |
| 209 | + newITContext(t) // anonymous: no authSetupForTest call |
| 210 | + |
| 211 | + _, _, err := run("resource", "some-token") |
| 212 | + if err == nil { |
| 213 | + t.Fatal("resource <token> (unauth) must error, got nil") |
| 214 | + } |
| 215 | + if code := ExitCodeFor(err); code != ExitAuthRequired { |
| 216 | + t.Errorf("resource <token> (unauth) exit code = %d, want %d (ExitAuthRequired)", |
| 217 | + code, ExitAuthRequired) |
| 218 | + } |
| 219 | + if !strings.Contains(err.Error(), "authentication required") { |
| 220 | + t.Errorf("expected `authentication required` message, got %q", err.Error()) |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +// TestCLI_MCP_11_ResourceDelete_Unauth_ExitsAuthRequired asserts the |
| 225 | +// destructive path also exits 3 on unauth, BEFORE the --yes confirmation |
| 226 | +// prompt. An agent that ran `instant resource delete X --yes` without auth |
| 227 | +// previously had no deterministic exit code to branch on. |
| 228 | +func TestCLI_MCP_11_ResourceDelete_Unauth_ExitsAuthRequired(t *testing.T) { |
| 229 | + newITContext(t) // anonymous |
| 230 | + |
| 231 | + _, _, err := run("resource", "delete", "some-token", "--yes") |
| 232 | + if err == nil { |
| 233 | + t.Fatal("resource delete (unauth) must error, got nil") |
| 234 | + } |
| 235 | + if code := ExitCodeFor(err); code != ExitAuthRequired { |
| 236 | + t.Errorf("resource delete (unauth) exit code = %d, want %d (ExitAuthRequired)", |
| 237 | + code, ExitAuthRequired) |
| 238 | + } |
| 239 | +} |
0 commit comments