Something went wrong. Try again.
A Kubernetes operator that runs Claude Code sessions on long-horizon work
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125package operator_test
import ( "encoding/json" "net/http" "testing" "testing/synctest" "time"
batchv1 "k8s.io/api/batch/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"tangled.org/guid.foo/simmer/api/v1alpha1" "tangled.org/guid.foo/simmer/cluster" "tangled.org/guid.foo/simmer/dataplane" "tangled.org/guid.foo/simmer/internal/world" "tangled.org/guid.foo/simmer/mandate")
// personal is the Mandate personal in telemetry.func personal() *v1alpha1.Mandate { return world.Mandate("telemetry", "personal", "upkeep") }
// dailyBoard is the Duty personal-board of personal, which runs at five every// morning.func dailyBoard() *v1alpha1.Duty { return world.Scheduled(personal(), "personal:board", "0 5 * * *", "America/New_York")}
// board starts the Mandate personal and a Duty of it, with an Account// personal whose five-hour window is at fiveHour. The brake holds when// the whole window is used.func board(t *testing.T, fiveHour float64, duty *v1alpha1.Duty) *running { account := world.Account("personal") account.Status.Usage = world.Reading(fiveHour, 0.01, time.Now().Add(84*time.Hour), time.Now()) return startWith(t, account, personal(), duty)}
// fire stores the Job that the CronJob controller makes from the Duty// personal-board's CronJob when the schedule falls due.func (r *running) fire(t *testing.T) cluster.Job { t.Helper() name := mandate.CronJobName("personal-board") c, ok := world.Get[cluster.CronJob](r.w, cluster.CronJobs, "telemetry", name) if !ok { t.Fatalf("the CronJob %s does not exist", name) } controller := true job := cluster.Job{Job: batchv1.Job{ ObjectMeta: metav1.ObjectMeta{ Name: name + "-29371260", Namespace: "telemetry", Labels: c.Spec.JobTemplate.Labels, OwnerReferences: []metav1.OwnerReference{{APIVersion: "batch/v1", Kind: "CronJob", Name: name, UID: c.UID, Controller: &controller}}, }, Spec: c.Spec.JobTemplate.Spec, }} r.w.Put(cluster.Jobs, &job) synctest.Wait() held, _ := world.Get[cluster.Job](r.w, cluster.Jobs, "telemetry", job.Name) return held}
// startRoute sends the start of the Duty personal-board from a pod of a// Job of its CronJob, as `simmer start` does.func (r *running) startRoute(t *testing.T, job cluster.Job) *http.Response { t.Helper() tokens := reviewer{} pod := r.podOf(t, tokens, job, "x8k2p") return r.call(tokens, http.MethodPost, "/v1/duties/telemetry/personal-board/start", pod).Result()}
func TestAScheduleStartsASessionOfItsDutyThatTakesATurn(t *testing.T) { synctest.Test(t, func(t *testing.T) { r := board(t, 0.1, dailyBoard()) r.pass(t) job := r.fire(t)
got := r.startRoute(t, job) var answer dataplane.StartAnswer decodeErr := json.NewDecoder(got.Body).Decode(&answer) r.pass(t)
if got.StatusCode != http.StatusOK || decodeErr != nil || !answer.Started { t.Errorf("the start route answered %d with %+v, %v; want a start", got.StatusCode, answer, decodeErr) } if s := r.only(t); s.Spec.Duty != "personal-board" || s.Spec.Skill != "personal:board" || s.Status.Phase != v1alpha1.PhaseRunning { t.Errorf("the Session is of the Duty %q and the skill %q, and %s; want personal-board, personal:board, Running", s.Spec.Duty, s.Spec.Skill, s.Status.Phase) } })}
// A Duty that names an McpServer that does not exist is not Ready,// so its schedule's start is refused and starts no Session.func TestAScheduleOfADutyThatIsNotReadyStartsNothing(t *testing.T) { synctest.Test(t, func(t *testing.T) { d := dailyBoard() d.Spec.MCP = []string{"browser"} r := board(t, 0.1, d) r.pass(t) job := r.fire(t)
got := r.startRoute(t, job) r.pass(t)
if sessions := world.List[v1alpha1.Session](r.w, cluster.Sessions); got.StatusCode != http.StatusConflict || len(sessions) != 0 { t.Errorf("the start route answered %d and %d Sessions exist, want 409 and none", got.StatusCode, len(sessions)) } })}
// The start reads no brake. The Session waits as Pending on it, and// the brake asks for a reading of the Account it waits on.func TestAScheduledSessionWaitsForTheBrake(t *testing.T) { synctest.Test(t, func(t *testing.T) { r := board(t, 1, dailyBoard()) r.pass(t) job := r.fire(t)
answer, err := r.o.Start(t.Context(), dataplane.DutyRef{Namespace: "telemetry", Name: "personal-board"}, job.Name) r.pass(t)
if s := r.only(t); err != nil || !answer.Started || s.Status.Phase != v1alpha1.PhasePending || s.Status.WaitingOn != v1alpha1.WaitingOnBrake { t.Errorf("answered %+v, %v, and the Session is %s on %s; want one started, Pending on the brake", answer, err, s.Status.Phase, s.Status.WaitingOn) } })}