package provider import ( "context" "io" "os" "path/filepath" "strings" "testing" "time" ) func TestOnboardingOfficialLogin(t *testing.T) { for _, name := range []string{"claude", "codex"} { t.Run(name, func(t *testing.T) { root := t.TempDir() binary := filepath.Join(root, name) script := `#!/bin/sh set -eu umask 077 case "$1" in --version) echo 'VERSION'; exit 0 ;; esac case "$*" in 'auth login --help'|'login --device-auth --help') exit 0 ;; 'auth status --json') printf '%s' '{"loggedIn":true,"authMethod":"claude.ai","email":"person@example.test"}'; exit 0 ;; 'app-server --strict-config --listen stdio://') IFS= read -r line printf '{"id":"1","result":{"userAgent":"equipo/0.165.0 fixture","codexHome":"%s"}}\n' "$CODEX_HOME" IFS= read -r line 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"}}' IFS= read -r line printf '%s\n' '{"id":"2","result":{"account":{"type":"chatgpt","email":"person@example.test"}}}' exit 0 ;; 'auth login') test "$HOME" = "$CLAUDE_CONFIG_DIR"; test "$HOME" = "$CLAUDE_SECURESTORAGE_CONFIG_DIR"; file=.credentials.json ;; 'login --device-auth') test "$HOME" = "$CODEX_HOME"; file=auth.json ;; *) exit 9 ;; esac IFS= read -r code test "$code" = RETURN-SECRET printf '%s' '{"token":"CREDENTIAL-SECRET"}' > "$HOME/$file" ` version := "0.165.0" if name == "claude" { version = "2.1.312 (Claude Code)" } script = strings.ReplaceAll(script, "VERSION", version) if err := os.WriteFile(binary, []byte(script), 0700); err != nil { t.Fatal(err) } t.Setenv("PATH", root) d := NewLoginDriver(LoginOptions{ClaudePath: "claude", CodexPath: "codex", Input: strings.NewReader("RETURN-SECRET\n"), Output: io.Discard, ErrorOutput: io.Discard}) if !d.(interface{ Supported(string) bool }).Supported(name) { t.Fatal("supported pinned CLI rejected") } home, err := NewLoginHome(filepath.Join(root, "accounts")) if err != nil { t.Fatal(err) } c, err := d.Begin(context.Background(), name, home) if err != nil { t.Fatal(err) } defer d.Cancel(context.Background(), c.ID) if c.VerificationURI != "" || c.UserCode != "" { t.Fatal("terminal display was parsed") } deadline := time.Now().Add(3 * time.Second) for time.Now().Before(deadline) { r, err := d.Poll(context.Background(), c.ID) if err != nil { t.Fatal(err) } if r.State == "pending" { time.Sleep(time.Millisecond) continue } if r.State != "connected" || r.Identity.AccountID != "person@example.test" || r.Identity.HomeID == "" { t.Fatal("login missing verified identity", r) } return } t.Fatal("login did not finish") }) } } func TestOnboardingOfficialLoginCapability(t *testing.T) { for _, mode := range []string{"wrong-version", "missing-subcommand"} { t.Run(mode, func(t *testing.T) { root := t.TempDir() binary := filepath.Join(root, "codex") script := "#!/bin/sh\nif [ \"$1\" = --version ]; then echo codex-cli 0.165.0; exit 0; fi\nexit 2\n" if mode == "wrong-version" { script = strings.ReplaceAll(script, "0.165.0", "0.1.0") script = strings.ReplaceAll(script, "exit 2\n", "exit 0\n") } if e := os.WriteFile(binary, []byte(script), 0700); e != nil { t.Fatal(e) } d := NewLoginDriver(LoginOptions{CodexPath: binary, Input: strings.NewReader(""), Output: io.Discard, ErrorOutput: io.Discard}) if d.(interface{ Supported(string) bool }).Supported("codex") { t.Fatal("unsupported CLI accepted") } if _, e := d.Begin(context.Background(), "codex", filepath.Join(root, "absent")); e != ErrLoginUnavailable { t.Fatal("unsupported login began", e) } }) } } func TestOnboardingOfficialLoginInterrupt(t *testing.T) { root := t.TempDir() binary := filepath.Join(root, "claude") script := `#!/bin/sh set -eu case "$*" in '--version') echo '2.1.312 (Claude Code)'; exit 0 ;; 'auth login --help') exit 0 ;; 'auth login') trap 'echo interrupted > "$HOME/interrupted"; exit 130' INT echo ready > "$HOME/ready" while :; do :; done ;; *) exit 2 ;; esac ` if e := os.WriteFile(binary, []byte(script), 0700); e != nil { t.Fatal(e) } home, e := NewLoginHome(filepath.Join(root, "accounts")) if e != nil { t.Fatal(e) } d := NewLoginDriver(LoginOptions{ClaudePath: binary, Input: strings.NewReader(""), Output: io.Discard, ErrorOutput: io.Discard}) c, e := d.Begin(context.Background(), "claude", home) if e != nil { t.Fatal(e) } defer d.Cancel(context.Background(), c.ID) deadline := time.Now().Add(time.Second) for { if _, e := os.Stat(filepath.Join(home, "ready")); e == nil { break } if time.Now().After(deadline) { t.Fatal("CLI not ready") } time.Sleep(time.Millisecond) } if e := d.Cancel(context.Background(), c.ID); e != nil { t.Fatal(e) } if _, e := os.Stat(filepath.Join(home, "interrupted")); e != nil { t.Fatal("SIGINT did not reach CLI", e) } r, e := d.Poll(context.Background(), c.ID) if e != nil || r.State != "cancelled" { t.Fatal("interrupt connected account", r, e) } }