2026-08-05 19:13:37 +00:00
package locationregistry
2026-08-04 00:07:47 +00:00
import (
"context"
"encoding/json"
2026-11-22 00:27:34 +00:00
"strings"
2026-08-04 00:07:47 +00:00
"testing"
2026-11-22 00:27:34 +00:00
"gitea.maximumdirect.net/eric/notarius/assets"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
2026-08-04 00:07:47 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
)
type recordingLocationNormalizerClient 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-08-04 00:07:47 +00:00
}
2026-11-22 00:27:34 +00:00
func ( c * recordingLocationNormalizerClient ) 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-08-04 00:07:47 +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-08-04 00:07:47 +00:00
}
response := c . response
2026-11-22 00:27:34 +00:00
if index < len ( c . responses ) {
response = c . responses [ index ]
}
2026-08-04 00:07:47 +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-08-04 00:07:47 +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 : location - normalize - test
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-08-04 00:07:47 +00:00
if err != nil {
2026-11-22 00:27:34 +00:00
t . Fatal ( err )
2026-08-04 00:07:47 +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 19:05:42 +00:00
func normalizeRequest ( value dnd . LocationRegistry ) contracts . TypedNormalizeRequest [ dnd . LocationRegistry ] {
2026-08-09 18:30:32 +00:00
return contracts . TypedNormalizeRequest [ dnd . LocationRegistry ] {
2026-11-22 00:27:34 +00:00
Source : & source . SourceDocument { } , LLMProfile : "location-normalize-test" ,
Operation : contracts . ProducerOperation [ dnd . LocationRegistry ] { Controls : contracts . ProducerControls { Mode : contracts . ModeStrict , Cache : contracts . CacheBypass } , ReuseEligible : true , Assess : func ( context . Context , dnd . LocationRegistry ) ( contracts . CandidateAssessment , error ) {
return contracts . CandidateAssessment { } , nil
} } ,
2026-08-09 18:30:32 +00:00
MergeOutput : contracts . MergeArtifact [ dnd . LocationRegistry ] { Value : value } ,
}
2026-08-04 00:07:47 +00:00
}
2026-11-22 00:27:34 +00:00
2026-08-05 19:05:42 +00:00
func normalizeRequestWithSource ( value dnd . LocationRegistry , doc * source . SourceDocument ) contracts . TypedNormalizeRequest [ dnd . LocationRegistry ] {
2026-08-04 00:07:47 +00:00
request := normalizeRequest ( value )
request . Source = doc
return request
}
func semanticDocument ( ) * source . SourceDocument {
return & source . SourceDocument { ID : "location-session" , Units : [ ] source . SourceUnit { { ID : 10 , Kind : "speech" , Text : "The old mill is the Greencloak's refuge." } , { ID : 20 , Kind : "speech" , Text : "The mill stands on the northern road." } , { ID : 30 , Kind : "speech" , Text : "The tavern is beside the mill." } , { ID : 40 , Kind : "speech" , Text : "The mill's cellar is flooded." } } }
}
2026-08-27 15:57:29 +00:00
func hasDiagnostic ( diagnostics [ ] contracts . ProducerDiagnostic , reason string , disposition contracts . DiagnosticDisposition ) bool {
for _ , diagnostic := range diagnostics {
if diagnostic . ReasonCode == reason && diagnostic . Disposition == disposition {
2026-08-04 00:07:47 +00:00
return true
}
}
return false
}