Files
archie/internal/app/controlplane/schedules_test.go
T
sam 84450ac96c fix(cronstore): write a schedule interval as a duration, not nanoseconds
The schedules document stored interval as a bare time.Duration, so its JSON was
the nanosecond count -- 1800000000000 for thirty minutes -- and the Web UI, which
now derives field metadata from the document type, correctly rendered a number
input. Same defect 02rm fixed for rate_limit.window, in the second stored time
this tree has. Nobody should be asked for a count of nanoseconds to configure a
schedule.

cronstore.Duration writes the string time.ParseDuration reads and reads that, a
nanosecond count, and nothing else: every schedule stored before this shape
existed carries a count, in both the control plane's schedules resource and this
package's own jobs table, which persists the spec as JSON. A value that parses as
neither is refused rather than decoding as zero, because a zero interval is a job
that never fires.

The derived schema needed no change and gets it for free -- which exposed a fix
the derivation itself needed: it classified durations by NAME, so cronstore.Duration
derived as an integer. durationLike now asks the type instead -- does it expose the
standard duration, and does it marshal as a quoted string -- so the next duration
type is classified correctly rather than silently rendering as a number box. That
failure mode was invisible until a live daemon showed it.

normalizeSchedules already re-marshals, so a stored document converges on its next
write while reads keep tolerating the count.

Verified: cronstore, crondelivery, controlplane, scheduling, webui and archied
suites, go vet ./..., go build ./..., golangci-lint run ./... (0 issues),
docs:check. Against a live daemon (8484): the catalog serves schedule.interval as
{type: string, format: duration}.
2026-09-22 04:22:46 +10:00

39 lines
1.1 KiB
Go

package controlplane
import (
"encoding/json"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/infrastructure/cronstore"
)
func TestScheduleDefinitionComputesFirstRunAndRejectsDuplicateIDs(t *testing.T) {
t.Parallel()
definition := scheduleDefinition()
jobs := []cronstore.JobSpec{{ID: "daily", Kind: cronstore.KindWorkflow, Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(time.Hour)}}}
input, err := json.Marshal(jobs)
if err != nil {
t.Fatal(err)
}
got, err := definition.Decode(input)
if err != nil {
t.Fatal(err)
}
var normalized []cronstore.JobSpec
if err := json.Unmarshal(got, &normalized); err != nil {
t.Fatal(err)
}
if len(normalized) != 1 || normalized[0].NextRun.IsZero() || normalized[0].Created.IsZero() || normalized[0].Updated.IsZero() {
t.Fatalf("normalized schedules = %+v", normalized)
}
duplicate, err := json.Marshal(append(jobs, jobs[0]))
if err != nil {
t.Fatal(err)
}
if _, err := definition.Decode(duplicate); err == nil {
t.Fatal("duplicate schedule IDs accepted")
}
}