schema_test.go

 1package cmd
 2
 3import (
 4	"encoding/json"
 5	"strings"
 6	"testing"
 7
 8	"github.com/charmbracelet/crush/internal/config"
 9	"github.com/invopop/jsonschema"
10	"github.com/stretchr/testify/require"
11)
12
13func TestSchemaNoBrokenRefs(t *testing.T) {
14	t.Parallel()
15
16	reflector := new(jsonschema.Reflector)
17	bts, err := json.Marshal(reflector.Reflect(&config.Config{}))
18	require.NoError(t, err)
19
20	var schema struct {
21		Defs map[string]json.RawMessage `json:"$defs"`
22	}
23	require.NoError(t, json.Unmarshal(bts, &schema))
24	require.NotEmpty(t, schema.Defs, "schema should have definitions")
25
26	for name := range schema.Defs {
27		require.NotContains(t, name, "/", "schema $def key %q contains '/' which breaks JSON Pointer $ref resolution", name)
28	}
29}
30
31func TestSchemaProvidersHasAdditionalProperties(t *testing.T) {
32	t.Parallel()
33
34	reflector := new(jsonschema.Reflector)
35	bts, err := json.Marshal(reflector.Reflect(&config.Config{}))
36	require.NoError(t, err)
37
38	var schema struct {
39		Defs map[string]json.RawMessage `json:"$defs"`
40	}
41	require.NoError(t, json.Unmarshal(bts, &schema))
42
43	var cfg struct {
44		Properties map[string]json.RawMessage `json:"properties"`
45	}
46	require.NoError(t, json.Unmarshal(schema.Defs["Config"], &cfg))
47
48	providersRaw, ok := cfg.Properties["providers"]
49	require.True(t, ok, "Config should have a providers property")
50
51	var providers struct {
52		Type                 string          `json:"type"`
53		AdditionalProperties json.RawMessage `json:"additionalProperties"`
54	}
55	require.NoError(t, json.Unmarshal(providersRaw, &providers))
56	require.Equal(t, "object", providers.Type)
57	require.True(t, strings.Contains(string(providers.AdditionalProperties), "ProviderConfig"),
58		"providers should use additionalProperties with a ProviderConfig ref, got: %s", string(providers.AdditionalProperties))
59}