2026-05-13 02:10:24 +00:00
package responseschema
import (
2026-05-24 06:51:50 -05:00
"encoding/json"
2026-11-23 04:32:15 +00:00
"io/fs"
2026-05-13 02:10:24 +00:00
"testing"
2026-11-11 04:13:55 +00:00
jsonschema "github.com/santhosh-tekuri/jsonschema/v6"
2026-05-13 02:10:24 +00:00
)
2026-11-11 04:13:55 +00:00
func TestCorrectionRepairSchema ( t * testing . T ) {
var document any
2026-11-23 04:32:15 +00:00
raw , err := fs . ReadFile ( Filesystem ( ) , AssetRoot + "/correction_repair.v1.json" )
if err != nil {
t . Fatal ( err )
}
if err := json . Unmarshal ( raw , & document ) ; err != nil {
2026-11-11 04:13:55 +00:00
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
} {
2026-11-15 02:41:32 +00:00
{ "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 } ,
2026-11-11 04:13:55 +00:00
} {
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 )
}
} )
}
}