package provider import ( "context" "os" "path/filepath" "strings" "testing" ) func TestLoginEnvironmentTMPDIR(t *testing.T) { for _, name := range []string{"codex", "claude"} { for _, value := range []string{"", "unset", "/scratch/test"} { t.Run(name+"/"+value, func(t *testing.T) { t.Setenv("TMPDIR", value) if value == "unset" { if err := os.Unsetenv("TMPDIR"); err != nil { t.Fatal(err) } } found := "" for _, entry := range LoginEnvironment(name, "/home/test") { if strings.HasPrefix(entry, "TMPDIR=") { found = entry } } want := "" if value != "" && value != "unset" { want = "TMPDIR=" + value } if found != want { t.Fatalf("TMPDIR entry=%q, want %q", found, want) } }) } } } func TestCodexLoginStartupNotice(t *testing.T) { notice := `{"method":"account/updated","params":{"authMode":"chatgpt","planType":"promax"}}` for _, tc := range []struct { name, frames string refuse bool }{ {"recorded", notice, false}, {"before-initialize", notice, false}, {"across-initialize", notice, true}, {"plan-ignored", `{"method":"account/updated","params":{"authMode":"chatgpt"}}`, false}, {"other-notice", notice + "\n" + `{"method":"account/rateLimits/updated","params":{"rateLimits":{}}}`, false}, {"duplicate", notice + "\n" + notice, true}, {"api-key", strings.ReplaceAll(notice, "chatgpt", "apikey"), true}, {"missing-mode", `{"method":"account/updated","params":{}}`, true}, {"null-mode", `{"method":"account/updated","params":{"authMode":null}}`, true}, {"id", `{"id":"2",` + notice[1:], true}, {"empty-id", `{"id":"",` + notice[1:], true}, {"null-id", `{"id":null,` + notice[1:], true}, } { t.Run(tc.name, func(t *testing.T) { root := t.TempDir() binary := filepath.Join(root, "codex") script := `#!/bin/sh case "$*" in '--version') echo 'codex-cli 0.165.0'; exit 0;; 'login --device-auth --help') exit 0;; esac IFS= read -r line PREINIT 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}}' printf '%s\n' 'FRAMES' IFS= read -r line printf '%s\n' '{"id":"2","result":{"account":{"type":"chatgpt","email":"fixture@example.invalid"}}}' ` preinit := "" if tc.name == "before-initialize" || tc.name == "across-initialize" { preinit = "printf '%s\\n' '" + notice + "'" } if tc.name == "before-initialize" { script = strings.ReplaceAll(script, "printf '%s\\n' 'FRAMES'", "") } script = strings.ReplaceAll(script, "PREINIT", preinit) script = strings.ReplaceAll(script, "FRAMES", tc.frames) if err := os.WriteFile(binary, []byte(script), 0700); err != nil { t.Fatal(err) } d := NewLoginDriver(LoginOptions{CodexPath: binary}).(*terminalLoginDriver) got, err := d.Probe(context.Background(), Binding{Provider: "codex", Home: root}) if (err != nil) != tc.refuse { t.Fatalf("Probe error=%v, refuse=%v", err, tc.refuse) } if err == nil && got.AccountID != "fixture@example.invalid" { t.Fatal(got) } }) } }