ARTEX/db/planner_progress_test.go

84 lines
2.8 KiB
Go

package db
import (
"testing"
"time"
)
func TestPlanningProgressWaitValidationAndRestart(t *testing.T) {
d, err := Open(testDSN(t))
if err != nil {
t.Fatal(err)
}
defer d.Close()
task, err := d.CreateTask("isolated wait validation", "unfinished comparison", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
defer d.DeleteTask(task.ID)
s := d.Exploration(task.ExplorationID)
if _, err = s.AddGoal(map[string]any{"text": "unfinished comparison"}, "fixture"); err != nil {
t.Fatal(err)
}
now := time.Now()
due := now.Add(time.Hour)
intent, err := s.AddIntent(map[string]any{"summary": "future observation", "not_before": due.Format(time.RFC3339)}, 0, nil, "fixture")
if err != nil {
t.Fatal(err)
}
next, err := s.ValidatePlanningWait(t.Context(), []int64{intent}, "requires future observation", now)
if err != nil || !next.After(now) {
t.Fatal(next, err)
}
for _, ids := range [][]int64{nil, {intent, intent}, {-1}, {999999999}} {
if _, err = s.ValidatePlanningWait(t.Context(), ids, "waiting", now); err == nil {
t.Fatal("accepted invalid references", ids)
}
}
if _, err = s.ValidatePlanningWait(t.Context(), []int64{intent}, "waiting", due.Add(time.Second)); err == nil {
t.Fatal("due work cannot justify waiting")
}
_, err = d.Exec(`UPDATE exploration_nodes SET payload=jsonb_set(payload,'{not_before_unix}',to_jsonb((payload->>'not_before_unix')::text)) WHERE id=$1`, intent)
if err != nil {
t.Fatal(err)
}
if _, err = s.ValidatePlanningWait(t.Context(), []int64{intent}, "waiting", now); err == nil {
t.Fatal("string encoded due time cannot be claimed and must not justify waiting")
}
snap, err := s.PlanningSnapshot(t.Context())
if err != nil {
t.Fatal(err)
}
for range 3 {
if err = s.SavePlanningProgress(t.Context(), snap, "stalled", "empty", &next); err != nil {
t.Fatal(err)
}
}
reopened := d.Exploration(task.ExplorationID)
status, err := reopened.PlanningProgress(t.Context())
if err != nil || status.State != "needs_attention" || status.Failures != 3 {
t.Fatal(status, err)
}
if _, err = reopened.AppendActivity(Activity{Worker: "planner", Kind: "text", Summary: "another empty response"}); err != nil {
t.Fatal(err)
}
allowed, err := reopened.PlanningAllowed(t.Context(), now.Add(24*time.Hour))
if err != nil || allowed {
t.Fatal("activity must not reset recovery", allowed, err)
}
if err = reopened.UpdateGoalPayload(mustPlanningGoal(t, d, task.ExplorationID), "updated user goal", ""); err != nil {
t.Fatal(err)
}
allowed, err = reopened.PlanningAllowed(t.Context(), now)
if err != nil || !allowed {
t.Fatal("goal edit must reopen recovery", allowed, err)
}
}
func mustPlanningGoal(t *testing.T, d *DB, exp int64) int64 {
t.Helper()
var id int64
if err := d.QueryRow(`SELECT id FROM exploration_nodes WHERE exploration_id=$1 AND kind='goal'`, exp).Scan(&id); err != nil {
t.Fatal(err)
}
return id
}