package main
import (
"testing"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/ci"
"craftdesign.group/quarry/internal/oltp"
)
// A run nothing has claimed reads as waiting for a runner, with since
// when, and reads as running the moment a step starts. Derived from the
// engine's steps on every poll; the stored row is never touched.
func TestAnUnclaimedRunIsDerivedNotStored(t *testing.T) {
const id = "run-unclaimed-1"
row := oltp.CIRun{RunID: id, Status: "running", Started: "2026-01-01T10:00:00Z"}
t.Cleanup(func() { noteUnclaimed(id, ci.Run{Status: "completed", CompletedAt: "x"}, "") })
queued := ci.Run{Status: "running", CreatedAt: "2026-01-01T10:00:05Z",
Steps: map[string]ci.Step{"build": {Status: "queued"}}}
noteUnclaimed(id, queued, row.Started)
got := api.CIRun{Status: row.Status}
markUnclaimed(row, &got)
if got.Status != api.RunUnclaimed || got.WaitingSince != "2026-01-01T10:00:05Z" {
t.Errorf("FAIL: a queued run reads %q since %q, want %q since its creation", got.Status, got.WaitingSince, api.RunUnclaimed)
}
if row.Status != "running" {
t.Errorf("FAIL: the stored row became %q; the status is derived, never stored", row.Status)
}
started := ci.Run{Status: "running", Steps: map[string]ci.Step{"build": {Status: "running", StartedAt: "2026-01-01T10:01:00Z"}}}
noteUnclaimed(id, started, row.Started)
got = api.CIRun{Status: row.Status}
markUnclaimed(row, &got)
if got.Status != "running" || got.WaitingSince != "" {
t.Errorf("FAIL: a run with a step running reads %q since %q, want plain running", got.Status, got.WaitingSince)
}
// A finished row is never relabelled, whatever the table last held.
noteUnclaimed(id, queued, row.Started)
done := oltp.CIRun{RunID: id, Status: "failed"}
got = api.CIRun{Status: done.Status}
markUnclaimed(done, &got)
if got.Status != "failed" {
t.Errorf("FAIL: a failed run reads %q", got.Status)
}
if !t.Failed() {
t.Log("ok")
}
}
// toAPIRuns is where every reader's runs come from.
func TestRunsReadUnclaimedThroughToAPIRuns(t *testing.T) {
const id = "run-unclaimed-2"
t.Cleanup(func() { noteUnclaimed(id, ci.Run{Status: "completed", CompletedAt: "x"}, "") })
noteUnclaimed(id, ci.Run{Status: "running", CreatedAt: "2026-01-01T10:00:05Z",
Steps: map[string]ci.Step{"a": {Status: "queued"}}}, "")
runs := toAPIRuns([]oltp.CIRun{{RunID: id, Status: "running"}, {RunID: "other", Status: "running"}})
if runs[0].Status != api.RunUnclaimed || runs[1].Status != "running" {
t.Errorf("FAIL: statuses %q and %q, want the queued run unclaimed and the other running", runs[0].Status, runs[1].Status)
}
if !t.Failed() {
t.Log("ok")
}
}
// A run between two steps is waiting for its next dispatch, not for a
// runner; and the table forgets runs the poller no longer holds.
func TestUnclaimedIsOnlyForRunsThatNeverStartedAndIsForgotten(t *testing.T) {
const id = "run-unclaimed-3"
between := ci.Run{Status: "running", Steps: map[string]ci.Step{
"a": {Status: "completed", StartedAt: "2026-01-01T10:00:00Z", CompletedAt: "2026-01-01T10:01:00Z"},
"b": {Status: "queued"}}}
noteUnclaimed(id, between, "")
if _, ok := unclaimedSince(id); ok {
t.Errorf("FAIL: a run between steps reads unclaimed")
}
noteUnclaimed(id, ci.Run{Status: "running", Steps: map[string]ci.Step{"a": {Status: "queued"}}}, "2026-01-01T10:00:00Z")
if _, ok := unclaimedSince(id); !ok {
t.Fatalf("FAIL: a never-started run is not unclaimed")
}
forgetUnclaimedExcept(map[string]bool{})
unclaimedRuns.mu.Lock()
n := len(unclaimedRuns.since)
unclaimedRuns.mu.Unlock()
if n != 0 {
t.Errorf("FAIL: %d entries left after the run left the unfinished set", n)
}
if !t.Failed() {
t.Log("ok")
}
}