package cli_test import ( "context" "encoding/json" "fmt" "path/filepath" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) // seedRun creates one run directly in the store against a fresh submitted // definition, without starting a stage executor, so the wait command is tested // against real runs over the real websocket transport. func seedRun(t *testing.T, store *pipeline.Store, key string) int64 { t.Helper() return seedRunIn(t, store, key, t.TempDir()) } // seedRunIn is seedRun with an explicit recorded workspace, so a test can pin a // path that is not present on this machine. func seedRunIn(t *testing.T, store *pipeline.Store, key, workspace string) int64 { t.Helper() ctx := context.Background() items, err := store.List(ctx) if err != nil { t.Fatal(err) } if len(items) == 0 { if _, err := store.Submit(ctx, "waiting", pipeline.Template([]string{"worker"}, true)); err != nil { t.Fatal(err) } if items, err = store.List(ctx); err != nil { t.Fatal(err) } } detail, err := store.Get(ctx, items[0].ID, 1) if err != nil { t.Fatal(err) } id, _, err := store.CreateRun(ctx, detail, key, workspace, false, nil, map[string]any{"prompt": "wait test"}) if err != nil { t.Fatal(err) } return id } // A run that finished unsuccessfully is still a finished run: one line is // printed for it and the command exits 0. func TestCLIWaitReportsTerminalRunWithoutFailing(t *testing.T) { run, store, _ := setup(t) ctx := context.Background() id := seedRun(t, store, "wait-failed") if err := store.FinishRun(ctx, id, "failed", "error", "boom", map[string]any{}); err != nil { t.Fatal(err) } output, err := run("wait", fmt.Sprint(id), "--timeout", "5s") if err != nil { t.Fatalf("a failed run must not fail the wait: %v", err) } t.Logf("pipelines wait %d:\n%s", id, strings.TrimSpace(output)) line := fmt.Sprintf("run %d · failed (error)", id) if !strings.Contains(output, line) { t.Fatalf("wait output missing %q:\n%s", line, output) } } // The wait blocks while the run is active and returns as soon as it goes // terminal. func TestCLIWaitBlocksUntilTerminal(t *testing.T) { run, store, _ := setup(t) ctx := context.Background() id := seedRun(t, store, "wait-active") go func() { time.Sleep(1200 * time.Millisecond) _ = store.FinishRun(ctx, id, "succeeded", "success", "", map[string]any{}) }() start := time.Now() output, err := run("wait", fmt.Sprint(id), "--timeout", "15s") if err != nil { t.Fatal(err) } if elapsed := time.Since(start); elapsed < time.Second { t.Fatalf("wait returned before the run was terminal: %s", elapsed) } if !strings.Contains(output, fmt.Sprintf("run %d · succeeded (success)", id)) { t.Fatalf("wait output = %q", output) } } // Several ids are waited together and every one is reported. func TestCLIWaitReportsEveryRunInABatch(t *testing.T) { run, store, _ := setup(t) ctx := context.Background() first := seedRun(t, store, "wait-batch-1") second := seedRun(t, store, "wait-batch-2") if err := store.FinishRun(ctx, first, "succeeded", "success", "", map[string]any{}); err != nil { t.Fatal(err) } if err := store.FinishRun(ctx, second, "canceled", "", "canceled", map[string]any{}); err != nil { t.Fatal(err) } output, err := run("wait", fmt.Sprint(first), fmt.Sprint(second), "--timeout", "5s") if err != nil { t.Fatal(err) } for _, want := range []string{ fmt.Sprintf("run %d · succeeded (success)", first), fmt.Sprintf("run %d · canceled", second), } { if !strings.Contains(output, want) { t.Fatalf("batch wait output missing %q:\n%s", want, output) } } } // An unknown run id exits non-zero and does not hang. func TestCLIWaitRefusesUnknownRunWithoutHanging(t *testing.T) { run, _, _ := setup(t) start := time.Now() _, err := run("wait", "999999", "--timeout", "5s") if err == nil || !strings.Contains(err.Error(), "run not found") { t.Fatalf("unknown run not refused: %v", err) } if elapsed := time.Since(start); elapsed > 3*time.Second { t.Fatalf("unknown run hung for %s", elapsed) } } // A timeout says the run is still active and leaves it untouched. func TestCLIWaitTimeoutLeavesRunUntouched(t *testing.T) { run, store, _ := setup(t) ctx := context.Background() id := seedRun(t, store, "wait-timeout") _, err := run("wait", fmt.Sprint(id), "--timeout", "300ms") if err == nil || !strings.Contains(err.Error(), "still active") || !strings.Contains(err.Error(), "not canceled") { t.Fatalf("timeout not reported plainly: %v", err) } detail, err := store.GetRun(ctx, id) if err != nil { t.Fatal(err) } if detail.Status != "queued" || detail.FinishedAt != "" { t.Fatalf("timeout changed the run: %+v", detail.RunSummary) } } // A wait resolves no workspace and is not refused by the workspace guard: it // reports a run whose recorded workspace is not present on this machine, // read-only. func TestCLIWaitNeedsNoWorkspace(t *testing.T) { run, store, _ := setup(t) ctx := context.Background() id := seedRunIn(t, store, "wait-no-workspace", filepath.Join(t.TempDir(), "removed")) if err := store.FinishRun(ctx, id, "succeeded", "success", "", map[string]any{}); err != nil { t.Fatal(err) } output, err := run("wait", fmt.Sprint(id), "--timeout", "5s") if err != nil { t.Fatalf("wait needed the workspace: %v", err) } if !strings.Contains(output, fmt.Sprintf("run %d · succeeded (success)", id)) { t.Fatalf("wait output = %q", output) } } // --json prints the final run envelope instead of the line. func TestCLIWaitJSONPrintsRunEnvelope(t *testing.T) { run, store, _ := setup(t) ctx := context.Background() id := seedRun(t, store, "wait-json") if err := store.FinishRun(ctx, id, "failed", "error", "boom", map[string]any{}); err != nil { t.Fatal(err) } output, err := run("wait", fmt.Sprint(id), "--json", "--timeout", "5s") if err != nil { t.Fatal(err) } var envelope protocol.Message if err := json.Unmarshal([]byte(output), &envelope); err != nil { t.Fatalf("--json output is not an envelope: %v\n%s", err, output) } if envelope.Type != protocol.Run || envelope.RunDetail == nil || envelope.RunDetail.ID != id || envelope.RunDetail.Status != "failed" { t.Fatalf("--json envelope = %+v", envelope) } }