| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestOnboardingOfficialLogin(t *testing.T) { |
| 14 | for _, name := range []string{"claude", "codex"} { |
| 15 | t.Run(name, func(t *testing.T) { |
| 16 | root := t.TempDir() |
| 17 | binary := filepath.Join(root, name) |
| 18 | script := `#!/bin/sh |
| 19 | set -eu |
| 20 | umask 077 |
| 21 | case "$1" in |
| 22 | --version) echo 'VERSION'; exit 0 ;; |
| 23 | esac |
| 24 | case "$*" in |
| 25 | 'auth login --help'|'login --device-auth --help') exit 0 ;; |
| 26 | 'auth status --json') printf '%s' '{"loggedIn":true,"authMethod":"claude.ai","email":"person@example.test"}'; exit 0 ;; |
| 27 | 'app-server --strict-config --listen stdio://') |
| 28 | IFS= read -r line |
| 29 | printf '{"id":"1","result":{"userAgent":"equipo/0.165.0 fixture","codexHome":"%s"}}\n' "$CODEX_HOME" |
| 30 | IFS= read -r line |
| 31 | printf '%s\n' '{"method":"remoteControl/status/changed","params":{"status":"disabled","serverName":"DOCS-T1","installationId":"fake-installation","environmentId":null}}' '{"method":"account/updated","params":{"authMode":"chatgpt","planType":"promax"}}' |
| 32 | IFS= read -r line |
| 33 | printf '%s\n' '{"id":"2","result":{"account":{"type":"chatgpt","email":"person@example.test"}}}' |
| 34 | exit 0 ;; |
| 35 | 'auth login') test "$HOME" = "$CLAUDE_CONFIG_DIR"; test "$HOME" = "$CLAUDE_SECURESTORAGE_CONFIG_DIR"; file=.credentials.json ;; |
| 36 | 'login --device-auth') test "$HOME" = "$CODEX_HOME"; file=auth.json ;; |
| 37 | *) exit 9 ;; |
| 38 | esac |
| 39 | IFS= read -r code |
| 40 | test "$code" = RETURN-SECRET |
| 41 | printf '%s' '{"token":"CREDENTIAL-SECRET"}' > "$HOME/$file" |
| 42 | ` |
| 43 | version := "0.165.0" |
| 44 | if name == "claude" { |
| 45 | version = "2.1.312 (Claude Code)" |
| 46 | } |
| 47 | script = strings.ReplaceAll(script, "VERSION", version) |
| 48 | if err := os.WriteFile(binary, []byte(script), 0700); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | t.Setenv("PATH", root) |
| 52 | d := NewLoginDriver(LoginOptions{ClaudePath: "claude", CodexPath: "codex", Input: strings.NewReader("RETURN-SECRET\n"), Output: io.Discard, ErrorOutput: io.Discard}) |
| 53 | if !d.(interface{ Supported(string) bool }).Supported(name) { |
| 54 | t.Fatal("supported pinned CLI rejected") |
| 55 | } |
| 56 | home, err := NewLoginHome(filepath.Join(root, "accounts")) |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | c, err := d.Begin(context.Background(), name, home) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | defer d.Cancel(context.Background(), c.ID) |
| 65 | if c.VerificationURI != "" || c.UserCode != "" { |
| 66 | t.Fatal("terminal display was parsed") |
| 67 | } |
| 68 | deadline := time.Now().Add(3 * time.Second) |
| 69 | for time.Now().Before(deadline) { |
| 70 | r, err := d.Poll(context.Background(), c.ID) |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | if r.State == "pending" { |
| 75 | time.Sleep(time.Millisecond) |
| 76 | continue |
| 77 | } |
| 78 | if r.State != "connected" || r.Identity.AccountID != "person@example.test" || r.Identity.HomeID == "" { |
| 79 | t.Fatal("login missing verified identity", r) |
| 80 | } |
| 81 | return |
| 82 | } |
| 83 | t.Fatal("login did not finish") |
| 84 | }) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestOnboardingOfficialLoginCapability(t *testing.T) { |
| 89 | for _, mode := range []string{"wrong-version", "missing-subcommand"} { |
| 90 | t.Run(mode, func(t *testing.T) { |
| 91 | root := t.TempDir() |
| 92 | binary := filepath.Join(root, "codex") |
| 93 | script := "#!/bin/sh\nif [ \"$1\" = --version ]; then echo codex-cli 0.165.0; exit 0; fi\nexit 2\n" |
| 94 | if mode == "wrong-version" { |
| 95 | script = strings.ReplaceAll(script, "0.165.0", "0.1.0") |
| 96 | script = strings.ReplaceAll(script, "exit 2\n", "exit 0\n") |
| 97 | } |
| 98 | if e := os.WriteFile(binary, []byte(script), 0700); e != nil { |
| 99 | t.Fatal(e) |
| 100 | } |
| 101 | d := NewLoginDriver(LoginOptions{CodexPath: binary, Input: strings.NewReader(""), Output: io.Discard, ErrorOutput: io.Discard}) |
| 102 | if d.(interface{ Supported(string) bool }).Supported("codex") { |
| 103 | t.Fatal("unsupported CLI accepted") |
| 104 | } |
| 105 | if _, e := d.Begin(context.Background(), "codex", filepath.Join(root, "absent")); e != ErrLoginUnavailable { |
| 106 | t.Fatal("unsupported login began", e) |
| 107 | } |
| 108 | }) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestOnboardingOfficialLoginInterrupt(t *testing.T) { |
| 113 | root := t.TempDir() |
| 114 | binary := filepath.Join(root, "claude") |
| 115 | script := `#!/bin/sh |
| 116 | set -eu |
| 117 | case "$*" in |
| 118 | '--version') echo '2.1.312 (Claude Code)'; exit 0 ;; |
| 119 | 'auth login --help') exit 0 ;; |
| 120 | 'auth login') |
| 121 | trap 'echo interrupted > "$HOME/interrupted"; exit 130' INT |
| 122 | echo ready > "$HOME/ready" |
| 123 | while :; do :; done ;; |
| 124 | *) exit 2 ;; |
| 125 | esac |
| 126 | ` |
| 127 | if e := os.WriteFile(binary, []byte(script), 0700); e != nil { |
| 128 | t.Fatal(e) |
| 129 | } |
| 130 | home, e := NewLoginHome(filepath.Join(root, "accounts")) |
| 131 | if e != nil { |
| 132 | t.Fatal(e) |
| 133 | } |
| 134 | d := NewLoginDriver(LoginOptions{ClaudePath: binary, Input: strings.NewReader(""), Output: io.Discard, ErrorOutput: io.Discard}) |
| 135 | c, e := d.Begin(context.Background(), "claude", home) |
| 136 | if e != nil { |
| 137 | t.Fatal(e) |
| 138 | } |
| 139 | defer d.Cancel(context.Background(), c.ID) |
| 140 | deadline := time.Now().Add(time.Second) |
| 141 | for { |
| 142 | if _, e := os.Stat(filepath.Join(home, "ready")); e == nil { |
| 143 | break |
| 144 | } |
| 145 | if time.Now().After(deadline) { |
| 146 | t.Fatal("CLI not ready") |
| 147 | } |
| 148 | time.Sleep(time.Millisecond) |
| 149 | } |
| 150 | if e := d.Cancel(context.Background(), c.ID); e != nil { |
| 151 | t.Fatal(e) |
| 152 | } |
| 153 | if _, e := os.Stat(filepath.Join(home, "interrupted")); e != nil { |
| 154 | t.Fatal("SIGINT did not reach CLI", e) |
| 155 | } |
| 156 | r, e := d.Poll(context.Background(), c.ID) |
| 157 | if e != nil || r.State != "cancelled" { |
| 158 | t.Fatal("interrupt connected account", r, e) |
| 159 | } |
| 160 | } |
| 161 | |