Files
notarius/internal/modules/dnd/normalize/npcregistry/normalizer_test.go

251 lines
10 KiB
Go
Raw Normal View History

package npcregistry
import (
"context"
2026-07-26 01:36:32 +00:00
"encoding/json"
"reflect"
"strings"
"testing"
"gitea.maximumdirect.net/eric/notarius/assets"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
"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"
)
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")
}
want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: []string{"merged"}, Provides: []string{"normalized"}, ArtifactKind: dnd.NPCRegistryKind}
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-26 01:36:32 +00:00
if _, err := New(nil, Options{}); err == nil {
t.Fatal("New(nil, Options{}) error = nil, want nil native engine rejection")
2026-07-26 01:36:32 +00:00
}
spec := ModuleSpec()
spec.Prompts[0] = "mutated"
if !reflect.DeepEqual(ModuleSpec(), want) {
t.Fatal("static dependencies are not owned")
}
}
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{
{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))
if err != nil {
2026-07-22 18:51:26 +00:00
t.Fatalf("Normalize() error = %v", err)
}
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-22 18:51:26 +00:00
for _, reason := range []string{ReasonCodeNPCFieldsNormalized, ReasonCodeSourceReferencesNormalized, ReasonCodeNPCIDRecomputed} {
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
}
}
}
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{
{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))
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-26 01:36:32 +00:00
result, err := newNormalizer(t, &recordingNPCNormalizerClient{}).Normalize(context.Background(), normalizeRequest(input))
if err != nil {
2026-07-22 18:51:26 +00:00
t.Fatalf("Normalize() error = %v", err)
}
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-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)
}
if !hasDiagnostic(result.Diagnostics, ReasonCodeDuplicateNPCCollapsed, "npcs[0]", contracts.DiagnosticDispositionObservation) {
t.Fatalf("diagnostics = %#v, want duplicate collapse", result.Diagnostics)
}
}
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-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])
}
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")
}
}
func TestNormalizeHandlesNilAndCanceledCalls(t *testing.T) {
2026-07-26 01:36:32 +00:00
normalizer := newNormalizer(t, &recordingNPCNormalizerClient{})
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}))
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)
}
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-26 01:36:32 +00:00
type recordingNPCNormalizerClient struct {
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
}
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-07-26 01:36:32 +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-07-26 01:36:32 +00:00
}
response := c.response
if index < len(c.responses) {
response = c.responses[index]
2026-07-26 01:36:32 +00:00
}
if response == "" {
response = `{"duplicate_groups":[]}`
}
return &promptkit.GenerateResponse{Content: response}, nil
}
func newNormalizer(t *testing.T, client promptkit.LLMClient) *Normalizer {
2026-07-26 01:36:32 +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: 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 {
t.Fatal(err)
2026-07-26 01:36:32 +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
}
2026-08-05 18:25:04 +00:00
func normalizeRequest(value dnd.NPCRegistry) contracts.TypedNormalizeRequest[dnd.NPCRegistry] {
return contracts.TypedNormalizeRequest[dnd.NPCRegistry]{
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
}},
MergeOutput: contracts.MergeArtifact[dnd.NPCRegistry]{Value: value},
}
}
2026-08-05 18:25:04 +00:00
func normalizeRequestWithSource(value dnd.NPCRegistry, doc *source.SourceDocument) contracts.TypedNormalizeRequest[dnd.NPCRegistry] {
request := normalizeRequest(value)
request.Source = doc
return request
}
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
}
}
}
return false
}