91 lines
5.1 KiB
Go
91 lines
5.1 KiB
Go
|
|
package shared
|
||
|
|
|
||
|
|
import (
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||
|
|
)
|
||
|
|
|
||
|
|
func contextReference(content string, collections []string) contracts.ResolvedReferenceSlot {
|
||
|
|
return contracts.ResolvedReferenceSlot{Items: []contracts.ReferenceItem{{MediaType: "application/json", Content: []byte(content), Collections: collections}}}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPreparedContextProjectsSelectedReferences(t *testing.T) {
|
||
|
|
party := `{"schema_version":"maximumdirect.rpg.party.v1","characters":{"z":{"character":{"name":"Mira","aliases":["Shadow"],"classes":[{"name":"Ranger","level":3}]},"player":{"name":"Dana"}},"a":{"character":{"name":"Mira"},"player":{"name":"Lee"}}},"instruction":"not a prompt"}`
|
||
|
|
glossary := `{"schema_version":"maximumdirect.glossary.v1","collections":{"campaign":{"entries":[{"id":"z","name":"Greencloak","category":"title","aliases":["Green"],"description":"local title","plural":"Greencloaks","instruction":"not a prompt"}]},"setting":{"entries":[]},"spells":{"entries":[{"id":"x","name":"Hidden Spell","category":"spell"}]}}}`
|
||
|
|
references := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||
|
|
"party": contextReference(party, nil),
|
||
|
|
"glossary": contextReference(glossary, nil),
|
||
|
|
"players": {Items: []contracts.ReferenceItem{{Content: []byte("Supplemental players")}}},
|
||
|
|
}}
|
||
|
|
prepared, err := PrepareContext(references)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
inputs := prepared.Inputs(contracts.NewLLMInputMaterial("source", "text/plain", []byte("current transcript"), "", ""))
|
||
|
|
partyBody := string(inputs["party"].Content)
|
||
|
|
if strings.Count(partyBody, `"name":"Mira"`) != 2 || !strings.Contains(partyBody, "Shadow") || !strings.Contains(partyBody, "Dana") || !strings.Contains(partyBody, "Ranger") || strings.Index(partyBody, "Lee") > strings.Index(partyBody, "Dana") {
|
||
|
|
t.Fatalf("party projection = %s", partyBody)
|
||
|
|
}
|
||
|
|
glossaryBody := string(inputs["glossary"].Content)
|
||
|
|
for _, value := range []string{"Greencloak", "Green", "title", "local title", "Greencloaks"} {
|
||
|
|
if !strings.Contains(glossaryBody, value) {
|
||
|
|
t.Fatalf("glossary projection missing %q: %s", value, glossaryBody)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if strings.Contains(glossaryBody, "Hidden Spell") || strings.Contains(partyBody+glossaryBody, "not a prompt") || strings.Contains(glossaryBody, `"id"`) {
|
||
|
|
t.Fatal("irrelevant document content reached model context")
|
||
|
|
}
|
||
|
|
if string(inputs["players"].Content) != "Supplemental players" || string(inputs["transcript"].Content) != "current transcript" {
|
||
|
|
t.Fatalf("supplemental context or transcript changed: %#v", inputs)
|
||
|
|
}
|
||
|
|
inputs["party"].Content[0] = '!'
|
||
|
|
if strings.HasPrefix(string(prepared.Inputs(contracts.LLMInputMaterial{})["party"].Content), "!") {
|
||
|
|
t.Fatal("caller mutated prepared context")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPreparedContextValidatesAllTypedSlotsAndSelection(t *testing.T) {
|
||
|
|
validParty := `{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"Aria"}}}}`
|
||
|
|
glossary := `{"schema_version":"maximumdirect.glossary.v1","collections":{"other":{"entries":[{"id":"a","name":"Chosen","category":"term"}]}}}`
|
||
|
|
refs := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||
|
|
"party": contextReference(validParty, nil),
|
||
|
|
"roster": contextReference("{}", nil),
|
||
|
|
}}
|
||
|
|
if _, err := PrepareContext(refs); err == nil || !strings.Contains(err.Error(), "roster") {
|
||
|
|
t.Fatalf("invalid overridden roster accepted: %v", err)
|
||
|
|
}
|
||
|
|
delete(refs.Slots, "roster")
|
||
|
|
refs.Slots["glossary"] = contextReference(glossary, []string{"other"})
|
||
|
|
got, err := PrepareContext(refs)
|
||
|
|
if err != nil || !strings.Contains(string(got.Inputs(contracts.LLMInputMaterial{})["glossary"].Content), "Chosen") {
|
||
|
|
t.Fatalf("selected glossary missing: %v", err)
|
||
|
|
}
|
||
|
|
refs.Slots["glossary"] = contextReference(glossary, []string{})
|
||
|
|
got, err = PrepareContext(refs)
|
||
|
|
if err != nil || string(got.Inputs(contracts.LLMInputMaterial{})["glossary"].Content) != " " {
|
||
|
|
t.Fatalf("empty selection = %#v, %v", got, err)
|
||
|
|
}
|
||
|
|
refs.Slots["glossary"] = contextReference(glossary, []string{"missing"})
|
||
|
|
if _, err := PrepareContext(refs); err == nil || !strings.Contains(err.Error(), "missing") {
|
||
|
|
t.Fatalf("missing selection accepted: %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPreparedContextRosterFallbackAndPartyPrecedence(t *testing.T) {
|
||
|
|
party := `{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"Party member"}}}}`
|
||
|
|
roster := `{"schema_version":"maximumdirect.rpg.party.v1","characters":{"b":{"character":{"name":"Roster member"}}}}`
|
||
|
|
refs := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{"roster": contextReference(roster, nil)}}
|
||
|
|
prepared, err := PrepareContext(refs)
|
||
|
|
if err != nil || !strings.Contains(string(prepared.Inputs(contracts.LLMInputMaterial{})["party"].Content), "Roster member") {
|
||
|
|
t.Fatalf("roster fallback: %v", err)
|
||
|
|
}
|
||
|
|
refs.Slots["party"] = contextReference(party, nil)
|
||
|
|
prepared, err = PrepareContext(refs)
|
||
|
|
body := string(prepared.Inputs(contracts.LLMInputMaterial{})["party"].Content)
|
||
|
|
if err != nil || !strings.Contains(body, "Party member") || strings.Contains(body, "Roster member") {
|
||
|
|
t.Fatalf("party precedence = %q, %v", body, err)
|
||
|
|
}
|
||
|
|
}
|