Files
pipelines/internal/pipeline/archive_test.go
T
alan 06bf580154 Archive pipelines: hide unused ones while keeping their history
archived_at on pipelines (migrated in place). archive_pipeline and
unarchive_pipeline by id or name with an expected-version guard;
list_pipelines hides archived unless include_archived. Archived
pipelines refuse runs, retries, new versions and name reuse, and
cannot be archived while an enabled schedule points at them.
CLI archive/unarchive and list --archived/--all, docs and skill note.
2026-11-04 23:25:55 +00:00

118 lines
4.0 KiB
Go

package pipeline
import (
"context"
"database/sql"
"errors"
"path/filepath"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
_ "modernc.org/sqlite"
)
func TestArchiveHidesReservesAndGuardsSchedules(t *testing.T) {
ctx := context.Background()
s, err := Open(filepath.Join(t.TempDir(), "archive.db"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
if _, err := s.Submit(ctx, "old", Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
if _, err := s.Submit(ctx, "keep", Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
id, err := s.ResolvePipelineID(ctx, 0, "old")
if err != nil {
t.Fatal(err)
}
if byID, err := s.ResolvePipelineID(ctx, 0, "1"); err != nil || byID != id {
t.Fatal("numeric name did not resolve as an ID", byID, err)
}
spec := protocol.SavedSchedule{Name: "nightly", PipelineID: id, Version: 1, Enabled: true, Workspace: t.TempDir(), Input: map[string]any{"prompt": "go"}, Rule: protocol.Schedule{Every: "1h"}}
saved, err := s.SaveSchedule(ctx, spec, time.Now())
if err != nil {
t.Fatal(err)
}
err = s.SetArchived(ctx, id, true, 0, time.Now())
if err == nil || !strings.Contains(err.Error(), `"nightly"`) || !strings.Contains(err.Error(), "pause") {
t.Fatal("archive with an enabled schedule was not refused by name", err)
}
paused := *saved
paused.Enabled = false
if _, err = s.SaveSchedule(ctx, paused, time.Now()); err != nil {
t.Fatal(err)
}
if err = s.SetArchived(ctx, id, true, 7, time.Now()); err == nil || !strings.Contains(err.Error(), "expected latest version 7") {
t.Fatal("stale expected version accepted", err)
}
if err = s.SetArchived(ctx, id, true, 1, time.Now()); err != nil {
t.Fatal(err)
}
if err = s.SetArchived(ctx, id, true, 0, time.Now()); err != nil {
t.Fatal("repeated archive must be a no-op", err)
}
active, _ := s.List(ctx)
if len(active) != 1 || active[0].Name != "keep" {
t.Fatal("archived pipeline listed by default", active)
}
all, _ := s.ListPipelines(ctx, true)
if len(all) != 2 || all[1].Name != "old" || all[1].ArchivedAt == "" || all[0].ArchivedAt != "" {
t.Fatal("include_archived did not mark the archived pipeline", all)
}
detail, err := s.Get(ctx, id, 1)
if err != nil || detail.ArchivedAt == "" || detail.Source == "" {
t.Fatal("archived detail lost history", detail, err)
}
var archived *ArchivedError
if _, err = s.Submit(ctx, "old", Template([]string{"one", "two"}, true), true); !errors.As(err, &archived) || !strings.Contains(err.Error(), "unarchive") {
t.Fatal("create-only reuse of an archived name not refused", err)
}
if _, err = s.Submit(ctx, "old", Template([]string{"one", "two"}, true)); !errors.As(err, &archived) {
t.Fatal("new version of an archived pipeline accepted", err)
}
enabled := paused
enabled.Revision++
enabled.Enabled = true
if _, err = s.SaveSchedule(ctx, enabled, time.Now()); !errors.As(err, &archived) {
t.Fatal("enabled schedule saved for an archived pipeline", err)
}
if err = s.SetArchived(ctx, id, false, 1, time.Now()); err != nil {
t.Fatal(err)
}
if active, _ = s.List(ctx); len(active) != 2 {
t.Fatal("unarchived pipeline still hidden", active)
}
if v, err := s.Submit(ctx, "old", Template([]string{"one", "two"}, true)); err != nil || v != 2 {
t.Fatal("unarchived pipeline cannot publish", v, err)
}
}
func TestArchiveColumnMigratesExistingDatabase(t *testing.T) {
path := filepath.Join(t.TempDir(), "legacy.db")
db, err := sql.Open("sqlite", path)
if err != nil {
t.Fatal(err)
}
if _, err = db.Exec(`CREATE TABLE pipelines (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE, created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP);
INSERT INTO pipelines(name) VALUES ('legacy');`); err != nil {
t.Fatal(err)
}
db.Close()
for range 2 {
s, err := Open(path)
if err != nil {
t.Fatal(err)
}
var stamp sql.NullString
if err = s.db.QueryRow(`SELECT archived_at FROM pipelines WHERE name='legacy'`).Scan(&stamp); err != nil || stamp.Valid {
t.Fatal("legacy pipeline not migrated as active", stamp, err)
}
s.Close()
}
}