fix: getCliClient() token not passed through#688
Conversation
|
Claude finished @rexxars's task in 3m 16s —— View job ReviewTwo issues to consider: 1. Test order fragility in
The comment at the top of the file explains the cross-file isolation rationale, but the intra-file ordering dependency is still fragile. Consider resetting the module between tests: beforeEach(() => {
vi.resetModules() // clears getCliToken's cachedToken
})Or reorder so the success test (which sets the cache) always runs last — which it currently does, but relies on implicit ordering.
2.
The fix from the new test file should be applied here too: vi.stubEnv('SANITY_AUTH_TOKEN', '') // add to setupTestAuth in exec.test.tsThe core logic changes in |
The assertion on the API response shape without a token is environment-dependent (empty object locally, error in CI). The test only needs to verify the script executed successfully. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Clear SANITY_AUTH_TOKEN in beforeEach and restore original in afterEach - Use vi.stubEnv for env var mutations within individual tests - Add tests for SANITY_AUTH_TOKEN env var fallback and priority Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Coverage Delta
Comparing 2 changed files against main @ Overall Coverage
|
…t stubEnv Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The test was only checking hasToken === true, which could pass even if the token came from a leaked SANITY_AUTH_TOKEN env var. Now the fixture outputs the token value and the test asserts it matches the specific test token configured via setupTestAuth. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Description
sanity exec --with-user-tokenwas not passing the token to the client returned bygetCliClient().The old approach used a
configClient.worker.tsthat was loaded via--importin the child process. The worker importedgetCliClientvia a relative path and mutated its__internal__getTokenproperty. But the user's script importsgetCliClientfromsanity/cli, which re-exports from@sanity/clithrough package exports. These two import paths can resolve to different module instances (e.g.src/vsdist/during development, or duplicate packages innode_modules), so the mutation never reaches the instance the script uses.The fix resolves the token in the parent process and passes it to the child via the
SANITY_AUTH_TOKENenvironment variable.getCliClient()now falls back to that env var when__internal__getTokenreturns nothing.What to review
execScript.ts- Token resolution moved from the child process worker to the parent. ThrowsCLIError(no stack trace) if no token is found.cliClient.ts- One-line change:getCliClient()falls back toprocess.env.SANITY_AUTH_TOKEN.configClient.worker.ts- Deleted, no longer referenced.exec-with-user-token.test.ts- New test file (separate fromexec.test.tsto avoidgetCliTokencache ordering issues). Tests both the error path and the token-passing path.exec-get-user-token.ts- Test fixture script that verifies the token reaches the client config.Testing
Two new tests in
exec-with-user-token.test.ts:--with-user-tokenwith no configured token throwsCLIErrorwith actionable messagegetCliClient()in the spawned script (uses a fake token, no real API call needed)