Twelve definitions each advertised a bare {"type":"object"} or {"type":"array"},
so no resource descriptor carried a single field: the Web UI had nothing to
label, describe or format a field with, and the one component that parsed a
schema was dead code. The constants could not be kept honest either -- a schema
written beside a struct drifts the moment a field is added, and nothing compares
the two.
Definition now names its document type and Descriptor derives the schema from it.
json tags give the keys, falling back to the Go field name because that is what
encoding/json stores; the Go type gives type and format; and a field's own
title, format and doc tags override. A duration is a string with format
"duration", which is how every duration field in the catalogue -- poll_interval,
max_uptime, rate_limit.window -- gets a client-side affordance from one rule
instead of twelve hand-written ones.
Deliberately structural: it emits no prose of its own. Field descriptions already
have one home (internal/webui/config_schema.go, keyed by dotted config path), and
a second would be two places to keep the same fact true. It also never emits
"required", because an empty string is legitimate for nearly every field here --
a blank workspace, a disabled rate limit -- so requiring most of them would
invite a client to enforce something the document does not mean.
Guarded by TestEveryResourceDescriptorDerivesProperties, which walks the catalogue
the production constructor builds and fails if any kind describes no fields --
the assertion that would have caught all twelve. It accepts a keyed collection's
additionalProperties as described, because a map document has no fixed keys.
Verified: controlplane, workflow, webui, archieui and staterpc suites, go vet,
go build ./..., golangci-lint run ./... (0 issues), docs:check.
180 lines
6.9 KiB
Go
180 lines
6.9 KiB
Go
package controlplane
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
)
|
|
|
|
type schemaFixtureNested struct {
|
|
Prompt string `json:"prompt"`
|
|
}
|
|
|
|
type schemaFixtureEmbedded struct {
|
|
Shared string `json:"shared"`
|
|
}
|
|
|
|
type schemaFixture struct {
|
|
schemaFixtureEmbedded
|
|
Name string `json:"name" title:"Display name"`
|
|
Skipped string `json:"-"`
|
|
Untagged string // no json tag: the document key is the Go name
|
|
Count int `json:"count"`
|
|
Ratio float64 `json:"ratio"`
|
|
Enabled bool `json:"enabled"`
|
|
Window config.Duration `json:"window"`
|
|
Lifetime channelDuration `json:"lifetime"`
|
|
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
|
|
}
|
|
|
|
// TestSchemaDerivesKeysTypesAndAnnotations pins the derivation itself, including
|
|
// the two 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
|
|
// string with a format rather than the integer its Go type would suggest.
|
|
func TestSchemaDerivesKeysTypesAndAnnotations(t *testing.T) {
|
|
expected := map[string]any{
|
|
"type": "object",
|
|
"properties": map[string]any{
|
|
"shared": map[string]any{"type": "string"},
|
|
"name": map[string]any{"type": "string", "title": "Display name"},
|
|
"Untagged": map[string]any{"type": "string"},
|
|
"count": map[string]any{"type": "integer"},
|
|
"ratio": map[string]any{"type": "number"},
|
|
"enabled": map[string]any{"type": "boolean"},
|
|
"window": map[string]any{"type": "string", "format": "duration"},
|
|
"lifetime": map[string]any{"type": "string", "format": "duration"},
|
|
"when": map[string]any{"type": "string", "format": "date-time"},
|
|
"tags": map[string]any{"type": "array", "items": map[string]any{"type": "string"}},
|
|
"roles": map[string]any{"type": "object", "additionalProperties": map[string]any{"type": "string"}},
|
|
"nested": map[string]any{"type": "object", "properties": map[string]any{"prompt": map[string]any{"type": "string"}}},
|
|
"hinted": map[string]any{"type": "string", "format": "duration", "description": "A hint."},
|
|
},
|
|
}
|
|
var got map[string]any
|
|
// The unexported field is populated rather than left blank, so the
|
|
// assertion below proves it is omitted even when it carries a value.
|
|
if err := json.Unmarshal([]byte(schemaJSON(schemaFixture{unexported: "not in the document"})), &got); err != nil {
|
|
t.Fatalf("unmarshal derived schema: %v", err)
|
|
}
|
|
if !reflect.DeepEqual(got, expected) {
|
|
t.Fatalf("derived schema =\n%#v\nwant\n%#v", got, expected)
|
|
}
|
|
properties, ok := got["properties"].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("derived schema has no properties object: %#v", got)
|
|
}
|
|
if _, present := properties["Skipped"]; present {
|
|
t.Error(`json:"-" field is described, so the schema names a key the document never stores`)
|
|
}
|
|
if _, present := properties["unexported"]; present {
|
|
t.Error("unexported field is described")
|
|
}
|
|
}
|
|
|
|
// TestEveryResourceDescriptorDerivesProperties is the regression guard for the
|
|
// defect this replaced: twelve definitions each advertised a bare
|
|
// {"type":"object"} or {"type":"array"}, so no descriptor carried a single field
|
|
// for the Web UI to label, describe or format. Every kind here must describe
|
|
// something, and this walks the catalogue the constructor production uses rather
|
|
// than the definitions in isolation.
|
|
func TestEveryResourceDescriptorDerivesProperties(t *testing.T) {
|
|
server := testServer(t, nil)
|
|
catalog, err := server.Catalog(t.Context(), nil)
|
|
if err != nil {
|
|
t.Fatalf("Catalog: %v", err)
|
|
}
|
|
describing := 0
|
|
for _, descriptor := range catalog.Resources {
|
|
if descriptor.SchemaJson == "" {
|
|
// Domain-managed kinds (identities, captures, mappings, bindings)
|
|
// carry commands rather than a document this side validates.
|
|
continue
|
|
}
|
|
describing++
|
|
var schema map[string]any
|
|
if err := json.Unmarshal([]byte(descriptor.SchemaJson), &schema); err != nil {
|
|
t.Errorf("%s: schema is not JSON: %v", descriptor.Kind, err)
|
|
continue
|
|
}
|
|
switch schema["type"] {
|
|
case "object":
|
|
// A fixed document describes properties; a keyed collection
|
|
// (providers, model roles) describes additionalProperties and has
|
|
// no fixed keys by design. Either is a described document; neither
|
|
// is the bare object this guard exists to catch.
|
|
properties, hasProperties := schema["properties"].(map[string]any)
|
|
_, hasAdditional := schema["additionalProperties"].(map[string]any)
|
|
if (!hasProperties || len(properties) == 0) && !hasAdditional {
|
|
t.Errorf("%s: object schema describes no fields: %s", descriptor.Kind, descriptor.SchemaJson)
|
|
}
|
|
case "array":
|
|
if _, ok := schema["items"].(map[string]any); !ok {
|
|
t.Errorf("%s: array schema describes no items: %s", descriptor.Kind, descriptor.SchemaJson)
|
|
}
|
|
default:
|
|
t.Errorf("%s: schema type = %#v, want object or array", descriptor.Kind, schema["type"])
|
|
}
|
|
}
|
|
if describing == 0 {
|
|
t.Fatal("no descriptor advertised a schema: the guard asserted nothing")
|
|
}
|
|
}
|
|
|
|
// TestChannelSettingsDescriptorFormatsTheRateLimitWindow is the concrete payoff:
|
|
// the field that asked an operator for a nanosecond count now carries a format a
|
|
// client can render an affordance for, and it arrives by derivation rather than
|
|
// by anyone remembering to write it down.
|
|
func TestChannelSettingsDescriptorFormatsTheRateLimitWindow(t *testing.T) {
|
|
server := testServer(t, nil)
|
|
catalog, err := server.Catalog(t.Context(), nil)
|
|
if err != nil {
|
|
t.Fatalf("Catalog: %v", err)
|
|
}
|
|
var schema struct {
|
|
Properties struct {
|
|
RateLimit struct {
|
|
Properties struct {
|
|
Window struct {
|
|
Type string `json:"type"`
|
|
Format string `json:"format"`
|
|
} `json:"window"`
|
|
MaxRequests struct {
|
|
Type string `json:"type"`
|
|
} `json:"max_requests"`
|
|
} `json:"properties"`
|
|
} `json:"rate_limit"`
|
|
Email struct {
|
|
Properties map[string]struct {
|
|
Type string `json:"type"`
|
|
} `json:"properties"`
|
|
} `json:"email"`
|
|
} `json:"properties"`
|
|
}
|
|
for _, descriptor := range catalog.Resources {
|
|
if descriptor.Kind != ChannelSettingsKind {
|
|
continue
|
|
}
|
|
if err := json.Unmarshal([]byte(descriptor.SchemaJson), &schema); err != nil {
|
|
t.Fatalf("unmarshal channel-settings schema: %v", err)
|
|
}
|
|
}
|
|
if schema.Properties.RateLimit.Properties.Window.Format != "duration" {
|
|
t.Errorf("rate_limit.window format = %q, want \"duration\"", schema.Properties.RateLimit.Properties.Window.Format)
|
|
}
|
|
if schema.Properties.RateLimit.Properties.MaxRequests.Type != "integer" {
|
|
t.Errorf("rate_limit.max_requests type = %q, want \"integer\"", schema.Properties.RateLimit.Properties.MaxRequests.Type)
|
|
}
|
|
if len(schema.Properties.Email.Properties) == 0 {
|
|
t.Error("email describes no properties, want listen_addr and relay_addr")
|
|
}
|
|
}
|