2026-07-21 04:34:56 +00:00
|
|
|
package spells
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
2026-08-05 18:38:22 +00:00
|
|
|
npccodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/npcregistry"
|
2026-07-21 04:34:56 +00:00
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity"
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/registry"
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-22 00:02:13 +00:00
|
|
|
func TestSpellExtractorProjectsNPCNamesWithoutIdentityOrProvenance(t *testing.T) {
|
2026-07-21 04:34:56 +00:00
|
|
|
value := registryFixture()
|
|
|
|
|
canonical, err := npccodec.New().Encode(value)
|
|
|
|
|
if err != nil {
|
2026-11-22 00:02:13 +00:00
|
|
|
t.Fatal(err)
|
2026-07-21 04:34:56 +00:00
|
|
|
}
|
|
|
|
|
pretty, err := json.MarshalIndent(value, "", " ")
|
|
|
|
|
if err != nil {
|
2026-11-22 00:02:13 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
var previous string
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
references contracts.ReferenceSet
|
|
|
|
|
want string
|
|
|
|
|
}{
|
|
|
|
|
{"unbound", contracts.ReferenceSet{}, `{"npcs":[]}`},
|
|
|
|
|
{"canonical", spellNPCRegistryReference(canonical, "file:///one.json"), `{"npcs":[{"name":"Mira Thorn"}]}`},
|
|
|
|
|
{"different formatting and origin", spellNPCRegistryReference(pretty, "file:///two.json"), `{"npcs":[{"name":"Mira Thorn"}]}`},
|
|
|
|
|
} {
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
|
client := &fakeSpellsLLMClient{content: []byte(`{"spell_casts":[]}`)}
|
|
|
|
|
if _, err := newExtractor(t, client, test.references).Extract(context.Background(), extractionRequest()); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
text := promptText(client.requests[0])
|
|
|
|
|
if !strings.Contains(text, test.want) {
|
|
|
|
|
t.Fatalf("missing names-only projection %q", test.want)
|
|
|
|
|
}
|
|
|
|
|
for _, private := range []string{value.NPCs[0].ID, "npc-session", "file:///one.json", "file:///two.json"} {
|
|
|
|
|
if strings.Contains(text, private) {
|
|
|
|
|
t.Fatalf("NPC prompt leaked %q", private)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if test.name == "different formatting and origin" && text != previous {
|
|
|
|
|
t.Fatal("semantic NPC projection changed with provenance/formatting")
|
|
|
|
|
}
|
|
|
|
|
previous = text
|
|
|
|
|
})
|
2026-07-21 04:34:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 00:02:13 +00:00
|
|
|
func TestSpellExtractorResolvesOperationNPCOverride(t *testing.T) {
|
2026-07-21 22:12:01 +00:00
|
|
|
canonical, err := npccodec.New().Encode(registryFixture())
|
|
|
|
|
if err != nil {
|
2026-11-22 00:02:13 +00:00
|
|
|
t.Fatal(err)
|
2026-07-21 22:12:01 +00:00
|
|
|
}
|
2026-11-22 00:02:13 +00:00
|
|
|
client := &fakeSpellsLLMClient{content: []byte(`{"spell_casts":[]}`)}
|
2026-07-21 22:12:01 +00:00
|
|
|
extractor := newExtractor(t, client)
|
|
|
|
|
request := extractionRequest()
|
|
|
|
|
request.References = spellNPCRegistryReference(canonical, "file:///generated.json")
|
|
|
|
|
if _, err := extractor.Extract(context.Background(), request); err != nil {
|
2026-11-22 00:02:13 +00:00
|
|
|
t.Fatal(err)
|
2026-07-21 22:12:01 +00:00
|
|
|
}
|
2026-11-22 00:02:13 +00:00
|
|
|
if !strings.Contains(promptText(client.requests[0]), `{"npcs":[{"name":"Mira Thorn"}]}`) {
|
|
|
|
|
t.Fatal("missing operation NPC projection")
|
2026-07-21 22:12:01 +00:00
|
|
|
}
|
2026-11-22 00:02:13 +00:00
|
|
|
if _, err := extractor.Extract(context.Background(), extractionRequest()); err != nil {
|
|
|
|
|
t.Fatal(err)
|
2026-07-21 22:12:01 +00:00
|
|
|
}
|
2026-11-22 00:02:13 +00:00
|
|
|
if !strings.Contains(promptText(client.requests[1]), `{"npcs":[]}`) {
|
|
|
|
|
t.Fatal("operation override mutated seeded resolver")
|
2026-07-21 22:12:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-05 18:25:04 +00:00
|
|
|
func registryFixture() dnd.NPCRegistry {
|
|
|
|
|
return dnd.NPCRegistry{NPCs: []dnd.NPC{{
|
2026-07-22 18:51:26 +00:00
|
|
|
ID: identity.DeriveID("Mira Thorn"),
|
|
|
|
|
Name: "Mira Thorn",
|
2026-07-21 04:34:56 +00:00
|
|
|
SourceRefs: []source.SourceRef{{SourceID: "npc-session", StartUnitID: 41, EndUnitID: 43}},
|
|
|
|
|
}}}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func spellNPCRegistryReference(content []byte, origin string) contracts.ReferenceSet {
|
|
|
|
|
return contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
|
|
|
|
registry.ReferenceSlot: {
|
|
|
|
|
Slot: contracts.ReferenceSlot{Name: registry.ReferenceSlot, AcceptedMediaTypes: []string{npccodec.MediaType}, MaxBytes: registry.MaxBytes},
|
|
|
|
|
Items: []contracts.ReferenceItem{{
|
|
|
|
|
SlotName: registry.ReferenceSlot,
|
|
|
|
|
MediaType: npccodec.MediaType,
|
|
|
|
|
Content: append([]byte(nil), content...),
|
|
|
|
|
Origin: contracts.ReferenceOrigin{Type: "file", URI: origin},
|
|
|
|
|
}},
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
}
|