Files
pipelines/internal/cli/harness_setup_test.go
alan 108c29f9eb One harness setup path: pipelines harness setup <claude|codex|opencode|pi>
Checks the CLI on the server's PATH (install command when missing), signs in
with the harness's own login only when needed (--login forces it), keeps the
OpenCode/Pi model pickers, and verifies with the server's check. Works on
native and Docker installs (Docker delegates to the container setup); --host
is a hidden compatibility alias. harness list shows all four harnesses'
installed/signed-in/configured/default status. ./install --opencode|--pi now
point to the command; ./install ends with the next-step hint.
2026-11-02 16:11:04 +00:00

210 lines
8.4 KiB
Go

package cli
import (
"bytes"
"context"
"encoding/json"
"net"
"os"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/installation"
)
// isolateHarnesses gives a test an empty PATH directory of fake harnesses and
// a private HOME, so no real harness, login, config or socket is reachable.
func isolateHarnesses(t *testing.T) (root, bin string) {
t.Helper()
root = t.TempDir()
bin = filepath.Join(root, "bin")
if err := os.Mkdir(bin, 0700); err != nil {
t.Fatal(err)
}
t.Setenv("HOME", root)
t.Setenv("PATH", bin)
t.Setenv("FIXTURE", root)
t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(root, "harnesses.json"))
t.Setenv("PIPELINES_CLAUDE", "")
t.Setenv("PIPELINES_OPENCODE", "")
t.Setenv("PIPELINES_CLAUDE_TOKEN_FILE", filepath.Join(root, "no-token"))
t.Setenv("CLAUDE_CONFIG_DIR", filepath.Join(root, ".claude"))
t.Setenv("CODEX_HOME", filepath.Join(root, "codex"))
previous := harnessFallbackDirs
harnessFallbackDirs = func() []string { return nil }
t.Cleanup(func() { harnessFallbackDirs = previous })
return root, bin
}
func fakeHarness(t *testing.T, bin, name, body string) string {
t.Helper()
path := filepath.Join(bin, name)
if err := os.WriteFile(path, []byte("#!/bin/sh\n"+body), 0700); err != nil {
t.Fatal(err)
}
return path
}
func runHarness(t *testing.T, app App, args ...string) (string, error) {
t.Helper()
var out bytes.Buffer
app.In, app.Out, app.Err = bytes.NewBuffer(nil), &out, &out
err := app.Run(context.Background(), append([]string{"harness"}, args...))
return out.String(), err
}
func TestHarnessSetupMissingCLIPrintsInstallCommand(t *testing.T) {
isolateHarnesses(t)
for _, name := range harnessNames {
_, err := runHarness(t, App{}, "setup", name)
if err == nil || !strings.Contains(err.Error(), harnessInstallCommand(name)) || !strings.Contains(err.Error(), "pipelines harness setup "+name) {
t.Fatalf("%s: missing CLI error = %v", name, err)
}
}
}
func TestHarnessSetupUsesTheServerPath(t *testing.T) {
root, bin := isolateHarnesses(t)
fakeHarness(t, bin, "claude", "echo '{\"loggedIn\":true}'\n")
servicePath := filepath.Join(root, "service-bin")
_, err := runHarness(t, App{Install: &installation.Config{Path: servicePath}}, "setup", "claude")
if err == nil || !strings.Contains(err.Error(), "not on the PATH the Pipelines server runs with") {
t.Fatalf("shell-only claude error = %v", err)
}
if _, err := runHarness(t, App{Install: &installation.Config{Path: bin}}, "setup", "claude"); err != nil {
t.Fatalf("claude on the server PATH: %v", err)
}
}
func TestHarnessSetupClaudeSignedInSkipsLogin(t *testing.T) {
root, bin := isolateHarnesses(t)
fakeHarness(t, bin, "claude", "case \"$*\" in\n'auth status') echo '{\"loggedIn\":true}' ;;\n*) echo \"$*\" >> \"$FIXTURE/claude-calls\"; exit 1 ;;\nesac\n")
out, err := runHarness(t, App{}, "setup", "claude")
if err != nil {
t.Fatal(err, out)
}
for _, want := range []string{"skipping login", "Verified: Claude Code is signed in", "every 10 s; no restart needed"} {
if !strings.Contains(out, want) {
t.Fatalf("missing %q:\n%s", want, out)
}
}
if _, err := os.Stat(filepath.Join(root, "claude-calls")); !os.IsNotExist(err) {
t.Fatal("a signed-in Claude Code was asked to log in")
}
if _, err := runHarness(t, App{}, "setup", "claude", "--model", "a/b"); err == nil {
t.Fatal("--model accepted for Claude Code")
}
}
func TestHarnessSetupClaudeRunsLoginWhenSignedOut(t *testing.T) {
root, bin := isolateHarnesses(t)
fakeHarness(t, bin, "claude", "case \"$*\" in\n'auth status') if [ -f \"$FIXTURE/logged\" ]; then echo '{\"loggedIn\":true}'; else echo '{\"loggedIn\":false}'; exit 1; fi ;;\n'auth login') : > \"$FIXTURE/logged\" ;;\n*) exit 91 ;;\nesac\n")
out, err := runHarness(t, App{}, "setup", "claude")
if err != nil {
t.Fatal(err, out)
}
if _, err := os.Stat(filepath.Join(root, "logged")); err != nil || !strings.Contains(out, "claude auth login") || !strings.Contains(out, "Verified") {
t.Fatalf("login not invoked:\n%s", out)
}
}
func TestHarnessSetupCodexLoginAndSocket(t *testing.T) {
root, bin := isolateHarnesses(t)
fakeHarness(t, bin, "codex", "case \"$*\" in\n'login status') [ -f \"$FIXTURE/logged\" ] ;;\nlogin) : > \"$FIXTURE/logged\" ;;\n*) exit 91 ;;\nesac\n")
out, err := runHarness(t, App{}, "setup", "codex")
if _, statErr := os.Stat(filepath.Join(root, "logged")); statErr != nil || !strings.Contains(out, "codex login") {
t.Fatalf("codex login not invoked:\n%s", out)
}
if err == nil || !strings.Contains(err.Error(), "app-server-control.sock") || !strings.Contains(err.Error(), "Open the Codex app") {
t.Fatalf("missing socket error = %v", err)
}
// Unix socket paths are length-limited; keep CODEX_HOME short.
home, err := os.MkdirTemp("/tmp", "cx")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(home) })
t.Setenv("CODEX_HOME", home)
if err := os.Mkdir(filepath.Join(home, "app-server-control"), 0700); err != nil {
t.Fatal(err)
}
listener, err := net.Listen("unix", filepath.Join(home, "app-server-control", "app-server-control.sock"))
if err != nil {
t.Fatal(err)
}
defer listener.Close()
out, err = runHarness(t, App{}, "setup", "codex")
if err != nil || !strings.Contains(out, "skipping login") || !strings.Contains(out, "socket") || !strings.Contains(out, "no restart needed") {
t.Fatalf("codex with socket: %v\n%s", err, out)
}
}
func TestHarnessSetupOpenCodeLogsInWhenNoProviderIsConnected(t *testing.T) {
root, bin := isolateHarnesses(t)
fakeHarness(t, bin, "opencode", "case \"$*\" in\n'api get /api/integration') echo '{}' ;;\nmodels) printf '%s\\n' fixture/harness fixture/stages ;;\n'auth list --format json') if [ -f \"$FIXTURE/logged\" ]; then echo '[{\"id\":\"fixture\",\"connections\":[{}]}]'; else echo '[]'; fi ;;\n'auth login') : > \"$FIXTURE/logged\" ;;\n*) exit 91 ;;\nesac\n")
out, err := runHarness(t, App{}, "setup", "opencode", "--model", "fixture/harness", "--pipeline-model", "fixture/stages")
if err != nil {
t.Fatal(err, out)
}
if _, err := os.Stat(filepath.Join(root, "logged")); err != nil {
t.Fatalf("OpenCode login not invoked:\n%s", out)
}
for _, want := range []string{"opening the OpenCode sign-in", "Configuration complete", "Verified: OpenCode lists both models", "no restart needed"} {
if !strings.Contains(out, want) {
t.Fatalf("missing %q:\n%s", want, out)
}
}
// Signed in now: a second setup skips the login.
out, err = runHarness(t, App{}, "setup", "opencode", "--model", "fixture/harness", "--pipeline-model", "fixture/stages")
if err != nil || !strings.Contains(out, "skipping login") {
t.Fatalf("signed-in OpenCode: %v\n%s", err, out)
}
}
func TestHarnessListShowsAllFour(t *testing.T) {
root, bin := isolateHarnesses(t)
fakeHarness(t, bin, "claude", "echo '{\"loggedIn\":true}'\n")
opencode := fakeHarness(t, bin, "opencode", "case \"$*\" in\n'api get /api/integration') echo '{}' ;;\nmodels) printf '%s\\n' fixture/harness fixture/stages ;;\n'auth list --format json') echo '[{\"id\":\"fixture\",\"connections\":[{}]}]' ;;\n*) exit 91 ;;\nesac\n")
writeHarnessConfig(t, filepath.Join(root, "harnesses.json"), `{"default":"opencode","profiles":{"opencode":{"executable":"`+opencode+`","model":"fixture/harness","pipeline_model":"fixture/stages"}}}`)
out, err := runHarness(t, App{}, "list")
if err != nil {
t.Fatal(err)
}
lines := map[string]string{}
for _, line := range strings.Split(out, "\n") {
if fields := strings.Fields(line); len(fields) > 0 {
lines[fields[0]] = strings.Join(fields, " ")
}
}
for name, want := range map[string]string{
"claude": "claude yes yes yes -",
"codex": "codex no - no -",
"opencode": "opencode yes yes yes yes model fixture/harness",
"pi": "pi no - no -",
} {
if !strings.HasPrefix(lines[name], want) {
t.Fatalf("%s row = %q, want prefix %q\n%s", name, lines[name], want, out)
}
}
out, err = runHarness(t, App{}, "list", "--json")
if err != nil {
t.Fatal(err)
}
var view struct {
Default string `json:"default"`
Harnesses []harnessStatus `json:"harnesses"`
}
if err := json.Unmarshal([]byte(out), &view); err != nil || view.Default != "opencode" || len(view.Harnesses) != 4 {
t.Fatalf("list --json = %v\n%s", err, out)
}
}
func TestHarnessSetupHelpHidesCompatibilityFlags(t *testing.T) {
isolateHarnesses(t)
out, err := runHarness(t, App{}, "setup", "--help")
if err != nil || !strings.Contains(out, "pipelines harness setup <claude|codex|opencode|pi>") || strings.Contains(out, "--host") || strings.Contains(out, "--versions") {
t.Fatalf("help: %v\n%s", err, out)
}
}