package spells import ( "context" "encoding/json" "strings" "testing" "time" "gitea.maximumdirect.net/eric/notarius/internal/framework/llm" "gitea.maximumdirect.net/eric/scriptorium" ) func TestScriptoriumPromptPreparesTranscriptReferencesAndTaskMessages(t *testing.T) { transcript := []byte(`{"id":"session-1","segments":[{"id":"u1","text":"Mira casts shield."}]}`) prepared := prepareSpellsPrompt(t, transcript, "Dana: Mira", "Mira: wizard", "Shield: abjuration") if prepared.PromptID != PromptID { t.Fatalf("prompt id = %q, want %q", prepared.PromptID, PromptID) } if prepared.OutputContract.SchemaPath != "dnd_spells_llm.v1.json" { t.Fatalf("schema path = %q, want LLM-only schema", prepared.OutputContract.SchemaPath) } 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"}, {role: "user", marker: "spell-cast artifacts"}, {role: "user", cached: true, marker: "source references must collectively support"}, {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) != 9 { t.Fatalf("prepared prompt has %d messages, want 9", len(prepared.Messages)) } if references := prepared.Messages[3].Content; !strings.Contains(references, "Dana: Mira") || !strings.Contains(references, "Mira: wizard") || !strings.Contains(references, "Shield: abjuration") { t.Fatalf("campaign references message = %q, want rendered reference inputs", references) } if registry := prepared.Messages[4].Content; !strings.Contains(registry, `{"npcs":[]}`) { t.Fatalf("NPC registry message = %q, want registry input", registry) } if catalog := prepared.Messages[5].Content; !strings.Contains(catalog, `{"spell_names":["Cure Wounds"]}`) { t.Fatalf("spell catalog message = %q, want catalog input", catalog) } if final := prepared.Messages[8].Content; !strings.Contains(final, string(transcript)) { t.Fatalf("final message = %q, want transcript", final) } } func TestScriptoriumPromptPreparesWithMissingOptionalReferences(t *testing.T) { transcript := []byte(`{"id":"session-1","segments":[]}`) prepared := prepareSpellsPrompt(t, transcript, " ", " ", " ") for _, message := range prepared.Messages { if strings.Contains(message.Content, "Player list reference:") && strings.Contains(message.Content, "Party roster reference:") && strings.Contains(message.Content, "Glossary reference:") { return } } t.Fatalf("reference message did not render empty optional reference placeholders") } func TestScriptoriumPromptDiagnosticsOmitRawMaterials(t *testing.T) { transcript := []byte(`{"secret":"source text"}`) reference := "private party note" prepared := prepareSpellsPrompt(t, transcript, "private player note", reference, " ") metadata := newExtractor(t, &fakeSpellsLLMClient{}, overlaySpellCatalogReference()).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", reference, "Cure Wounds", "Aegis of Emberfall", "Emberfall Aegis", "Private campaign source", "file:///private-source.json", `"properties"`, "spell_casts", } { if strings.Contains(diagnostics, forbidden) { t.Fatalf("diagnostics leaked %q: %s", forbidden, diagnostics) } } if metadata["prompt_id"] != PromptID || metadata["prompt_version"] != SchemaVersion { 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 prepareSpellsPrompt(t *testing.T, transcript []byte, players string, party string, glossary string) *scriptorium.PreparedRun { t.Helper() registry := llm.NewAssetRegistry() if err := RegisterPromptAssets(registry); err != nil { t.Fatalf("register spell prompt assets: %v", 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: "spell-test-profile", Endpoint: "http://127.0.0.1:1/v1", Model: "spell-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: "spell-test-profile", Inputs: map[string]scriptorium.ArtifactRef{ "transcript": scriptorium.InlineWithURI("file:///session.json", string(transcript)), "spell_catalog": scriptorium.Inline(`{"spell_names":["Cure Wounds"]}`), "npcs": scriptorium.Inline(`{"npcs":[]}`), "players": scriptorium.Inline(players), "party": scriptorium.Inline(party), "glossary": scriptorium.Inline(glossary), }, }) if err != nil { t.Fatalf("Prepare() error = %v, want nil", err) } return prepared }