Make prompt tests resilient to wording changes

This commit is contained in:
2026-11-26 03:04:54 +00:00
parent 97e9d51db0
commit fef0530533
9 changed files with 84 additions and 103 deletions

View File

@@ -2,25 +2,32 @@ package assets_test
import (
"context"
"io/fs"
"strings"
"testing"
"gitea.maximumdirect.net/eric/notarius/assets"
"go.maximumdirect.net/promptkit"
)
func TestExtractionPromptComposition(t *testing.T) {
const (
transcriptSentinel = "shared-transcript-sentinel"
playersSentinel = "shared-players-sentinel"
partySentinel = "shared-party-sentinel"
glossarySentinel = "shared-glossary-sentinel"
npcSentinel = "npc-registry-sentinel"
catalogSentinel = "spell-catalog-sentinel"
evidenceSentinel = "Transcript units are the only evidence"
endpointOrderSentinel = "according to transcript order"
combatPolicySentinel = "substantive active combat materially organizes the"
transcriptSentinel = "shared-transcript-sentinel"
playersSentinel = "shared-players-sentinel"
partySentinel = "shared-party-sentinel"
glossarySentinel = "shared-glossary-sentinel"
npcSentinel = "npc-registry-sentinel"
catalogSentinel = "spell-catalog-sentinel"
)
_, engine := loadNativeAssets(t)
evidence, err := fs.ReadFile(assets.FS(), "dnd/shared/prompts/common-dnd-extraction-evidence.md")
if err != nil {
t.Fatal(err)
}
combatPolicy, err := fs.ReadFile(assets.FS(), "dnd/shared/prompts/common-dnd-scene-combat-policy.md")
if err != nil {
t.Fatal(err)
}
commonInputs := map[string]promptkit.ArtifactRef{
"transcript": promptkit.InlineWithURI("file:///session.json", `{"sentinel":"`+transcriptSentinel+`"}`),
"players": promptkit.Inline(playersSentinel),
@@ -28,26 +35,26 @@ func TestExtractionPromptComposition(t *testing.T) {
"glossary": promptkit.Inline(glossarySentinel),
}
cases := []struct {
name string
promptID string
inputs map[string]promptkit.ArtifactRef
suffixGroups [][]string
middleSentinel string
name string
promptID string
inputs map[string]promptkit.ArtifactRef
suffixGroups [][]string
scenePolicy bool
}{
{name: "npcs", promptID: "notarius/dnd.npc_registry", inputs: commonInputs, suffixGroups: [][]string{{evidenceSentinel, endpointOrderSentinel}}},
{name: "locations", promptID: "notarius/dnd.location_registry", inputs: commonInputs, suffixGroups: [][]string{{evidenceSentinel, endpointOrderSentinel}}},
{name: "npcs", promptID: "notarius/dnd.npc_registry", inputs: commonInputs},
{name: "locations", promptID: "notarius/dnd.location_registry", inputs: commonInputs},
{name: "item occurrences", promptID: "notarius/dnd.item_occurrences", inputs: withPromptInputs(commonInputs, map[string]promptkit.ArtifactRef{
"item_registry": promptkit.Inline(`{"items":[{"id":"item-registry-sentinel","name":"Torch"}]}`),
}), suffixGroups: [][]string{{evidenceSentinel, endpointOrderSentinel}, {"item-registry-sentinel"}}},
{name: "item registry", promptID: "notarius/dnd.item_registry", inputs: commonInputs, suffixGroups: [][]string{{evidenceSentinel, endpointOrderSentinel}}},
{name: "scene descriptions", promptID: "notarius/dnd.scene_descriptions", inputs: commonInputs, middleSentinel: combatPolicySentinel},
}), suffixGroups: [][]string{{"item-registry-sentinel"}}},
{name: "item registry", promptID: "notarius/dnd.item_registry", inputs: commonInputs},
{name: "scene descriptions", promptID: "notarius/dnd.scene_descriptions", inputs: commonInputs, scenePolicy: true},
{
name: "combat turns",
promptID: "notarius/dnd.combat_turns",
inputs: withPromptInputs(commonInputs, map[string]promptkit.ArtifactRef{
"npc_registry": promptkit.Inline(`{"sentinel":"` + npcSentinel + `"}`),
}),
suffixGroups: [][]string{{evidenceSentinel, endpointOrderSentinel}, {npcSentinel}},
suffixGroups: [][]string{{npcSentinel}},
},
{
name: "enemy events",
@@ -58,7 +65,6 @@ func TestExtractionPromptComposition(t *testing.T) {
"npc_occurrences": promptkit.Inline(`{"sentinel":"npc-occurrences-sentinel"}`),
}),
suffixGroups: [][]string{
{evidenceSentinel, endpointOrderSentinel},
{npcSentinel},
{"combat-turns-sentinel", "npc-occurrences-sentinel"},
},
@@ -69,7 +75,7 @@ func TestExtractionPromptComposition(t *testing.T) {
inputs: withPromptInputs(commonInputs, map[string]promptkit.ArtifactRef{
"npc_registry": promptkit.Inline(`{"sentinel":"` + npcSentinel + `"}`),
}),
suffixGroups: [][]string{{evidenceSentinel, endpointOrderSentinel}, {npcSentinel}},
suffixGroups: [][]string{{npcSentinel}},
},
{
name: "location occurrences",
@@ -77,7 +83,7 @@ func TestExtractionPromptComposition(t *testing.T) {
inputs: withPromptInputs(commonInputs, map[string]promptkit.ArtifactRef{
"location_registry": promptkit.Inline(`{"sentinel":"location-registry-sentinel"}`),
}),
suffixGroups: [][]string{{evidenceSentinel, endpointOrderSentinel}, {"location-registry-sentinel"}},
suffixGroups: [][]string{{"location-registry-sentinel"}},
},
{
name: "spells",
@@ -86,7 +92,7 @@ func TestExtractionPromptComposition(t *testing.T) {
"npc_registry": promptkit.Inline(`{"sentinel":"` + npcSentinel + `"}`),
"spell_catalog": promptkit.Inline(`{"sentinel":"` + catalogSentinel + `"}`),
}),
suffixGroups: [][]string{{evidenceSentinel, endpointOrderSentinel}, {npcSentinel}, {catalogSentinel}},
suffixGroups: [][]string{{npcSentinel}, {catalogSentinel}},
},
}
@@ -105,12 +111,24 @@ func TestExtractionPromptComposition(t *testing.T) {
prepared := handle.Details()
transcriptIndex := renderedInputMessageIndex(t, prepared.Messages, transcriptSentinel)
referenceIndex := renderedInputMessageIndex(t, prepared.Messages, playersSentinel)
for _, sentinel := range []string{partySentinel, glossarySentinel} {
if index := renderedInputMessageIndex(t, prepared.Messages, sentinel); index != referenceIndex {
t.Fatalf("reference %q rendered at message %d, want %d", sentinel, index, referenceIndex)
}
}
if referenceIndex >= transcriptIndex {
t.Fatalf("campaign references rendered at message %d, want before transcript message %d", referenceIndex, transcriptIndex)
}
assertEphemeralCache(t, prepared.Messages, referenceIndex)
assertEphemeralCache(t, prepared.Messages, transcriptIndex)
previousIndex := transcriptIndex
shared := evidence
if testCase.scenePolicy {
shared = combatPolicy
}
previousIndex := renderedInputMessageIndex(t, prepared.Messages, strings.TrimSpace(string(shared)))
if previousIndex <= transcriptIndex || prepared.Messages[previousIndex].Role != "system" {
t.Fatal("shared system message must follow the transcript")
}
for _, group := range testCase.suffixGroups {
groupIndex := -1
for _, sentinel := range group {
@@ -127,12 +145,6 @@ func TestExtractionPromptComposition(t *testing.T) {
previousIndex = groupIndex
}
instructionIndex := len(prepared.Messages) - 1
if testCase.middleSentinel != "" {
policyIndex := renderedInputMessageIndex(t, prepared.Messages, testCase.middleSentinel)
if policyIndex <= transcriptIndex || policyIndex >= instructionIndex {
t.Fatalf("combat policy rendered at message %d, want after transcript message %d and before instructions message %d", policyIndex, transcriptIndex, instructionIndex)
}
}
if instructionIndex <= previousIndex {
t.Fatalf("instructions rendered at message %d, want after lane input message %d", instructionIndex, previousIndex)
}