Files
skillshare/internal/plugin/codex_cli_test.go
T
Willie 70cb287672 fix(plugin): find the Codex CLI the desktop app ships (#383)
* fix(plugin): find the Codex CLI the desktop app ships

Plugin commands ran the bare name `codex` from PATH, so a machine with only
the Codex desktop app, or a scheduled job started with a minimal PATH,
reported the CLI as missing. The account-only `cli` override did not help:
the default codex target cannot set it, and an absolute path in a config
shared between macOS and Windows is wrong on one of them.

When `codex` is not on PATH, look in Homebrew's folders and the CLI the app
installs (Codex.app on macOS, %LOCALAPPDATA%\OpenAI\Codex\bin on Windows).
SKILLSHARE_CODEX_CLI overrides the search on one machine without touching
the synced config. The missing-CLI error now lists every place searched, in
the CLI and the dashboard.

Refs #338

* docs(plugin): note where Skillshare finds the Codex CLI

The plugin reference and the sharing guide still said every native CLI must
be on PATH, and the built-in skill told agents a missing CLI has no
fallback. Codex now has one, plus a machine-local override that agents
should prefer over a path in a shared config.

Refs #338

* fix(plugin): look for the Codex CLI inside ChatGPT.app

Since the Codex desktop app merged with ChatGPT, macOS installs it as
ChatGPT.app, so an app-only Mac with a minimal PATH still reported Codex as
missing. Try ChatGPT.app before Codex.app in /Applications and
~/Applications, the order Codex's own desktop-app finder uses.

Refs #338

* fix(plugin): run the packaged CLI entrypoint of the ChatGPT app

ChatGPT Desktop 26.924 removed Contents/Resources/codex and ships the CLI
at the entrypoint its package manifest declares, codex-cli/bin/codex, so a
current app-only Mac still reported Codex as missing. Try that entrypoint
first in each app bundle and keep the old path for earlier builds.

Refs #338

* fix(plugin): find the Codex CLI from the Windows installer

The official PowerShell installer puts codex.exe in
%LOCALAPPDATA%\Programs\OpenAI\Codex\bin and adds it to the user PATH. A job
started without that PATH missed it, the same gap Homebrew's folders cover
on macOS. Try it before the desktop app's copy.

Refs #338

* fix(plugin): keep an account's cli even when it is codex

The Codex search ran whenever the executable was the bare name codex, so an
account that set cli: codex could get SKILLSHARE_CODEX_CLI or the app's CLI
instead, against the documented rule that an account's own cli is kept.
Search only when no account cli is set.

Refs #338
2026-10-04 15:16:14 +08:00

153 lines
5.9 KiB
Go

package plugin
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
"time"
)
func notOnPath(string) (string, error) { return "", exec.ErrNotFound }
func noEnv(string) string { return "" }
func writeExe(t *testing.T, path string, mtime time.Time) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, nil, 0o755); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(path, mtime, mtime); err != nil {
t.Fatal(err)
}
}
// SKILLSHARE_CODEX_CLI is set on one machine only, so it wins over everything found.
func TestCodexFinderPrefersEnvironment(t *testing.T) {
f := codexFinder{goos: "darwin", getenv: func(string) string { return "/opt/codex" }, lookPath: func(string) (string, error) { return "/usr/bin/codex", nil }}
if bin, _ := f.find(); bin != "/opt/codex" {
t.Fatalf("bin = %q", bin)
}
}
// A codex on PATH runs by name, as before.
func TestCodexFinderUsesPath(t *testing.T) {
home := t.TempDir()
writeExe(t, filepath.Join(home, "Applications", "Codex.app", "Contents", "Resources", "codex"), time.Now())
f := codexFinder{goos: "darwin", home: home, getenv: noEnv, lookPath: func(string) (string, error) { return "/usr/bin/codex", nil }}
if bin, _ := f.find(); bin != "codex" {
t.Fatalf("bin = %q", bin)
}
}
// With only the macOS app installed, its bundled CLI runs.
func TestCodexFinderFindsMacApp(t *testing.T) {
home := t.TempDir()
want := filepath.Join(home, "Applications", "Codex.app", "Contents", "Resources", "codex")
writeExe(t, want, time.Now())
f := codexFinder{goos: "darwin", home: home, getenv: noEnv, lookPath: notOnPath}
if bin, _ := f.find(); bin != want {
t.Fatalf("bin = %q, want %q", bin, want)
}
}
// Since the ChatGPT app merger the bundle is ChatGPT.app; it is tried before Codex.app.
func TestCodexFinderFindsChatGPTApp(t *testing.T) {
home := t.TempDir()
want := filepath.Join(home, "Applications", "ChatGPT.app", "Contents", "Resources", "codex")
writeExe(t, want, time.Now())
writeExe(t, filepath.Join(home, "Applications", "Codex.app", "Contents", "Resources", "codex"), time.Now())
f := codexFinder{goos: "darwin", home: home, getenv: noEnv, lookPath: notOnPath}
if bin, _ := f.find(); bin != want {
t.Fatalf("bin = %q, want %q", bin, want)
}
}
// ChatGPT 26.924 moved the CLI to the packaged entrypoint codex-cli/bin/codex.
func TestCodexFinderFindsPackagedEntrypoint(t *testing.T) {
home := t.TempDir()
want := filepath.Join(home, "Applications", "ChatGPT.app", "Contents", "Resources", "codex-cli", "bin", "codex")
writeExe(t, want, time.Now())
f := codexFinder{goos: "darwin", home: home, getenv: noEnv, lookPath: notOnPath}
if bin, _ := f.find(); bin != want {
t.Fatalf("bin = %q, want %q", bin, want)
}
}
// The Windows app keeps one folder per version it installed; the newest one runs.
func TestCodexFinderFindsNewestWindowsApp(t *testing.T) {
local := t.TempDir()
old := filepath.Join(local, "OpenAI", "Codex", "bin", "aaa", "codex.exe")
want := filepath.Join(local, "OpenAI", "Codex", "bin", "bbb", "codex.exe")
writeExe(t, want, time.Now())
writeExe(t, old, time.Now().Add(-time.Hour))
f := codexFinder{goos: "windows", localAppData: local, getenv: noEnv, lookPath: notOnPath}
if bin, _ := f.find(); bin != want {
t.Fatalf("bin = %q, want %q", bin, want)
}
}
// The official PowerShell installer's CLI comes before the app's copy, like Homebrew on macOS.
func TestCodexFinderPrefersWindowsInstaller(t *testing.T) {
local := t.TempDir()
want := filepath.Join(local, "Programs", "OpenAI", "Codex", "bin", "codex.exe")
writeExe(t, want, time.Now())
writeExe(t, filepath.Join(local, "OpenAI", "Codex", "bin", "aaa", "codex.exe"), time.Now())
f := codexFinder{goos: "windows", localAppData: local, getenv: noEnv, lookPath: notOnPath}
if bin, _ := f.find(); bin != want {
t.Fatalf("bin = %q, want %q", bin, want)
}
}
// Found nowhere, codex still runs by name so it fails as missing, and every place is named.
func TestCodexFinderListsWhereItLooked(t *testing.T) {
local := t.TempDir()
f := codexFinder{goos: "windows", localAppData: local, getenv: noEnv, lookPath: notOnPath}
bin, looked := f.find()
want := []string{"PATH", filepath.Join(local, "Programs", "OpenAI", "Codex", "bin", "codex.exe"), filepath.Join(local, "OpenAI", "Codex", "bin", "*", "codex.exe")}
if bin != "codex" || !slices.Equal(looked, want) {
t.Fatalf("bin = %q, looked = %v", bin, looked)
}
}
// A missing Codex CLI is still a missing CLI, and its error names where Skillshare looked.
func TestMissingCodexNamesWhereItLooked(t *testing.T) {
s := &Service{
findCodex: func() (string, []string) {
return "codex", []string{"PATH", "/Applications/Codex.app/Contents/Resources/codex"}
},
Run: func(context.Context, string, []string, string, ...string) ([]byte, error) {
return nil, agentError{cause: ErrCLIMissing, key: "plugins.error.cliMissing", message: "codex CLI is not installed"}
},
}
s.ProjectRoot = t.TempDir()
_, err := s.run(context.Background(), "codex", "--version")
key, args := ErrorKey(err)
if !errors.Is(err, ErrCLIMissing) || key != "plugins.error.codexMissing" || !strings.Contains(err.Error(), "/Applications/Codex.app") || args["looked"] != "PATH, /Applications/Codex.app/Contents/Resources/codex" {
t.Fatalf("err = %v, key = %q, args = %v", err, key, args)
}
}
// An account's own cli is what it asked for, even the bare name codex; the search does not replace it.
func TestCodexAccountCLIIsNotSearched(t *testing.T) {
var ran string
s := &Service{
Accounts: map[string]Account{"codex-work": {Agent: "codex", Dir: t.TempDir(), CLI: "codex"}},
findCodex: func() (string, []string) { t.Fatal("searched for codex"); return "", nil },
Run: func(_ context.Context, _ string, _ []string, bin string, _ ...string) ([]byte, error) {
ran = bin
return nil, nil
},
}
if _, err := s.run(context.Background(), "codex-work", "--version"); err != nil || ran != "codex" {
t.Fatalf("ran %q, err = %v", ran, err)
}
}