Files
pipelines/internal/server/cli_test.go
T

92 lines
3.1 KiB
Go

package server
import (
"bytes"
"context"
"encoding/json"
"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/stage"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
)
func TestCLIHeadlessRunInspectionCancellationAndRPC(t *testing.T) {
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)
}
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", "--timeout", "100ms"); err == nil || !strings.Contains(err.Error(), "remains active") {
t.Fatal("wait timeout canceled or incorrectly succeeded", err)
}
if _, err = run("cancel", "1", "--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)
}
}