test(backend): catch active-session state drift between SQL and Go (#3236)

Co-authored-by: nova28 <17953305+nova28@users.noreply.github.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
This commit is contained in:
Henry
2026-09-01 15:20:06 +00:00
committed by GitHub
parent 5a3d1bf63a
commit 033244af5d
4 changed files with 148 additions and 0 deletions
@@ -1458,6 +1458,25 @@ const (
TaskSessionStateCancelled TaskSessionState = "CANCELLED"
)
// AllTaskSessionStates is the canonical, exhaustive list of TaskSessionState
// constants. Code that must cover every state (drift-guard tests, admin
// tooling) should range over this slice instead of hand-writing its own
// literal: Go does not enforce switch/slice exhaustiveness (the exhaustive
// linter is not enabled in this repo), so a hand-written literal silently
// stops covering new states the moment one is added here. Add a new
// TaskSessionState constant to this slice in the same change that adds the
// const.
var AllTaskSessionStates = []TaskSessionState{
TaskSessionStateCreated,
TaskSessionStateStarting,
TaskSessionStateRunning,
TaskSessionStateIdle,
TaskSessionStateWaitingForInput,
TaskSessionStateCompleted,
TaskSessionStateFailed,
TaskSessionStateCancelled,
}
// SessionBranchInfo is a lightweight projection of a session with its worktree branch.
// Used by the PR watch reconciler to find sessions that may need PR watches.
type SessionBranchInfo struct {
@@ -0,0 +1,22 @@
package models
// IsTaskLookupActiveSessionState reports whether a session belongs to the active
// set used by GetActiveTaskSessionByTaskID. The active states are Created,
// Starting, Running, and WaitingForInput. TestActiveSessionStateMatchesSQLFilter
// cross-checks this predicate against the SQL filter for every state in
// AllTaskSessionStates.
//
// This is a different set from its neighbour IsResumableSessionState: active
// INCLUDES Created (a session not yet picked up still counts as the task's
// active one) and EXCLUDES Idle (an office session between turns is resumable
// but not the task's currently active session); resumable is the exact inverse
// on both points.
func IsTaskLookupActiveSessionState(state TaskSessionState) bool {
switch state {
case TaskSessionStateCreated, TaskSessionStateStarting,
TaskSessionStateRunning, TaskSessionStateWaitingForInput:
return true
default:
return false
}
}
@@ -0,0 +1,50 @@
package models
import "testing"
// TestIsTaskLookupActiveSessionState verifies the active-state contract used by
// GetActiveTaskSessionByTaskID.
func TestIsTaskLookupActiveSessionState(t *testing.T) {
want := map[TaskSessionState]bool{
TaskSessionStateCreated: true,
TaskSessionStateStarting: true,
TaskSessionStateRunning: true,
TaskSessionStateIdle: false,
TaskSessionStateWaitingForInput: true,
TaskSessionStateCompleted: false,
TaskSessionStateFailed: false,
TaskSessionStateCancelled: false,
}
// AllTaskSessionStates is the canonical list this test ranges over. The checks
// below assert it and want cover exactly the same set, with no duplicates — so
// extending AllTaskSessionStates without extending want, or a duplicated/dropped
// entry silently shrinking coverage, fails here. What this CANNOT catch: a new
// TaskSessionState constant added to models.go but never added to
// AllTaskSessionStates. Go does not enforce exhaustiveness and the exhaustive
// linter is not enabled in this repo.
seen := make(map[TaskSessionState]bool, len(AllTaskSessionStates))
for _, state := range AllTaskSessionStates {
if seen[state] {
t.Fatalf("AllTaskSessionStates contains duplicate %q; coverage silently shrinks", state)
}
seen[state] = true
if _, ok := want[state]; !ok {
t.Fatalf("no expectation recorded for state %q; add one to this test's want map", state)
}
}
for state := range want {
if !seen[state] {
t.Fatalf("want has %q but AllTaskSessionStates does not; the canonical list lost a state", state)
}
}
for _, state := range AllTaskSessionStates {
t.Run(string(state), func(t *testing.T) {
wantActive := want[state]
if got := IsTaskLookupActiveSessionState(state); got != wantActive {
t.Errorf("IsTaskLookupActiveSessionState(%q) = %v, want %v", state, got, wantActive)
}
})
}
}
@@ -0,0 +1,57 @@
package sqlite
import (
"context"
"errors"
"testing"
"github.com/kandev/kandev/internal/task/models"
)
// TestActiveSessionStateMatchesSQLFilter cross-checks GetActiveTaskSessionByTaskID's
// SQL state filter against models.IsTaskLookupActiveSessionState for every state in
// models.AllTaskSessionStates, the package's single canonical state list. Editing
// the SQL filter without updating the predicate (or vice versa) fails this test
// instead of only a stale comment — but only for states already present in
// AllTaskSessionStates: Go does not enforce switch/slice exhaustiveness, so a new
// TaskSessionState constant added to models.go without also being added to
// AllTaskSessionStates is not caught by this test (or by any linter — the
// exhaustive linter is not enabled in this repo).
func TestActiveSessionStateMatchesSQLFilter(t *testing.T) {
for _, state := range models.AllTaskSessionStates {
t.Run(string(state), func(t *testing.T) {
repo := newRepoForSessionTests(t)
ctx := context.Background()
taskID := "task-active-state-" + string(state)
sessionID := "session-active-state-" + string(state)
if err := repo.CreateTask(ctx, &models.Task{ID: taskID, Title: "Active state check"}); err != nil {
t.Fatalf("CreateTask: %v", err)
}
if err := repo.CreateTaskSession(ctx, &models.TaskSession{
ID: sessionID,
TaskID: taskID,
State: state,
}); err != nil {
t.Fatalf("CreateTaskSession: %v", err)
}
got, err := repo.GetActiveTaskSessionByTaskID(ctx, taskID)
wantActive := models.IsTaskLookupActiveSessionState(state)
if wantActive {
if err != nil {
t.Fatalf("GetActiveTaskSessionByTaskID(%q) = %v, want session (state %q is active)", taskID, err, state)
}
if got == nil || got.ID != sessionID {
t.Fatalf("GetActiveTaskSessionByTaskID(%q) returned wrong session: %+v", taskID, got)
}
return
}
if !errors.Is(err, models.ErrTaskSessionNotFound) {
t.Fatalf("GetActiveTaskSessionByTaskID(%q) = (%v, %v), want ErrTaskSessionNotFound (state %q is not active)", taskID, got, err, state)
}
})
}
}