Files
pipelines/internal/server/cli_test.go
alan 05ae530442 Make Pipelines Docker-only with OpenCode and Pi
Install current verified harness releases inside Docker, preserve persistent accounts and settings, and provide separate interactive and pipeline model defaults with terminal onboarding and fuzzy pickers.

Add Docker-aware authoring transport, Pi stage execution, bounded workflow controls, reliable final-result readback and usage settlement, and finite short-lived CLI pipe draining. Keep dated installation evidence and packaged authoring guidance consistent.

Verified with a real Docker installation, both harness stages and interactive authors, the built-in plan/implement/review starter, independent host race/vet checks, installer tests, and packaged skill tests. Generated release receipts stay ignored in clean checkouts.
2026-10-11 23:00:25 +01:00

146 lines
5.4 KiB
Go

package server
import (
"bytes"
"context"
"encoding/json"
"fmt"
"git.a-wan.net/alan/pipelines/internal/buildinfo"
"git.a-wan.net/alan/pipelines/internal/cli"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/runinspect"
"git.a-wan.net/alan/pipelines/internal/stage"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestCLIPingReportsServerBuild(t *testing.T) {
// Keep the host executable authoritative for CLI integration: a developer's
// real Docker-linked harness profile must never influence an ephemeral test
// endpoint. A missing native fixture matches an unconfigured setup.
t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json"))
srv := httptest.NewServer(Handler(context.Background()))
defer srv.Close()
endpoint := "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect"
for _, jsonMode := range []bool{false, true} {
var out bytes.Buffer
app := cli.App{Out: &out, Err: &out, In: strings.NewReader("")}
args := []string{"--url", endpoint, "ping"}
if jsonMode {
args = append(args, "--json")
}
if err := app.Run(context.Background(), args); err != nil {
t.Fatal(err)
}
build := buildinfo.Current()
if !strings.Contains(out.String(), build.Version) || !strings.Contains(out.String(), build.Commit) {
t.Fatal("ping omitted server version", out.String())
}
}
}
func TestCLIHeadlessRunInspectionCancellationAndRPC(t *testing.T) {
// Isolate harness configuration: this test's debug query builds a CLI prefix
// locally, so a developer's real Docker-linked profile would fail closed
// against this ephemeral httptest endpoint ("refusing to redirect"). A
// missing native fixture keeps the host executable authoritative, matching
// the isolation already used by internal/cli's setup.
t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json"))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "cli-runs.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
store.Submit(ctx, "workflow", pipeline.Template([]string{"worker"}, true))
handler := Handler(ctx, store)
r := handler.(*runHandler).runner
r.execute = func(ctx context.Context, _ stage.Definition, input stage.Values) (stage.Result, error) {
<-ctx.Done()
return stage.Result{}, ctx.Err()
}
http := httptest.NewServer(handler)
defer http.Close()
defer func() { cancel(); WaitRuns(handler) }()
url := "ws" + strings.TrimPrefix(http.URL, "http") + "/connect"
run := func(args ...string) (string, error) {
var out bytes.Buffer
app := cli.App{Out: &out, Err: &out, In: strings.NewReader(`{"type":"list_templates"}`)}
err := app.Run(ctx, append([]string{"--url", url}, args...))
return out.String(), err
}
output, err := run("run", "workflow", "exact task", "--yes", "--json", "--request-key", "same-key")
if err != nil {
t.Fatal(err)
}
var response protocol.Message
json.Unmarshal([]byte(output), &response)
id := response.RunDetail.ID
if response.RunDetail.Input["prompt"] != "exact task" || response.RunDetail.AllowTools {
t.Fatal("prompt or approval changed", response)
}
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
cwd, err = filepath.EvalSymlinks(cwd)
if err != nil {
t.Fatal(err)
}
if response.RunDetail.Workspace != cwd || response.RunDetail.Input["pwd"] != cwd {
t.Fatal("CLI did not use invocation directory", response.RunDetail.Workspace, cwd)
}
debugOutput, err := run("debug", fmt.Sprint(id), "--json")
if err != nil {
t.Fatal("CLI debug query failed", err)
}
var investigation runinspect.Debug
if err := json.Unmarshal([]byte(debugOutput), &investigation); err != nil || investigation.RunID != id || investigation.Workspace != cwd || !strings.Contains(investigation.Prompt, "inspect "+fmt.Sprint(id)+" --json") {
t.Fatal("debug did not retrieve active run context", investigation, err)
}
output, err = run("run", "workflow", "exact task", "--yes", "--json", "--request-key", "same-key")
if err != nil {
t.Fatal(err)
}
json.Unmarshal([]byte(output), &response)
if response.RunDetail.ID != id {
t.Fatal("idempotency lost")
}
if _, err = run("inspect", "1", "--json"); err != nil {
t.Fatal(err)
}
if _, err = run("runs", "--pipeline", "workflow", "--json"); err != nil {
t.Fatal(err)
}
if _, err = run("run", "workflow", "exact task", "--yes", "--json", "--request-key", "same-key", "--wait", "--wait-timeout", "100ms"); err == nil || !strings.Contains(err.Error(), "remains active") {
t.Fatal("wait timeout canceled or incorrectly succeeded", err)
}
if _, err = run("cancel", "1", "--yes", "--json"); err != nil {
t.Fatal(err)
}
r.wg.Wait()
detail, err := store.GetRun(ctx, id)
if err != nil || detail.Status != "canceled" {
t.Fatal(detail, err)
}
if output, err = run("rpc", "-"); err != nil || !strings.Contains(output, "templates") {
t.Fatal(output, err)
}
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
return stage.Result{Outcome: "success", Output: stage.Values{"summary": "done"}}, nil
}
output, err = run("run", "workflow", "second task", "--yes", "--json", "--wait", "--timeout", "3s")
if err != nil {
t.Fatal(err)
}
json.Unmarshal([]byte(output), &response)
if response.RunDetail.Status != "succeeded" {
t.Fatal("wait lost terminal state", output)
}
}