Rewrite brittle validation and schema tests
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/santhosh-tekuri/jsonschema/v6"
|
||||
)
|
||||
|
||||
func TestLoadResponseSchemaUsesExtractorOwnedLLMSchema(t *testing.T) {
|
||||
@@ -19,19 +21,23 @@ func TestLoadResponseSchemaUsesExtractorOwnedLLMSchema(t *testing.T) {
|
||||
t.Fatalf("schema metadata = %#v, want valid hashed JSON", schema)
|
||||
}
|
||||
|
||||
var decoded map[string]any
|
||||
if err := json.Unmarshal(schema.JSONSchema, &decoded); err != nil {
|
||||
t.Fatalf("Unmarshal(schema.JSONSchema) error = %v", err)
|
||||
valid := validSpellsResponse()
|
||||
validJSON, err := json.Marshal(valid)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal(validSpellsResponse()) error = %v, want nil", err)
|
||||
}
|
||||
if decoded["$id"] != "notarius.dnd.spells.llm" {
|
||||
t.Fatalf("LLM schema $id = %#v, want extractor transport schema", decoded["$id"])
|
||||
if err := validateJSONSchema(validJSON, schema.JSONSchema); err != nil {
|
||||
t.Fatalf("valid private spells response rejected: %v", err)
|
||||
}
|
||||
properties := decoded["properties"].(map[string]any)
|
||||
spellCastProperties := properties["spell_casts"].(map[string]any)["items"].(map[string]any)["properties"].(map[string]any)
|
||||
sourceRefItems := spellCastProperties["source_refs"].(map[string]any)["items"].(map[string]any)
|
||||
sourceRefProperties := sourceRefItems["properties"].(map[string]any)
|
||||
if _, ok := sourceRefProperties["source_id"]; ok {
|
||||
t.Fatalf("LLM source ref schema contains canonical source_id: %#v", sourceRefProperties)
|
||||
|
||||
withCanonicalSourceID := validSpellsResponse()
|
||||
withCanonicalSourceID["spell_casts"].([]any)[0].(map[string]any)["source_refs"].([]any)[0].(map[string]any)["source_id"] = "session-alpha"
|
||||
content, err := json.Marshal(withCanonicalSourceID)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal(response with source_id) error = %v, want nil", err)
|
||||
}
|
||||
if err := validateJSONSchema(content, schema.JSONSchema); err == nil {
|
||||
t.Fatal("validateJSONSchema() error = nil, want canonical source_id rejected")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,3 +66,39 @@ func TestResponseSchemaDiagnosticsOmitRawSchema(t *testing.T) {
|
||||
t.Fatalf("diagnostics include raw schema: %#v", diagnostics)
|
||||
}
|
||||
}
|
||||
|
||||
func validSpellsResponse() map[string]any {
|
||||
return map[string]any{
|
||||
"spell_casts": []any{
|
||||
map[string]any{
|
||||
"caster": "Aria",
|
||||
"spell": "Cure Wounds",
|
||||
"effect": "The wounds close.",
|
||||
"narrative_description": "Aria casts Cure Wounds.",
|
||||
"source_refs": []any{
|
||||
map[string]any{"start_unit_id": 1, "end_unit_id": 2},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func validateJSONSchema(instanceContent, schemaContent []byte) error {
|
||||
instance, err := jsonschema.UnmarshalJSON(bytes.NewReader(instanceContent))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
schemaDocument, err := jsonschema.UnmarshalJSON(bytes.NewReader(schemaContent))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
compiler := jsonschema.NewCompiler()
|
||||
if err := compiler.AddResource("schema.json", schemaDocument); err != nil {
|
||||
return err
|
||||
}
|
||||
schema, err := compiler.Compile("schema.json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return schema.Validate(instance)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user