@@ -2,16 +2,21 @@ package config
22
33import (
44 "io/fs"
5+ "os"
56 "path/filepath"
67 "testing"
78
9+ "github.com/goccy/go-yaml"
810 "github.com/stretchr/testify/assert"
911 "github.com/stretchr/testify/require"
12+ "github.com/xeipuuv/gojsonschema"
1013)
1114
12- func TestParseExamples (t * testing.T ) {
15+ func collectExamples (t * testing.T ) []string {
16+ t .Helper ()
17+
1318 var files []string
14- err := filepath .WalkDir ("../../ examples" , func (path string , d fs.DirEntry , err error ) error {
19+ err := filepath .WalkDir (filepath . Join ( ".." , ".." , " examples") , func (path string , d fs.DirEntry , err error ) error {
1520 if err != nil {
1621 return err
1722 }
@@ -23,7 +28,11 @@ func TestParseExamples(t *testing.T) {
2328 require .NoError (t , err )
2429 assert .NotEmpty (t , files )
2530
26- for _ , file := range files {
31+ return files
32+ }
33+
34+ func TestParseExamples (t * testing.T ) {
35+ for _ , file := range collectExamples (t ) {
2736 t .Run (file , func (t * testing.T ) {
2837 t .Parallel ()
2938
@@ -36,3 +45,29 @@ func TestParseExamples(t *testing.T) {
3645 })
3746 }
3847}
48+
49+ func TestJsonSchemaWorksForExamples (t * testing.T ) {
50+ // Read json schema.
51+ schemaFile , err := os .ReadFile (filepath .Join (".." , ".." , "cagent-schema.json" ))
52+ require .NoError (t , err )
53+
54+ schema , err := gojsonschema .NewSchema (gojsonschema .NewBytesLoader (schemaFile ))
55+ require .NoError (t , err )
56+
57+ for _ , file := range collectExamples (t ) {
58+ t .Run (file , func (t * testing.T ) {
59+ t .Parallel ()
60+
61+ buf , err := os .ReadFile (file )
62+ require .NoError (t , err )
63+
64+ var rawJSON any
65+ err = yaml .Unmarshal (buf , & rawJSON )
66+ require .NoError (t , err )
67+
68+ result , err := schema .Validate (gojsonschema .NewRawLoader (rawJSON ))
69+ require .NoError (t , err )
70+ assert .True (t , result .Valid (), "Example %s does not match schema: %v" , file , result .Errors ())
71+ })
72+ }
73+ }
0 commit comments