122 lines
4.6 KiB
Go
122 lines
4.6 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 index, want := range []struct {
|
|
role string
|
|
cached bool
|
|
marker string
|
|
}{
|
|
{role: "system", marker: "Dungeons & Dragons gameplay transcripts"},
|
|
{role: "user", marker: "Transcript units are the only evidence"},
|
|
{role: "user", cached: true, marker: "most specific supported in-world"},
|
|
{role: "user", cached: true},
|
|
{role: "user", cached: true},
|
|
{role: "user", marker: "interaction occurrences"},
|
|
{role: "user", cached: true, marker: "Use exactly one kind per occurrence"},
|
|
{role: "user"},
|
|
} {
|
|
if index >= len(prepared.Messages) {
|
|
t.Fatalf("prepared prompt has %d messages, want at least %d", len(prepared.Messages), index+1)
|
|
}
|
|
message := prepared.Messages[index]
|
|
if message.Role != want.role {
|
|
t.Errorf("message %d role = %q, want %q", index, message.Role, want.role)
|
|
}
|
|
if want.cached {
|
|
if message.CacheControl == nil || message.CacheControl.Type != scriptorium.CacheControlEphemeral {
|
|
t.Errorf("message %d cache control = %#v, want ephemeral", index, message.CacheControl)
|
|
}
|
|
} else if message.CacheControl != nil {
|
|
t.Errorf("message %d cache control = %#v, want nil", index, message.CacheControl)
|
|
}
|
|
if want.marker != "" && !strings.Contains(message.Content, want.marker) {
|
|
t.Errorf("message %d content does not contain purpose marker %q", index, want.marker)
|
|
}
|
|
}
|
|
if len(prepared.Messages) != 8 {
|
|
t.Fatalf("prepared prompt has %d messages, want 8", len(prepared.Messages))
|
|
}
|
|
if references := prepared.Messages[3].Content; !strings.Contains(references, "Dana: Mira") || !strings.Contains(references, "Mira: ranger") || !strings.Contains(references, "Greencloak: title") {
|
|
t.Fatalf("campaign references message = %q, want rendered reference inputs", references)
|
|
}
|
|
if registry := prepared.Messages[4].Content; !strings.Contains(registry, `{"npcs":[{"name":"Mira Thorn"}]}`) {
|
|
t.Fatalf("NPC registry message = %q, want names-only registry input", registry)
|
|
}
|
|
if final := prepared.Messages[7].Content; !strings.Contains(final, transcript) {
|
|
t.Fatalf("final message = %q, want transcript", final)
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|