Files
notarius/internal/modules/dnd/normalize/locationregistry/test_helpers_test.go

126 lines
4.5 KiB
Go
Raw Normal View History

package locationregistry
2026-08-04 00:07:47 +00:00
import (
"context"
"encoding/json"
"strings"
2026-08-04 00:07:47 +00:00
"testing"
"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 {
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
}
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-04 00:07:47 +00:00
if c.err != nil {
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
if index < len(c.responses) {
response = c.responses[index]
}
2026-08-04 00:07:47 +00:00
if response == "" {
response = `{"duplicate_groups":[]}`
}
return &promptkit.GenerateResponse{Content: response}, nil
}
func newNormalizer(t *testing.T, client promptkit.LLMClient) *Normalizer {
2026-08-04 00:07:47 +00:00
t.Helper()
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 {
t.Fatal(err)
2026-08-04 00:07:47 +00:00
}
return normalizer
}
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
}
func normalizeRequest(value dnd.LocationRegistry) contracts.TypedNormalizeRequest[dnd.LocationRegistry] {
return contracts.TypedNormalizeRequest[dnd.LocationRegistry]{
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
}},
MergeOutput: contracts.MergeArtifact[dnd.LocationRegistry]{Value: value},
}
2026-08-04 00:07:47 +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."}}}
}
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
}