| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestLoginEnvironmentTMPDIR(t *testing.T) { |
| 12 | for _, name := range []string{"codex", "claude"} { |
| 13 | for _, value := range []string{"", "unset", "/scratch/test"} { |
| 14 | t.Run(name+"/"+value, func(t *testing.T) { |
| 15 | t.Setenv("TMPDIR", value) |
| 16 | if value == "unset" { |
| 17 | if err := os.Unsetenv("TMPDIR"); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | } |
| 21 | found := "" |
| 22 | for _, entry := range LoginEnvironment(name, "/home/test") { |
| 23 | if strings.HasPrefix(entry, "TMPDIR=") { |
| 24 | found = entry |
| 25 | } |
| 26 | } |
| 27 | want := "" |
| 28 | if value != "" && value != "unset" { |
| 29 | want = "TMPDIR=" + value |
| 30 | } |
| 31 | if found != want { |
| 32 | t.Fatalf("TMPDIR entry=%q, want %q", found, want) |
| 33 | } |
| 34 | }) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestCodexLoginStartupNotice(t *testing.T) { |
| 40 | notice := `{"method":"account/updated","params":{"authMode":"chatgpt","planType":"promax"}}` |
| 41 | for _, tc := range []struct { |
| 42 | name, frames string |
| 43 | refuse bool |
| 44 | }{ |
| 45 | {"recorded", notice, false}, |
| 46 | {"before-initialize", notice, false}, |
| 47 | {"across-initialize", notice, true}, |
| 48 | {"plan-ignored", `{"method":"account/updated","params":{"authMode":"chatgpt"}}`, false}, |
| 49 | {"other-notice", notice + "\n" + `{"method":"account/rateLimits/updated","params":{"rateLimits":{}}}`, false}, |
| 50 | {"duplicate", notice + "\n" + notice, true}, |
| 51 | {"api-key", strings.ReplaceAll(notice, "chatgpt", "apikey"), true}, |
| 52 | {"missing-mode", `{"method":"account/updated","params":{}}`, true}, |
| 53 | {"null-mode", `{"method":"account/updated","params":{"authMode":null}}`, true}, |
| 54 | {"id", `{"id":"2",` + notice[1:], true}, |
| 55 | {"empty-id", `{"id":"",` + notice[1:], true}, |
| 56 | {"null-id", `{"id":null,` + notice[1:], true}, |
| 57 | } { |
| 58 | t.Run(tc.name, func(t *testing.T) { |
| 59 | root := t.TempDir() |
| 60 | binary := filepath.Join(root, "codex") |
| 61 | script := `#!/bin/sh |
| 62 | case "$*" in |
| 63 | '--version') echo 'codex-cli 0.165.0'; exit 0;; |
| 64 | 'login --device-auth --help') exit 0;; |
| 65 | esac |
| 66 | IFS= read -r line |
| 67 | PREINIT |
| 68 | printf '{"id":"1","result":{"userAgent":"equipo/0.165.0 fixture","codexHome":"%s"}}\n' "$CODEX_HOME" |
| 69 | IFS= read -r line |
| 70 | printf '%s\n' '{"method":"remoteControl/status/changed","params":{"status":"disabled","serverName":"DOCS-T1","installationId":"fake-installation","environmentId":null}}' |
| 71 | printf '%s\n' 'FRAMES' |
| 72 | IFS= read -r line |
| 73 | printf '%s\n' '{"id":"2","result":{"account":{"type":"chatgpt","email":"fixture@example.invalid"}}}' |
| 74 | ` |
| 75 | preinit := "" |
| 76 | if tc.name == "before-initialize" || tc.name == "across-initialize" { |
| 77 | preinit = "printf '%s\\n' '" + notice + "'" |
| 78 | } |
| 79 | if tc.name == "before-initialize" { |
| 80 | script = strings.ReplaceAll(script, "printf '%s\\n' 'FRAMES'", "") |
| 81 | } |
| 82 | script = strings.ReplaceAll(script, "PREINIT", preinit) |
| 83 | script = strings.ReplaceAll(script, "FRAMES", tc.frames) |
| 84 | if err := os.WriteFile(binary, []byte(script), 0700); err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | d := NewLoginDriver(LoginOptions{CodexPath: binary}).(*terminalLoginDriver) |
| 88 | got, err := d.Probe(context.Background(), Binding{Provider: "codex", Home: root}) |
| 89 | if (err != nil) != tc.refuse { |
| 90 | t.Fatalf("Probe error=%v, refuse=%v", err, tc.refuse) |
| 91 | } |
| 92 | if err == nil && got.AccountID != "fixture@example.invalid" { |
| 93 | t.Fatal(got) |
| 94 | } |
| 95 | }) |
| 96 | } |
| 97 | } |
| 98 | |