package server import ( "bytes" "context" "encoding/json" "fmt" "net/http/httptest" "path/filepath" "strings" "sync/atomic" "testing" "time" "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" ) func TestArchivePipelineOverCLIKeepsHistoryAndRefusesRuns(t *testing.T) { t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json")) ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "archive.db")) if err != nil { t.Fatal(err) } defer store.Close() if _, err = store.Submit(ctx, "old flow", pipeline.Template([]string{"worker"}, true)); err != nil { t.Fatal(err) } if _, err = store.Submit(ctx, "current", pipeline.Template([]string{"worker"}, true)); err != nil { t.Fatal(err) } handler := Handler(ctx, store) r := handler.(*runHandler).runner // The first run fails so a retry of it can be attempted after archiving. var failing atomic.Bool failing.Store(true) r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) { if failing.Load() { return stage.Result{}, fmt.Errorf("stage failed") } return stage.Result{Outcome: "success", Output: stage.Values{"summary": "done"}}, nil } srv := httptest.NewServer(handler) defer srv.Close() defer func() { cancel(); WaitRuns(handler) }() url := "ws" + strings.TrimPrefix(srv.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 } workspace := t.TempDir() output, err := run("run", "old flow", "before archive", "--yes", "--json", "--workspace", workspace) if err != nil { t.Fatal(output, err) } var started protocol.Message if err = json.Unmarshal([]byte(output), &started); err != nil || started.RunDetail == nil { t.Fatal(output, err) } runID := started.RunDetail.ID if detail := waitTerminal(t, r, runID); detail.Status != "failed" { t.Fatal(detail.Status) } failing.Store(false) items, _ := store.List(ctx) schedule := protocol.SavedSchedule{Name: "nightly", PipelineID: items[1].ID, Version: 1, Enabled: true, Workspace: workspace, Input: map[string]any{"prompt": "go"}, Rule: protocol.Schedule{Every: "24h"}} if items[1].Name != "old flow" { t.Fatal(items) } saved, err := store.SaveSchedule(ctx, schedule, time.Now()) if err != nil { t.Fatal(err) } if output, err = run("archive", "old flow", "--yes"); err == nil || !strings.Contains(err.Error(), `"nightly"`) { t.Fatal("archive with an enabled schedule not refused by name", output, err) } saved.Enabled = false if _, err = store.SaveSchedule(ctx, *saved, time.Now()); err != nil { t.Fatal(err) } if output, err = run("archive", "old flow", "--yes"); err != nil || !strings.Contains(output, "Archived old flow") { t.Fatal(output, err) } if output, _ = run("list"); strings.Contains(output, "old flow") || !strings.Contains(output, "current") { t.Fatal("default list shows archived pipeline", output) } if output, _ = run("list", "--archived"); !strings.Contains(output, "old flow\tv1\t1 stages\tarchived ") || strings.Contains(output, "current") { t.Fatal("list --archived", output) } if output, _ = run("list", "--all"); !strings.Contains(output, "old flow") || !strings.Contains(output, "current") { t.Fatal("list --all", output) } if output, err = run("run", "old flow", "after archive", "--yes", "--json", "--workspace", workspace); err == nil || !strings.Contains(err.Error(), "is archived; unarchive it first") { t.Fatal("archived pipeline started a run", output, err) } if output, err = run("retry", fmt.Sprint(runID), "--yes", "--mode", "restart", "--json"); err == nil || !strings.Contains(err.Error(), "archived") { t.Fatal("archived pipeline retried a run", output, err) } // History stays: the run, its inspection, filtering by name and search. if output, err = run("inspect", fmt.Sprint(runID), "--json"); err != nil || !strings.Contains(output, `"old flow"`) { t.Fatal("archived run inspection", output, err) } if output, err = run("runs", "--pipeline", "old flow"); err != nil || !strings.Contains(output, "old flow") { t.Fatal("archived run history", output, err) } if output, err = run("show", "old flow", "--json"); err != nil || !strings.Contains(output, "archived_at") { t.Fatal("archived show", output, err) } if output, err = run("search", "old flow", "--type", "run"); err != nil || !strings.Contains(output, fmt.Sprintf("run:%d", runID)) { t.Fatal("archived run search", output, err) } if output, err = run("new", "old flow", "--no-open", "--yes"); err == nil || !strings.Contains(err.Error(), "archived") { t.Fatal("archived name reused by new", output, err) } if output, err = run("unarchive", "old flow"); err != nil || !strings.Contains(output, "Unarchived old flow") { t.Fatal(output, err) } if output, err = run("run", "old flow", "after unarchive", "--yes", "--json", "--workspace", workspace); err != nil { t.Fatal("unarchived pipeline cannot run", output, err) } } func TestArchiveProtocolByNameReportsDetail(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "archive-rpc.db")) if err != nil { t.Fatal(err) } defer store.Close() if _, err = store.Submit(ctx, "retire", pipeline.Template([]string{"one"}, true)); err != nil { t.Fatal(err) } handler := HandlerWithOptions(ctx, Options{}, store) defer func() { cancel(); WaitRuns(handler) }() srv := httptest.NewServer(handler) defer srv.Close() var out bytes.Buffer app := cli.App{Out: &out, Err: &out, In: strings.NewReader(`{"type":"archive_pipeline","name":"retire","expected_version":1}`)} if err = app.Run(ctx, []string{"--url", "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect", "rpc", "-"}); err != nil { t.Fatal(out.String(), err) } var reply protocol.Message if err = json.Unmarshal(out.Bytes(), &reply); err != nil || reply.Type != protocol.PipelineArchived || reply.Detail == nil || reply.Detail.ArchivedAt == "" { t.Fatal("archive reply", out.String(), err) } all, _ := store.ListPipelines(ctx, true) if len(all) != 1 || all[0].ArchivedAt == "" { t.Fatal(all) } }