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

@@ -40,17 +40,9 @@ func TestCombatPromptPairPreservesNativeContractAndFixedGrounding(t *testing.T)
if first.SelectedProfileID != "notarius/dnd-extraction" || first.SelectedProfileID != next.SelectedProfileID || first.StructuredOutput == nil || !reflect.DeepEqual(first.OutputContract, next.OutputContract) {
t.Fatal("combat native pair lost its schema/default profile")
}
var system, all string
var all string
for _, message := range first.Messages {
all += message.Content
if message.Role == "system" {
system += message.Content
}
}
for _, principle := range []string{"Unmatched actors remain permitted.", "Exclude initiative setup without a turn", "Transcript units are the only evidence", "legendary_action", "lair_action"} {
if !strings.Contains(strings.Join(strings.Fields(system), " "), principle) {
t.Fatalf("missing enduring rule %q", principle)
}
}
for _, marker := range []string{"combat-transcript-marker", "combat-npc-marker", "combat-party-marker"} {
if strings.Count(all, marker) != 1 {

View File

@@ -151,16 +151,15 @@ func TestRegistryNormalizationPromptPairsRetainTheirNativeContracts(t *testing.T
for _, test := range []struct {
id string
guidance string
rules []string
}{
{"notarius/dnd.npc_registry.normalize", "Preserve distinct individuals.", []string{"same individual", "unadorned proper name"}},
{"notarius/dnd.location_registry.normalize", "Keep parent and child places separate.", []string{"same physical place", "parent and child places"}},
{"notarius/dnd.npc_registry.normalize", "Preserve distinct individuals."},
{"notarius/dnd.location_registry.normalize", "Keep parent and child places separate."},
} {
t.Run(test.id, func(t *testing.T) {
initial, err := engine.PrepareExecution(context.Background(), promptkit.RunRequest{
PromptID: test.id, APIKey: "test-only", Inputs: map[string]promptkit.ArtifactRef{
"candidates": promptkit.Inline(`{"candidate_number_range":{"first":1,"last":2},"candidates":[]}`),
"transcript": promptkit.Inline(`{"windows":[]}`),
"candidates": promptkit.Inline(`{"sentinel":"normalization-candidates-sentinel"}`),
"transcript": promptkit.Inline(`{"sentinel":"normalization-transcript-sentinel"}`),
},
})
if err != nil {
@@ -180,20 +179,15 @@ func TestRegistryNormalizationPromptPairsRetainTheirNativeContracts(t *testing.T
first.StructuredOutput == nil || next.StructuredOutput == nil || !reflect.DeepEqual(first.OutputContract, next.OutputContract) {
t.Fatal("normalization pair lost its shared native defaults/output contract")
}
var system string
for _, message := range first.Messages {
if message.Role == "system" {
system += message.Content
}
}
for _, rule := range append(test.rules, "candidate_number_range", "start_unit_id", "canonical_candidate_number") {
if !strings.Contains(system, rule) {
t.Fatalf("missing enduring system principle %q", rule)
}
}
if len(next.Messages) != 1 || next.Messages[0].Role != "user" || !strings.Contains(next.Messages[0].Content, test.guidance) {
t.Fatal("correction did not supply only the findings and replacement assignment")
}
for _, sentinel := range []string{"normalization-candidates-sentinel", "normalization-transcript-sentinel"} {
renderedInputMessageIndex(t, first.Messages, sentinel)
if strings.Contains(next.Messages[0].Content, sentinel) {
t.Fatalf("correction repeated input %q", sentinel)
}
}
})
}
}

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)
}

View File

@@ -12,15 +12,14 @@ import (
"go.maximumdirect.net/promptkit"
)
func TestRegistryExtractionPromptPairsRetainDomainRulesAndContracts(t *testing.T) {
func TestRegistryExtractionPromptPairsRetainInputsAndContracts(t *testing.T) {
_, engine := loadNativeAssets(t)
for _, test := range []struct {
family, instruction string
rules []string
}{
{"npc", "Revise the NPC citations.", []string{"proper name or a stable, individually distinguishing", "factual third-party mention", "Exclude human players"}},
{"item", "Revise the item citations.", []string{"named unique items", "concrete reusable item types", "currency denomination", "generic weapons", "Do not use capitalization as an eligibility test"}},
{"location", "Revise the location citations.", []string{"stable proper name or unique in-world designation", "the room", "Do not use capitalization as an eligibility test"}},
{"npc", "Revise the NPC citations."},
{"item", "Revise the item citations."},
{"location", "Revise the location citations."},
} {
t.Run(test.family, func(t *testing.T) {
id := "notarius/dnd." + test.family + "_registry"
@@ -47,23 +46,15 @@ func TestRegistryExtractionPromptPairsRetainDomainRulesAndContracts(t *testing.T
first.StructuredOutput == nil || correction.StructuredOutput == nil || !reflect.DeepEqual(first.OutputContract, correction.OutputContract) {
t.Fatal("registry prompt pair lost native schema/defaults")
}
var system, all string
var all string
for _, message := range first.Messages {
all += message.Content
if message.Role == "system" {
system += message.Content
}
if strings.Contains(message.Content, "registry-transcript") || strings.Contains(message.Content, "registry-players") {
if message.CacheControl == nil || message.CacheControl.Type != promptkit.CacheControlEphemeral {
t.Fatal("fixed input cache hint lost")
}
}
}
for _, rule := range append(test.rules, "Transcript units are the only evidence", "Use the most specific supported in-world") {
if !strings.Contains(strings.Join(strings.Fields(system), " "), rule) {
t.Fatalf("missing enduring domain rule %q", rule)
}
}
for _, input := range []string{"registry-transcript", "registry-players", "registry-party", "registry-glossary"} {
if strings.Count(all, input) != 1 {
t.Fatalf("fixed input not rendered once: %q", input)

View File

@@ -97,19 +97,11 @@ func TestSceneDescriptionPromptPairRetainsPolicyAndFixedInputs(t *testing.T) {
if first.SelectedProfileID != "notarius/dnd-extraction" || first.SelectedProfileID != next.SelectedProfileID || first.StructuredOutput == nil || !reflect.DeepEqual(first.OutputContract, next.OutputContract) {
t.Fatal("scene pair lost schema/defaults")
}
var system, all string
var all string
for _, message := range first.Messages {
all += message.Content
if message.Role == "system" {
system += message.Content
}
}
for _, rule := range []string{"complete chunk is the evidence boundary", "substantive active combat materially organizes the", "must not invent a proper noun"} {
if !strings.Contains(strings.Join(strings.Fields(system), " "), rule) {
t.Fatalf("missing enduring rule %q", rule)
}
}
for _, material := range []string{"scene-description-transcript", "scene-description-player", "substantive active combat materially organizes the"} {
for _, material := range []string{"scene-description-transcript", "scene-description-player"} {
if strings.Count(all, material) != 1 {
t.Fatalf("fixed input/policy repeated: %q", material)
}