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

63 lines
2.2 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"
"strings"
"testing"
)
2026-07-17 06:50:08 +00:00
func TestLoadResponseSchemaUsesExtractorOwnedLLMSchema(t *testing.T) {
schema, err := loadResponseSchema()
if err != nil {
t.Fatalf("loadResponseSchema() error = %v, want nil", err)
}
2026-07-17 06:50:08 +00:00
if schema.Key != ResponseSchemaKey || schema.ID != ResponseSchemaID || schema.Version != SchemaVersion || schema.Name != ResponseSchemaName {
t.Fatalf("schema identity = %#v, want maintained response identity", schema)
2026-07-03 23:31:19 +00:00
}
2026-07-17 06:50:08 +00:00
if !strings.HasPrefix(schema.SHA256, "sha256:") || !json.Valid(schema.JSONSchema) {
t.Fatalf("schema metadata = %#v, want valid hashed JSON", schema)
2026-07-03 23:31:19 +00:00
}
var decoded map[string]any
if err := json.Unmarshal(schema.JSONSchema, &decoded); err != nil {
2026-07-17 06:50:08 +00:00
t.Fatalf("Unmarshal(schema.JSONSchema) error = %v", err)
}
2026-07-17 06:50:08 +00:00
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 {
2026-07-17 06:50:08 +00:00
t.Fatalf("LLM source ref schema contains canonical source_id: %#v", sourceRefProperties)
}
}
func TestResponseSchemaJSONIsMutationSafe(t *testing.T) {
first, err := loadResponseSchema()
if err != nil {
2026-07-17 06:50:08 +00:00
t.Fatalf("loadResponseSchema() error = %v", err)
}
first.JSONSchema[0] = '['
second, err := loadResponseSchema()
2026-07-17 06:50:08 +00:00
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)
}
}
2026-07-03 23:31:19 +00:00
func TestResponseSchemaDiagnosticsOmitRawSchema(t *testing.T) {
schema, err := loadResponseSchema()
if err != nil {
2026-07-17 06:50:08 +00:00
t.Fatalf("loadResponseSchema() error = %v", err)
}
2026-07-03 23:31:19 +00:00
diagnostics := schema.DiagnosticsMap()
if diagnostics["key"] != ResponseSchemaKey {
2026-07-17 06:50:08 +00:00
t.Fatalf("diagnostics = %#v, want response key", diagnostics)
2026-07-03 23:31:19 +00:00
}
if _, ok := diagnostics["json_schema"]; ok {
2026-07-17 06:50:08 +00:00
t.Fatalf("diagnostics include raw schema: %#v", diagnostics)
}
}