Files
descrybe/apps/api/internal/jobs/heartbeat_test.go
T
greeneclipse 8580c996c3 Initial commit of Descrybe v2 without local scratch artifacts.
Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
2026-08-09 22:47:43 +02:00

95 lines
2.3 KiB
Go

package jobs_test
import (
"context"
"errors"
"testing"
"time"
"github.com/descrybe/descrybe-v2/apps/api/internal/jobs"
"github.com/jackc/pgx/v5"
)
type stubRow struct {
scan func(dest ...any) error
}
func (r stubRow) Scan(dest ...any) error {
if r.scan == nil {
return pgx.ErrNoRows
}
return r.scan(dest...)
}
type stubQuerier struct {
pending int64
pendingErr error
lastSeen time.Time
seenErr error
calls int
}
func (q *stubQuerier) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
q.calls++
if q.calls == 1 {
return stubRow{scan: func(dest ...any) error {
if q.pendingErr != nil {
return q.pendingErr
}
*(dest[0].(*int64)) = q.pending
return nil
}}
}
return stubRow{scan: func(dest ...any) error {
if q.seenErr != nil {
return q.seenErr
}
*(dest[0].(*time.Time)) = q.lastSeen
return nil
}}
}
func TestProbeWorkerReadinessOK(t *testing.T) {
t.Parallel()
q := &stubQuerier{pending: 3, lastSeen: time.Now()}
probe := jobs.ProbeWorkerReadiness(context.Background(), q, time.Minute)
if !probe.OK || probe.WorkerCheck != "ok" || probe.QueueCheck != "ok" || probe.PendingJobs != 3 {
t.Fatalf("probe = %#v", probe)
}
}
func TestProbeWorkerReadinessMissing(t *testing.T) {
t.Parallel()
q := &stubQuerier{seenErr: pgx.ErrNoRows}
probe := jobs.ProbeWorkerReadiness(context.Background(), q, time.Minute)
if probe.OK || probe.WorkerCheck != "missing" || probe.ErrMsg == "" || probe.Reason == "" {
t.Fatalf("probe = %#v", probe)
}
}
func TestProbeWorkerReadinessStale(t *testing.T) {
t.Parallel()
q := &stubQuerier{lastSeen: time.Now().Add(-2 * time.Minute)}
probe := jobs.ProbeWorkerReadiness(context.Background(), q, time.Minute)
if probe.OK || probe.WorkerCheck != "stale" || probe.Reason == "" {
t.Fatalf("probe = %#v", probe)
}
}
func TestProbeWorkerReadinessNil(t *testing.T) {
t.Parallel()
probe := jobs.ProbeWorkerReadiness(context.Background(), nil, 0)
if probe.OK || probe.WorkerCheck != "unavailable" || probe.Reason == "" {
t.Fatalf("probe = %#v", probe)
}
}
func TestProbeWorkerReadinessQueueFail(t *testing.T) {
t.Parallel()
q := &stubQuerier{pendingErr: errors.New("closed")}
probe := jobs.ProbeWorkerReadiness(context.Background(), q, time.Minute)
if probe.OK || probe.QueueCheck != "fail" || probe.WorkerCheck != "fail" {
t.Fatalf("probe = %#v", probe)
}
}