63 lines
2.2 KiB
Go
63 lines
2.2 KiB
Go
package spells
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoadResponseSchemaUsesExtractorOwnedLLMSchema(t *testing.T) {
|
|
schema, err := loadResponseSchema()
|
|
if err != nil {
|
|
t.Fatalf("loadResponseSchema() error = %v, want nil", err)
|
|
}
|
|
if schema.Key != ResponseSchemaKey || schema.ID != ResponseSchemaID || schema.Version != SchemaVersion || schema.Name != ResponseSchemaName {
|
|
t.Fatalf("schema identity = %#v, want maintained response identity", schema)
|
|
}
|
|
if !strings.HasPrefix(schema.SHA256, "sha256:") || !json.Valid(schema.JSONSchema) {
|
|
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)
|
|
}
|
|
if decoded["$id"] != "notarius.dnd.spells.llm" {
|
|
t.Fatalf("LLM schema $id = %#v, want extractor transport schema", decoded["$id"])
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestResponseSchemaJSONIsMutationSafe(t *testing.T) {
|
|
first, err := loadResponseSchema()
|
|
if err != nil {
|
|
t.Fatalf("loadResponseSchema() error = %v", err)
|
|
}
|
|
first.JSONSchema[0] = '['
|
|
second, err := loadResponseSchema()
|
|
if err != nil || !json.Valid(second.JSONSchema) || bytes.Equal(first.JSONSchema, second.JSONSchema) {
|
|
t.Fatalf("second schema = %s, %v; want defensive valid copy", second.JSONSchema, err)
|
|
}
|
|
}
|
|
|
|
func TestResponseSchemaDiagnosticsOmitRawSchema(t *testing.T) {
|
|
schema, err := loadResponseSchema()
|
|
if err != nil {
|
|
t.Fatalf("loadResponseSchema() error = %v", err)
|
|
}
|
|
diagnostics := schema.DiagnosticsMap()
|
|
if diagnostics["key"] != ResponseSchemaKey {
|
|
t.Fatalf("diagnostics = %#v, want response key", diagnostics)
|
|
}
|
|
if _, ok := diagnostics["json_schema"]; ok {
|
|
t.Fatalf("diagnostics include raw schema: %#v", diagnostics)
|
|
}
|
|
}
|