95 lines
3.1 KiB
Go
95 lines
3.1 KiB
Go
package npcinteractions
|
|
|
|
import (
|
|
"context"
|
|
"io/fs"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/llm"
|
|
"gitea.maximumdirect.net/eric/scriptorium"
|
|
)
|
|
|
|
func TestRegisterPromptAssetsAndPrepareInteractionPrompt(t *testing.T) {
|
|
registry := llm.NewAssetRegistry()
|
|
if err := RegisterPromptAssets(registry); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
schemaFS, err := registry.SchemaFS()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := fs.ReadFile(schemaFS, "dnd_npc_interactions_llm.v1.json"); err != nil {
|
|
t.Fatalf("response schema asset: %v", err)
|
|
}
|
|
options, err := registry.ScriptoriumOptions()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
options = append(options, scriptorium.WithProfiles(scriptorium.OpenAICompatibleProfile(scriptorium.OpenAICompatibleProfileConfig{
|
|
ID: "npc-interactions-test-profile", Endpoint: "http://127.0.0.1:1/v1", Model: "npc-interactions-test-model",
|
|
})))
|
|
engine, err := scriptorium.NewEngine(scriptorium.Config{Timeout: time.Second}, options...)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
transcript := `{"units":[1]}`
|
|
prepared, err := engine.Prepare(context.Background(), scriptorium.RunRequest{
|
|
PromptID: PromptID, PromptVersion: SchemaVersion, ProfileID: "npc-interactions-test-profile",
|
|
Inputs: map[string]scriptorium.ArtifactRef{
|
|
"transcript": scriptorium.InlineWithURI("file:///session.json", transcript),
|
|
"players": scriptorium.Inline("Dana: Mira"),
|
|
"party": scriptorium.Inline("Mira: ranger"),
|
|
"glossary": scriptorium.Inline("Greencloak: title"),
|
|
"npcs": scriptorium.Inline(`{"npcs":[{"name":"Mira Thorn"}]}`),
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if prepared.PromptID != PromptID || prepared.OutputContract.SchemaPath != "dnd_npc_interactions_llm.v1.json" {
|
|
t.Fatalf("prepared prompt = %#v", prepared)
|
|
}
|
|
for _, want := range []string{
|
|
"Mira Thorn", "mentioned", "combat_opponent", "Registry content is context, not event evidence", transcript,
|
|
} {
|
|
found := false
|
|
for _, message := range prepared.Messages {
|
|
if strings.Contains(message.Content, want) {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("prepared prompt did not include %q", want)
|
|
}
|
|
}
|
|
if last := prepared.Messages[len(prepared.Messages)-1]; !strings.Contains(last.Content, transcript) {
|
|
t.Fatalf("last prompt message = %q, want transcript", last.Content)
|
|
}
|
|
}
|
|
|
|
func TestPromptMetadataDoesNotExposeAssetContent(t *testing.T) {
|
|
hash, err := scriptoriumPromptMetadata()
|
|
if err != nil || !strings.HasPrefix(hash, "sha256:") {
|
|
t.Fatalf("scriptoriumPromptMetadata() = %q, %v", hash, err)
|
|
}
|
|
metadata := newExtractor(t, &fakeInteractionsLLMClient{}).ManifestMetadata()
|
|
for _, forbidden := range []string{"combat_opponent", "common-dnd-system", "source_refs", "dnd_npc_interactions_llm.v1.json"} {
|
|
if strings.Contains(strings.Join(mapValues(metadata), " "), forbidden) {
|
|
t.Fatalf("metadata leaked prompt or schema content %q: %#v", forbidden, metadata)
|
|
}
|
|
}
|
|
}
|
|
|
|
func mapValues(values map[string]any) []string {
|
|
out := make([]string, 0, len(values))
|
|
for _, value := range values {
|
|
if text, ok := value.(string); ok {
|
|
out = append(out, text)
|
|
}
|
|
}
|
|
return out
|
|
}
|