2026-11-22 01:13:57 +00:00
package assets_test
import (
"context"
"encoding/json"
"io/fs"
"reflect"
"strings"
"testing"
"gitea.maximumdirect.net/eric/notarius/assets"
"go.maximumdirect.net/promptkit"
)
func TestCombatPromptPairPreservesNativeContractAndFixedGrounding ( t * testing . T ) {
_ , engine := loadNativeAssets ( t )
initial , err := engine . PrepareExecution ( context . Background ( ) , promptkit . RunRequest {
PromptID : "notarius/dnd.combat_turns" , APIKey : "test-only" ,
Inputs : map [ string ] promptkit . ArtifactRef {
"transcript" : promptkit . Inline ( ` { "units":[ { "text":"combat-transcript-marker"}]} ` ) ,
"npc_registry" : promptkit . Inline ( ` { "npcs":[ { "name":"combat-npc-marker"}]} ` ) ,
"party" : promptkit . Inline ( "combat-party-marker" ) ,
"players" : promptkit . Inline ( " " ) ,
"glossary" : promptkit . Inline ( " " ) ,
} ,
} )
if err != nil {
t . Fatal ( err )
}
defer initial . Discard ( )
correction , err := engine . PrepareExecution ( context . Background ( ) , promptkit . RunRequest {
PromptID : "notarius/dnd.combat_turns.repair" , APIKey : "test-only" ,
Inputs : map [ string ] promptkit . ArtifactRef { "guidance" : promptkit . Inline ( "Fix the actor citation." ) } ,
} )
if err != nil {
t . Fatal ( err )
}
defer correction . Discard ( )
first , next := initial . Details ( ) , correction . Details ( )
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" )
}
2026-11-26 03:04:54 +00:00
var all string
2026-11-22 01:13:57 +00:00
for _ , message := range first . Messages {
all += message . Content
}
for _ , marker := range [ ] string { "combat-transcript-marker" , "combat-npc-marker" , "combat-party-marker" } {
if strings . Count ( all , marker ) != 1 {
t . Fatalf ( "fixed input repeated: %q" , marker )
}
}
if len ( next . Messages ) != 1 || next . Messages [ 0 ] . Role != "user" || ! strings . Contains ( next . Messages [ 0 ] . Content , "Fix the actor citation." ) || strings . Contains ( next . Messages [ 0 ] . Content , "combat-transcript-marker" ) {
t . Fatal ( "correction did not contain only findings and a replacement assignment" )
}
}
func TestCombatResponseSchemaUsesPrivateShape ( t * testing . T ) {
schema , err := fs . ReadFile ( assets . FS ( ) , "dnd/combat-turns/schemas/dnd_combat_turns_llm.v1.json" )
if err != nil {
t . Fatal ( err )
}
valid := validCombatResponse ( )
content , err := json . Marshal ( valid )
if err != nil {
t . Fatal ( err )
}
if err := validateJSONSchema ( content , schema ) ; err != nil {
t . Fatalf ( "valid combat response rejected: %v" , err )
}
withSourceID := validCombatResponse ( )
withSourceID [ "combat_turns" ] . ( [ ] any ) [ 0 ] . ( map [ string ] any ) [ "source_refs" ] . ( [ ] any ) [ 0 ] . ( map [ string ] any ) [ "source_id" ] = "session-alpha"
content , err = json . Marshal ( withSourceID )
if err != nil {
t . Fatal ( err )
}
if err := validateJSONSchema ( content , schema ) ; err == nil {
t . Fatal ( "response schema accepted source_id, want private source-reference shape" )
}
}
func TestCombatResponseSchemaLeavesNonCategoricalSemanticsToValidators ( t * testing . T ) {
schema , err := fs . ReadFile ( assets . FS ( ) , "dnd/combat-turns/schemas/dnd_combat_turns_llm.v1.json" )
if err != nil {
t . Fatal ( err )
}
semanticCandidate := validCombatResponse ( )
turn := semanticCandidate [ "combat_turns" ] . ( [ ] any ) [ 0 ] . ( map [ string ] any )
turn [ "actor" ] = ""
ref := turn [ "source_refs" ] . ( [ ] any ) [ 0 ] . ( map [ string ] any )
ref [ "start_unit_id" ] = 0
ref [ "end_unit_id" ] = - 1
content , err := json . Marshal ( semanticCandidate )
if err != nil {
t . Fatal ( err )
}
if err := validateJSONSchema ( content , schema ) ; err != nil {
t . Fatalf ( "private schema rejected non-categorical validator-owned semantics: %v" , err )
}
turn [ "source_refs" ] = [ ] any { }
content , err = json . Marshal ( semanticCandidate )
if err != nil {
t . Fatal ( err )
}
if err := validateJSONSchema ( content , schema ) ; err != nil {
t . Fatalf ( "private schema rejected empty validator-owned collections: %v" , err )
}
}
func TestCombatResponseSchemaRetainsStructuralBoundary ( t * testing . T ) {
schema , err := fs . ReadFile ( assets . FS ( ) , "dnd/combat-turns/schemas/dnd_combat_turns_llm.v1.json" )
if err != nil {
t . Fatal ( err )
}
for _ , test := range [ ] struct {
name string
mutate func ( map [ string ] any )
} {
{ name : "missing actor" , mutate : func ( turn map [ string ] any ) { delete ( turn , "actor" ) } } ,
{ name : "wrong actor type" , mutate : func ( turn map [ string ] any ) { turn [ "actor" ] = 1 } } ,
{ name : "unknown field" , mutate : func ( turn map [ string ] any ) { turn [ "unexpected" ] = true } } ,
{ name : "missing source refs" , mutate : func ( turn map [ string ] any ) { delete ( turn , "source_refs" ) } } ,
{ name : "unsupported turn kind" , mutate : func ( turn map [ string ] any ) { turn [ "turn_kind" ] = "unsupported" } } ,
} {
t . Run ( test . name , func ( t * testing . T ) {
candidate := validCombatResponse ( )
test . mutate ( candidate [ "combat_turns" ] . ( [ ] any ) [ 0 ] . ( map [ string ] any ) )
content , err := json . Marshal ( candidate )
if err != nil {
t . Fatal ( err )
}
if err := validateJSONSchema ( content , schema ) ; err == nil {
t . Fatal ( "private schema accepted structurally invalid response" )
}
} )
}
}
func TestCombatResponseSchemaAcceptsEverySupportedTurnKind ( t * testing . T ) {
schema , err := fs . ReadFile ( assets . FS ( ) , "dnd/combat-turns/schemas/dnd_combat_turns_llm.v1.json" )
if err != nil {
t . Fatal ( err )
}
for _ , kind := range [ ] string { "turn" , "reaction" , "legendary_action" , "lair_action" , "other" } {
candidate := validCombatResponse ( )
candidate [ "combat_turns" ] . ( [ ] any ) [ 0 ] . ( map [ string ] any ) [ "turn_kind" ] = kind
content , err := json . Marshal ( candidate )
if err != nil {
t . Fatal ( err )
}
if err := validateJSONSchema ( content , schema ) ; err != nil {
t . Fatalf ( "supported turn kind %q was rejected: %v" , kind , err )
}
}
}
func validCombatResponse ( ) map [ string ] any {
return map [ string ] any {
"combat_turns" : [ ] any {
map [ string ] any {
"actor" : "Aria" ,
"turn_kind" : "reaction" ,
"source_refs" : [ ] any { map [ string ] any { "start_unit_id" : 1 , "end_unit_id" : 2 } } ,
} ,
} ,
}
}