2026-05-13 02:10:24 +00:00
package responseschema
import (
2026-05-24 06:51:50 -05:00
"bytes"
2026-05-13 02:10:24 +00:00
"crypto/sha256"
"encoding/hex"
2026-05-24 06:51:50 -05:00
"encoding/json"
2026-09-24 01:44:23 +00:00
"io"
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-09-24 01:44:23 +00:00
func TestPromptKitSchemaPathsUseCanonicalAssets ( t * testing . T ) {
2026-11-11 04:11:34 +00:00
for _ , key := range [ ] Key { CorrectionSetKey , CorrectionRepairKey , ValidatorDecisionSetKey } {
2026-09-24 01:44:23 +00:00
path , ok := AssetPath ( key )
if ! ok {
t . Fatalf ( "missing path for %q" , key )
}
file , err := Filesystem ( ) . Open ( AssetRoot + "/" + path )
if err != nil {
t . Fatal ( err )
}
data , err := io . ReadAll ( file )
file . Close ( )
if err != nil {
t . Fatal ( err )
}
if ! bytes . Equal ( data , MustLookup ( key ) . JSONSchema ) {
t . Fatalf ( "PromptKit schema differs for %q" , key )
}
}
}
2026-05-13 02:10:24 +00:00
func TestLookupKnownSchemas ( t * testing . T ) {
correction , ok := Lookup ( CorrectionSetKey )
if ! ok {
t . Fatalf ( "expected correction-set schema to be registered" )
}
if correction . ID != correctionSetSchemaID {
t . Fatalf ( "unexpected correction-set schema id %q" , correction . ID )
}
if correction . Version != schemaVersionV1 {
t . Fatalf ( "unexpected correction-set schema version %q" , correction . Version )
}
if correction . Name != "audita_correction_set_v1" {
t . Fatalf ( "unexpected correction-set schema name %q" , correction . Name )
}
validation , ok := Lookup ( ValidatorDecisionSetKey )
if ! ok {
t . Fatalf ( "expected validator-decision schema to be registered" )
}
if validation . ID != validatorDecisionSchemaID {
t . Fatalf ( "unexpected validator-decision schema id %q" , validation . ID )
}
if validation . Version != schemaVersionV1 {
t . Fatalf ( "unexpected validator-decision schema version %q" , validation . Version )
}
if validation . Name != "audita_validator_decision_set_v1" {
t . Fatalf ( "unexpected validator-decision schema name %q" , validation . Name )
}
}
func TestLookupUnknownSchema ( t * testing . T ) {
if _ , ok := Lookup ( Key ( "missing" ) ) ; ok {
t . Fatalf ( "expected unknown schema lookup to fail" )
}
}
func TestSchemaHashesMatchRegisteredJSON ( t * testing . T ) {
2026-11-11 04:11:34 +00:00
for _ , key := range [ ] Key { CorrectionSetKey , CorrectionRepairKey , ValidatorDecisionSetKey } {
2026-05-13 02:10:24 +00:00
schema , ok := Lookup ( key )
if ! ok {
t . Fatalf ( "missing schema %q" , key )
}
sum := sha256 . Sum256 ( schema . JSONSchema )
expected := hex . EncodeToString ( sum [ : ] )
if schema . SHA256 != expected {
t . Fatalf ( "unexpected hash for %q: got %q want %q" , key , schema . SHA256 , expected )
}
}
}
func TestLookupReturnsSchemaCopy ( t * testing . T ) {
schema , ok := Lookup ( CorrectionSetKey )
if ! ok {
t . Fatalf ( "missing correction-set schema" )
}
if len ( schema . JSONSchema ) == 0 {
t . Fatalf ( "expected non-empty schema payload" )
}
schema . JSONSchema [ 0 ] = 'x'
again , ok := Lookup ( CorrectionSetKey )
if ! ok {
t . Fatalf ( "missing correction-set schema on second lookup" )
}
if len ( again . JSONSchema ) == 0 {
t . Fatalf ( "unexpected empty schema payload" )
}
if again . JSONSchema [ 0 ] != '{' {
t . Fatalf ( "expected lookup to return independent schema copy" )
}
}
2026-05-23 17:48:37 +00:00
func TestDiagnosticsMapIncludesStableSchemaMetadataShapeForAllSchemas ( t * testing . T ) {
registered := Registered ( )
if len ( registered ) == 0 {
t . Fatalf ( "expected registered response schemas" )
}
for _ , schema := range registered {
metadataMap := schema . DiagnosticsMap ( )
if metadataMap [ "id" ] != schema . ID ||
metadataMap [ "version" ] != schema . Version ||
metadataMap [ "name" ] != schema . Name ||
metadataMap [ "sha256" ] != schema . SHA256 {
t . Fatalf ( "unexpected diagnostics metadata map for %q: %+v" , schema . ID , metadataMap )
}
}
}
2026-11-11 04:13:55 +00:00
func TestCorrectionRepairSchema ( t * testing . T ) {
var document any
if err := json . Unmarshal ( MustLookup ( CorrectionRepairKey ) . JSONSchema , & 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
} {
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 )
}
} )
}
}