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" "git.a-wan.net/alan/pipelines/internal/summarise" "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) } } // TestCLIRunTitlesExplicitDerivedAndRename is the end-to-end title check: it // drives the real CLI against the real websocket server and store, starts one // run with --title and one without, and verifies the title is visible in the // human `runs` list, in `inspect`, and after a `title` rename. Stage execution is // stubbed because the environment must not call a model, so the without-flag run // exercises the server-side prompt fallback rather than the shared summariser. func TestCLIRunTitlesExplicitDerivedAndRename(t *testing.T) { 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-titles.db")) if err != nil { t.Fatal(err) } defer store.Close() if _, err := store.Submit(ctx, "workflow", pipeline.Template([]string{"worker"}, true)); err != nil { t.Fatal(err) } handler := Handler(ctx, store) r := handler.(*runHandler).runner r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) { return stage.Result{Outcome: "success", Output: stage.Values{"summary": "done"}}, nil } 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("")} err := app.Run(ctx, append([]string{"--url", url}, args...)) return out.String(), err } output, err := run("run", "workflow", "first prompt line\nmore detail", "--title", "Operator chosen title", "--yes", "--json", "--wait") if err != nil { t.Fatal(err) } var started protocol.Message if err := json.Unmarshal([]byte(output), &started); err != nil { t.Fatal(err) } explicitID := started.RunDetail.ID if started.RunDetail.Title != "Operator chosen title" || started.RunDetail.TitleSource != summarise.SourceExplicit { t.Fatalf("explicit --title = %+v", started.RunDetail) } output, err = run("run", "workflow", "Derived title from prompt\nsecond line", "--yes", "--json", "--wait") if err != nil { t.Fatal(err) } if err := json.Unmarshal([]byte(output), &started); err != nil { t.Fatal(err) } if started.RunDetail.Title != "Derived title from prompt" || started.RunDetail.TitleSource != summarise.SourceFallback { t.Fatalf("derived title = %+v", started.RunDetail) } output, err = run("runs") if err != nil { t.Fatal(err) } t.Logf("pipelines runs:\n%s", output) for _, want := range []string{ fmt.Sprintf("%d\t%s\t%s\tv1\t%s\n", explicitID, "Operator chosen title", "workflow", "succeeded"), fmt.Sprintf("%d\t%s\t%s\tv1\t%s\n", started.RunDetail.ID, "Derived title from prompt", "workflow", "succeeded"), } { if !strings.Contains(output, want) { t.Fatalf("runs output missing %q:\n%s", want, output) } } output, err = run("inspect", fmt.Sprint(explicitID), "--json") if err != nil { t.Fatal(err) } var detail protocol.RunDetail if err := json.Unmarshal([]byte(output), &detail); err != nil { t.Fatal(err) } if detail.Title != "Operator chosen title" || detail.Name != "workflow" || detail.Version != 1 { t.Fatalf("inspect omitted title/name/version: %+v", detail) } output, err = run("title", fmt.Sprint(explicitID), "Renamed after the fact") if err != nil { t.Fatal(err) } t.Logf("pipelines title: %s", strings.TrimSpace(output)) output, err = run("inspect", fmt.Sprint(explicitID), "--json") if err != nil { t.Fatal(err) } if err := json.Unmarshal([]byte(output), &detail); err != nil { t.Fatal(err) } if detail.Title != "Renamed after the fact" || detail.TitleSource != summarise.SourceExplicit { t.Fatalf("rename not visible in inspect: %+v", detail) } }