154 lines
5.9 KiB
Go
154 lines
5.9 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/cli"
|
|
)
|
|
|
|
// fixtureDocker installs a fake docker and a fake host opencode. It returns the
|
|
// captured-arguments path and the host-openCode marker path.
|
|
func fixtureDocker(t *testing.T, exitCode int) (capture, opencodeMarker string) {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
capture = filepath.Join(root, "docker-args")
|
|
docker := filepath.Join(root, "docker")
|
|
script := "#!/bin/sh\nprintf '%s\\n' \"$@\" > " + capture + "\nprintf '{\"healthy\":true,\"session_id\":\"ses_docker\"}'\nexit " + strconv.Itoa(exitCode) + "\n"
|
|
if err := os.WriteFile(docker, []byte(script), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
opencodeMarker = filepath.Join(root, "opencode-called")
|
|
opencode := filepath.Join(root, "opencode")
|
|
if err := os.WriteFile(opencode, []byte("#!/bin/sh\ntouch "+opencodeMarker+"\nexit 9\n"), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PATH", root+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
return capture, opencodeMarker
|
|
}
|
|
|
|
func dockerLinkedConfig(t *testing.T, harness string) {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "harnesses.json")
|
|
config := `{"default":"` + harness + `","profiles":{"` + harness + `":{"executable":"` + harness + `","model":"fixture/model","pipeline_model":"fixture/model","docker_directory":"/checkout"}}}`
|
|
if err := os.WriteFile(path, []byte(config), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PIPELINES_HARNESS_CONFIG", path)
|
|
}
|
|
|
|
func TestProviderCheckDispatchesDockerProfileWithoutHostGeneration(t *testing.T) {
|
|
capture, opencodeMarker := fixtureDocker(t, 0)
|
|
dockerLinkedConfig(t, "opencode")
|
|
var out, stderr bytes.Buffer
|
|
app := cli.App{Out: &out, Err: &stderr, In: strings.NewReader("")}
|
|
err := app.Run(context.Background(), []string{"provider-check", "--model", "fixture/test", "--json", "--yes", "--timeout", "45s"})
|
|
if err != nil {
|
|
t.Fatal(err, stderr.String())
|
|
}
|
|
if !strings.Contains(out.String(), "ses_docker") {
|
|
t.Fatalf("docker stdout not forwarded: %q", out.String())
|
|
}
|
|
if _, statErr := os.Stat(opencodeMarker); !os.IsNotExist(statErr) {
|
|
t.Fatal("host OpenCode generator was invoked for a Docker-linked profile")
|
|
}
|
|
data, err := os.ReadFile(capture)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
args := string(data)
|
|
for _, want := range []string{"compose", "--project-directory", "/checkout", "exec", "-T", "--workdir", "pipelines", "provider-check", "--harness", "opencode", "--model", "fixture/test", "--yes", "--json", "--timeout", "45s"} {
|
|
if !strings.Contains(args, want) {
|
|
t.Fatalf("docker dispatch missing %q in %q", want, args)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProviderCheckDockerPropagatesExitCode(t *testing.T) {
|
|
fixtureDocker(t, 3)
|
|
dockerLinkedConfig(t, "pi")
|
|
var out, stderr bytes.Buffer
|
|
app := cli.App{Out: &out, Err: &stderr, In: strings.NewReader("")}
|
|
err := app.Run(context.Background(), []string{"provider-check", "--harness", "pi", "--model", "fixture/test", "--json", "--yes"})
|
|
var exit *cli.ExitError
|
|
if !errors.As(err, &exit) || exit.ExitCode() != 3 {
|
|
t.Fatalf("exit code not propagated: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestProviderCheckDockerTextModeOmitsJSON pins that a plain-text invocation
|
|
// keeps readable output: the Docker child is not forced into --json.
|
|
func TestProviderCheckDockerTextModeOmitsJSON(t *testing.T) {
|
|
capture, opencodeMarker := fixtureDocker(t, 0)
|
|
dockerLinkedConfig(t, "opencode")
|
|
var out, stderr bytes.Buffer
|
|
app := cli.App{Out: &out, Err: &stderr, In: strings.NewReader("")}
|
|
if err := app.Run(context.Background(), []string{"provider-check", "--model", "fixture/test", "--yes"}); err != nil {
|
|
t.Fatal(err, stderr.String())
|
|
}
|
|
if !strings.Contains(out.String(), "ses_docker") {
|
|
t.Fatalf("docker stdout not forwarded: %q", out.String())
|
|
}
|
|
if _, statErr := os.Stat(opencodeMarker); !os.IsNotExist(statErr) {
|
|
t.Fatal("host OpenCode generator was invoked for a Docker-linked profile")
|
|
}
|
|
data, err := os.ReadFile(capture)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
args := string(data)
|
|
for _, want := range []string{"exec", "-T", "provider-check", "--harness", "opencode", "--model", "fixture/test", "--yes", "--timeout"} {
|
|
if !strings.Contains(args, want) {
|
|
t.Fatalf("docker dispatch missing %q in %q", want, args)
|
|
}
|
|
}
|
|
if strings.Contains(args, "--json") {
|
|
t.Fatalf("text-mode dispatch forced --json: %q", args)
|
|
}
|
|
}
|
|
|
|
func TestProviderCheckNativeProfileKeepsHostGenerator(t *testing.T) {
|
|
root := t.TempDir()
|
|
marker := filepath.Join(root, "opencode-called")
|
|
opencode := filepath.Join(root, "opencode")
|
|
// The native generator runs the host opencode; this fixture echoes a healthy
|
|
// report so the test needs no provider.
|
|
script := "#!/bin/sh\ntouch " + marker + `
|
|
case "$*" in
|
|
*--help*) echo api;;
|
|
*'/api/session/ses_check/prompt'*) echo '{}';;
|
|
*'/api/session/ses_check/message?'*) echo '{"data":[{"finish":"stop","content":[{"type":"text","text":"PIPELINES_PROVIDER_OK"}]}]}';;
|
|
*'/api/session/ses_check/interrupt'*) echo '{}';;
|
|
*'/api/session/active'*) echo '{"data":{}}';;
|
|
*'/api/session'*) echo '{"data":{"id":"ses_check"}}';;
|
|
*) exit 9;;
|
|
esac
|
|
`
|
|
if err := os.WriteFile(opencode, []byte(script), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PIPELINES_OPENCODE", opencode)
|
|
path := filepath.Join(t.TempDir(), "harnesses.json")
|
|
if err := os.WriteFile(path, []byte(`{"default":"opencode","profiles":{"opencode":{"executable":"`+opencode+`","model":"fixture/model","pipeline_model":"fixture/model"}}}`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PIPELINES_HARNESS_CONFIG", path)
|
|
var out, stderr bytes.Buffer
|
|
app := cli.App{Out: &out, Err: &stderr, In: strings.NewReader("")}
|
|
if err := app.Run(context.Background(), []string{"provider-check", "--model", "fixture/test", "--json", "--yes"}); err != nil {
|
|
t.Fatal(err, stderr.String())
|
|
}
|
|
if _, err := os.Stat(marker); err != nil {
|
|
t.Fatal("native profile did not use the host generator:", err)
|
|
}
|
|
if !strings.Contains(out.String(), "ses_check") {
|
|
t.Fatalf("native generator output missing: %q", out.String())
|
|
}
|
|
}
|