2026-08-05 18:38:22 +00:00
package npcregistry
2026-07-21 02:36:59 +00:00
import (
"context"
2026-07-26 01:36:32 +00:00
"encoding/json"
2026-07-21 02:36:59 +00:00
"reflect"
"strings"
"testing"
2026-11-22 00:27:34 +00:00
"gitea.maximumdirect.net/eric/notarius/assets"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
2026-07-21 02:36:59 +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"
2026-07-22 18:51:26 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity"
2026-07-21 02:36:59 +00:00
)
func TestModuleContractAndIdentity ( t * testing . T ) {
if _ , err := DecodeOptions ( nil ) ; err != nil {
t . Fatalf ( "DecodeOptions(nil) error = %v, want nil" , err )
}
2026-07-22 18:51:26 +00:00
if _ , err := DecodeOptions ( map [ string ] any { "unexpected" : true } ) ; err == nil {
t . Fatal ( "DecodeOptions() accepted unknown option" )
2026-07-21 02:36:59 +00:00
}
2026-11-22 00:27:34 +00:00
want := pipeline . ModuleSpec { Key : Key , Stage : pipeline . StageNormalize , ExecutionClass : contracts . ExecutionClassLLMBacked , Prompts : [ ] string { PromptID , CorrectionPromptID } , Requires : [ ] string { "merged" } , Provides : [ ] string { "normalized" } , ArtifactKind : dnd . NPCRegistryKind }
2026-07-21 02:36:59 +00:00
if got := ModuleSpec ( ) ; ! reflect . DeepEqual ( got , want ) {
t . Fatalf ( "ModuleSpec() = %#v, want %#v" , got , want )
}
registry := pipeline . NewNormalizerRegistry ( )
if err := Register ( registry ) ; err != nil {
2026-07-22 18:51:26 +00:00
t . Fatalf ( "Register() error = %v" , err )
2026-07-21 02:36:59 +00:00
}
2026-07-26 01:36:32 +00:00
if _ , err := New ( nil , Options { } ) ; err == nil {
2026-11-22 00:27:34 +00:00
t . Fatal ( "New(nil, Options{}) error = nil, want nil native engine rejection" )
2026-07-26 01:36:32 +00:00
}
2026-11-22 00:27:34 +00:00
spec := ModuleSpec ( )
spec . Prompts [ 0 ] = "mutated"
if ! reflect . DeepEqual ( ModuleSpec ( ) , want ) {
t . Fatal ( "static dependencies are not owned" )
2026-07-21 02:36:59 +00:00
}
2026-11-22 00:27:34 +00:00
2026-07-21 02:36:59 +00:00
}
2026-07-22 18:51:26 +00:00
func TestNormalizeNamesEvidenceAndIDs ( t * testing . T ) {
2026-08-05 18:25:04 +00:00
input := dnd . NPCRegistry { NPCs : [ ] dnd . NPC { {
2026-07-22 18:51:26 +00:00
ID : "wrong" , Name : " Lady\tAsh " , SourceRefs : [ ] source . SourceRef {
2026-07-21 02:36:59 +00:00
{ SourceID : "b" , StartUnitID : 2 , EndUnitID : 3 } ,
{ SourceID : "a" , StartUnitID : 4 , EndUnitID : 4 } ,
{ SourceID : "b" , StartUnitID : 2 , EndUnitID : 3 } ,
} ,
} } }
2026-07-26 01:36:32 +00:00
result , err := newNormalizer ( t , & recordingNPCNormalizerClient { } ) . Normalize ( context . Background ( ) , normalizeRequest ( input ) )
2026-07-21 02:36:59 +00:00
if err != nil {
2026-07-22 18:51:26 +00:00
t . Fatalf ( "Normalize() error = %v" , err )
2026-07-21 02:36:59 +00:00
}
2026-07-22 18:51:26 +00:00
want := dnd . NPC { ID : identity . DeriveID ( "Lady Ash" ) , Name : "Lady Ash" , SourceRefs : [ ] source . SourceRef { { SourceID : "a" , StartUnitID : 4 , EndUnitID : 4 } , { SourceID : "b" , StartUnitID : 2 , EndUnitID : 3 } } }
if ! reflect . DeepEqual ( result . Value . NPCs [ 0 ] , want ) {
t . Fatalf ( "NPC = %#v, want %#v" , result . Value . NPCs [ 0 ] , want )
2026-07-21 02:36:59 +00:00
}
2026-07-22 18:51:26 +00:00
for _ , reason := range [ ] string { ReasonCodeNPCFieldsNormalized , ReasonCodeSourceReferencesNormalized , ReasonCodeNPCIDRecomputed } {
2026-08-27 15:57:29 +00:00
if ! hasDiagnostic ( result . Diagnostics , reason , "npcs[0]" , contracts . DiagnosticDispositionObservation ) {
t . Fatalf ( "diagnostics = %#v, want %s" , result . Diagnostics , reason )
2026-07-22 18:51:26 +00:00
}
2026-07-21 02:36:59 +00:00
}
}
2026-07-24 14:20:10 +00:00
func TestNormalizeOrdersEvidenceBySourceDocumentPosition ( t * testing . T ) {
doc := & source . SourceDocument { ID : "source" , Units : [ ] source . SourceUnit { { ID : 30 } , { ID : 10 } } }
2026-08-05 18:25:04 +00:00
input := dnd . NPCRegistry { NPCs : [ ] dnd . NPC { { Name : "Lady Ash" , SourceRefs : [ ] source . SourceRef {
2026-07-24 14:20:10 +00:00
{ SourceID : doc . ID , StartUnitID : 10 , EndUnitID : 10 } ,
{ SourceID : doc . ID , StartUnitID : 30 , EndUnitID : 30 } ,
{ SourceID : doc . ID , StartUnitID : 999 , EndUnitID : 999 } ,
} } } }
2026-07-26 01:36:32 +00:00
result , err := newNormalizer ( t , & recordingNPCNormalizerClient { } ) . Normalize ( context . Background ( ) , normalizeRequestWithSource ( input , doc ) )
2026-07-24 14:20:10 +00:00
if err != nil {
t . Fatalf ( "Normalize() error = %v" , err )
}
got := result . Value . NPCs [ 0 ] . SourceRefs
if got [ 0 ] . StartUnitID != 30 || got [ 1 ] . StartUnitID != 10 || got [ 2 ] . StartUnitID != 999 {
t . Fatalf ( "source refs = %#v, want source-document order followed by invalid reference" , got )
}
}
2026-07-22 18:51:26 +00:00
func TestNormalizeConsolidatesCanonicalNamesOnlyAndUnionsEvidence ( t * testing . T ) {
2026-08-05 18:25:04 +00:00
input := dnd . NPCRegistry { NPCs : [ ] dnd . NPC {
2026-07-22 18:51:26 +00:00
{ Name : " Captain Vale " , SourceRefs : [ ] source . SourceRef { { SourceID : "a" , StartUnitID : 1 , EndUnitID : 1 } } } ,
{ Name : "captain vale" , SourceRefs : [ ] source . SourceRef { { SourceID : "b" , StartUnitID : 2 , EndUnitID : 2 } } } ,
{ Name : "The Captain" , SourceRefs : [ ] source . SourceRef { { SourceID : "c" , StartUnitID : 3 , EndUnitID : 3 } } } ,
2026-07-21 02:36:59 +00:00
} }
2026-07-26 01:36:32 +00:00
result , err := newNormalizer ( t , & recordingNPCNormalizerClient { } ) . Normalize ( context . Background ( ) , normalizeRequest ( input ) )
2026-07-21 02:36:59 +00:00
if err != nil {
2026-07-22 18:51:26 +00:00
t . Fatalf ( "Normalize() error = %v" , err )
2026-07-21 02:36:59 +00:00
}
2026-07-22 18:51:26 +00:00
if len ( result . Value . NPCs ) != 2 || result . Value . NPCs [ 0 ] . Name != "Captain Vale" || result . Value . NPCs [ 1 ] . Name != "The Captain" {
t . Fatalf ( "NPCs = %#v, want name-only stable consolidation" , result . Value . NPCs )
2026-07-21 02:36:59 +00:00
}
2026-07-22 18:51:26 +00:00
if refs := result . Value . NPCs [ 0 ] . SourceRefs ; len ( refs ) != 2 || refs [ 0 ] . SourceID != "a" || refs [ 1 ] . SourceID != "b" {
t . Fatalf ( "source refs = %#v, want evidence union" , refs )
2026-07-21 02:36:59 +00:00
}
2026-08-27 15:57:29 +00:00
if ! hasDiagnostic ( result . Diagnostics , ReasonCodeDuplicateNPCCollapsed , "npcs[0]" , contracts . DiagnosticDispositionObservation ) {
t . Fatalf ( "diagnostics = %#v, want duplicate collapse" , result . Diagnostics )
2026-07-21 02:36:59 +00:00
}
}
2026-07-22 18:51:26 +00:00
func TestNormalizePreservesInvalidCandidatesAndDoesNotAliasInput ( t * testing . T ) {
2026-08-05 18:25:04 +00:00
input := dnd . NPCRegistry { NPCs : [ ] dnd . NPC { { Name : " " , SourceRefs : [ ] source . SourceRef { { SourceID : "source" , StartUnitID : 0 , EndUnitID : - 1 } } } } }
before := dnd . NPCRegistry { NPCs : [ ] dnd . NPC { { Name : " " , SourceRefs : [ ] source . SourceRef { { SourceID : "source" , StartUnitID : 0 , EndUnitID : - 1 } } } } }
2026-07-26 01:36:32 +00:00
result , err := newNormalizer ( t , & recordingNPCNormalizerClient { } ) . Normalize ( context . Background ( ) , normalizeRequest ( input ) )
2026-07-22 18:51:26 +00:00
if err != nil || ! reflect . DeepEqual ( input , before ) {
t . Fatalf ( "Normalize() = %#v, %v; input mutated to %#v" , result , err , input )
2026-07-21 02:36:59 +00:00
}
2026-07-22 18:51:26 +00:00
if result . Value . NPCs [ 0 ] . Name != "" || result . Value . NPCs [ 0 ] . SourceRefs [ 0 ] . EndUnitID != - 1 {
t . Fatalf ( "candidate = %#v, want invalid semantics preserved" , result . Value . NPCs [ 0 ] )
2026-07-21 02:36:59 +00:00
}
result . Value . NPCs [ 0 ] . SourceRefs [ 0 ] . SourceID = "changed"
2026-07-22 18:51:26 +00:00
if input . NPCs [ 0 ] . SourceRefs [ 0 ] . SourceID != "source" {
t . Fatal ( "output aliases input evidence" )
2026-07-21 02:36:59 +00:00
}
}
func TestNormalizeHandlesNilAndCanceledCalls ( t * testing . T ) {
2026-07-26 01:36:32 +00:00
normalizer := newNormalizer ( t , & recordingNPCNormalizerClient { } )
2026-08-09 18:30:32 +00:00
if _ , err := normalizer . Normalize ( context . Background ( ) , contracts . TypedNormalizeRequest [ dnd . NPCRegistry ] { } ) ; err == nil || ! strings . Contains ( err . Error ( ) , "source document must not be nil" ) {
t . Fatalf ( "nil source Normalize() error = %v" , err )
}
2026-08-05 18:25:04 +00:00
result , err := normalizer . Normalize ( context . Background ( ) , normalizeRequest ( dnd . NPCRegistry { NPCs : nil } ) )
2026-07-21 02:36:59 +00:00
if err != nil || result . Value . NPCs != nil {
2026-07-22 18:51:26 +00:00
t . Fatalf ( "nil list result = %#v, error = %v" , result . Value , err )
2026-07-21 02:36:59 +00:00
}
canceled , cancel := context . WithCancel ( context . Background ( ) )
cancel ( )
2026-08-05 18:25:04 +00:00
if _ , err := normalizer . Normalize ( canceled , normalizeRequest ( dnd . NPCRegistry { } ) ) ; err == nil || ! strings . Contains ( err . Error ( ) , "context error" ) {
2026-07-22 18:51:26 +00:00
t . Fatalf ( "canceled Normalize() error = %v" , err )
2026-07-21 02:50:37 +00:00
}
}
2026-07-26 01:36:32 +00:00
type recordingNPCNormalizerClient struct {
2026-11-22 00:27:34 +00:00
response string
responses [ ] string
err error
errors [ ] error
requests [ ] promptkit . GenerateRequest
onGenerate func ( context . Context , promptkit . GenerateRequest )
2026-07-26 01:36:32 +00:00
}
2026-11-22 00:27:34 +00:00
func ( c * recordingNPCNormalizerClient ) Generate ( ctx context . Context , request promptkit . GenerateRequest ) ( * promptkit . GenerateResponse , error ) {
index := len ( c . requests )
c . requests = append ( c . requests , request )
if c . onGenerate != nil {
c . onGenerate ( ctx , request )
2026-08-26 23:24:20 +00:00
}
2026-07-26 01:36:32 +00:00
if c . err != nil {
2026-11-22 00:27:34 +00:00
return nil , c . err
}
if index < len ( c . errors ) && c . errors [ index ] != nil {
return nil , c . errors [ index ]
2026-07-26 01:36:32 +00:00
}
response := c . response
2026-11-22 00:27:34 +00:00
if index < len ( c . responses ) {
response = c . responses [ index ]
2026-07-26 01:36:32 +00:00
}
if response == "" {
response = ` { "duplicate_groups":[]} `
}
2026-11-22 00:27:34 +00:00
return & promptkit . GenerateResponse { Content : response } , nil
2026-08-08 15:05:35 +00:00
}
2026-11-22 00:27:34 +00:00
func newNormalizer ( t * testing . T , client promptkit . LLMClient ) * Normalizer {
2026-07-26 01:36:32 +00:00
t . Helper ( )
2026-11-22 00:27:34 +00:00
type settings struct {
PromptKit promptkit . Settings ` yaml:"promptkit" `
}
definition := promptkit . Configure ( configkit . Definition [ settings ] { } , func ( value * settings ) * promptkit . Settings { return & value . PromptKit } )
filesystem := configkit . NewFilesystem ( "Notarius assets" , assets . FS ( ) )
result , err := configkit . LoadLayered ( configkit . Input { Source : configkit . Bytes ( "normalizer fixture" , [ ] byte ( ` promptkit :
profiles :
- id : normalizer - profile
endpoint : https : //example.invalid/v1
model : normalization - test - model
api_key_required : false
` ) ) } , configkit . LayeredOptions [ settings ] { Definition : definition , Underlays : [ ] configkit . Underlay {
promptkit . BundledOpenRouterUnderlay ( ) ,
{ Inputs : [ ] configkit . Input { { Source : filesystem . File ( "notarius.yml" ) , Imports : true , Filesystem : & configkit . FilesystemContext { FS : & filesystem , Root : "." } } } } ,
} } )
if err != nil {
t . Fatal ( err )
}
engine , err := promptkit . NewFromConfig ( result , promptkit . RuntimeConfig { } , promptkit . WithLLMClient ( client ) )
if err != nil {
t . Fatal ( err )
}
normalizer , err := New ( engine , Options { } )
2026-07-26 01:36:32 +00:00
if err != nil {
2026-11-22 00:27:34 +00:00
t . Fatal ( err )
2026-07-26 01:36:32 +00:00
}
return normalizer
}
2026-11-22 00:27:34 +00:00
func renderedInput ( t * testing . T , request promptkit . GenerateRequest , name string ) [ ] byte {
t . Helper ( )
key := "candidate_number_range"
if name == "transcript" {
key = "windows"
}
for _ , message := range request . Prompt . Messages {
start := strings . Index ( message . Content , "{" )
if start < 0 {
continue
}
content := [ ] byte ( message . Content [ start : ] )
var fields map [ string ] json . RawMessage
if json . Unmarshal ( content , & fields ) == nil && fields [ key ] != nil {
return content
}
}
t . Fatalf ( "missing rendered %s input" , name )
return nil
}
2026-08-05 18:25:04 +00:00
func normalizeRequest ( value dnd . NPCRegistry ) contracts . TypedNormalizeRequest [ dnd . NPCRegistry ] {
2026-08-09 18:30:32 +00:00
return contracts . TypedNormalizeRequest [ dnd . NPCRegistry ] {
2026-11-22 00:27:34 +00:00
Source : & source . SourceDocument { } , LLMProfile : "normalizer-profile" ,
Operation : contracts . ProducerOperation [ dnd . NPCRegistry ] { Controls : contracts . ProducerControls { Mode : contracts . ModeStrict , Cache : contracts . CacheBypass } , ReuseEligible : true , Assess : func ( context . Context , dnd . NPCRegistry ) ( contracts . CandidateAssessment , error ) {
return contracts . CandidateAssessment { } , nil
} } ,
2026-08-09 18:30:32 +00:00
MergeOutput : contracts . MergeArtifact [ dnd . NPCRegistry ] { Value : value } ,
}
2026-07-21 02:36:59 +00:00
}
2026-08-05 18:25:04 +00:00
func normalizeRequestWithSource ( value dnd . NPCRegistry , doc * source . SourceDocument ) contracts . TypedNormalizeRequest [ dnd . NPCRegistry ] {
2026-07-24 14:20:10 +00:00
request := normalizeRequest ( value )
request . Source = doc
return request
}
2026-08-27 15:57:29 +00:00
func hasDiagnostic ( diagnostics [ ] contracts . ProducerDiagnostic , reason , scope string , disposition contracts . DiagnosticDisposition ) bool {
for _ , diagnostic := range diagnostics {
if diagnostic . ReasonCode != reason || diagnostic . Disposition != disposition {
continue
}
for _ , sample := range diagnostic . Samples {
if sample . Scope == scope {
return true
}
2026-07-21 02:36:59 +00:00
}
}
return false
}