2026-08-03 20:44:44 +00:00
package enemyevents
import (
"context"
"errors"
"reflect"
"strings"
"testing"
2026-11-22 01:20:46 +00:00
"go.maximumdirect.net/promptkit"
2026-08-03 20:44:44 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
)
func TestExtractMapsEnemyEventsInSourceOrder ( t * testing . T ) {
client := & fakeEnemyEventsLLMClient { response : extractionResponse { Events : [ ] enemyEventResponse {
2026-08-28 13:45:21 +00:00
{ Name : "Ashfang" , Kind : "killed" , SourceRefs : [ ] enemySourceRefResponse { { StartUnitID : 4 , EndUnitID : 3 } } } ,
2026-08-03 20:44:44 +00:00
{ Name : "Ashfang" , Kind : "engaged" , SourceRefs : [ ] enemySourceRefResponse { { StartUnitID : 1 , EndUnitID : 1 } , { StartUnitID : 1 , EndUnitID : 1 } } } ,
{ Name : "Orcs" , Kind : "fled" , SourceRefs : [ ] enemySourceRefResponse { { StartUnitID : 3 , EndUnitID : 3 } } } ,
{ Name : "One orc" , Kind : "captured" , SourceRefs : [ ] enemySourceRefResponse { { StartUnitID : 2 , EndUnitID : 2 } } } ,
{ Name : "Ashfang" , Kind : "incapacitated" , SourceRefs : [ ] enemySourceRefResponse { { StartUnitID : 2 , EndUnitID : 2 } } } ,
} } }
result , err := newEnemyExtractor ( t , client ) . Extract ( context . Background ( ) , enemyExtractionRequest ( t ) )
if err != nil {
t . Fatal ( err )
}
if got := [ ] dnd . EnemyEventKind { result . Value . Events [ 0 ] . Kind , result . Value . Events [ 1 ] . Kind , result . Value . Events [ 2 ] . Kind , result . Value . Events [ 3 ] . Kind , result . Value . Events [ 4 ] . Kind } ; ! reflect . DeepEqual ( got , [ ] dnd . EnemyEventKind {
dnd . EnemyEventKindEngaged ,
dnd . EnemyEventKindCaptured ,
dnd . EnemyEventKindIncapacitated ,
dnd . EnemyEventKindKilled ,
2026-08-28 13:45:21 +00:00
dnd . EnemyEventKindFled ,
2026-08-03 20:44:44 +00:00
} ) {
t . Fatalf ( "event order = %#v" , got )
}
if refs := result . Value . Events [ 0 ] . SourceRefs ; ! reflect . DeepEqual ( refs , [ ] source . SourceRef { { SourceID : "combat-session" , StartUnitID : 1 , EndUnitID : 1 } } ) {
t . Fatalf ( "canonical source refs = %#v" , refs )
}
2026-08-28 13:45:21 +00:00
if refs := result . Value . Events [ 3 ] . SourceRefs ; ! reflect . DeepEqual ( refs , [ ] source . SourceRef { { SourceID : "combat-session" , StartUnitID : 3 , EndUnitID : 4 } } ) {
t . Fatalf ( "reversed source refs = %#v" , refs )
}
2026-08-03 20:44:44 +00:00
if len ( client . requests ) != 1 {
t . Fatalf ( "LLM calls = %d, want 1" , len ( client . requests ) )
}
request := client . requests [ 0 ]
2026-11-22 01:20:46 +00:00
if request . Prompt . SessionID != "session-123" {
t . Fatal ( "session selection lost" )
2026-08-03 20:44:44 +00:00
}
2026-11-22 01:20:46 +00:00
if got := string ( renderedInput ( t , request , CombatTurnReferenceSlot ) ) ; ! strings . Contains ( got , ` "actor":"Ashfang" ` ) || strings . Contains ( got , "source_ref" ) {
t . Fatalf ( "combat grounding=%s" , got )
2026-08-03 20:44:44 +00:00
}
2026-11-22 01:20:46 +00:00
if got := string ( renderedInput ( t , request , NPCOccurrenceReferenceSlot ) ) ; ! strings . Contains ( got , ` "kind":"combat_opponent" ` ) || strings . Contains ( got , "Aria" ) {
t . Fatalf ( "opponent grounding=%s" , got )
2026-08-03 20:44:44 +00:00
}
}
func TestExtractPreservesSemanticCandidatesAndResponseOwnership ( t * testing . T ) {
client := & fakeEnemyEventsLLMClient { response : extractionResponse { Events : [ ] enemyEventResponse { {
2026-08-29 01:24:45 +00:00
Name : " " , Kind : "engaged" , SourceRefs : [ ] enemySourceRefResponse { { StartUnitID : 99 , EndUnitID : 0 } } ,
2026-08-03 20:44:44 +00:00
} } } }
result , err := newEnemyExtractor ( t , client ) . Extract ( context . Background ( ) , enemyExtractionRequest ( t ) )
if err != nil {
t . Fatal ( err )
}
event := result . Value . Events [ 0 ]
2026-08-29 01:24:45 +00:00
if event . Name != " " || event . Kind != "engaged" || event . SourceRefs [ 0 ] != ( source . SourceRef { SourceID : "combat-session" , StartUnitID : 99 } ) {
2026-08-03 20:44:44 +00:00
t . Fatalf ( "semantic candidate = %#v" , event )
}
result . Value . Events [ 0 ] . SourceRefs [ 0 ] . StartUnitID = 7
if client . response . Events [ 0 ] . SourceRefs [ 0 ] . StartUnitID != 99 {
t . Fatal ( "mapped event aliases model-owned source references" )
}
}
func TestExtractSkipsModelForIneligibleScenes ( t * testing . T ) {
for _ , test := range [ ] struct {
name string
kind dnd . SceneKind
change func ( * source . Chunk )
wantWarning bool
} {
{ name : "non-combat" , kind : dnd . SceneKindNarrative } ,
{ name : "missing" , kind : dnd . SceneKindCombat , change : func ( chunk * source . Chunk ) { chunk . ID = "other" } , wantWarning : true } ,
{ name : "mismatched" , kind : dnd . SceneKindCombat , change : func ( chunk * source . Chunk ) { chunk . Ref . EndUnitID ++ } , wantWarning : true } ,
} {
t . Run ( test . name , func ( t * testing . T ) {
client := & fakeEnemyEventsLLMClient { }
request := enemyExtractionRequest ( t )
request . References = groundingReferences ( t , "Ashfang" , test . kind )
if test . change != nil {
test . change ( request . Chunk )
}
2026-11-22 01:20:46 +00:00
request . Operation . ReuseEligible = false
prepared , assessed := 0 , 0
request . Operation . Execution . OnPrepared = func ( context . Context , promptkit . PreparedRun ) { prepared ++ }
request . Operation . Assess = func ( context . Context , dnd . EnemyEventList ) ( contracts . CandidateAssessment , error ) {
assessed ++
return contracts . CandidateAssessment { } , nil
}
2026-08-03 20:44:44 +00:00
result , err := newEnemyExtractor ( t , client ) . Extract ( context . Background ( ) , request )
if err != nil {
t . Fatal ( err )
}
if len ( client . requests ) != 0 || result . Value . Events == nil || len ( result . Value . Events ) != 0 {
t . Fatalf ( "result = %#v, calls = %d" , result , len ( client . requests ) )
}
2026-11-22 01:20:46 +00:00
if result . Outcome . Disposition != contracts . ProducerAccepted || result . Outcome . ReuseEligible || prepared != 0 || assessed != 1 {
t . Fatalf ( "deterministic skip=%#v, prepared/assessed=%d/%d" , result . Outcome , prepared , assessed )
}
2026-08-03 20:44:44 +00:00
if test . wantWarning {
2026-08-27 16:40:17 +00:00
if len ( result . Diagnostics ) != 1 || result . Diagnostics [ 0 ] . ReasonCode != "scene_classification_unavailable" || result . Diagnostics [ 0 ] . Disposition != contracts . DiagnosticDispositionWarning || result . Diagnostics [ 0 ] . Category != contracts . DiagnosticCategoryDegradation || result . Diagnostics [ 0 ] . Samples [ 0 ] . Scope != SceneDescriptionReferenceSlot {
2026-08-27 16:04:32 +00:00
t . Fatalf ( "diagnostics = %#v" , result . Diagnostics )
2026-08-03 20:44:44 +00:00
}
2026-08-27 16:40:17 +00:00
} else if len ( result . Diagnostics ) != 0 {
2026-08-27 16:04:32 +00:00
t . Fatalf ( "diagnostics = %#v" , result . Diagnostics )
2026-08-03 20:44:44 +00:00
}
} )
}
}
func TestExtractReportsRequiredGroundingAndProviderFailures ( t * testing . T ) {
client := & fakeEnemyEventsLLMClient { }
request := enemyExtractionRequest ( t )
request . References = withoutSlot ( request . References , NPCRegistryReferenceSlot )
2026-11-22 01:20:46 +00:00
if _ , err := ( & Extractor { engine : newEnemyExtractor ( t , client ) . engine , grounding : mustGroundingResolver ( t , request . References ) } ) . Extract ( context . Background ( ) , request ) ; err == nil || ! strings . Contains ( err . Error ( ) , "NPC registry" ) {
2026-08-03 20:44:44 +00:00
t . Fatalf ( "Extract() error = %v, want required grounding context" , err )
}
provider := & fakeEnemyEventsLLMClient { err : errors . New ( "provider unavailable" ) }
2026-11-22 01:20:46 +00:00
if _ , err := newEnemyExtractor ( t , provider ) . Extract ( context . Background ( ) , enemyExtractionRequest ( t ) ) ; err == nil || ! strings . Contains ( err . Error ( ) , "produce enemy events" ) || ! strings . Contains ( err . Error ( ) , "provider unavailable" ) {
2026-08-03 20:44:44 +00:00
t . Fatalf ( "Extract() error = %v, want provider context" , err )
}
}
2026-11-22 01:20:46 +00:00
func TestConstructorSpecOptionsAndNativeDependencies ( t * testing . T ) {
if _ , err := New ( nil , Options { } ) ; err == nil || ! strings . Contains ( err . Error ( ) , "native engine" ) {
2026-08-03 20:44:44 +00:00
t . Fatalf ( "New(nil) error = %v" , err )
}
2026-11-22 01:20:46 +00:00
if _ , err := New ( newEnemyExtractor ( t , & fakeEnemyEventsLLMClient { } ) . engine , Options { } , contracts . ReferenceSet { } , contracts . ReferenceSet { } ) ; err == nil || ! strings . Contains ( err . Error ( ) , "at most one" ) {
2026-08-03 20:44:44 +00:00
t . Fatalf ( "New() error = %v" , err )
}
if _ , err := DecodeOptions ( map [ string ] any { "unexpected" : true } ) ; err == nil || ! strings . Contains ( err . Error ( ) , "unknown option" ) {
t . Fatalf ( "DecodeOptions() error = %v" , err )
}
first := ModuleSpec ( )
first . Requires [ 0 ] = "changed"
first . ReferenceSlots [ 0 ] . AcceptedMediaTypes [ 0 ] = "changed"
second := ModuleSpec ( )
2026-11-22 01:20:46 +00:00
if second . Requires [ 0 ] != "chunks" || second . ArtifactKind != dnd . EnemyEventListKind || second . ExecutionClass != contracts . ExecutionClassLLMBacked || ! reflect . DeepEqual ( second . Prompts , [ ] string { PromptID , CorrectionPromptID } ) || second . ReferenceSlots [ 0 ] . AcceptedMediaTypes [ 0 ] == "changed" {
2026-08-03 20:44:44 +00:00
t . Fatalf ( "ModuleSpec() reused mutable state: %#v" , second )
}
registry := pipeline . NewExtractorRegistry ( )
if err := Register ( registry ) ; err != nil {
t . Fatal ( err )
}
if spec , ok := registry . Spec ( Key ) ; ! ok || spec . Key != Key || spec . ArtifactKind != dnd . EnemyEventListKind {
t . Fatalf ( "registered spec = %#v, %t" , spec , ok )
}
}