Files
archie/internal/infrastructure/cronstore/schedule_duration_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

92 lines
3.5 KiB
Go

package cronstore
import (
"encoding/json"
"strings"
"testing"
"time"
)
// TestScheduleIntervalWritesADurationString pins the document form: the interval
// an operator edits must read as a duration, not as the nanosecond count that
// asked them to type 1800000000000.
func TestScheduleIntervalWritesADurationString(t *testing.T) {
encoded, err := json.Marshal(Schedule{Kind: ScheduleInterval, Interval: Duration(30 * time.Minute)})
if err != nil {
t.Fatalf("marshal: %v", err)
}
if got := string(encoded); !strings.Contains(got, `"interval":"30m0s"`) {
t.Errorf("marshalled schedule = %s, want interval as the string \"30m0s\"", got)
}
if strings.Contains(string(encoded), "1800000000000") {
t.Errorf("marshalled schedule = %s, want no nanosecond count", encoded)
}
}
// TestScheduleIntervalReadsEveryStoredForm covers what the store already holds.
// Every schedule written before this shape existed carries a nanosecond count, in
// both the control plane's schedules resource and this package's own jobs table,
// and both have to keep working without being re-saved first.
func TestScheduleIntervalReadsEveryStoredForm(t *testing.T) {
for _, tt := range []struct {
name string
document string
expected time.Duration
}{
{"legacy nanosecond count", `{"kind":"interval","interval":1800000000000}`, 30 * time.Minute},
{"duration string", `{"kind":"interval","interval":"30m"}`, 30 * time.Minute},
{"duration string, canonical", `{"kind":"interval","interval":"30m0s"}`, 30 * time.Minute},
} {
t.Run(tt.name, func(t *testing.T) {
var schedule Schedule
if err := json.Unmarshal([]byte(tt.document), &schedule); err != nil {
t.Fatalf("unmarshal %s: %v", tt.document, err)
}
if schedule.Interval.Std() != tt.expected {
t.Errorf("Interval = %v, want %v", schedule.Interval, tt.expected)
}
if err := schedule.Validate(); err != nil {
t.Errorf("Validate() = %v, want the decoded schedule to be usable", err)
}
})
}
}
// TestScheduleIntervalRefusesWhatItCannotUse keeps the tolerance above from
// becoming indifference: a duration that does not parse must fail loudly rather
// than decode as zero, because a zero interval is a job that never fires.
func TestScheduleIntervalRefusesWhatItCannotUse(t *testing.T) {
for _, tt := range []struct{ name, document string }{
{"an unparseable duration", `{"kind":"interval","interval":"soon"}`},
{"a fractional count", `{"kind":"interval","interval":1.5}`},
{"a null interval", `{"kind":"interval","interval":null}`},
} {
t.Run(tt.name, func(t *testing.T) {
var schedule Schedule
if err := json.Unmarshal([]byte(tt.document), &schedule); err == nil {
t.Fatalf("unmarshal(%s) = nil, want a refusal: a silent zero interval never fires", tt.document)
}
})
}
}
// TestScheduleIntervalRoundTripsThroughTheStore pins the shape the jobs table
// keeps, since it persists the spec as JSON rather than as columns.
func TestScheduleIntervalRoundTripsThroughTheStore(t *testing.T) {
spec := JobSpec{ID: "nightly", Schedule: Schedule{Kind: ScheduleInterval, Interval: Duration(90 * time.Minute)}}
encoded, err := json.Marshal(spec)
if err != nil {
t.Fatalf("marshal: %v", err)
}
if !strings.Contains(string(encoded), `"interval":"1h30m0s"`) {
t.Fatalf("marshalled spec = %s, want the interval as \"1h30m0s\"", encoded)
}
var decoded JobSpec
if err := json.Unmarshal(encoded, &decoded); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if decoded.Schedule.Interval.Std() != 90*time.Minute {
t.Errorf("round-tripped Interval = %v, want 1h30m0s", decoded.Schedule.Interval)
}
}