54 lines
2.0 KiB
Go
54 lines
2.0 KiB
Go
package responseschema
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/fs"
|
|
"testing"
|
|
|
|
jsonschema "github.com/santhosh-tekuri/jsonschema/v6"
|
|
)
|
|
|
|
func TestCorrectionRepairSchema(t *testing.T) {
|
|
var document any
|
|
raw, err := fs.ReadFile(Filesystem(), AssetRoot+"/correction_repair.v1.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := json.Unmarshal(raw, &document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
compiler := jsonschema.NewCompiler()
|
|
if err := compiler.AddResource("repair.json", document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
schema, err := compiler.Compile("repair.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
name, content string
|
|
valid bool
|
|
}{
|
|
{"delete whitespace", `{"proposal_ref":1,"decision":{"action":"revise","correction":{"id":1,"original_text":" ","corrected_text":""}}}`, true},
|
|
{"withdraw", `{"proposal_ref":1,"decision":{"action":"withdraw","correction":null}}`, true},
|
|
{"revision without correction", `{"proposal_ref":1,"decision":{"action":"revise","correction":null}}`, false},
|
|
{"withdrawal with correction", `{"proposal_ref":1,"decision":{"action":"withdraw","correction":{"id":1,"original_text":"cat","corrected_text":"dog"}}}`, false},
|
|
{"flat response", `{"proposal_ref":1,"action":"withdraw","correction":null}`, false},
|
|
{"missing correction", `{"proposal_ref":1,"decision":{"action":"withdraw"}}`, false},
|
|
{"extra field", `{"proposal_ref":1,"extra":1,"decision":{"action":"withdraw","correction":null}}`, false},
|
|
{"zero reference", `{"proposal_ref":0,"decision":{"action":"withdraw","correction":null}}`, false},
|
|
{"unknown action", `{"proposal_ref":1,"decision":{"action":"other","correction":null}}`, false},
|
|
{"empty original", `{"proposal_ref":1,"decision":{"action":"revise","correction":{"id":1,"original_text":"","corrected_text":"x"}}}`, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var value any
|
|
if err := json.Unmarshal([]byte(tc.content), &value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := schema.Validate(value); (err == nil) != tc.valid {
|
|
t.Fatalf("validation = %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|