package cli import ( "bytes" "context" "encoding/json" "errors" "fmt" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/wstest" ) func TestCLIRunsRmSendsDeleteRuns(t *testing.T) { var got protocol.Message endpoint := wstest.Server(t, func(request protocol.Message) protocol.Message { got = request return protocol.Message{Type: protocol.RunsDeleted, RunsDeleted: &protocol.RunsDeletedResult{ Deleted: []int64{3}, Refused: []protocol.RunDeleteRefusal{{RunID: 4, Reason: "run is running; cancel it or wait for it to finish"}}, FreedBytes: 2048, }} }) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var out, errOut bytes.Buffer app := App{Out: &out, Err: &errOut, In: strings.NewReader("")} err := app.Run(ctx, []string{"--url", endpoint, "runs", "rm", "3", "4", "--yes", "--json"}) var exit *ExitError if !errors.As(err, &exit) || exit.Code != 1 { t.Fatalf("err = %v, want exit 1 for a refusal", err) } if got.Type != protocol.DeleteRuns || fmt.Sprint(got.RunIDs) != "[3 4]" { t.Fatalf("wire request = %+v", got) } var decoded protocol.RunsDeletedResult if err := json.Unmarshal(out.Bytes(), &decoded); err != nil { t.Fatalf("stdout is not JSON: %v\n%s", err, out.String()) } if fmt.Sprint(decoded.Deleted) != "[3]" || decoded.FreedBytes != 2048 || len(decoded.Refused) != 1 { t.Fatalf("decoded = %+v", decoded) } // Without --yes a declined prompt sends nothing. got = protocol.Message{} declined := App{Out: &bytes.Buffer{}, Err: &bytes.Buffer{}, In: strings.NewReader("n\n")} if err := declined.Run(ctx, []string{"--url", endpoint, "runs", "rm", "3"}); err == nil { t.Fatal("declined deletion succeeded") } if got.Type != "" { t.Fatalf("declined deletion sent %+v", got) } if err := declined.Run(ctx, []string{"--url", endpoint, "runs", "rm", "x", "--yes"}); err == nil { t.Fatal("non-numeric id accepted") } }