test(controlplane): pin a bare time.Duration as an integer, not a duration

schemaFixture covered config.Duration and channelDuration, both of which marshal
as the string time.ParseDuration reads, but not a bare time.Duration, which
marshals as its nanosecond count. The derivation already distinguishes them by
named type rather than by kind; this pins it, because the failure mode is a
client rendering a text box for a number, and the schedules document has exactly
that field (cronstore.Schedule.Interval).
This commit is contained in:
sam committed 2026-09-22 03:56:19 +10:00
1 parent aa650e2ccf
commit b5050c7560
1 file changed
+10 -3
+10 -3
View File
@@ -27,6 +27,11 @@ type schemaFixture struct {
Enabled bool `json:"enabled"` Enabled bool `json:"enabled"`
Window config.Duration `json:"window"` Window config.Duration `json:"window"`
Lifetime channelDuration `json:"lifetime"` Lifetime channelDuration `json:"lifetime"`
// A bare time.Duration marshals as its nanosecond count, unlike the two
// named types above, which marshal as the string time.ParseDuration reads.
// The schema describes the wire form, not the Go type's name, or a client
// renders a text box for a number.
Timeout time.Duration `json:"timeout"`
When time.Time `json:"when"` When time.Time `json:"when"`
Tags []string `json:"tags"` Tags []string `json:"tags"`
Roles map[string]string `json:"roles"` Roles map[string]string `json:"roles"`
@@ -37,9 +42,10 @@ type schemaFixture struct {
} }
// TestSchemaDerivesKeysTypesAndAnnotations pins the derivation itself, including // TestSchemaDerivesKeysTypesAndAnnotations pins the derivation itself, including
// the two rules that are easy to get subtly wrong: a field with no json tag is // the rules that are easy to get subtly wrong: a field with no json tag is
// described by the Go name the document actually stores, and a duration is a // described by the Go name the document actually stores, a named duration type
// string with a format rather than the integer its Go type would suggest. // is a string with a format because that is what it marshals as, and a bare
// time.Duration stays an integer for the same reason.
func TestSchemaDerivesKeysTypesAndAnnotations(t *testing.T) { func TestSchemaDerivesKeysTypesAndAnnotations(t *testing.T) {
expected := map[string]any{ expected := map[string]any{
"type": "object", "type": "object",
@@ -52,6 +58,7 @@ func TestSchemaDerivesKeysTypesAndAnnotations(t *testing.T) {
"enabled": map[string]any{"type": "boolean"}, "enabled": map[string]any{"type": "boolean"},
"window": map[string]any{"type": "string", "format": "duration"}, "window": map[string]any{"type": "string", "format": "duration"},
"lifetime": map[string]any{"type": "string", "format": "duration"}, "lifetime": map[string]any{"type": "string", "format": "duration"},
"timeout": map[string]any{"type": "integer"},
"when": map[string]any{"type": "string", "format": "date-time"}, "when": map[string]any{"type": "string", "format": "date-time"},
"tags": map[string]any{"type": "array", "items": map[string]any{"type": "string"}}, "tags": map[string]any{"type": "array", "items": map[string]any{"type": "string"}},
"roles": map[string]any{"type": "object", "additionalProperties": map[string]any{"type": "string"}}, "roles": map[string]any{"type": "object", "additionalProperties": map[string]any{"type": "string"}},