Files
odidere/internal/job/job_test.go

191 lines
4.1 KiB
Go
Raw Permalink Normal View History

2026-06-02 12:03:03 +00:00
package job
import (
"log/slog"
"testing"
"time"
2026-06-19 14:31:41 +00:00
"code.chimeric.al/chimerical/odidere/internal/config"
2026-06-02 12:03:03 +00:00
)
func TestConfigValidate(t *testing.T) {
tests := []struct {
name string
2026-06-19 14:31:41 +00:00
config config.JobConfig
2026-06-02 12:03:03 +00:00
wantErr bool
}{
{
name: "empty config",
2026-06-19 14:31:41 +00:00
config: config.JobConfig{},
2026-06-02 12:03:03 +00:00
wantErr: true,
},
{
name: "missing name",
2026-06-19 14:31:41 +00:00
config: config.JobConfig{
2026-06-02 12:03:03 +00:00
Schedule: "0 9 * * *",
Task: "do something",
},
wantErr: true,
},
{
name: "missing task",
2026-06-19 14:31:41 +00:00
config: config.JobConfig{
2026-06-02 12:03:03 +00:00
Name: "test",
Schedule: "0 9 * * *",
},
wantErr: true,
},
{
name: "missing schedule",
2026-06-19 14:31:41 +00:00
config: config.JobConfig{
2026-06-02 12:03:03 +00:00
Name: "test",
Task: "do something",
},
wantErr: true,
},
{
name: "negative max iterations",
2026-06-19 14:31:41 +00:00
config: config.JobConfig{
2026-06-02 12:03:03 +00:00
Name: "test",
Task: "do something",
Schedule: "0 9 * * *",
MaxIterations: -1,
},
wantErr: true,
},
{
name: "minimal valid config",
2026-06-19 14:31:41 +00:00
config: config.JobConfig{
2026-06-02 12:03:03 +00:00
Name: "test",
Task: "do something",
Schedule: "0 9 * * *",
},
wantErr: false,
},
{
name: "full valid config",
2026-06-19 14:31:41 +00:00
config: config.JobConfig{
2026-06-02 12:03:03 +00:00
Name: "test",
Task: "do something",
Schedule: "0 9 * * *",
Timeout: "10m",
MaxIterations: 20,
SystemMessage: "You are an assistant.",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.Validate()
if (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestConfigMaxIterations(t *testing.T) {
// Zero means unlimited
2026-06-19 14:31:41 +00:00
cfg := config.JobConfig{Name: "test"}
2026-06-02 12:03:03 +00:00
if got := cfg.MaxIterations; got != 0 {
t.Errorf("default max iterations = %d, want 0", got)
}
cfg.MaxIterations = 0
if got := cfg.MaxIterations; got != 0 {
t.Errorf("explicit zero max iterations = %d, want 0", got)
}
// Positive value passes through
cfg.MaxIterations = 25
if got := cfg.MaxIterations; got != 25 {
t.Errorf("custom max iterations = %d, want 25", got)
}
}
func TestConfigTimeout(t *testing.T) {
2026-06-19 14:31:41 +00:00
cfg := config.JobConfig{Name: "test"}
2026-06-02 12:03:03 +00:00
if cfg.Timeout != "" {
t.Errorf("default timeout = %q, want empty", cfg.Timeout)
}
cfg.Timeout = "10m"
got, err := time.ParseDuration(cfg.Timeout)
if err != nil {
t.Fatalf("ParseDuration(%q) error = %v", cfg.Timeout, err)
}
if got.Minutes() != 10 {
t.Errorf("parsed timeout = %v, want 10m", got)
}
}
func TestNew(t *testing.T) {
2026-06-19 14:31:41 +00:00
cfg := config.JobConfig{
2026-06-02 12:03:03 +00:00
Name: "test",
Task: "do something",
Schedule: "0 9 * * *",
Timeout: "10m",
MaxIterations: 20,
SystemMessage: "You are an assistant.",
}
2026-06-19 14:31:41 +00:00
j, err := New(cfg, slog.Default(), nil, "test-model")
2026-06-02 12:03:03 +00:00
if err != nil {
t.Fatalf("New() error = %v", err)
}
if j.Name() != "test" {
t.Errorf("Name() = %q, want %q", j.Name(), "test")
}
if j.Schedule() != "0 9 * * *" {
t.Errorf("Schedule() = %q, want %q", j.Schedule(), "0 9 * * *")
}
if j.SystemMessage() != "You are an assistant." {
t.Errorf("SystemMessage() = %q, want %q", j.SystemMessage(), "You are an assistant.")
}
if j.Task() != "do something" {
t.Errorf("Task() = %q, want %q", j.Task(), "do something")
}
if j.Timeout().Minutes() != 10 {
t.Errorf("Timeout() = %v, want 10m", j.Timeout())
}
if j.MaxIterations() != 20 {
t.Errorf("MaxIterations() = %d, want 20", j.MaxIterations())
}
2026-06-19 14:31:41 +00:00
if j.Model() != "test-model" {
t.Errorf("Model() = %q, want %q", j.Model(), "test-model")
}
2026-06-02 12:03:03 +00:00
if j.Cron() == nil {
t.Error("Cron() = nil, want non-nil")
}
}
func TestNew_InvalidSchedule(t *testing.T) {
2026-06-19 14:31:41 +00:00
cfg := config.JobConfig{
2026-06-02 12:03:03 +00:00
Name: "test",
Task: "do something",
Schedule: "not-a-cron",
}
2026-06-19 14:31:41 +00:00
_, err := New(cfg, slog.Default(), nil, "test-model")
2026-06-02 12:03:03 +00:00
if err == nil {
t.Fatal("New() expected error for invalid schedule")
}
}
func TestNew_InvalidTimeout(t *testing.T) {
2026-06-19 14:31:41 +00:00
cfg := config.JobConfig{
2026-06-02 12:03:03 +00:00
Name: "test",
Task: "do something",
Schedule: "0 9 * * *",
Timeout: "not-a-duration",
}
2026-06-19 14:31:41 +00:00
_, err := New(cfg, slog.Default(), nil, "test-model")
2026-06-02 12:03:03 +00:00
if err == nil {
t.Fatal("New() expected error for invalid timeout")
}
}