152 lines
4.8 KiB
Go
152 lines
4.8 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/stage"
|
|
)
|
|
|
|
func TestLegacyScheduleBindsExecutionDefaultNotOfflineOpenDirectory(t *testing.T) {
|
|
ctx := context.Background()
|
|
path := filepath.Join(t.TempDir(), "legacy.db")
|
|
seed, err := pipeline.Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = seed.Submit(ctx, "legacy research", pipeline.Template([]string{"one"}, true)); err != nil {
|
|
seed.Close()
|
|
t.Fatal(err)
|
|
}
|
|
items, err := seed.List(ctx)
|
|
if err != nil {
|
|
seed.Close()
|
|
t.Fatal(err)
|
|
}
|
|
pid := items[0].ID
|
|
seed.Close()
|
|
// Construct an old published schedule and its exact persisted cadence in a
|
|
// scratch DB. New publication APIs intentionally cannot create this source.
|
|
source := "# settings\n```schedule\nevery=15m\n```\n```prompt\nLegacy research\n```\n" + pipeline.Template([]string{"one"}, true)
|
|
graph, err := pipeline.Parse(source)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
encoded, err := json.Marshal(graph)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(source)))
|
|
due := time.Now().Add(15 * time.Minute).UTC()
|
|
db, err := sql.Open("sqlite", path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = db.Exec(`UPDATE pipeline_versions SET source=?,graph_json=?,content_hash=? WHERE pipeline_id=? AND version=1`, source, string(encoded), hash, pid); err != nil {
|
|
db.Close()
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = db.Exec(`INSERT INTO schedule_state(pipeline_id,version,slot,next_due) VALUES(?,1,0,?)`, pid, due.Format(time.RFC3339Nano)); err != nil {
|
|
db.Close()
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = db.Exec(`DELETE FROM schedule_migrations`); err != nil {
|
|
db.Close()
|
|
t.Fatal(err)
|
|
}
|
|
db.Close()
|
|
|
|
// An offline tool is allowed to open/migrate the database from arbitrary A.
|
|
a, b, c := t.TempDir(), t.TempDir(), t.TempDir()
|
|
t.Chdir(a)
|
|
store, err := pipeline.Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
entries, err := store.ListSchedules(ctx, pid)
|
|
if err != nil || len(entries) != 1 || entries[0].Workspace != "" || entries[0].NextDue != due.Format(time.RFC3339Nano) {
|
|
t.Fatal("offline open captured an untrusted default", entries, err)
|
|
}
|
|
original := entries[0]
|
|
// Runtime B, not Store.Open's A, is the only trusted old default.
|
|
t.Chdir(b)
|
|
resolvedB, err := pipeline.ResolveRunWorkspace(b, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runCtx, cancel := context.WithCancel(ctx)
|
|
r, err := newRunner(runCtx, store)
|
|
if err != nil {
|
|
cancel()
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { cancel(); r.schedulerWG.Wait(); r.wg.Wait() }()
|
|
entries, err = store.ListSchedules(ctx, pid)
|
|
if err != nil || len(entries) != 1 || entries[0].Workspace != resolvedB || entries[0].ID != original.ID || entries[0].Revision != original.Revision || entries[0].NextDue != original.NextDue || entries[0].Input["prompt"] != original.Input["prompt"] {
|
|
t.Fatal("runtime did not bind only the missing folder", entries, err)
|
|
}
|
|
bound := entries[0]
|
|
bound.Workspace = ""
|
|
beforeJSON, _ := json.Marshal(original)
|
|
afterJSON, _ := json.Marshal(bound)
|
|
if string(beforeJSON) != string(afterJSON) {
|
|
t.Fatal("binding changed other saved information", original, bound)
|
|
}
|
|
detail, err := store.Get(ctx, pid, 1)
|
|
if err != nil || detail.Source != source || detail.ContentHash != hash {
|
|
t.Fatal("binding changed immutable history", detail, err)
|
|
}
|
|
// Keep even test execution explicitly silent; the inherited workspace and
|
|
// cadence stay unchanged by this ordinary run-information edit.
|
|
entry := entries[0]
|
|
entry.NotifyMode = "off"
|
|
if _, err = store.SaveSchedule(ctx, entry, time.Now()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.execute = func(_ context.Context, _ stage.Definition, input stage.Values) (stage.Result, error) {
|
|
if input["pwd"] != resolvedB {
|
|
t.Error("dispatch used an offline/client cwd", input["pwd"])
|
|
}
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"done": true}}, nil
|
|
}
|
|
if err = r.scheduleTick(due, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.wg.Wait()
|
|
runs, err := store.ListRuns(ctx)
|
|
if err != nil || len(runs) != 1 || runs[0].Workspace != resolvedB || !runs[0].Scheduled || runs[0].ScheduleID != original.ID {
|
|
t.Fatal(runs, err)
|
|
}
|
|
cancel()
|
|
r.schedulerWG.Wait()
|
|
r.wg.Wait()
|
|
store.Close()
|
|
|
|
// Restart from a different runtime C must not rebind explicit B.
|
|
t.Chdir(c)
|
|
reopened, err := pipeline.Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer reopened.Close()
|
|
ctx2, cancel2 := context.WithCancel(ctx)
|
|
defer cancel2()
|
|
r2, err := newRunner(ctx2, reopened)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { cancel2(); r2.schedulerWG.Wait(); r2.wg.Wait() }()
|
|
entries, err = reopened.ListSchedules(ctx, pid)
|
|
if err != nil || len(entries) != 1 || entries[0].ID != original.ID || entries[0].Workspace != resolvedB {
|
|
t.Fatal("restart rebound a saved default", entries, err)
|
|
}
|
|
}
|