schema_test.go 1.7 KB

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