| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestClaudeStatusPlan(t *testing.T) { |
| 11 | home := t.TempDir() |
| 12 | bin := filepath.Join(home, "claude") |
| 13 | script := `#!/bin/sh |
| 14 | case "$*" in |
| 15 | '--version') echo '2.1.312 (Claude Code)';; |
| 16 | 'auth login --help') exit 0;; |
| 17 | 'auth status --json') echo '{"loggedIn":true,"authMethod":"claude.ai","email":"a@example.com","subscriptionType":"max"}';; |
| 18 | *) exit 99;; |
| 19 | esac |
| 20 | ` |
| 21 | if err := os.WriteFile(bin, []byte(script), 0700); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | id, plan, err := ReadClaudeStatus(context.Background(), Binding{Home: home, Provider: "claude"}, LoginOptions{ClaudePath: bin}) |
| 25 | if err != nil || plan != "max" || id.AccountID != "a@example.com" { |
| 26 | t.Fatalf("%+v %q %v", id, plan, err) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestClaudeOAuthTokenFromLease(t *testing.T) { |
| 31 | b, rules := accountFixture(t) |
| 32 | b.Provider = "claude" |
| 33 | writeFake(t, filepath.Join(b.Home, ".credentials.json"), `{"claudeAiOauth":{"accessToken":"fixture-private"}}`) |
| 34 | l := openFixture(t, b, rules) |
| 35 | token, err := l.ClaudeOAuthToken() |
| 36 | if err != nil || string(token) != "fixture-private" { |
| 37 | t.Fatal("missing lease token", err) |
| 38 | } |
| 39 | clear(token) |
| 40 | // The read must come from the checked runtime copy, not a changed source. |
| 41 | writeFake(t, filepath.Join(b.Home, ".credentials.json"), `{"claudeAiOauth":{"accessToken":"changed-source"}}`) |
| 42 | token, err = l.ClaudeOAuthToken() |
| 43 | if err != nil || string(token) != "fixture-private" { |
| 44 | t.Fatal("read source instead of lease") |
| 45 | } |
| 46 | clear(token) |
| 47 | l.Close() |
| 48 | if _, err = l.ClaudeOAuthToken(); err == nil { |
| 49 | t.Fatal("closed lease exposed token") |
| 50 | } |
| 51 | } |
| 52 | |