package npcs import ( "context" "reflect" "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/modules/dnd" "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) } if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil { t.Fatal("DecodeOptions() accepted unknown option") } want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, Requires: []string{"merged"}, Provides: []string{"normalized"}, ArtifactKind: dnd.NPCListKind} 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.Fatalf("Register() error = %v", err) } normalizer := New(Options{}) if metadata := normalizer.ManifestMetadata(); metadata["identity_policy"] != identity.Policy || metadata["normalization_policy"] != normalizationPolicy { t.Fatalf("metadata = %#v", metadata) } wantFingerprints := []pipeline.CheckpointFingerprint{{Name: "identity_policy", Value: identity.Policy}, {Name: "normalization_policy", Value: normalizationPolicy}} if got := normalizer.CheckpointFingerprints(); !reflect.DeepEqual(got, wantFingerprints) { t.Fatalf("fingerprints = %#v, want %#v", got, wantFingerprints) } } func TestNormalizeNamesEvidenceAndIDs(t *testing.T) { input := dnd.NPCList{NPCs: []dnd.NPC{{ 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}, }, }}} result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(input)) if err != nil { t.Fatalf("Normalize() error = %v", err) } 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) } for _, reason := range []string{ReasonCodeNPCFieldsNormalized, ReasonCodeSourceReferencesNormalized, ReasonCodeNPCIDRecomputed} { if !hasWarning(result.Warnings, reason, "npcs[0]") { t.Fatalf("warnings = %#v, want %s", result.Warnings, reason) } } } func TestNormalizeConsolidatesCanonicalNamesOnlyAndUnionsEvidence(t *testing.T) { input := dnd.NPCList{NPCs: []dnd.NPC{ {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}}}, }} result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(input)) if err != nil { t.Fatalf("Normalize() error = %v", err) } 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) } 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 !hasWarning(result.Warnings, ReasonCodeDuplicateNPCCollapsed, "npcs[0]") { t.Fatalf("warnings = %#v, want duplicate collapse", result.Warnings) } } func TestNormalizePreservesInvalidCandidatesAndDoesNotAliasInput(t *testing.T) { input := dnd.NPCList{NPCs: []dnd.NPC{{Name: " ", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 0, EndUnitID: -1}}}}} before := dnd.NPCList{NPCs: []dnd.NPC{{Name: " ", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 0, EndUnitID: -1}}}}} result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(input)) if err != nil || !reflect.DeepEqual(input, before) { t.Fatalf("Normalize() = %#v, %v; input mutated to %#v", result, err, input) } 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" if input.NPCs[0].SourceRefs[0].SourceID != "source" { t.Fatal("output aliases input evidence") } } func TestNormalizeHandlesNilAndCanceledCalls(t *testing.T) { normalizer := New(Options{}) result, err := normalizer.Normalize(context.Background(), normalizeRequest(dnd.NPCList{NPCs: nil})) if err != nil || result.Value.NPCs != nil { t.Fatalf("nil list result = %#v, error = %v", result.Value, err) } canceled, cancel := context.WithCancel(context.Background()) cancel() if _, err := normalizer.Normalize(canceled, normalizeRequest(dnd.NPCList{})); err == nil || !strings.Contains(err.Error(), "context error") { t.Fatalf("canceled Normalize() error = %v", err) } } func normalizeRequest(value dnd.NPCList) contracts.TypedNormalizeRequest[dnd.NPCList] { return contracts.TypedNormalizeRequest[dnd.NPCList]{MergeOutput: contracts.MergeArtifact[dnd.NPCList]{Value: value}} } func hasWarning(warnings []contracts.Warning, reason, scope string) bool { for _, warning := range warnings { if warning.ReasonCode == reason && warning.Scope == scope { return true } } return false }