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

226 lines
12 KiB
Go
Raw Normal View History

package locationregistry
2026-08-04 00:07:47 +00:00
import (
"context"
"encoding/json"
"fmt"
2026-08-04 00:07:47 +00:00
"reflect"
"strconv"
2026-08-04 00:07:47 +00:00
"strings"
"testing"
"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/framework/semanticreconcile"
2026-08-04 00:07:47 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/locations/identity"
)
func TestModuleContractAndNativeDependencies(t *testing.T) {
want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: []string{"merged"}, Provides: []string{"normalized"}, ArtifactKind: dnd.LocationRegistryKind}
2026-08-04 00:07:47 +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 {
t.Fatal(err)
}
if _, err := New(nil, Options{}); err == nil {
t.Fatal("New() accepted nil native engine")
2026-08-04 00:07:47 +00:00
}
spec := ModuleSpec()
spec.Prompts[0] = "mutated"
if !reflect.DeepEqual(ModuleSpec(), want) {
t.Fatal("static dependencies are not owned")
2026-08-04 00:07:47 +00:00
}
2026-08-04 00:07:47 +00:00
}
func TestNormalizeRejectsNilSourceDocument(t *testing.T) {
_, err := newNormalizer(t, &recordingLocationNormalizerClient{}).Normalize(
context.Background(),
contracts.TypedNormalizeRequest[dnd.LocationRegistry]{},
)
if err == nil || !strings.Contains(err.Error(), "source document must not be nil") {
t.Fatalf("Normalize() error = %v, want nil source rejection", err)
}
}
2026-08-04 00:07:47 +00:00
func TestNormalizePreparesOnlyExactDuplicatesAndRetainsSameNameAndNestedPlaces(t *testing.T) {
input := dnd.LocationRegistry{Locations: []dnd.Location{
2026-08-04 00:07:47 +00:00
{Name: " The Tavern ", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}},
{Name: "the tavern", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}},
{Name: "The Tavern", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 2, EndUnitID: 2}}},
{Name: "The Tavern Cellar", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 2, EndUnitID: 2}}},
}}
before := dnd.LocationRegistry{Locations: append([]dnd.Location(nil), input.Locations...)}
client := &recordingLocationNormalizerClient{}
result, err := newNormalizer(t, client).Normalize(context.Background(), normalizeRequest(input))
2026-08-04 00:07:47 +00:00
if err != nil || len(result.Value.Locations) != 3 {
t.Fatalf("Normalize() = %#v, %v; want one exact duplicate removed", result, err)
}
if !reflect.DeepEqual(input, before) {
t.Fatalf("Normalize() mutated input: %#v", input)
}
if got := []string{result.Value.Locations[0].Name, result.Value.Locations[1].Name, result.Value.Locations[2].Name}; !reflect.DeepEqual(got, []string{"The Tavern", "The Tavern", "The Tavern Cellar"}) {
t.Fatalf("locations = %#v, want same names and nested place retained", got)
}
if result.Value.Locations[0].ID == result.Value.Locations[1].ID || !hasDiagnostic(result.Diagnostics, ReasonCodeDuplicateLocationCollapsed, contracts.DiagnosticDispositionObservation) || len(client.requests) != 0 {
t.Fatalf("result = %#v, want evidence-anchored IDs and exact duplicate finding", result)
2026-08-04 00:07:47 +00:00
}
}
func BenchmarkExactDuplicateGroupsManyDistinct(b *testing.B) {
records := make([]normalizedRecord, 1_000)
for index := range records {
records[index] = normalizedRecord{location: dnd.Location{
Name: "Location " + strconv.Itoa(index),
SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: index + 1, EndUnitID: index + 1}},
}}
}
b.ReportAllocs()
b.ResetTimer()
for iteration := 0; iteration < b.N; iteration++ {
if groups := exactDuplicateGroups(records); len(groups) != len(records) {
b.Fatalf("group count = %d, want %d", len(groups), len(records))
}
}
}
func TestNormalizeAppliesSafeAliasGroupAndUsesContextualInputs(t *testing.T) {
client := &recordingLocationNormalizerClient{response: `{"duplicate_groups":[{"candidate_numbers":[1,2],"canonical_candidate_number":2}]}`}
2026-08-04 00:07:47 +00:00
doc := semanticDocument()
input := dnd.LocationRegistry{Locations: []dnd.Location{
2026-08-04 00:07:47 +00:00
{Name: "Old Mill", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}}},
{Name: "the Greencloak's refuge", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 20, EndUnitID: 20}}},
{Name: "The Tavern", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}}},
}}
result, err := newNormalizer(t, client).Normalize(context.Background(), normalizeRequestWithSource(input, doc))
if err != nil || result.Outcome.Disposition != contracts.ProducerAccepted || len(result.Value.Locations) != 2 {
2026-08-04 00:07:47 +00:00
t.Fatalf("Normalize() = %#v, %v", result, err)
}
merged := result.Value.Locations[0]
if merged.Name != "the Greencloak's refuge" || merged.ID != identity.DeriveID(merged.Name, merged.SourceRefs) || len(merged.SourceRefs) != 2 || !hasDiagnostic(result.Diagnostics, ReasonCodeDuplicateLocationCollapsed, contracts.DiagnosticDispositionObservation) {
t.Fatalf("merged location = %#v, diagnostics = %#v", merged, result.Diagnostics)
2026-08-04 00:07:47 +00:00
}
encoded := string(renderedInput(t, client.requests[0], "candidates")) + string(renderedInput(t, client.requests[0], "transcript"))
if strings.Contains(encoded, doc.ID) || strings.Contains(encoded, "candidate-") || strings.Contains(encoded, merged.ID) || !strings.Contains(encoded, `"source_refs"`) {
2026-08-04 00:07:47 +00:00
t.Fatalf("private inputs = %s", encoded)
}
}
func TestNormalizeRejectsUnsafeAndOverlappingGroupsWithoutLosingCandidates(t *testing.T) {
doc := semanticDocument()
input := dnd.LocationRegistry{Locations: []dnd.Location{
2026-08-04 00:07:47 +00:00
{Name: "Old Mill", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}}},
{Name: "Mill", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 20, EndUnitID: 20}}},
{Name: "Tavern", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}}},
}}
client := &recordingLocationNormalizerClient{response: `{"duplicate_groups":[{"candidate_numbers":[1,2],"canonical_candidate_number":1},{"candidate_numbers":[2,3],"canonical_candidate_number":3}]}`}
request := normalizeRequestWithSource(input, doc)
request.Operation.Controls.Mode = contracts.ModePermissive
request.Operation.Controls.MaxAdditionalTurns = 1
result, err := newNormalizer(t, client).Normalize(context.Background(), request)
if err != nil || result.Outcome.Disposition != contracts.ProducerFallback || result.Outcome.ReuseEligible || len(result.Value.Locations) != 3 {
t.Fatalf("safe partial fallback = %#v %v", result, err)
}
if len(client.requests) != 2 {
t.Fatalf("native calls = %d, want initial/correction", len(client.requests))
}
messages := client.requests[1].Prompt.Messages
guidance := messages[len(messages)-1].Content
if !strings.Contains(guidance, "Duplicate groups 1, 2") || !strings.Contains(guidance, "at most one duplicate group") || strings.Contains(guidance, "overlapping_member") {
t.Fatalf("model-facing overlap guidance = %q", guidance)
}
var warning string
for _, diagnostic := range result.Diagnostics {
if diagnostic.ReasonCode == ReasonCodeLocationSemanticReconciliationExhausted && len(diagnostic.Samples) > 0 {
warning = diagnostic.Samples[0].Message
}
}
if !strings.Contains(warning, "2 proposal group") || !strings.Contains(warning, "preserved separately") {
t.Fatalf("fallback warning = %q", warning)
2026-08-04 00:07:47 +00:00
}
2026-08-04 00:07:47 +00:00
}
func TestReconciliationCandidatesKeepSameNameEvidenceDistinct(t *testing.T) {
doc := semanticDocument()
records := []normalizedRecord{
{location: dnd.Location{Name: "The Tavern", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}}}, inputIndexes: []int{0}, earliest: 0},
{location: dnd.Location{Name: "The Tavern", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}}}, inputIndexes: []int{1}, earliest: 1},
}
candidates, _, err := reconciliationInputs(records)
if err != nil {
t.Fatalf("reconciliationInputs() error = %v", err)
}
preparation, err := semanticreconcile.Prepare(doc, candidates, semanticreconcile.DefaultLimits())
if err != nil || preparation.Disposition() != semanticreconcile.Ready {
t.Fatalf("Prepare() = %#v, %v; want ready candidates", preparation, err)
}
var candidateInput struct {
Candidates []struct {
CandidateNumber int `json:"candidate_number"`
Label string `json:"label"`
} `json:"candidates"`
}
if err := json.Unmarshal(preparation.Materials()["candidates"], &candidateInput); err != nil {
t.Fatal(err)
}
if len(candidateInput.Candidates) != 2 || candidateInput.Candidates[0].CandidateNumber != 1 || candidateInput.Candidates[1].CandidateNumber != 2 || candidateInput.Candidates[0].Label != "The Tavern" || candidateInput.Candidates[1].Label != "The Tavern" {
t.Fatalf("candidate input = %#v, want distinct integer handles for equal names", candidateInput)
}
}
func TestPreparationLimitUsesResolvedModeWithoutExecutionOrAssessment(t *testing.T) {
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 1, Kind: "speech", Text: "A crowded hall"}}}
limit := semanticreconcile.DefaultLimits().MaximumCandidates
input := dnd.LocationRegistry{Locations: make([]dnd.Location, limit+1)}
for i := range input.Locations {
input.Locations[i] = dnd.Location{Name: fmt.Sprintf("Fixture %d", i), SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 1, EndUnitID: 1}}}
input.Locations[i].ID = identity.DeriveID(input.Locations[i].Name, input.Locations[i].SourceRefs)
}
for _, mode := range []contracts.ExecutionMode{contracts.ModeStrict, contracts.ModePermissive} {
t.Run(string(mode), func(t *testing.T) {
client := &recordingLocationNormalizerClient{}
req := normalizeRequestWithSource(input, doc)
req.Operation.Controls.Mode = mode
assessed := 0
req.Operation.Assess = func(context.Context, dnd.LocationRegistry) (contracts.CandidateAssessment, error) {
assessed++
return contracts.CandidateAssessment{}, nil
}
result, err := newNormalizer(t, client).Normalize(context.Background(), req)
if len(client.requests) != 0 || assessed != 0 {
t.Fatal("unready preparation performed native execution or ordinary assessment")
}
if mode == contracts.ModeStrict {
if err == nil {
t.Fatal("strict preparation limit succeeded")
}
} else if err != nil || result.Outcome.Disposition != contracts.ProducerFallback || result.Outcome.ReuseEligible || !reflect.DeepEqual(result.Value, input) {
t.Fatalf("permissive preparation limit = %#v %v", result, err)
}
})
2026-08-04 00:07:47 +00:00
}
}
func TestNormalizeOrdersEvidenceAndIsIdempotent(t *testing.T) {
doc := &source.SourceDocument{ID: "ordered", Units: []source.SourceUnit{{ID: 30}, {ID: 10}}}
input := dnd.LocationRegistry{Locations: []dnd.Location{{Name: "Old Mill", SourceRefs: []source.SourceRef{
2026-08-04 00:07:47 +00:00
{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10},
{SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30},
{SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30},
}}}}
normalizer := newNormalizer(t, &recordingLocationNormalizerClient{})
first, err := normalizer.Normalize(context.Background(), normalizeRequestWithSource(input, doc))
if err != nil || !reflect.DeepEqual([]int{first.Value.Locations[0].SourceRefs[0].StartUnitID, first.Value.Locations[0].SourceRefs[1].StartUnitID}, []int{30, 10}) {
t.Fatalf("first Normalize() = %#v, %v; want document-ordered evidence", first, err)
}
second, err := normalizer.Normalize(context.Background(), normalizeRequestWithSource(first.Value, doc))
if err != nil || !reflect.DeepEqual(second.Value, first.Value) {
t.Fatalf("second Normalize() = %#v, %v; want idempotent value %#v", second, err, first.Value)
}
}