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
+23 -16
+23 -16
View File
@@ -19,27 +19,33 @@ type schemaFixtureEmbedded struct {
type schemaFixture struct { type schemaFixture struct {
schemaFixtureEmbedded schemaFixtureEmbedded
Name string `json:"name" title:"Display name"` Name string `json:"name" title:"Display name"`
Skipped string `json:"-"` Skipped string `json:"-"`
Untagged string // no json tag: the document key is the Go name Untagged string // no json tag: the document key is the Go name
Count int `json:"count"` Count int `json:"count"`
Ratio float64 `json:"ratio"` Ratio float64 `json:"ratio"`
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"`
When time.Time `json:"when"` // A bare time.Duration marshals as its nanosecond count, unlike the two
Tags []string `json:"tags"` // named types above, which marshal as the string time.ParseDuration reads.
Roles map[string]string `json:"roles"` // The schema describes the wire form, not the Go type's name, or a client
Nested schemaFixtureNested `json:"nested"` // renders a text box for a number.
Hinted string `json:"hinted" format:"duration" doc:"A hint."` Timeout time.Duration `json:"timeout"`
When time.Time `json:"when"`
Tags []string `json:"tags"`
Roles map[string]string `json:"roles"`
Nested schemaFixtureNested `json:"nested"`
Hinted string `json:"hinted" format:"duration" doc:"A hint."`
unexported string unexported string
} }
// 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"}},