Files
notarius/internal/modules/dnd/extract/spells/schema_test.go

133 lines
3.7 KiB
Go
Raw Normal View History

2026-07-03 23:31:19 +00:00
package spells
import (
2026-07-17 06:50:08 +00:00
"bytes"
2026-07-03 23:31:19 +00:00
"encoding/json"
"io/fs"
2026-07-03 23:31:19 +00:00
"testing"
"gitea.maximumdirect.net/eric/notarius/assets"
"github.com/santhosh-tekuri/jsonschema/v6"
2026-07-03 23:31:19 +00:00
)
func TestSpellResponseSchemaPreservesPrivateSelectionContract(t *testing.T) {
schema, err := fs.ReadFile(assets.FS(), "dnd/spells/schemas/dnd_spells_llm.v1.json")
if err != nil {
t.Fatal(err)
2026-07-23 03:57:55 +00:00
}
valid := validSpellsResponse()
validJSON, err := json.Marshal(valid)
if err != nil {
t.Fatalf("Marshal(validSpellsResponse()) error = %v, want nil", err)
}
if err := validateJSONSchema(validJSON, schema); err != nil {
t.Fatalf("valid private spells response rejected: %v", err)
}
2026-07-22 14:38:35 +00:00
for _, test := range []struct {
name string
response map[string]any
valid bool
}{
{
name: "semantic blanks and empty evidence",
response: map[string]any{"spell_casts": []any{map[string]any{
"caster": "", "spell": "", "source_refs": []any{},
2026-07-22 14:38:35 +00:00
}}},
valid: true,
},
{
name: "nonpositive unit candidates",
response: map[string]any{"spell_casts": []any{map[string]any{
"caster": "Aria", "spell": "Cure Wounds",
2026-07-22 14:38:35 +00:00
"source_refs": []any{map[string]any{"start_unit_id": 0, "end_unit_id": -1}},
}}},
valid: true,
},
{
name: "missing required field",
response: map[string]any{"spell_casts": []any{map[string]any{
"spell": "Cure Wounds", "source_refs": []any{},
2026-07-22 14:38:35 +00:00
}}},
},
{
name: "unknown field",
response: map[string]any{"spell_casts": []any{map[string]any{
"caster": "Aria", "spell": "Cure Wounds", "source_refs": []any{}, "id": "assigned later",
2026-07-22 14:38:35 +00:00
}}},
},
{
name: "wrong field type",
response: map[string]any{"spell_casts": []any{map[string]any{
"caster": 7, "spell": "Cure Wounds", "source_refs": []any{},
2026-07-22 14:38:35 +00:00
}}},
},
{
name: "noninteger source identifier",
response: map[string]any{"spell_casts": []any{map[string]any{
"caster": "Aria", "spell": "Cure Wounds",
2026-07-22 14:38:35 +00:00
"source_refs": []any{map[string]any{"start_unit_id": 1.5, "end_unit_id": 2}},
}}},
},
} {
t.Run(test.name, func(t *testing.T) {
content, err := json.Marshal(test.response)
if err != nil {
t.Fatal(err)
}
err = validateJSONSchema(content, schema)
2026-07-22 14:38:35 +00:00
if (err == nil) != test.valid {
t.Fatalf("validateJSONSchema() error = %v, want valid=%t", err, test.valid)
}
})
}
if err := validateJSONSchema([]byte(`{"spell_casts":`), schema); err == nil {
2026-07-22 14:38:35 +00:00
t.Fatal("validateJSONSchema() error = nil, want malformed JSON rejected")
}
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); err == nil {
t.Fatal("validateJSONSchema() error = nil, want canonical source_id rejected")
}
}
func validSpellsResponse() map[string]any {
return map[string]any{
"spell_casts": []any{
map[string]any{
"caster": "Aria",
"spell": "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)
}