108 lines
4.0 KiB
Go
108 lines
4.0 KiB
Go
package npcs
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/llm"
|
|
"gitea.maximumdirect.net/eric/scriptorium"
|
|
)
|
|
|
|
func TestRegisterPromptAssetsAndPrepareNPCPrompt(t *testing.T) {
|
|
registry := llm.NewAssetRegistry()
|
|
if err := RegisterPromptAssets(registry); err != nil {
|
|
t.Fatalf("RegisterPromptAssets() error = %v, want nil", err)
|
|
}
|
|
options, err := registry.ScriptoriumOptions()
|
|
if err != nil {
|
|
t.Fatalf("ScriptoriumOptions() error = %v, want nil", err)
|
|
}
|
|
options = append(options, scriptorium.WithProfiles(scriptorium.OpenAICompatibleProfile(scriptorium.OpenAICompatibleProfileConfig{
|
|
ID: "npc-test-profile", Endpoint: "http://127.0.0.1:1/v1", Model: "npc-test-model",
|
|
})))
|
|
engine, err := scriptorium.NewEngine(scriptorium.Config{Timeout: time.Second}, options...)
|
|
if err != nil {
|
|
t.Fatalf("NewEngine() error = %v, want nil", err)
|
|
}
|
|
prepared, err := engine.Prepare(context.Background(), scriptorium.RunRequest{
|
|
PromptID: PromptID, PromptVersion: SchemaVersion, ProfileID: "npc-test-profile",
|
|
Inputs: map[string]scriptorium.ArtifactRef{
|
|
"transcript": scriptorium.InlineWithURI("file:///session.json", `{"units":[1]}`),
|
|
"players": scriptorium.Inline("Dana: Mira"),
|
|
"party": scriptorium.Inline("Mira: ranger"),
|
|
"glossary": scriptorium.Inline("Greencloak: title"),
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Prepare() error = %v, want nil", err)
|
|
}
|
|
if prepared.PromptID != PromptID || prepared.OutputContract.SchemaPath != "dnd_npcs_llm.v1.json" {
|
|
t.Fatalf("prepared prompt = %#v, want NPC prompt identity and wiring", prepared)
|
|
}
|
|
for index, want := range []struct {
|
|
role string
|
|
cached bool
|
|
}{
|
|
{role: "system"},
|
|
{role: "user"},
|
|
{role: "user", cached: true},
|
|
{role: "user", cached: true},
|
|
{role: "user"},
|
|
{role: "user", cached: true},
|
|
{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 len(prepared.Messages) != 7 {
|
|
t.Fatalf("prepared prompt has %d messages, want 7", 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 transcript := prepared.Messages[6].Content; !strings.Contains(transcript, `{"units":[1]}`) {
|
|
t.Fatalf("final message = %q, want transcript", transcript)
|
|
}
|
|
transcriptMessages := 0
|
|
for _, message := range prepared.Messages {
|
|
if strings.Contains(message.Content, `{"units":[1]}`) {
|
|
transcriptMessages++
|
|
}
|
|
}
|
|
if transcriptMessages != 1 {
|
|
t.Fatalf("raw transcript rendered in %d messages, want exactly one", transcriptMessages)
|
|
}
|
|
}
|
|
|
|
func TestPromptMetadataAndDiagnosticsDoNotContainRawAssets(t *testing.T) {
|
|
hash, err := scriptoriumPromptMetadata()
|
|
if err != nil || !strings.HasPrefix(hash, "sha256:") {
|
|
t.Fatalf("scriptoriumPromptMetadata() = %q, %v; want hash", hash, err)
|
|
}
|
|
metadata := newExtractor(t, &fakeNPCsLLMClient{}).ManifestMetadata()
|
|
payload, err := json.Marshal(metadata)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, forbidden := range []string{"Include an in-world", "common-dnd-system", "source-unit", "dnd_npcs_llm.v1.json"} {
|
|
if strings.Contains(string(payload), forbidden) {
|
|
t.Fatalf("metadata leaked raw prompt/schema content %q: %s", forbidden, payload)
|
|
}
|
|
}
|
|
}
|