115 lines
3.9 KiB
Go
115 lines
3.9 KiB
Go
package itemevents
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io/fs"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/llm"
|
|
"gitea.maximumdirect.net/eric/scriptorium"
|
|
)
|
|
|
|
func TestPromptAssetsUseSharedSequenceAndTranscriptLast(t *testing.T) {
|
|
registry := llm.NewAssetRegistry()
|
|
if err := RegisterPromptAssets(registry); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
options, err := registry.ScriptoriumOptions()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
options = append(options, scriptorium.WithProfiles(scriptorium.OpenAICompatibleProfile(scriptorium.OpenAICompatibleProfileConfig{
|
|
ID: "item-events-test-profile", Endpoint: "http://127.0.0.1:1/v1", Model: "item-events-test-model",
|
|
})))
|
|
engine, err := scriptorium.NewEngine(scriptorium.Config{Timeout: time.Second}, options...)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
prepared, err := engine.Prepare(context.Background(), scriptorium.RunRequest{
|
|
PromptID: PromptID, PromptVersion: SchemaVersion, ProfileID: "item-events-test-profile",
|
|
Inputs: map[string]scriptorium.ArtifactRef{
|
|
"transcript": scriptorium.InlineWithURI("file:///session.json", `{"segments":[1]}`),
|
|
"players": scriptorium.Inline("Dana: Aria"),
|
|
"party": scriptorium.Inline("Aria: ranger"),
|
|
"glossary": scriptorium.Inline("Moonblade: heirloom"),
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if prepared.PromptID != PromptID || prepared.OutputContract.SchemaPath != "dnd_item_events_llm.v1.json" {
|
|
t.Fatalf("prepared prompt = %#v", prepared)
|
|
}
|
|
want := []struct {
|
|
role string
|
|
cached bool
|
|
marker string
|
|
}{
|
|
{"system", false, "Dungeons & Dragons gameplay transcripts"},
|
|
{"user", false, "Transcript units are the only evidence"},
|
|
{"user", true, "most specific supported in-world"},
|
|
{"user", true, "Dana: Aria"},
|
|
{"user", false, "item and currency events"},
|
|
{"user", true, "Ordinary non-depleting use"},
|
|
{"user", false, `{"segments":[1]}`},
|
|
}
|
|
if len(prepared.Messages) != len(want) {
|
|
t.Fatalf("prompt messages = %d, want %d", len(prepared.Messages), len(want))
|
|
}
|
|
for index, expected := range want {
|
|
message := prepared.Messages[index]
|
|
if message.Role != expected.role || !strings.Contains(message.Content, expected.marker) {
|
|
t.Fatalf("message %d = %#v", index, message)
|
|
}
|
|
if (message.CacheControl != nil) != expected.cached {
|
|
t.Fatalf("message %d cache control = %#v", index, message.CacheControl)
|
|
}
|
|
}
|
|
if strings.Contains(prepared.Messages[len(prepared.Messages)-1].Content, "Moonblade: heirloom") {
|
|
t.Fatal("transcript message contains optional reference content")
|
|
}
|
|
}
|
|
|
|
func TestPromptAssetsDoNotLeakIntoMetadata(t *testing.T) {
|
|
hash, err := scriptoriumPromptMetadata()
|
|
if err != nil || !strings.HasPrefix(hash, "sha256:") {
|
|
t.Fatalf("scriptoriumPromptMetadata() = %q, %v", hash, err)
|
|
}
|
|
metadata := newExtractor(t, &fakeItemEventsLLMClient{}).ManifestMetadata()
|
|
payload, err := json.Marshal(metadata)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, forbidden := range []string{"meaningful Dungeons", "common-dnd-system", "start_segment", "dnd_item_events_llm.v1.json"} {
|
|
if strings.Contains(string(payload), forbidden) {
|
|
t.Fatalf("metadata leaked raw asset content %q: %s", forbidden, payload)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPromptManifestReusesOnlySharedAssets(t *testing.T) {
|
|
want := []string{
|
|
"common-dnd-system.md",
|
|
"common-dnd-extraction-evidence.md",
|
|
"common-dnd-identity.md",
|
|
"common-dnd-references.md",
|
|
"common-dnd-transcript.md",
|
|
}
|
|
if !reflect.DeepEqual(promptAssetManifest.SharedFiles, want) {
|
|
t.Fatalf("shared assets = %#v, want %#v", promptAssetManifest.SharedFiles, want)
|
|
}
|
|
for _, path := range []string{"assets/prompts/task.md", "assets/prompts/instructions.md"} {
|
|
content, err := fs.ReadFile(embeddedAssets, path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(content), "Transcript units are the only evidence") || strings.Contains(string(content), "Dungeons & Dragons gameplay transcripts") {
|
|
t.Fatalf("module asset %q copied shared prompt text", path)
|
|
}
|
|
}
|
|
}
|