Files

48 lines
1.4 KiB
Go

package claude
import (
"context"
"slices"
"testing"
)
func TestAuthEnvKeepsAWorkingDefault(t *testing.T) {
calls := 0
a := authEnv{status: func(_ context.Context, _ string, env []string) bool {
calls++
return !hasEnv(env, secureStorageEnvKey)
}}
got := a.resolve(context.Background(), "claude", []string{"HOME=/h"})
if hasEnv(got, secureStorageEnvKey) || calls != 1 {
t.Fatalf("env = %v after %d checks", got, calls)
}
}
func TestAuthEnvFallsBackToTheLoginFile(t *testing.T) {
t.Setenv("CLAUDE_CONFIG_DIR", "")
a := authEnv{status: func(_ context.Context, _ string, env []string) bool {
return hasEnv(env, secureStorageEnvKey)
}}
got := a.resolve(context.Background(), "claude", []string{"HOME=/h"})
if !slices.Contains(got, secureStorageEnvKey+"="+ConfigDir()) {
t.Fatalf("env = %v", got)
}
// Decided once; later processes reuse the choice.
again := a.resolve(context.Background(), "claude", []string{"HOME=/h"})
if !hasEnv(again, secureStorageEnvKey) {
t.Fatalf("second env = %v", again)
}
}
func TestAuthEnvLeavesExplicitCredentialsAlone(t *testing.T) {
checked := false
a := authEnv{status: func(context.Context, string, []string) bool {
checked = true
return false
}}
base := []string{"CLAUDE_CODE_OAUTH_TOKEN=x"}
if got := a.resolve(context.Background(), "claude", base); !slices.Equal(got, base) || checked {
t.Fatalf("env = %v checked=%v", got, checked)
}
}