Minimize D&D NPC extraction contracts
This commit is contained in:
@@ -92,9 +92,7 @@ func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
{Name: "identity_policy", Value: identity.Policy},
|
||||
}
|
||||
seeded := n.npcResolver.Seeded()
|
||||
if seeded.Bound() {
|
||||
fingerprints = append(fingerprints, pipeline.CheckpointFingerprint{Name: "npc_registry", Value: seeded.Digest()})
|
||||
}
|
||||
fingerprints = append(fingerprints, pipeline.CheckpointFingerprint{Name: "npc_registry", Value: seeded.ProjectionDigest()})
|
||||
return fingerprints
|
||||
}
|
||||
|
||||
|
||||
@@ -26,12 +26,12 @@ func TestNormalizeCanonicalizesFieldsAndRegistryIdentities(t *testing.T) {
|
||||
}
|
||||
resolution := " the target is hit "
|
||||
input := dnd.CombatTurnList{CombatTurns: []dnd.CombatTurn{{
|
||||
Actor: " storm ",
|
||||
Actor: " aria ",
|
||||
TurnKind: dnd.CombatTurnKindTurn,
|
||||
Actions: []dnd.CombatAction{{
|
||||
Category: dnd.CombatActionCategoryAttack,
|
||||
Declaration: " attacks\n with a sword ",
|
||||
Targets: []string{" minion ", "goblin", " unknown combatant "},
|
||||
Targets: []string{" goblin ", "goblin", " unknown combatant "},
|
||||
Resolution: &resolution,
|
||||
}},
|
||||
Summary: " Aria\n attacks ",
|
||||
@@ -77,10 +77,10 @@ func TestNormalizeResolvesOperationNPCOverrideWithoutSingletonMetadata(t *testin
|
||||
t.Fatalf("New() error = %v", err)
|
||||
}
|
||||
input := dnd.CombatTurnList{CombatTurns: []dnd.CombatTurn{{
|
||||
Actor: "Storm",
|
||||
Actor: "aria",
|
||||
TurnKind: dnd.CombatTurnKindTurn,
|
||||
Actions: []dnd.CombatAction{{Category: dnd.CombatActionCategoryAttack, Declaration: "attacks", Targets: []string{"Minion"}}},
|
||||
Summary: "Storm attacks",
|
||||
Actions: []dnd.CombatAction{{Category: dnd.CombatActionCategoryAttack, Declaration: "attacks", Targets: []string{"goblin"}}},
|
||||
Summary: "Aria attacks",
|
||||
SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}},
|
||||
}}}
|
||||
result, err := normalizer.Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.CombatTurnList]{
|
||||
@@ -222,7 +222,7 @@ func TestNormalizerPreparationMetadataFingerprintsAndModuleContract(t *testing.T
|
||||
if metadata := unbound.ManifestMetadata(); metadata["npc_registry_digest"] != nil || metadata["npc_count"] != nil || metadata["normalization_policy"] != normalizationPolicy || metadata["identity_policy"] != identity.Policy {
|
||||
t.Fatalf("unbound metadata = %#v", metadata)
|
||||
}
|
||||
if got := unbound.CheckpointFingerprints(); len(got) != 2 || got[0].Name != "normalization_policy" || got[1].Name != "identity_policy" {
|
||||
if got := unbound.CheckpointFingerprints(); len(got) != 3 || got[0].Name != "normalization_policy" || got[1].Name != "identity_policy" || got[2].Name != "npc_registry" || got[2].Value == "" {
|
||||
t.Fatalf("unbound fingerprints = %#v", got)
|
||||
}
|
||||
|
||||
@@ -305,8 +305,8 @@ func testDocument() *source.SourceDocument {
|
||||
func npcReferences(t *testing.T) contracts.ReferenceSet {
|
||||
t.Helper()
|
||||
npcs := dnd.NPCList{NPCs: []dnd.NPC{
|
||||
{ID: identity.DeriveID("Aria"), Name: "Aria", Aliases: []string{"Storm"}, Description: "a fighter", Relationships: []dnd.NPCRelationship{{Target: "Goblin", Relationship: "fights"}}, SourceRefs: []source.SourceRef{{SourceID: "npc-run", StartUnitID: 1, EndUnitID: 1}}},
|
||||
{ID: identity.DeriveID("Goblin"), Name: "Goblin", Aliases: []string{"Minion"}, Description: "a goblin", Relationships: []dnd.NPCRelationship{{Target: "Aria", Relationship: "fights"}}, SourceRefs: []source.SourceRef{{SourceID: "npc-run", StartUnitID: 1, EndUnitID: 1}}},
|
||||
{ID: identity.DeriveID("Aria"), Name: "Aria", SourceRefs: []source.SourceRef{{SourceID: "npc-run", StartUnitID: 1, EndUnitID: 1}}},
|
||||
{ID: identity.DeriveID("Goblin"), Name: "Goblin", SourceRefs: []source.SourceRef{{SourceID: "npc-run", StartUnitID: 1, EndUnitID: 1}}},
|
||||
}}
|
||||
content, err := npccodec.New().Encode(npcs)
|
||||
if err != nil {
|
||||
|
||||
@@ -22,11 +22,10 @@ const (
|
||||
normalizationPolicy = "dnd.npcs.normalize.v1"
|
||||
NormalizationPolicy = normalizationPolicy
|
||||
|
||||
ReasonCodeNPCFieldsNormalized = "npc_fields_normalized"
|
||||
ReasonCodeNPCIDRecomputed = "npc_id_recomputed"
|
||||
ReasonCodeSourceReferencesNormalized = "source_references_normalized"
|
||||
ReasonCodeDuplicateNPCCollapsed = "duplicate_npc_collapsed"
|
||||
ReasonCodeRelationshipTargetCanonicalized = "relationship_target_canonicalized"
|
||||
ReasonCodeNPCFieldsNormalized = "npc_fields_normalized"
|
||||
ReasonCodeNPCIDRecomputed = "npc_id_recomputed"
|
||||
ReasonCodeSourceReferencesNormalized = "source_references_normalized"
|
||||
ReasonCodeDuplicateNPCCollapsed = "duplicate_npc_collapsed"
|
||||
)
|
||||
|
||||
var requiredCapabilities = []string{"merged"}
|
||||
@@ -37,23 +36,17 @@ var _ contracts.ManifestMetadataProvider = (*Normalizer)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Normalizer)(nil)
|
||||
|
||||
type Options struct{}
|
||||
|
||||
type Normalizer struct{}
|
||||
|
||||
func New(Options) *Normalizer { return &Normalizer{} }
|
||||
|
||||
func (n *Normalizer) Key() string { return Key }
|
||||
|
||||
func New(Options) *Normalizer { return &Normalizer{} }
|
||||
func (n *Normalizer) Key() string { return Key }
|
||||
func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return nil }
|
||||
|
||||
func (n *Normalizer) ManifestMetadata() map[string]any {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
return map[string]any{
|
||||
"identity_policy": identity.Policy,
|
||||
"normalization_policy": normalizationPolicy,
|
||||
}
|
||||
return map[string]any{"identity_policy": identity.Policy, "normalization_policy": normalizationPolicy}
|
||||
}
|
||||
|
||||
func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
@@ -76,193 +69,106 @@ func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalize
|
||||
if err := ctx.Err(); err != nil {
|
||||
return contracts.TypedNormalizeResult[dnd.NPCList]{}, normalizerErrorf("context error before normalize: %w", err)
|
||||
}
|
||||
|
||||
value, warnings := normalizeList(req.MergeOutput.Value)
|
||||
return contracts.TypedNormalizeResult[dnd.NPCList]{Value: value, Warnings: warnings}, nil
|
||||
}
|
||||
|
||||
type normalizedRecord struct {
|
||||
npc dnd.NPC
|
||||
fieldsChanged bool
|
||||
referencesChanged bool
|
||||
npc dnd.NPC
|
||||
}
|
||||
|
||||
func normalizeList(input dnd.NPCList) (dnd.NPCList, []contracts.Warning) {
|
||||
if input.NPCs == nil {
|
||||
return dnd.NPCList{}, nil
|
||||
}
|
||||
|
||||
records := make([]normalizedRecord, len(input.NPCs))
|
||||
warnings := make([]contracts.Warning, 0)
|
||||
for index, inputNPC := range input.NPCs {
|
||||
npc, fieldsChanged, referencesChanged := normalizeRecord(inputNPC)
|
||||
records[index] = normalizedRecord{
|
||||
npc: npc,
|
||||
fieldsChanged: fieldsChanged,
|
||||
referencesChanged: referencesChanged,
|
||||
}
|
||||
records[index] = normalizedRecord{npc: npc}
|
||||
if fieldsChanged {
|
||||
warnings = append(warnings, contracts.Warning{
|
||||
Scope: npcScope(index),
|
||||
ReasonCode: ReasonCodeNPCFieldsNormalized,
|
||||
Message: fmt.Sprintf("input index %d: NPC fields normalized for %s",
|
||||
index, diagnostics.Quote(inputNPC.Name)),
|
||||
})
|
||||
warnings = append(warnings, contracts.Warning{Scope: npcScope(index), ReasonCode: ReasonCodeNPCFieldsNormalized, Message: fmt.Sprintf("input index %d: NPC name normalized for %s", index, diagnostics.Quote(inputNPC.Name))})
|
||||
}
|
||||
if referencesChanged {
|
||||
warnings = append(warnings, contracts.Warning{
|
||||
Scope: npcScope(index),
|
||||
ReasonCode: ReasonCodeSourceReferencesNormalized,
|
||||
Message: fmt.Sprintf("input index %d: source references normalized (original count %d, final count %d)",
|
||||
index, len(inputNPC.SourceRefs), len(npc.SourceRefs)),
|
||||
})
|
||||
warnings = append(warnings, contracts.Warning{Scope: npcScope(index), ReasonCode: ReasonCodeSourceReferencesNormalized, Message: fmt.Sprintf("input index %d: source references normalized (original count %d, final count %d)", index, len(inputNPC.SourceRefs), len(npc.SourceRefs))})
|
||||
}
|
||||
if inputNPC.ID != npc.ID {
|
||||
warnings = append(warnings, contracts.Warning{
|
||||
Scope: npcScope(index),
|
||||
ReasonCode: ReasonCodeNPCIDRecomputed,
|
||||
Message: fmt.Sprintf("input index %d: NPC ID recomputed from %s",
|
||||
index, diagnostics.Quote(npc.Name)),
|
||||
})
|
||||
warnings = append(warnings, contracts.Warning{Scope: npcScope(index), ReasonCode: ReasonCodeNPCIDRecomputed, Message: fmt.Sprintf("input index %d: NPC ID recomputed from %s", index, diagnostics.Quote(npc.Name))})
|
||||
}
|
||||
}
|
||||
|
||||
components := identityComponents(records)
|
||||
output := dnd.NPCList{NPCs: make([]dnd.NPC, 0, len(components))}
|
||||
retainedIndexes := make([]int, 0, len(components))
|
||||
for _, members := range components {
|
||||
consolidated, sourceChanged := consolidate(records, members)
|
||||
groups := canonicalNameGroups(records)
|
||||
output := dnd.NPCList{NPCs: make([]dnd.NPC, 0, len(groups))}
|
||||
for _, members := range groups {
|
||||
consolidated, referencesChanged := consolidate(records, members)
|
||||
retainedIndex := members[0]
|
||||
output.NPCs = append(output.NPCs, consolidated)
|
||||
retainedIndexes = append(retainedIndexes, retainedIndex)
|
||||
|
||||
if sourceChanged {
|
||||
warnings = append(warnings, contracts.Warning{
|
||||
Scope: npcScope(retainedIndex),
|
||||
ReasonCode: ReasonCodeSourceReferencesNormalized,
|
||||
Message: fmt.Sprintf("input index %d: source references normalized during identity consolidation (final count %d)",
|
||||
retainedIndex, len(consolidated.SourceRefs)),
|
||||
})
|
||||
if referencesChanged {
|
||||
warnings = append(warnings, contracts.Warning{Scope: npcScope(retainedIndex), ReasonCode: ReasonCodeSourceReferencesNormalized, Message: fmt.Sprintf("input index %d: source references normalized during identity consolidation (final count %d)", retainedIndex, len(consolidated.SourceRefs))})
|
||||
}
|
||||
if len(members) > 1 {
|
||||
warnings = append(warnings, duplicateWarning(retainedIndex, members[1:]))
|
||||
}
|
||||
}
|
||||
|
||||
warnings = append(warnings, canonicalizeRelationshipTargets(&output, retainedIndexes)...)
|
||||
return output, warnings
|
||||
}
|
||||
|
||||
func normalizeRecord(input dnd.NPC) (dnd.NPC, bool, bool) {
|
||||
output := cloneNPC(input)
|
||||
output.Name = identity.NormalizeDisplay(input.Name)
|
||||
output.Aliases = normalizeAliases(input.Aliases, output.Name)
|
||||
output.Description = strings.TrimSpace(input.Description)
|
||||
output.Relationships = normalizeRelationships(input.Relationships)
|
||||
output.SourceRefs, _, _ = canonicalizeSourceRefs(input.SourceRefs)
|
||||
output.ID = identity.DeriveID(output.Name)
|
||||
|
||||
fieldsChanged := input.Name != output.Name ||
|
||||
!reflect.DeepEqual(input.Aliases, output.Aliases) ||
|
||||
input.Description != output.Description ||
|
||||
!reflect.DeepEqual(input.Relationships, output.Relationships)
|
||||
referencesChanged := !reflect.DeepEqual(input.SourceRefs, output.SourceRefs)
|
||||
return output, fieldsChanged, referencesChanged
|
||||
return output, input.Name != output.Name, !reflect.DeepEqual(input.SourceRefs, output.SourceRefs)
|
||||
}
|
||||
|
||||
func cloneNPC(input dnd.NPC) dnd.NPC {
|
||||
output := input
|
||||
if input.Aliases != nil {
|
||||
output.Aliases = make([]string, len(input.Aliases))
|
||||
copy(output.Aliases, input.Aliases)
|
||||
}
|
||||
if input.Relationships != nil {
|
||||
output.Relationships = make([]dnd.NPCRelationship, len(input.Relationships))
|
||||
copy(output.Relationships, input.Relationships)
|
||||
}
|
||||
if input.SourceRefs != nil {
|
||||
output.SourceRefs = make([]source.SourceRef, len(input.SourceRefs))
|
||||
copy(output.SourceRefs, input.SourceRefs)
|
||||
}
|
||||
return output
|
||||
input.SourceRefs = cloneSourceRefs(input.SourceRefs)
|
||||
return input
|
||||
}
|
||||
|
||||
func normalizeAliases(input []string, canonicalName string) []string {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
canonicalKey := identity.ComparisonKey(canonicalName)
|
||||
output := make([]string, 0, len(input))
|
||||
seen := make(map[string]struct{}, len(input))
|
||||
for _, alias := range input {
|
||||
normalized := identity.NormalizeDisplay(alias)
|
||||
key := identity.ComparisonKey(normalized)
|
||||
if canonicalKey != "" && key == canonicalKey {
|
||||
continue
|
||||
func canonicalNameGroups(records []normalizedRecord) [][]int {
|
||||
groups := make([][]int, 0, len(records))
|
||||
ownerByKey := make(map[string]int, len(records))
|
||||
for index, record := range records {
|
||||
key := identity.ComparisonKey(record.npc.Name)
|
||||
if key != "" {
|
||||
if groupIndex, ok := ownerByKey[key]; ok {
|
||||
groups[groupIndex] = append(groups[groupIndex], index)
|
||||
continue
|
||||
}
|
||||
ownerByKey[key] = len(groups)
|
||||
}
|
||||
if _, exists := seen[key]; exists {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
output = append(output, normalized)
|
||||
groups = append(groups, []int{index})
|
||||
}
|
||||
return output
|
||||
return groups
|
||||
}
|
||||
|
||||
func normalizeRelationships(input []dnd.NPCRelationship) []dnd.NPCRelationship {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
output := make([]dnd.NPCRelationship, 0, len(input))
|
||||
seen := make(map[relationshipIdentity]struct{}, len(input))
|
||||
for _, relationship := range input {
|
||||
normalized := dnd.NPCRelationship{
|
||||
Target: identity.NormalizeDisplay(relationship.Target),
|
||||
Relationship: strings.TrimSpace(relationship.Relationship),
|
||||
}
|
||||
key := relationshipKey(normalized)
|
||||
if _, exists := seen[key]; exists {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
output = append(output, normalized)
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
type relationshipIdentity struct {
|
||||
target string
|
||||
relationship string
|
||||
}
|
||||
|
||||
func relationshipKey(relationship dnd.NPCRelationship) relationshipIdentity {
|
||||
return relationshipIdentity{
|
||||
target: identity.ComparisonKey(relationship.Target),
|
||||
relationship: identity.ComparisonKey(relationship.Relationship),
|
||||
func consolidate(records []normalizedRecord, members []int) (dnd.NPC, bool) {
|
||||
output := cloneNPC(records[members[0]].npc)
|
||||
originalRefs := cloneSourceRefs(output.SourceRefs)
|
||||
for _, member := range members[1:] {
|
||||
output.SourceRefs = append(output.SourceRefs, records[member].npc.SourceRefs...)
|
||||
}
|
||||
output.SourceRefs, _, _ = canonicalizeSourceRefs(output.SourceRefs)
|
||||
output.ID = identity.DeriveID(output.Name)
|
||||
return output, !reflect.DeepEqual(originalRefs, output.SourceRefs)
|
||||
}
|
||||
|
||||
func canonicalizeSourceRefs(input []source.SourceRef) ([]source.SourceRef, bool, int) {
|
||||
if input == nil {
|
||||
return nil, false, 0
|
||||
}
|
||||
|
||||
canonical := make([]source.SourceRef, len(input))
|
||||
copy(canonical, input)
|
||||
canonical := cloneSourceRefs(input)
|
||||
sort.SliceStable(canonical, func(left, right int) bool {
|
||||
return sourceRefLess(canonical[left], canonical[right])
|
||||
})
|
||||
|
||||
orderChanged := false
|
||||
for index := range input {
|
||||
if input[index] != canonical[index] {
|
||||
orderChanged = true
|
||||
break
|
||||
if canonical[left].SourceID != canonical[right].SourceID {
|
||||
return canonical[left].SourceID < canonical[right].SourceID
|
||||
}
|
||||
}
|
||||
|
||||
if canonical[left].StartUnitID != canonical[right].StartUnitID {
|
||||
return canonical[left].StartUnitID < canonical[right].StartUnitID
|
||||
}
|
||||
return canonical[left].EndUnitID < canonical[right].EndUnitID
|
||||
})
|
||||
orderChanged := !reflect.DeepEqual(input, canonical)
|
||||
unique := make([]source.SourceRef, 0, len(canonical))
|
||||
for _, ref := range canonical {
|
||||
if len(unique) == 0 || unique[len(unique)-1] != ref {
|
||||
@@ -272,219 +178,11 @@ func canonicalizeSourceRefs(input []source.SourceRef) ([]source.SourceRef, bool,
|
||||
return unique, orderChanged, len(input) - len(unique)
|
||||
}
|
||||
|
||||
func sourceRefLess(left, right source.SourceRef) bool {
|
||||
if left.SourceID != right.SourceID {
|
||||
return left.SourceID < right.SourceID
|
||||
}
|
||||
if left.StartUnitID != right.StartUnitID {
|
||||
return left.StartUnitID < right.StartUnitID
|
||||
}
|
||||
return left.EndUnitID < right.EndUnitID
|
||||
}
|
||||
|
||||
func identityComponents(records []normalizedRecord) [][]int {
|
||||
parent := make([]int, len(records))
|
||||
for index := range parent {
|
||||
parent[index] = index
|
||||
}
|
||||
|
||||
for left := 0; left < len(records); left++ {
|
||||
for right := left + 1; right < len(records); right++ {
|
||||
if recordsCanMerge(records[left].npc, records[right].npc) {
|
||||
union(parent, left, right)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
byRoot := make(map[int][]int, len(records))
|
||||
for index := range records {
|
||||
root := find(parent, index)
|
||||
byRoot[root] = append(byRoot[root], index)
|
||||
}
|
||||
roots := make([]int, 0, len(byRoot))
|
||||
for root := range byRoot {
|
||||
roots = append(roots, root)
|
||||
}
|
||||
sort.Slice(roots, func(left, right int) bool {
|
||||
return byRoot[roots[left]][0] < byRoot[roots[right]][0]
|
||||
})
|
||||
|
||||
components := make([][]int, 0, len(roots))
|
||||
for _, root := range roots {
|
||||
components = append(components, byRoot[root])
|
||||
}
|
||||
return components
|
||||
}
|
||||
|
||||
func recordsCanMerge(left, right dnd.NPC) bool {
|
||||
leftCanonical := identity.ComparisonKey(left.Name)
|
||||
rightCanonical := identity.ComparisonKey(right.Name)
|
||||
return leftCanonical == rightCanonical ||
|
||||
containsAliasKey(right.Aliases, leftCanonical) ||
|
||||
containsAliasKey(left.Aliases, rightCanonical)
|
||||
}
|
||||
|
||||
func containsAliasKey(aliases []string, wanted string) bool {
|
||||
for _, alias := range aliases {
|
||||
if identity.ComparisonKey(alias) == wanted {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func find(parent []int, index int) int {
|
||||
for parent[index] != index {
|
||||
parent[index] = parent[parent[index]]
|
||||
index = parent[index]
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
func union(parent []int, left, right int) {
|
||||
leftRoot := find(parent, left)
|
||||
rightRoot := find(parent, right)
|
||||
if leftRoot == rightRoot {
|
||||
return
|
||||
}
|
||||
if leftRoot < rightRoot {
|
||||
parent[rightRoot] = leftRoot
|
||||
} else {
|
||||
parent[leftRoot] = rightRoot
|
||||
}
|
||||
}
|
||||
|
||||
func consolidate(records []normalizedRecord, members []int) (dnd.NPC, bool) {
|
||||
output := cloneNPC(records[members[0]].npc)
|
||||
originalRefs := cloneSourceRefs(output.SourceRefs)
|
||||
canonicalKey := identity.ComparisonKey(output.Name)
|
||||
|
||||
for _, member := range members[1:] {
|
||||
candidate := records[member].npc
|
||||
appendAlias(&output.Aliases, candidate.Name, canonicalKey)
|
||||
for _, alias := range candidate.Aliases {
|
||||
appendAlias(&output.Aliases, alias, canonicalKey)
|
||||
}
|
||||
for _, relationship := range candidate.Relationships {
|
||||
appendRelationship(&output.Relationships, relationship)
|
||||
}
|
||||
output.SourceRefs = append(output.SourceRefs, candidate.SourceRefs...)
|
||||
}
|
||||
|
||||
output.SourceRefs, _, _ = canonicalizeSourceRefs(output.SourceRefs)
|
||||
output.ID = identity.DeriveID(output.Name)
|
||||
return output, !reflect.DeepEqual(originalRefs, output.SourceRefs)
|
||||
}
|
||||
|
||||
func cloneSourceRefs(input []source.SourceRef) []source.SourceRef {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
output := make([]source.SourceRef, len(input))
|
||||
copy(output, input)
|
||||
return output
|
||||
}
|
||||
|
||||
func appendAlias(aliases *[]string, value, canonicalKey string) {
|
||||
key := identity.ComparisonKey(value)
|
||||
if canonicalKey != "" && key == canonicalKey {
|
||||
return
|
||||
}
|
||||
for _, existing := range *aliases {
|
||||
if identity.ComparisonKey(existing) == key {
|
||||
return
|
||||
}
|
||||
}
|
||||
*aliases = append(*aliases, value)
|
||||
}
|
||||
|
||||
func appendRelationship(relationships *[]dnd.NPCRelationship, relationship dnd.NPCRelationship) {
|
||||
key := relationshipKey(relationship)
|
||||
for _, existing := range *relationships {
|
||||
if relationshipKey(existing) == key {
|
||||
return
|
||||
}
|
||||
}
|
||||
*relationships = append(*relationships, relationship)
|
||||
}
|
||||
|
||||
func canonicalizeRelationshipTargets(list *dnd.NPCList, retainedIndexes []int) []contracts.Warning {
|
||||
if list == nil || len(list.NPCs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
owners := make(map[string][]int)
|
||||
for index, npc := range list.NPCs {
|
||||
if key := identity.ComparisonKey(npc.Name); key != "" {
|
||||
owners[key] = append(owners[key], index)
|
||||
}
|
||||
for _, alias := range npc.Aliases {
|
||||
if key := identity.ComparisonKey(alias); key != "" {
|
||||
owners[key] = appendUniqueIndex(owners[key], index)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
warnings := make([]contracts.Warning, 0)
|
||||
for outputIndex := range list.NPCs {
|
||||
npc := &list.NPCs[outputIndex]
|
||||
originalRelationshipCount := len(npc.Relationships)
|
||||
for relationshipIndex := range npc.Relationships {
|
||||
relationship := &npc.Relationships[relationshipIndex]
|
||||
key := identity.ComparisonKey(relationship.Target)
|
||||
if key == "" || len(owners[key]) != 1 {
|
||||
continue
|
||||
}
|
||||
target := list.NPCs[owners[key][0]].Name
|
||||
if relationship.Target == target {
|
||||
continue
|
||||
}
|
||||
oldTarget := relationship.Target
|
||||
relationship.Target = target
|
||||
warnings = append(warnings, contracts.Warning{
|
||||
Scope: npcScope(retainedIndexes[outputIndex]),
|
||||
ReasonCode: ReasonCodeRelationshipTargetCanonicalized,
|
||||
Message: fmt.Sprintf("input index %d: relationship target canonicalized from %s to %s",
|
||||
retainedIndexes[outputIndex], diagnostics.Quote(oldTarget), diagnostics.Quote(target)),
|
||||
})
|
||||
}
|
||||
npc.Relationships = deduplicateRelationships(npc.Relationships)
|
||||
if len(npc.Relationships) != originalRelationshipCount {
|
||||
warnings = append(warnings, contracts.Warning{
|
||||
Scope: npcScope(retainedIndexes[outputIndex]),
|
||||
ReasonCode: ReasonCodeNPCFieldsNormalized,
|
||||
Message: fmt.Sprintf("input index %d: duplicate relationships removed after target canonicalization",
|
||||
retainedIndexes[outputIndex]),
|
||||
})
|
||||
}
|
||||
}
|
||||
return warnings
|
||||
}
|
||||
|
||||
func deduplicateRelationships(input []dnd.NPCRelationship) []dnd.NPCRelationship {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
output := make([]dnd.NPCRelationship, 0, len(input))
|
||||
seen := make(map[relationshipIdentity]struct{}, len(input))
|
||||
for _, relationship := range input {
|
||||
key := relationshipKey(relationship)
|
||||
if _, exists := seen[key]; exists {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
output = append(output, relationship)
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func appendUniqueIndex(values []int, wanted int) []int {
|
||||
for _, value := range values {
|
||||
if value == wanted {
|
||||
return values
|
||||
}
|
||||
}
|
||||
return append(values, wanted)
|
||||
return append([]source.SourceRef(nil), input...)
|
||||
}
|
||||
|
||||
func duplicateWarning(retainedIndex int, removed []int) contracts.Warning {
|
||||
@@ -497,28 +195,17 @@ func duplicateWarning(retainedIndex int, removed []int) contracts.Warning {
|
||||
for index, removedIndex := range displayed {
|
||||
indices[index] = strconv.Itoa(removedIndex)
|
||||
}
|
||||
|
||||
message := fmt.Sprintf("retained input index %d; removed input indices [%s]", retainedIndex, strings.Join(indices, ", "))
|
||||
if omitted := len(removed) - len(displayed); omitted > 0 {
|
||||
message += fmt.Sprintf("; %d additional removed input indices omitted", omitted)
|
||||
}
|
||||
return contracts.Warning{
|
||||
Scope: npcScope(retainedIndex),
|
||||
ReasonCode: ReasonCodeDuplicateNPCCollapsed,
|
||||
Message: message,
|
||||
}
|
||||
return contracts.Warning{Scope: npcScope(retainedIndex), ReasonCode: ReasonCodeDuplicateNPCCollapsed, Message: message}
|
||||
}
|
||||
|
||||
func npcScope(index int) string { return fmt.Sprintf("npcs[%d]", index) }
|
||||
|
||||
func ModuleSpec() pipeline.ModuleSpec {
|
||||
return pipeline.ModuleSpec{
|
||||
Key: Key,
|
||||
Stage: pipeline.StageNormalize,
|
||||
Requires: append([]string(nil), requiredCapabilities...),
|
||||
Provides: append([]string(nil), providedCapabilities...),
|
||||
ArtifactKind: dnd.NPCListKind,
|
||||
}
|
||||
return pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.NPCListKind}
|
||||
}
|
||||
|
||||
func Register(registry *pipeline.NormalizerRegistry) error {
|
||||
@@ -531,10 +218,7 @@ func Register(registry *pipeline.NormalizerRegistry) error {
|
||||
})
|
||||
}
|
||||
|
||||
func validateOptions(options map[string]any) error {
|
||||
_, err := DecodeOptions(options)
|
||||
return err
|
||||
}
|
||||
func validateOptions(options map[string]any) error { _, err := DecodeOptions(options); return err }
|
||||
|
||||
func DecodeOptions(options map[string]any) (Options, error) {
|
||||
if err := pipeline.RejectUnknownOptions(options); err != nil {
|
||||
|
||||
@@ -10,223 +10,91 @@ import (
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
identity "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity"
|
||||
"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 || !strings.Contains(err.Error(), "unknown option") {
|
||||
t.Fatalf("DecodeOptions() error = %v, want unknown option error", err)
|
||||
}
|
||||
|
||||
want := pipeline.ModuleSpec{
|
||||
Key: Key,
|
||||
Stage: pipeline.StageNormalize,
|
||||
Requires: []string{"merged"},
|
||||
Provides: []string{"normalized"},
|
||||
ArtifactKind: dnd.NPCListKind,
|
||||
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)
|
||||
}
|
||||
if slots := New(Options{}).ReferenceSlots(); slots != nil {
|
||||
t.Fatalf("ReferenceSlots() = %#v, want nil", slots)
|
||||
}
|
||||
|
||||
registry := pipeline.NewNormalizerRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
t.Fatalf("Register() error = %v", err)
|
||||
}
|
||||
if registered, ok := registry.SpecForArtifact(Key, dnd.NPCListKind); !ok || !reflect.DeepEqual(registered, want) {
|
||||
t.Fatalf("registered spec = %#v, ok = %t, want %#v", registered, ok, want)
|
||||
}
|
||||
if err := Register(nil); err == nil || !strings.Contains(err.Error(), "normalizer registry") {
|
||||
t.Fatalf("Register(nil) error = %v, want registry error", err)
|
||||
}
|
||||
|
||||
normalizer := New(Options{})
|
||||
metadata := normalizer.ManifestMetadata()
|
||||
if metadata["identity_policy"] != identity.Policy || metadata["normalization_policy"] != normalizationPolicy {
|
||||
t.Fatalf("metadata = %#v, want identity and normalization policies", metadata)
|
||||
if metadata := normalizer.ManifestMetadata(); metadata["identity_policy"] != identity.Policy || metadata["normalization_policy"] != normalizationPolicy {
|
||||
t.Fatalf("metadata = %#v", metadata)
|
||||
}
|
||||
fingerprints := normalizer.CheckpointFingerprints()
|
||||
wantFingerprints := []pipeline.CheckpointFingerprint{
|
||||
{Name: "identity_policy", Value: identity.Policy},
|
||||
{Name: "normalization_policy", Value: normalizationPolicy},
|
||||
}
|
||||
if !reflect.DeepEqual(fingerprints, wantFingerprints) {
|
||||
t.Fatalf("fingerprints = %#v, want %#v", fingerprints, wantFingerprints)
|
||||
}
|
||||
fingerprints[0].Value = "changed"
|
||||
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 were not defensive: %#v", got)
|
||||
t.Fatalf("fingerprints = %#v, want %#v", got, wantFingerprints)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizePerRecordFieldsAndEvidence(t *testing.T) {
|
||||
func TestNormalizeNamesEvidenceAndIDs(t *testing.T) {
|
||||
input := dnd.NPCList{NPCs: []dnd.NPC{{
|
||||
ID: "wrong",
|
||||
Name: " Lady\tAsh ",
|
||||
Aliases: []string{" Ash ", " L.A. ", "l.a.", " Lady Ash "},
|
||||
Description: " first description\n",
|
||||
Relationships: []dnd.NPCRelationship{
|
||||
{Target: " Lord\nOak ", Relationship: " friend "},
|
||||
{Target: "lord oak", Relationship: "friend"},
|
||||
},
|
||||
SourceRefs: []source.SourceRef{
|
||||
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, want nil", err)
|
||||
t.Fatalf("Normalize() error = %v", err)
|
||||
}
|
||||
got := result.Value.NPCs[0]
|
||||
if got.Name != "Lady Ash" || got.Description != "first description" {
|
||||
t.Fatalf("normalized fields = %#v, want normalized display and description", got)
|
||||
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)
|
||||
}
|
||||
if !reflect.DeepEqual(got.Aliases, []string{"Ash", "L.A."}) {
|
||||
t.Fatalf("aliases = %#v, want canonical alias removal and deduplication", got.Aliases)
|
||||
}
|
||||
wantRelationships := []dnd.NPCRelationship{{Target: "Lord Oak", Relationship: "friend"}}
|
||||
if !reflect.DeepEqual(got.Relationships, wantRelationships) {
|
||||
t.Fatalf("relationships = %#v, want %#v", got.Relationships, wantRelationships)
|
||||
}
|
||||
wantRefs := []source.SourceRef{
|
||||
{SourceID: "a", StartUnitID: 4, EndUnitID: 4},
|
||||
{SourceID: "b", StartUnitID: 2, EndUnitID: 3},
|
||||
}
|
||||
if !reflect.DeepEqual(got.SourceRefs, wantRefs) {
|
||||
t.Fatalf("source refs = %#v, want %#v", got.SourceRefs, wantRefs)
|
||||
}
|
||||
if got.ID != identity.DeriveID("Lady Ash") {
|
||||
t.Fatalf("ID = %q, want derived ID", got.ID)
|
||||
}
|
||||
if !hasWarning(result.Warnings, ReasonCodeNPCFieldsNormalized, "npcs[0]") ||
|
||||
!hasWarning(result.Warnings, ReasonCodeSourceReferencesNormalized, "npcs[0]") ||
|
||||
!hasWarning(result.Warnings, ReasonCodeNPCIDRecomputed, "npcs[0]") {
|
||||
t.Fatalf("warnings = %#v, want field, source, and ID warnings", result.Warnings)
|
||||
for _, reason := range []string{ReasonCodeNPCFieldsNormalized, ReasonCodeSourceReferencesNormalized, ReasonCodeNPCIDRecomputed} {
|
||||
if !hasWarning(result.Warnings, reason, "npcs[0]") {
|
||||
t.Fatalf("warnings = %#v, want %s", result.Warnings, reason)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeConsolidatesIdentityComponentsInStableOrder(t *testing.T) {
|
||||
func TestNormalizeConsolidatesCanonicalNamesOnlyAndUnionsEvidence(t *testing.T) {
|
||||
input := dnd.NPCList{NPCs: []dnd.NPC{
|
||||
{
|
||||
Name: " Captain Vale ",
|
||||
Description: "first description",
|
||||
Aliases: []string{"Vale"},
|
||||
Relationships: []dnd.NPCRelationship{{Target: "Archivist", Relationship: "knows"}},
|
||||
SourceRefs: []source.SourceRef{{SourceID: "a", StartUnitID: 1, EndUnitID: 1}},
|
||||
},
|
||||
{
|
||||
Name: "Captain Vale",
|
||||
Description: "second description",
|
||||
Aliases: []string{"CV"},
|
||||
SourceRefs: []source.SourceRef{{SourceID: "b", StartUnitID: 1, EndUnitID: 1}},
|
||||
},
|
||||
{
|
||||
Name: "The Sage",
|
||||
Description: "sage description",
|
||||
Aliases: []string{"Archivist"},
|
||||
SourceRefs: []source.SourceRef{{SourceID: "c", StartUnitID: 1, EndUnitID: 1}},
|
||||
},
|
||||
{
|
||||
Name: "Archivist",
|
||||
Description: "later sage description",
|
||||
Aliases: []string{"Chronicler"},
|
||||
SourceRefs: []source.SourceRef{{SourceID: "d", StartUnitID: 1, EndUnitID: 1}},
|
||||
},
|
||||
{
|
||||
Name: "North",
|
||||
Description: "north description",
|
||||
SourceRefs: []source.SourceRef{{SourceID: "e", StartUnitID: 1, EndUnitID: 1}},
|
||||
},
|
||||
{
|
||||
Name: "North Old",
|
||||
Description: "old north description",
|
||||
Aliases: []string{"North"},
|
||||
SourceRefs: []source.SourceRef{{SourceID: "f", StartUnitID: 1, EndUnitID: 1}},
|
||||
},
|
||||
{
|
||||
Name: "North Renamed",
|
||||
Description: "renamed north description",
|
||||
Aliases: []string{"North Old"},
|
||||
SourceRefs: []source.SourceRef{{SourceID: "g", StartUnitID: 1, EndUnitID: 1}},
|
||||
},
|
||||
{Name: "Red", Description: "red", Aliases: []string{"Shared"}, SourceRefs: []source.SourceRef{{SourceID: "h", StartUnitID: 1, EndUnitID: 1}}},
|
||||
{Name: "Blue", Description: "blue", Aliases: []string{"Shared"}, SourceRefs: []source.SourceRef{{SourceID: "i", StartUnitID: 1, EndUnitID: 1}}},
|
||||
{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, want nil", err)
|
||||
t.Fatalf("Normalize() error = %v", err)
|
||||
}
|
||||
if got := len(result.Value.NPCs); got != 5 {
|
||||
t.Fatalf("normalized NPC count = %d, want five components", got)
|
||||
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)
|
||||
}
|
||||
|
||||
first := result.Value.NPCs[0]
|
||||
if first.Name != "Captain Vale" || first.Description != "first description" || !reflect.DeepEqual(first.Aliases, []string{"Vale", "CV"}) {
|
||||
t.Fatalf("first component = %#v, want first description and ordered aliases", first)
|
||||
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 !reflect.DeepEqual(first.SourceRefs, []source.SourceRef{
|
||||
{SourceID: "a", StartUnitID: 1, EndUnitID: 1},
|
||||
{SourceID: "b", StartUnitID: 1, EndUnitID: 1},
|
||||
}) {
|
||||
t.Fatalf("first provenance = %#v, want unioned refs", first.SourceRefs)
|
||||
}
|
||||
|
||||
second := result.Value.NPCs[1]
|
||||
if second.Name != "The Sage" || !reflect.DeepEqual(second.Aliases, []string{"Archivist", "Chronicler"}) || second.Description != "sage description" {
|
||||
t.Fatalf("canonical-to-alias component = %#v, want consolidated sage", second)
|
||||
}
|
||||
if first.Relationships[0].Target != "The Sage" {
|
||||
t.Fatalf("relationship target = %q, want The Sage", first.Relationships[0].Target)
|
||||
}
|
||||
if !hasWarning(result.Warnings, ReasonCodeRelationshipTargetCanonicalized, "npcs[0]") ||
|
||||
!hasWarning(result.Warnings, ReasonCodeDuplicateNPCCollapsed, "npcs[0]") ||
|
||||
!hasWarning(result.Warnings, ReasonCodeDuplicateNPCCollapsed, "npcs[2]") {
|
||||
t.Fatalf("warnings = %#v, want collapse and target warnings", result.Warnings)
|
||||
}
|
||||
|
||||
if got := result.Value.NPCs[2].Aliases; !reflect.DeepEqual(got, []string{"North Old", "North Renamed"}) {
|
||||
t.Fatalf("transitive aliases = %#v, want ordered canonical members", got)
|
||||
}
|
||||
if result.Value.NPCs[3].Name != "Red" || result.Value.NPCs[4].Name != "Blue" {
|
||||
t.Fatalf("shared-alias ordering = %#v, want Red then Blue", result.Value.NPCs[3:])
|
||||
if !hasWarning(result.Warnings, ReasonCodeDuplicateNPCCollapsed, "npcs[0]") {
|
||||
t.Fatalf("warnings = %#v, want duplicate collapse", result.Warnings)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizePreservesInvalidEvidenceAndDoesNotAliasInput(t *testing.T) {
|
||||
invalid := dnd.NPCList{NPCs: []dnd.NPC{{
|
||||
Name: " ",
|
||||
Aliases: []string{""},
|
||||
Description: " ",
|
||||
Relationships: []dnd.NPCRelationship{{Target: " ", Relationship: " "}},
|
||||
SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 9, EndUnitID: 9}},
|
||||
}}}
|
||||
original := cloneNPCListForTest(invalid)
|
||||
result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(invalid))
|
||||
if err != nil {
|
||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||
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 !reflect.DeepEqual(invalid, original) {
|
||||
t.Fatalf("normalizer mutated input: got %#v, want %#v", invalid, original)
|
||||
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])
|
||||
}
|
||||
if result.Value.NPCs[0].Name != "" || result.Value.NPCs[0].Aliases[0] != "" || result.Value.NPCs[0].Relationships[0].Target != "" {
|
||||
t.Fatalf("invalid evidence was unexpectedly removed: %#v", result.Value.NPCs[0])
|
||||
}
|
||||
|
||||
result.Value.NPCs[0].Aliases[0] = "changed"
|
||||
result.Value.NPCs[0].Relationships[0].Target = "changed"
|
||||
result.Value.NPCs[0].SourceRefs[0].SourceID = "changed"
|
||||
if invalid.NPCs[0].Aliases[0] != "" || invalid.NPCs[0].Relationships[0].Target != " " || invalid.NPCs[0].SourceRefs[0].SourceID != "source" {
|
||||
t.Fatalf("output aliases input storage: input = %#v", invalid)
|
||||
if input.NPCs[0].SourceRefs[0].SourceID != "source" {
|
||||
t.Fatal("output aliases input evidence")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,52 +102,17 @@ 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, want nil NPC slice", result.Value, err)
|
||||
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, want context error", err)
|
||||
}
|
||||
if _, err := normalizer.Normalize(nil, normalizeRequest(dnd.NPCList{})); err == nil || !strings.Contains(err.Error(), "context must not be nil") {
|
||||
t.Fatalf("nil context Normalize() error = %v, want context error", err)
|
||||
}
|
||||
var nilNormalizer *Normalizer
|
||||
if _, err := nilNormalizer.Normalize(context.Background(), normalizeRequest(dnd.NPCList{})); err == nil {
|
||||
t.Fatal("nil normalizer Normalize() error = nil, want error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizePreservesPresentEmptyAliases(t *testing.T) {
|
||||
result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(dnd.NPCList{NPCs: []dnd.NPC{{
|
||||
Name: "Hooded Guard",
|
||||
Aliases: []string{},
|
||||
Description: "A distinguishable sentry.",
|
||||
SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}},
|
||||
}}}))
|
||||
if err != nil {
|
||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||
}
|
||||
if result.Value.NPCs[0].Aliases == nil || len(result.Value.NPCs[0].Aliases) != 0 {
|
||||
t.Fatalf("aliases = %#v, want present empty array", result.Value.NPCs[0].Aliases)
|
||||
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 cloneNPCListForTest(input dnd.NPCList) dnd.NPCList {
|
||||
output := dnd.NPCList{}
|
||||
if input.NPCs != nil {
|
||||
output.NPCs = make([]dnd.NPC, len(input.NPCs))
|
||||
for index, npc := range input.NPCs {
|
||||
output.NPCs[index] = cloneNPC(npc)
|
||||
}
|
||||
}
|
||||
return output
|
||||
return contracts.TypedNormalizeRequest[dnd.NPCList]{MergeOutput: contracts.MergeArtifact[dnd.NPCList]{Value: value}}
|
||||
}
|
||||
|
||||
func hasWarning(warnings []contracts.Warning, reason, scope string) bool {
|
||||
|
||||
Reference in New Issue
Block a user