package thin import ( "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // A pinned Run now (protocol 29, paused work B1): the person was told it // would run on one connection. Both Claude accounts are over the normal // threshold (the step holds), and with leave both admit; unpinned leave // takes the roomier claude-a. Pinned to claude-b it runs on claude-b, so // a runner that ignored the pin fails here (re-review nit). func TestSchedulePinnedReleaseRunsOnlyThere(t *testing.T) { r := newSchedRig(t, "claude-a", "claude-b") r.tensorx(bigBudget) r.reading("claude-a", 0.82, time.Now().Add(9*time.Hour)) r.reading("claude-b", 0.9, time.Now().Add(9*time.Hour)) r.start() r.dispatch("p1", wire.PriorityNormal) r.until(5*time.Second, "the hold", func() bool { return len(r.reports("p1")) > 0 }) if kept, err := r.state.Release(r.ctx, "fr-p1", wire.ReleaseCommand{ID: "rl_p1", Person: connPerson, TaskID: "tk-p1", StepID: "p1", StepRevision: 1, Connection: "claude-b"}); err != nil || !kept { t.Fatalf("release %v %v", kept, err) } r.steps.MeteredChanged() r.until(5*time.Second, "the released step", func() bool { _, ok := r.final("p1"); return ok }) if on := r.ranOn("p1", 1); on != "claude-b" { t.Fatalf("a release pinned to claude-b ran on %q", on) } } // Only a metered connection is free: both subscriptions are full and // TensorX has budget. A release pinned to a subscription keeps holding; // unpinned leave would bill TensorX. func TestSchedulePinnedReleaseNeverBillsMetered(t *testing.T) { r := newSchedRig(t, "claude-a", "claude-b") r.tensorx(bigBudget) r.reading("claude-a", 1.0, time.Now().Add(9*time.Hour)) r.reading("claude-b", 1.0, time.Now().Add(9*time.Hour)) r.start() r.dispatch("p4", wire.PriorityNormal) r.until(5*time.Second, "the hold", func() bool { return len(r.reports("p4")) > 0 }) if _, err := r.state.Release(r.ctx, "fr-p4", wire.ReleaseCommand{ID: "rl_p4", Person: connPerson, TaskID: "tk-p4", StepID: "p4", StepRevision: 1, Connection: "claude-b"}); err != nil { t.Fatal(err) } r.steps.MeteredChanged() time.Sleep(300 * time.Millisecond) if _, done := r.final("p4"); done || r.ranOn("p4", 1) != "" { t.Fatalf("a release pinned to a full subscription ran on %q", r.ranOn("p4", 1)) } } // The reading was stale: the pinned connection filled before the runner // decided. The step keeps holding, on its own connection's reset, and // TensorX is never billed in its place; a failover inside a pinned grant // holds too rather than moving to a metered connection. func TestSchedulePinnedReleaseNeverFallsBack(t *testing.T) { r := newSchedRig(t, "claude-a", "claude-b") r.tensorx(bigBudget) r.reading("claude-a", 0.85, time.Now().Add(9*time.Hour)) r.reading("claude-b", 1.0, time.Now().Add(9*time.Hour)) r.start() r.dispatch("p2", wire.PriorityNormal) r.until(5*time.Second, "the hold", func() bool { return len(r.reports("p2")) > 0 }) if _, err := r.state.Release(r.ctx, "fr-p2", wire.ReleaseCommand{ID: "rl_p2", Person: connPerson, TaskID: "tk-p2", StepID: "p2", StepRevision: 1, Connection: "claude-b"}); err != nil { t.Fatal(err) } r.steps.MeteredChanged() time.Sleep(300 * time.Millisecond) if _, done := r.final("p2"); done || r.ranOn("p2", 1) != "" { t.Fatalf("a release pinned to a full Claude account ran on %q", r.ranOn("p2", 1)) } var state string r.state.db.QueryRow("SELECT state FROM releases WHERE id='rl_p2'").Scan(&state) if state != releasePending { t.Fatalf("the pinned release is %q, not still pending", state) } // A failover inside a pinned grant: Claude refuses on quota; the next // attempt may not go to TensorX. r.fail["claude-a"] = true r.dispatch("p3", wire.PriorityNormal) r.until(5*time.Second, "the third hold", func() bool { return len(r.reports("p3")) > 0 }) if _, err := r.state.Release(r.ctx, "fr-p3", wire.ReleaseCommand{ID: "rl_p3", Person: connPerson, TaskID: "tk-p3", StepID: "p3", StepRevision: 1, Connection: "claude-a"}); err != nil { t.Fatal(err) } r.steps.MeteredChanged() r.until(5*time.Second, "the first attempt", func() bool { return r.ranOn("p3", 1) == "claude-a" }) time.Sleep(300 * time.Millisecond) if on := r.ranOn("p3", 2); on == piLocal { t.Fatal("a failover inside a pinned grant billed TensorX") } }