Skip to content

test: add snapshotEnv helper and close env and auth cleanup gaps (TA-001 slice 4) - #2335

Merged
jatmn merged 1 commit into
mainfrom
test/ta001-slice4-env-cleanup
Oct 22, 2026
Merged

jatmn merged 1 commit into
mainfrom
test/ta001-slice4-env-cleanup

Conversation

@jatmn

@jatmn jatmn commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • TA-001 slice 4 (Phase 0 plan item 6), narrowed to the env cleanup gaps found in the post-slice re-measure. Test files and CONTRIBUTING only.
  • New helper src/test/envSnapshot.ts: snapshotEnv(keys, { clear? }) saves the named variables, can optionally unset them, and returns a restore function. Restore puts back the exact previous values (including empty strings) and deletes the ones that were unset. It is not a blanket scrub. Covered by src/test/envSnapshot.test.ts.
  • Fixes:
    • src/utils/providerFlag.test.ts: CLAUDE_CODE_PROVIDER_ROUTE_ID (set to commandcode in one test) was missing from the hand-maintained snapshot/restore lists and leaked out of the file. The lists are replaced with the helper, and the route id is included.
    • src/services/api/openaiShim.ollamaTextToolCalls.test.ts and src/services/api/openaiShim/xmlToolCallParsing.test.ts: cleanup deleted provider keys instead of restoring whatever was set before. They now use the helper.
    • src/utils/fastMode.test.ts and src/hooks/useApiKeyVerification.test.tsx: cleanup re-registered the live utils/auth namespace, which Bun had already redirected to the mock, so the auth stubs leaked into later files (follow-up noted in test: isolate attribution suites from leaked auth, beta and routing mocks (TA-001 slice 5) #2334). Both now restore from a real-module snapshot copied before any mock is registered, the same pattern as earlier TA-001 slices.
  • CONTRIBUTING.md: one test-guidance bullet on using snapshotEnv.

Impact

  • user-facing impact: none (test-only).
  • developer/maintainer impact: one shared way to save and restore env vars in tests. The betas and compact tests' intentional clears are unchanged. --isolate stays in CI and is handled by the next slice.

Testing

  • I ran the required local preflight.
  • exact commands and results (after bun run build, temp config dirs):
    • bun run typecheck: pass.
    • bun run check: pass, including all 8 --isolate shards.
    • single-process whole-repo bun test (no --isolate): 10434 pass, 31 skip, 0 fail across 782 files.
  • focused tests: all six touched test files alone and together, 274 pass.
  • documented skipped checks or verified pre-existing failures: an earlier whole-repo run on this same diff failed 2 gitSettings.test.ts tests ("omits git instructions outside a git repository" and "follows the session cwd"). On the build host, a sandboxed tool outside this repo briefly creates an empty .git directory in the system temp dir, and that run overlapped one. The file passes on its own (8/8) and in the full rerun above, so the failures were environmental and unrelated to this diff.

Notes

  • provider/model path tested: OpenAI-compatible shim (Ollama, XML tool calls), provider flag routing, Anthropic auth (fastMode, API key verification).
  • follow-up work or known limitations: removing --isolate is the next slice.

Summary by CodeRabbit

  • Documentation
    • Clarified development guidance for tests that modify environment variables, including how to preserve and restore their original state.
  • Tests
    • Improved test isolation so environment variables and authentication module mocks are restored after tests, reducing the risk of tests affecting one another or the existing environment.
    • Added coverage for preserving and restoring environment-variable values, including variables that were previously unset.

@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between c84411b and e9a3987.

📒 Files selected for processing: 8
  • CONTRIBUTING.md
  • src/hooks/useApiKeyVerification.test.tsx
  • src/services/api/openaiShim.ollamaTextToolCalls.test.ts
  • src/services/api/openaiShim/xmlToolCallParsing.test.ts
  • src/test/envSnapshot.test.ts
  • src/test/envSnapshot.ts
  • src/utils/fastMode.test.ts
  • src/utils/providerFlag.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📜 Recent review details
⏰ Context from checks skipped due to timeout: 19
  • GitHub Check: unit-tests (3)
  • GitHub Check: launcher-node-floor
  • GitHub Check: unit-tests (5)
  • GitHub Check: unit-tests (7)
  • GitHub Check: unit-tests (8)
  • GitHub Check: unit-tests (4)
  • GitHub Check: unit-tests (1)
  • GitHub Check: unit-tests (2)
  • GitHub Check: dedicated-tests
  • GitHub Check: launcher-compatibility (22)
  • GitHub Check: unit-tests (6)
  • GitHub Check: launcher-compatibility (24.11.x)
  • GitHub Check: typecheck
  • GitHub Check: smoke-and-tests
  • GitHub Check: changes
  • GitHub Check: CodeQL (actions)
  • GitHub Check: pr-intent-scan
  • GitHub Check: CodeQL (javascript-typescript)
  • GitHub Check: Analyze (javascript-typescript)
🧰 Additional context used
📓 Path-based instructions: 4
Review provider routing, model selection, env precedence, auth/token handling, OpenAI-compatible shims, retries, proxy behavior, and outbound HTTP behavior with high scrutiny.

⚙️ CodeRabbit configuration file

Files:

  • src/services/api/openaiShim.ollamaTextToolCalls.test.ts
  • src/utils/providerFlag.test.ts
  • src/services/api/openaiShim/xmlToolCallParsing.test.ts
Review tests for meaningful coverage of the changed behavior, isolation of global/env/config state, async cleanup, fake timers, provider profile leaks, and Windows-compatible assumptions.

⚙️ CodeRabbit configuration file

Files:

  • src/utils/fastMode.test.ts
  • src/test/envSnapshot.test.ts
  • src/services/api/openaiShim.ollamaTextToolCalls.test.ts
  • src/hooks/useApiKeyVerification.test.tsx
  • src/utils/providerFlag.test.ts
  • src/services/api/openaiShim/xmlToolCallParsing.test.ts
Review docs for accuracy against current code behavior.

⚙️ CodeRabbit configuration file

Files:

  • CONTRIBUTING.md
Apply the OpenClaude maintainer review rubric from AGENTS.md.

⚙️ CodeRabbit configuration file

Files:

  • src/utils/fastMode.test.ts
  • src/test/envSnapshot.test.ts
  • CONTRIBUTING.md
  • src/services/api/openaiShim.ollamaTextToolCalls.test.ts
  • src/hooks/useApiKeyVerification.test.tsx
  • src/utils/providerFlag.test.ts
  • src/test/envSnapshot.ts
  • src/services/api/openaiShim/xmlToolCallParsing.test.ts
🪛 ast-grep (0.45.3)
src/utils/fastMode.test.ts

[warning] 13-13: Do not use Math.random() to generate security-sensitive values such as tokens, secrets, passwords, API keys, salts, nonces, OTPs, or session IDs. Math.random() is not cryptographically secure and is predictable. Use crypto.randomBytes()/crypto.randomUUID() (Node) or crypto.getRandomValues() (Web Crypto) instead.
Context: Math.random()
Note: [CWE-330] Use of Insufficiently Random Values.

(insecure-random-security-token-typescript)

🔇 Additional comments: 8
src/hooks/useApiKeyVerification.test.tsx: 1

3-3: LGTM!

Also applies to: 13-16, 82-84

src/utils/fastMode.test.ts: 1

1-1: LGTM!

Also applies to: 13-16, 250-252

src/test/envSnapshot.ts: 1

1-24: LGTM!

src/test/envSnapshot.test.ts: 1

1-97: LGTM!

CONTRIBUTING.md: 1

163-163: LGTM!

src/services/api/openaiShim.ollamaTextToolCalls.test.ts: 1

13-13: LGTM!

Also applies to: 17-17, 230-230, 233-233, 240-240, 284-284, 287-287, 294-294, 391-391, 394-394, 401-401, 447-447, 450-450, 457-457, 519-519, 522-522, 529-529

src/services/api/openaiShim/xmlToolCallParsing.test.ts: 1

18-18: LGTM!

Also applies to: 287-287, 290-290, 297-297

src/utils/providerFlag.test.ts: 1

17-17: LGTM!

Also applies to: 20-20, 72-72, 76-76, 86-86

📝 Walkthrough

The change adds snapshotEnv to save and restore selected environment variables. It updates test suites to use the helper and restores real auth module mocks after those suites.

Changes: Test isolation

Layer / File(s) Summary
Environment snapshot helper
src/test/envSnapshot.ts, src/test/envSnapshot.test.ts, CONTRIBUTING.md
Adds snapshotEnv and tests value restoration, optional clearing, originally unset keys, and changes to unlisted variables. Documents snapshot and restore guidance, including use with the shared mutation lock.
Environment isolation in test suites
src/services/api/openaiShim.ollamaTextToolCalls.test.ts, src/services/api/openaiShim/xmlToolCallParsing.test.ts, src/utils/providerFlag.test.ts
Uses snapshots to restore environment variables to their prior values in API streaming and provider-flag tests.
Auth module restoration
src/hooks/useApiKeyVerification.test.tsx, src/utils/fastMode.test.ts
Loads real auth modules through nonce-specific imports and restores the module mocks after each test suite.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: gnanam1990, rodrigoeqnit, chioarub

Merge Risk: ⚪ Minimal · up to e9a39

The test-isolation changes are mergeable after normal checks; no actionable failure was established.

Pre-merge checks: Passed 6 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 7 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 6 passed
Check name Status Explanation
Title check Passed The title is concise, scoped, and accurately describes the snapshotEnv helper and environment and auth cleanup changes.
Description check Passed The description includes the required Summary, Impact, Testing, and Notes sections. It explains the changes, impact, commands and results, focused tests, known environmental failures, and limitations.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Risk Surface Disclosed Passed The PR touches auth test-module cleanup and provider-routing environment isolation. The authored description identifies both areas, states that the changes are test-only, and records no user-facing im…
No Hidden Policy Change Passed PASS — the authoritative diff changes only test files, src/test/envSnapshot.ts, and CONTRIBUTING.md. The code saves/restores test environment variables and test module mocks; it does not alter pro…
Full details: Risk Surface Disclosed

Explanation

The PR touches auth test-module cleanup and provider-routing environment isolation. The authored description identifies both areas, states that the changes are test-only, and records no user-facing impact. This discloses the risk surface and indicates that the PR introduces no blocker.

Full details: No Hidden Policy Change

Explanation

PASS — the authoritative diff changes only test files, src/test/envSnapshot.ts, and CONTRIBUTING.md. The code saves/restores test environment variables and test module mocks; it does not alter product routing defaults, trust decisions, telemetry or network behavior, permissions, or production runtime code. No hidden policy decision requires maintainer alignment.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 7 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@jatmn jatmn left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

— jatmn's PR Review agent

🟢 Thanks for the contribution. I do not see any actionable issues from my review.

@jatmn
jatmn marked this pull request as ready for review October 22, 2026 14:17
@jatmn jatmn self-assigned this Oct 22, 2026
@jatmn
jatmn merged commit c823efe into main Oct 22, 2026
22 checks passed
@jatmn
jatmn deleted the test/ta001-slice4-env-cleanup branch October 22, 2026 14:34
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