#16658: test: isolate env-dependent gateway/auth fixtures
gateway
size: S
## Summary
- isolate browser profile CRUD tests from host gateway token/password env vars
- isolate runtime auth test from host token/password env vars
- ensure `withTempHome` clears/restores `OPENCLAW_AGENT_DIR` and `PI_CODING_AGENT_DIR` so auth-profile tests use temp home paths
## Why
Several tests were reading host env vars (`OPENCLAW_GATEWAY_TOKEN`, `OPENCLAW_AGENT_DIR`) and became non-deterministic across machines/runners.
## Validation
- `npx vitest run src/browser/server.post-tabs-open-profile-unknown-returns-404.test.ts src/gateway/server-runtime-config.test.ts src/infra/provider-usage.auth.normalizes-keys.test.ts`
## AI-Assisted
Yes — implemented and reviewed with AI assistance.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Properly isolated environment-dependent test fixtures from host environment variables. The changes ensure `OPENCLAW_GATEWAY_TOKEN`, `OPENCLAW_GATEWAY_PASSWORD`, `OPENCLAW_AGENT_DIR`, and `PI_CODING_AGENT_DIR` don't leak into tests, preventing non-deterministic behavior across different machines and CI runners.
- Extended `withTempHome` helper to snapshot/restore `OPENCLAW_AGENT_DIR` and `PI_CODING_AGENT_DIR`
- Added proper cleanup to browser profile CRUD test fixtures
- Wrapped auth test assertions in try-finally to guarantee env restoration
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The changes are straightforward test infrastructure improvements that add proper environment isolation. All modifications follow consistent patterns: snapshot env vars before modification, restore them in cleanup. The code doesn't affect production logic, only test fixtures. The PR includes validation command showing the specific tests that were fixed.
- No files require special attention
<sub>Last reviewed commit: a1bc4f5</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
## Local Validation
```bash
pnpm build && pnpm check && pnpm test
```
Build passes. Lint passes. 4743/4747 tests pass (4 pre-existing env-isolation failures fixed by #16658).
## Local Validation
```bash
pnpm build && pnpm check && pnpm test
```
Build passes. Lint passes. **All tests pass** (including the 4 that were failing without this fix: `provider-usage.auth.normalizes-keys.test.ts`).
Most Similar PRs
#19885: test(gateway,browser): isolate tests from ambient OPENCLAW_GATEWAY_...
by NewdlDewdl · 2026-02-18
84.9%
#19937: fix(gateway): validate token/password auth modes and isolate gatewa...
by NewdlDewdl · 2026-02-18
82.7%
#23139: test: fix flaky auth tests when OPENCLAW_GATEWAY_TOKEN is present
by Imccccc · 2026-02-22
81.5%
#23055: test: clear gateway env vars in beforeEach to prevent leakage
by thinstripe · 2026-02-21
78.9%
#20404: test: isolate gateway auth env vars in test runner
by victorGPT · 2026-02-18
77.9%
#22178: test(web): allow fixture roots in media local file tests
by Kansodata · 2026-02-20
77.4%
#7085: test: skip flaky workspace-paths & safe-bins tests on non-Linux/CI ...
by ThinkIbrokeIt · 2026-02-02
76.4%
#9200: Fix: Strip dangerous env vars from baseEnv in host execution
by vishaltandale00 · 2026-02-05
75.0%
#17456: fix(test): stabilize media root guard test against tmpdir HOME overlap
by widingmarcus-cyber · 2026-02-15
74.8%
#10367: CLI/Ops: resilient browser fill + failover hardening + operations t...
by cluster2600 · 2026-02-06
74.4%