Skip to content

test(cli): make CLI tests hermetic against inherited AMPERSEND_* env vars - #137

Open
pcarranzav wants to merge 1 commit into
mainfrom
pcv/hermetic-cli-tests
Open

test(cli): make CLI tests hermetic against inherited AMPERSEND_* env vars#137
pcarranzav wants to merge 1 commit into
mainfrom
pcv/hermetic-cli-tests

Conversation

@pcarranzav

Copy link
Copy Markdown
Member

Problem

The CLI honors several env vars as hard bypassesAMPERSEND_API_URL (overrides the resolved API URL), AMPERSEND_CONTEXT (active context), AMPERSEND_AGENT_* (agent credentials). When this SDK is checked out inside a host repo whose direnv/devenv exports one of them (e.g. the Ampersend monorepo exports AMPERSEND_API_URL=http://localhost:3002), the value leaks into the vitest process.

The result: tests/cli/setup.test.ts and tests/cli/card.test.ts pass in CI (clean env) but fail locally — the non-prod API URL flips context auto-naming from ctx-1a2b to localhost:3002-ctx-1a2b and changes API resolution. (setup.test.ts cleared AMPERSEND_API_URL in afterEach but not beforeEach, so the first test still inherited it; card.test.ts never cleared it.)

Fix

Add a vitest setupFiles entry that strips all inherited AMPERSEND_* env vars before the suite runs, so the suite is hermetic regardless of the shell it's invoked from. Tests that need a specific value still set it themselves (the setup only removes inherited ones at startup).

Verification

From a shell with AMPERSEND_API_URL=http://localhost:3002 exported:

  • before: 7 failed (card + setup)
  • after: full suite green (359/359)

🤖 Generated with Claude Code

The CLI treats several env vars as hard bypasses — `AMPERSEND_API_URL`
(overrides the resolved API URL), `AMPERSEND_CONTEXT` (active context),
`AMPERSEND_AGENT_*` (agent credentials). When the SDK is checked out
inside a host repo whose direnv/devenv exports e.g.
`AMPERSEND_API_URL=http://localhost:3002`, those values leak into the
test process: the CLI tests pass in CI (clean env) but fail locally
because the non-prod URL changes context auto-naming and API resolution
(e.g. `localhost:3002-ctx-1a2b` instead of the expected `ctx-1a2b`).

Add a vitest setup file that clears all inherited `AMPERSEND_*` vars
before the suite runs, keeping it hermetic regardless of where it runs.
Tests that need a specific value still set it themselves.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@pcarranzav
pcarranzav force-pushed the pcv/hermetic-cli-tests branch from 99e7bcf to b6e7dc7 Compare July 1, 2026 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant