Centralize D&D source reference ordering
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"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"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
|
||||
combatshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/combatturns/shape"
|
||||
)
|
||||
@@ -19,7 +20,7 @@ import (
|
||||
const (
|
||||
Key = "normalize/dnd/combat-turns/invariants"
|
||||
ReasonCode = "invalid_combat_turn_normalization"
|
||||
policy = "dnd.combat_turns.validator.normalized.v1"
|
||||
policy = "dnd.combat_turns.validator.normalized.v2"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
@@ -50,14 +51,14 @@ func Validate(doc *source.SourceDocument, value dnd.CombatTurnList) error {
|
||||
if combatshape.Validate(value) != nil || !sourceRefsValid(doc, value) {
|
||||
return nil
|
||||
}
|
||||
issues := issuesFor(doc, value)
|
||||
issues := issuesFor(shared.NewSourceRefOrder(doc), value)
|
||||
if len(issues) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s", diagnostics.Aggregate("invalid combat turn normalization", issues))
|
||||
}
|
||||
|
||||
func issuesFor(doc *source.SourceDocument, value dnd.CombatTurnList) []string {
|
||||
func issuesFor(order shared.SourceRefOrder, value dnd.CombatTurnList) []string {
|
||||
issues := make([]string, 0)
|
||||
seenIdentity := make(map[string]int)
|
||||
previousPosition := -1
|
||||
@@ -69,14 +70,14 @@ func issuesFor(doc *source.SourceDocument, value dnd.CombatTurnList) []string {
|
||||
for refIndex := 1; refIndex < len(turn.SourceRefs); refIndex++ {
|
||||
previous := turn.SourceRefs[refIndex-1]
|
||||
current := turn.SourceRefs[refIndex]
|
||||
if sourceRefLess(current, previous) {
|
||||
if order.Less(current, previous) {
|
||||
issues = append(issues, fmt.Sprintf("%s.source_refs are not in canonical order at index %d", prefix, refIndex))
|
||||
} else if current == previous {
|
||||
issues = append(issues, fmt.Sprintf("%s.source_refs[%d] duplicates the previous reference", prefix, refIndex))
|
||||
}
|
||||
}
|
||||
|
||||
position, ok := earliestSourcePosition(doc, turn)
|
||||
position, ok := order.EarliestValid(turn.SourceRefs)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
@@ -107,36 +108,6 @@ func sourceRefsValid(doc *source.SourceDocument, value dnd.CombatTurnList) bool
|
||||
return true
|
||||
}
|
||||
|
||||
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 earliestSourcePosition(doc *source.SourceDocument, turn dnd.CombatTurn) (int, bool) {
|
||||
if doc == nil {
|
||||
return 0, false
|
||||
}
|
||||
earliest := 0
|
||||
found := false
|
||||
for _, ref := range turn.SourceRefs {
|
||||
if source.ValidateRef(doc, ref) != nil {
|
||||
continue
|
||||
}
|
||||
index, ok := source.UnitIndex(doc, ref.StartUnitID)
|
||||
if !ok || (found && index >= earliest) {
|
||||
continue
|
||||
}
|
||||
earliest = index
|
||||
found = true
|
||||
}
|
||||
return earliest, found
|
||||
}
|
||||
|
||||
func duplicateKey(turn dnd.CombatTurn) (string, bool) {
|
||||
if len(turn.SourceRefs) == 0 {
|
||||
return "", false
|
||||
|
||||
@@ -57,6 +57,24 @@ func TestValidateRejectsOwnedNormalizedInvariants(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateUsesSourceDocumentOrder(t *testing.T) {
|
||||
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 30}, {ID: 10}}}
|
||||
value := dnd.CombatTurnList{CombatTurns: []dnd.CombatTurn{
|
||||
{Actor: "Aria", TurnKind: dnd.CombatTurnKindTurn, SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}, {SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}}},
|
||||
}}
|
||||
if err := Validate(doc, value); err == nil || !strings.Contains(err.Error(), "not in canonical order") {
|
||||
t.Fatalf("Validate() error = %v, want document-order reference rejection", err)
|
||||
}
|
||||
|
||||
value = dnd.CombatTurnList{CombatTurns: []dnd.CombatTurn{
|
||||
{Actor: "Aria", TurnKind: dnd.CombatTurnKindTurn, SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}}},
|
||||
{Actor: "Borin", TurnKind: dnd.CombatTurnKindTurn, SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}}},
|
||||
}}
|
||||
if err := Validate(doc, value); err == nil || !strings.Contains(err.Error(), "out of chronological order") {
|
||||
t.Fatalf("Validate() error = %v, want document-order chronology rejection", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorDefersShapeAndSourceReferenceFailures(t *testing.T) {
|
||||
doc := invariantDocument()
|
||||
shapeInvalid := normalizedList()
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
interactionmodel "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcinteractions"
|
||||
npcregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/registry"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
|
||||
interactionshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/npcinteractions/shape"
|
||||
)
|
||||
@@ -89,7 +90,7 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
if !npcRegistry.Bound() {
|
||||
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: "invalid NPC interaction normalization: NPC registry reference is required"}, nil
|
||||
}
|
||||
issues := issuesFor(req.Source, req.Value, npcRegistry)
|
||||
issues := issuesFor(req.Source, shared.NewSourceRefOrder(req.Source), req.Value, npcRegistry)
|
||||
if len(issues) == 0 {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
@@ -109,7 +110,7 @@ func allSourceRefsValid(doc *source.SourceDocument, value dnd.NPCInteractionList
|
||||
return true
|
||||
}
|
||||
|
||||
func issuesFor(doc *source.SourceDocument, value dnd.NPCInteractionList, npcRegistry *npcregistry.Registry) []string {
|
||||
func issuesFor(doc *source.SourceDocument, order shared.SourceRefOrder, value dnd.NPCInteractionList, npcRegistry *npcregistry.Registry) []string {
|
||||
issues := make([]string, 0)
|
||||
for index, interaction := range value.Interactions {
|
||||
prefix := fmt.Sprintf("interactions[%d]", index)
|
||||
@@ -119,7 +120,7 @@ func issuesFor(doc *source.SourceDocument, value dnd.NPCInteractionList, npcRegi
|
||||
for refIndex := 1; refIndex < len(interaction.SourceRefs); refIndex++ {
|
||||
previous := interaction.SourceRefs[refIndex-1]
|
||||
current := interaction.SourceRefs[refIndex]
|
||||
if interactionmodel.SourceRefLess(doc, current, previous) {
|
||||
if order.Less(current, previous) {
|
||||
issues = append(issues, fmt.Sprintf("%s.source_refs are not in canonical order at index %d", prefix, refIndex))
|
||||
} else if current == previous {
|
||||
issues = append(issues, fmt.Sprintf("%s.source_refs[%d] duplicates the previous reference", prefix, refIndex))
|
||||
@@ -128,7 +129,7 @@ func issuesFor(doc *source.SourceDocument, value dnd.NPCInteractionList, npcRegi
|
||||
}
|
||||
|
||||
if !sort.SliceIsSorted(value.Interactions, func(left, right int) bool {
|
||||
return interactionmodel.Less(doc, value.Interactions[left], value.Interactions[right])
|
||||
return interactionmodel.Less(order, value.Interactions[left], value.Interactions[right])
|
||||
}) {
|
||||
issues = append(issues, "interactions are not in canonical order")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user