121 lines
5.3 KiB
Go
121 lines
5.3 KiB
Go
package assets_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io/fs"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/assets"
|
|
"go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
func TestRegistryExtractionPromptPairsRetainDomainRulesAndContracts(t *testing.T) {
|
|
_, engine := loadNativeAssets(t)
|
|
for _, test := range []struct {
|
|
family, instruction string
|
|
rules []string
|
|
}{
|
|
{"npc", "Revise the NPC citations.", []string{"proper name or a stable, individually distinguishing", "factual third-party mention", "Exclude human players"}},
|
|
{"item", "Revise the item citations.", []string{"named unique items", "concrete reusable item types", "currency denomination", "generic weapons", "Do not use capitalization as an eligibility test"}},
|
|
{"location", "Revise the location citations.", []string{"stable proper name or unique in-world designation", "the room", "Do not use capitalization as an eligibility test"}},
|
|
} {
|
|
t.Run(test.family, func(t *testing.T) {
|
|
id := "notarius/dnd." + test.family + "_registry"
|
|
initial, err := engine.PrepareExecution(context.Background(), promptkit.RunRequest{
|
|
PromptID: id, APIKey: "test-only",
|
|
Inputs: map[string]promptkit.ArtifactRef{
|
|
"transcript": promptkit.Inline(`{"units":[{"sentinel":"registry-transcript"}]}`),
|
|
"players": promptkit.Inline("registry-players"), "party": promptkit.Inline("registry-party"), "glossary": promptkit.Inline("registry-glossary"),
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer initial.Discard()
|
|
next, err := engine.PrepareExecution(context.Background(), promptkit.RunRequest{
|
|
PromptID: id + ".repair", APIKey: "test-only", Inputs: map[string]promptkit.ArtifactRef{"guidance": promptkit.Inline(test.instruction)},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer next.Discard()
|
|
first, correction := initial.Details(), next.Details()
|
|
if first.SelectedProfileID != "notarius/dnd-extraction" || correction.SelectedProfileID != first.SelectedProfileID ||
|
|
first.StructuredOutput == nil || correction.StructuredOutput == nil || !reflect.DeepEqual(first.OutputContract, correction.OutputContract) {
|
|
t.Fatal("registry prompt pair lost native schema/defaults")
|
|
}
|
|
var system, all string
|
|
for _, message := range first.Messages {
|
|
all += message.Content
|
|
if message.Role == "system" {
|
|
system += message.Content
|
|
}
|
|
if strings.Contains(message.Content, "registry-transcript") || strings.Contains(message.Content, "registry-players") {
|
|
if message.CacheControl == nil || message.CacheControl.Type != promptkit.CacheControlEphemeral {
|
|
t.Fatal("fixed input cache hint lost")
|
|
}
|
|
}
|
|
}
|
|
for _, rule := range append(test.rules, "Transcript units are the only evidence", "Use the most specific supported in-world") {
|
|
if !strings.Contains(strings.Join(strings.Fields(system), " "), rule) {
|
|
t.Fatalf("missing enduring domain rule %q", rule)
|
|
}
|
|
}
|
|
for _, input := range []string{"registry-transcript", "registry-players", "registry-party", "registry-glossary"} {
|
|
if strings.Count(all, input) != 1 {
|
|
t.Fatalf("fixed input not rendered once: %q", input)
|
|
}
|
|
}
|
|
if len(correction.Messages) != 1 || correction.Messages[0].Role != "user" || !strings.Contains(correction.Messages[0].Content, test.instruction) ||
|
|
strings.Contains(correction.Messages[0].Content, "registry-transcript") {
|
|
t.Fatal("correction repeated fixed inputs instead of supplying findings/assignment")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRegistryResponseSchemasSeparateStructureFromSemanticValidity(t *testing.T) {
|
|
for _, test := range []struct{ family, field string }{
|
|
{"npc", "npcs"}, {"item", "items"}, {"location", "locations"},
|
|
} {
|
|
t.Run(test.family, func(t *testing.T) {
|
|
schema, err := fs.ReadFile(assets.FS(), "dnd/"+test.family+"-registry/extract/schemas/dnd_"+test.family+"_registry_llm.v1.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, value := range []struct {
|
|
name, record string
|
|
valid bool
|
|
}{
|
|
{"grounded record", `{"name":"Canonical Name","source_refs":[{"start_unit_id":1,"end_unit_id":2}]}`, true},
|
|
{"blank name and empty evidence", `{"name":"","source_refs":[]}`, true},
|
|
{"nonpositive source candidate", `{"name":"Canonical Name","source_refs":[{"start_unit_id":0,"end_unit_id":-1}]}`, true},
|
|
{"missing name", `{"source_refs":[]}`, false},
|
|
{"opaque ID", `{"name":"Canonical Name","source_refs":[],"id":"assigned-later"}`, false},
|
|
{"wrong name type", `{"name":7,"source_refs":[]}`, false},
|
|
{"noninteger source identifier", `{"name":"Canonical Name","source_refs":[{"start_unit_id":1.5,"end_unit_id":2}]}`, false},
|
|
} {
|
|
t.Run(value.name, func(t *testing.T) {
|
|
content := []byte(`{"` + test.field + `":[` + value.record + `]}`)
|
|
if err := validateJSONSchema(content, schema); (err == nil) != value.valid {
|
|
t.Fatalf("schema validity=%t, error=%v", value.valid, err)
|
|
}
|
|
})
|
|
}
|
|
for _, content := range []string{`{"` + test.field + `":[]}`, `{"` + test.field + `":null}`, `{}`} {
|
|
var value map[string]any
|
|
if err := json.Unmarshal([]byte(content), &value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, isArray := value[test.field].([]any)
|
|
if err := validateJSONSchema([]byte(content), schema); (err == nil) != isArray {
|
|
t.Fatalf("collection shape %s: %v", content, err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|