138 lines
5.2 KiB
Go
138 lines
5.2 KiB
Go
package scenes
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/llm"
|
|
"gitea.maximumdirect.net/eric/promptkit"
|
|
)
|
|
|
|
func TestScriptoriumPromptPreparesTranscriptAndTaskMessages(t *testing.T) {
|
|
transcript := []byte(`{"sentinel":"scene-transcript"}`)
|
|
prepared := prepareScenesPrompt(t, transcript, "scene-players", "scene-party", "scene-glossary")
|
|
|
|
if prepared.PromptID != PromptID {
|
|
t.Fatalf("prompt id = %q, want %q", prepared.PromptID, PromptID)
|
|
}
|
|
if len(prepared.Messages) != 5 {
|
|
t.Fatalf("prepared messages = %d, want 5", len(prepared.Messages))
|
|
}
|
|
for index, role := range []string{"system", "user", "user", "user", "user"} {
|
|
if prepared.Messages[index].Role != role {
|
|
t.Errorf("message %d role = %q, want %q", index, prepared.Messages[index].Role, role)
|
|
}
|
|
}
|
|
for _, index := range []int{1, 4} {
|
|
if cache := prepared.Messages[index].CacheControl; cache == nil || cache.Type != promptkit.CacheControlEphemeral {
|
|
t.Errorf("message %d cache control = %#v, want ephemeral", index, cache)
|
|
}
|
|
}
|
|
for _, index := range []int{0, 2, 3} {
|
|
if cache := prepared.Messages[index].CacheControl; cache != nil {
|
|
t.Errorf("message %d cache control = %#v, want nil", index, cache)
|
|
}
|
|
}
|
|
if references := prepared.Messages[1].Content; !strings.Contains(references, "scene-players") || !strings.Contains(references, "scene-party") || !strings.Contains(references, "scene-glossary") {
|
|
t.Fatalf("reference message = %q, want supplied reference inputs", references)
|
|
}
|
|
if transcriptMessage := prepared.Messages[4].Content; !strings.Contains(transcriptMessage, "scene-transcript") {
|
|
t.Fatalf("final message = %q, want transcript input", transcriptMessage)
|
|
}
|
|
for index, message := range prepared.Messages {
|
|
if index != 1 && (strings.Contains(message.Content, "scene-players") || strings.Contains(message.Content, "scene-party") || strings.Contains(message.Content, "scene-glossary")) {
|
|
t.Errorf("message %d unexpectedly rendered reference input", index)
|
|
}
|
|
if index != 4 && strings.Contains(message.Content, "scene-transcript") {
|
|
t.Errorf("message %d unexpectedly rendered transcript input", index)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestScriptoriumPromptDiagnosticsOmitRawMaterials(t *testing.T) {
|
|
transcript := []byte(`{"secret":"source text"}`)
|
|
prepared := prepareScenesPrompt(t, transcript, "private player note", "private party note", "private glossary note")
|
|
metadata := newChunker(t, &fakeScenesLLMClient{}).ManifestMetadata()
|
|
|
|
payload, err := json.Marshal(map[string]any{
|
|
"prepared": map[string]any{
|
|
"prompt_id": prepared.PromptID,
|
|
"prompt_version": prepared.PromptVersion,
|
|
"prompt_hash": prepared.PromptHash,
|
|
"rendered_prompt_hash": prepared.RenderedPromptHash,
|
|
"selected_profile_id": prepared.SelectedProfileID,
|
|
"output_contract": prepared.OutputContract,
|
|
"input_hashes": prepared.InputHashes,
|
|
"effective_model_params": prepared.EffectiveModelParams,
|
|
},
|
|
"manifest": metadata,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal diagnostics: %v", err)
|
|
}
|
|
diagnostics := string(payload)
|
|
for _, forbidden := range []string{
|
|
"source text",
|
|
"private player note",
|
|
"private party note",
|
|
"private glossary note",
|
|
`"properties"`,
|
|
"start_unit_id",
|
|
} {
|
|
if strings.Contains(diagnostics, forbidden) {
|
|
t.Fatalf("diagnostics leaked %q: %s", forbidden, diagnostics)
|
|
}
|
|
}
|
|
if metadata["prompt_id"] != PromptID || metadata["prompt_version"] != ResponseSchemaVersion {
|
|
t.Fatalf("manifest prompt metadata = %#v", metadata)
|
|
}
|
|
if !strings.HasPrefix(metadata["prompt_sha256"].(string), "sha256:") {
|
|
t.Fatalf("manifest prompt hash = %#v, want sha256-prefixed", metadata["prompt_sha256"])
|
|
}
|
|
}
|
|
|
|
func prepareScenesPrompt(t *testing.T, transcript []byte, players string, party string, glossary string) *promptkit.PreparedRun {
|
|
t.Helper()
|
|
registry := llm.NewAssetRegistry()
|
|
if err := RegisterPromptAssets(registry); err != nil {
|
|
t.Fatalf("register scene prompt assets: %v", err)
|
|
}
|
|
engine := newScenesScriptoriumEngine(t, registry)
|
|
prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{
|
|
PromptID: PromptID,
|
|
PromptVersion: ResponseSchemaVersion,
|
|
ProfileID: "scene-test-profile",
|
|
Inputs: map[string]promptkit.ArtifactRef{
|
|
"transcript": promptkit.InlineWithURI("file:///session.json", string(transcript)),
|
|
"players": promptkit.Inline(players),
|
|
"party": promptkit.Inline(party),
|
|
"glossary": promptkit.Inline(glossary),
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Prepare() error = %v, want nil", err)
|
|
}
|
|
return prepared
|
|
}
|
|
|
|
func newScenesScriptoriumEngine(t *testing.T, registry *llm.AssetRegistry) *promptkit.Engine {
|
|
t.Helper()
|
|
options, err := registry.PromptKitOptions()
|
|
if err != nil {
|
|
t.Fatalf("PromptKitOptions() error = %v, want nil", err)
|
|
}
|
|
options = append(options, promptkit.WithProfiles(promptkit.OpenAICompatibleProfile(promptkit.OpenAICompatibleProfileConfig{
|
|
ID: "scene-test-profile",
|
|
Endpoint: "http://127.0.0.1:1/v1",
|
|
Model: "scene-test-model",
|
|
})))
|
|
engine, err := promptkit.NewEngine(promptkit.Config{Timeout: time.Second}, options...)
|
|
if err != nil {
|
|
t.Fatalf("NewEngine() error = %v, want nil", err)
|
|
}
|
|
return engine
|
|
}
|