ARTEX/server/task_planning_test.go
BaaRai c54f7045bb fix(planner): bound retries with backoff, expose state, resume planning, and migrate 0021 (#676)
- B1: count retrying rounds toward MaxPlanningFailures with exponential backoff
- B2: exclude planner-authored writes from fingerprint; capture start snapshot
  and include non-agent task_scope and linked assets
- B3: expose planner recovery state in task API DTO and task detail UI
- B4: clear recovery state again when Engine.Resume lands mid-round and add
  dedicated resume_planning control action + UI button
- B5: skip stale time.Ticker heartbeats fired during a long planning round
- B6: move planner_progress DDL from db/schema.sql to migration 0021
  (0020 reserved for security batch chat_type backfill)
- i18n: add matching vi.json translations alongside tr.json and zh-CN.json
- tests: initialize secrets-at-rest key in TestPlanningCredentialUpdateReopensPlanning
2026-11-30 03:16:35 +01:00

70 lines
2.6 KiB
Go

package server
import (
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/Autumn-27/artex/db"
)
// GET /api/tasks/{id} carries the task's planner_progress row as planning and
// leaves it out when there is none; the task list does not carry it. Needs
// PostgreSQL (ARTEX_PG_DSN); skipped without it.
func TestTaskDetailPlanningState(t *testing.T) {
g := newGateServer(t)
task := g.newTask(t, db.TaskCreateOptions{})
ctx := t.Context()
path := "/api/tasks/" + task.ID
code, body := g.do(t, "GET", path, "")
if _, ok := body["planning"]; code != 200 || ok {
t.Fatalf("no planner_progress row: %d planning=%v, want 200 without planning", code, body["planning"])
}
snap, err := task.Store.PlanningSnapshot(ctx)
if err != nil {
t.Fatal(err)
}
failedAt := time.Now().Truncate(time.Second)
if err := task.Store.RecordPlanningRetry(ctx, snap, "synthetic provider outage", failedAt); err != nil {
t.Fatal(err)
}
code, body = g.do(t, "GET", path, "")
planning, ok := body["planning"].(map[string]any)
if code != 200 || !ok {
t.Fatalf("retrying row: %d %v, want a planning object", code, body["planning"])
}
next, _ := planning["next_check_at"].(string)
at, err := time.Parse(time.RFC3339, next)
if planning["state"] != "retrying" || planning["reason"] != "synthetic provider outage" || planning["failures"] != float64(1) ||
err != nil || !at.Equal(failedAt.Add(30*time.Second)) {
t.Fatalf("planning=%v, want retrying, the reason, failures 1 and next_check_at 30 s after the failure", planning)
}
// needs_attention waits for new input or a Resume: next_check_at is null.
if err := task.Store.SavePlanningProgress(ctx, snap, "needs_attention", "provider refused the request", nil); err != nil {
t.Fatal(err)
}
_, body = g.do(t, "GET", path, "")
planning, ok = body["planning"].(map[string]any)
if v, present := planning["next_check_at"]; !ok || planning["state"] != "needs_attention" || !present || v != nil {
t.Fatalf("needs_attention: planning=%v, want next_check_at null", body["planning"])
}
req := httptest.NewRequest("GET", "/api/tasks", nil)
req.Header.Set("Authorization", "Bearer "+g.token)
rec := httptest.NewRecorder()
g.s.Handler().ServeHTTP(rec, req)
if rec.Code != 200 || strings.Contains(rec.Body.String(), `"planning"`) {
t.Fatalf("task list: %d, carries planning=%v; the list endpoint is unchanged", rec.Code, strings.Contains(rec.Body.String(), `"planning"`))
}
if err := task.Store.ResetPlanningProgress(ctx); err != nil {
t.Fatal(err)
}
if _, body = g.do(t, "GET", path, ""); body["planning"] != nil {
t.Fatalf("after a reset: planning=%v, want it left out", body["planning"])
}
}