Add D&D scene description validation
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
// Package invariants validates normalized D&D scene description artifacts.
|
||||
package invariants
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"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/shared/diagnostics"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/scenedescriptions/shape"
|
||||
)
|
||||
|
||||
const (
|
||||
Key = "normalize/dnd/scene-descriptions/invariants"
|
||||
ReasonCode = "invalid_scene_description_normalization"
|
||||
policy = "dnd.scene_descriptions.validator.invariants.v1"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.SceneDescriptionList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.SceneDescriptionList]) (contracts.ValidationResult, error) {
|
||||
if shape.Validate(req.Value) != nil || !sourceRefsValid(req.Source, req.Value) {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
issues := issuesFor(req.Source, req.Value)
|
||||
if len(issues) == 0 {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
return contracts.ValidationResult{
|
||||
Approved: false, ReasonCode: ReasonCode,
|
||||
Message: diagnostics.Aggregate("invalid scene description normalization", issues),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func sourceRefsValid(doc *source.SourceDocument, value dnd.SceneDescriptionList) bool {
|
||||
for _, scene := range value.Scenes {
|
||||
if source.ValidateRef(doc, scene.SourceRef) != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func issuesFor(doc *source.SourceDocument, value dnd.SceneDescriptionList) []string {
|
||||
issues := make([]string, 0)
|
||||
if !sort.SliceIsSorted(value.Scenes, func(left, right int) bool {
|
||||
leftStart, _ := source.UnitIndex(doc, value.Scenes[left].SourceRef.StartUnitID)
|
||||
rightStart, _ := source.UnitIndex(doc, value.Scenes[right].SourceRef.StartUnitID)
|
||||
if leftStart != rightStart {
|
||||
return leftStart < rightStart
|
||||
}
|
||||
return value.Scenes[left].ID < value.Scenes[right].ID
|
||||
}) {
|
||||
issues = append(issues, "scenes are not in canonical source order")
|
||||
}
|
||||
|
||||
byID := make(map[string]int, len(value.Scenes))
|
||||
byRange := make(map[source.SourceRef]int, len(value.Scenes))
|
||||
for index, scene := range value.Scenes {
|
||||
if index > 0 && value.Scenes[index-1] == scene {
|
||||
issues = append(issues, fmt.Sprintf("scenes[%d] duplicates scenes[%d]", index, index-1))
|
||||
}
|
||||
if previous, ok := byID[scene.ID]; ok && value.Scenes[previous] != scene {
|
||||
issues = append(issues, fmt.Sprintf("scenes[%d] conflicts with scenes[%d] for scene ID", index, previous))
|
||||
} else if !ok {
|
||||
byID[scene.ID] = index
|
||||
}
|
||||
if previous, ok := byRange[scene.SourceRef]; ok && !sameModelContent(value.Scenes[previous], scene) {
|
||||
issues = append(issues, fmt.Sprintf("scenes[%d] conflicts with scenes[%d] for source range", index, previous))
|
||||
} else if !ok {
|
||||
byRange[scene.SourceRef] = index
|
||||
}
|
||||
}
|
||||
return issues
|
||||
}
|
||||
|
||||
func sameModelContent(left, right dnd.SceneDescription) bool {
|
||||
return left.Kind == right.Kind && left.Title == right.Title && left.Summary == right.Summary
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
return pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic}
|
||||
}
|
||||
|
||||
func Register(registry *pipeline.ValidatorRegistry) error {
|
||||
return pipeline.RegisterTypedValidatorBuilder(registry, dnd.SceneDescriptionListKind, Spec(), validateOptions, func(request pipeline.BuildRequest) (contracts.TypedValidator[dnd.SceneDescriptionList], error) {
|
||||
options, err := DecodeOptions(request.Options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return New(options), nil
|
||||
})
|
||||
}
|
||||
|
||||
func DecodeOptions(options map[string]any) (Options, error) {
|
||||
if err := pipeline.RejectUnknownOptions(options); err != nil {
|
||||
return Options{}, err
|
||||
}
|
||||
return Options{}, nil
|
||||
}
|
||||
|
||||
func validateOptions(options map[string]any) error { _, err := DecodeOptions(options); return err }
|
||||
@@ -0,0 +1,69 @@
|
||||
package invariants
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestValidatorEnforcesOrderDuplicatesAndConflicts(t *testing.T) {
|
||||
doc := document()
|
||||
first := scene("first", 1, dnd.SceneKindNarrative, "Arrival", "The party arrives.")
|
||||
second := scene("second", 2, dnd.SceneKindCombat, "Ambush", "Bandits attack.")
|
||||
validator := New(Options{})
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
value dnd.SceneDescriptionList
|
||||
approved bool
|
||||
}{
|
||||
{name: "normalized", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{first, second}}, approved: true},
|
||||
{name: "out of order", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{second, first}}},
|
||||
{name: "exact duplicate", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{first, first}}},
|
||||
{name: "same ID conflict", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{first, scene("first", 2, dnd.SceneKindCombat, "Ambush", "Bandits attack.")}}},
|
||||
{name: "same range conflict", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{first, scene("other", 1, dnd.SceneKindMeta, "Rules", "The table checks rules.")}}},
|
||||
{name: "same range same content different ID", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{scene("a", 1, dnd.SceneKindNarrative, "Arrival", "The party arrives."), scene("b", 1, dnd.SceneKindNarrative, "Arrival", "The party arrives.")}}, approved: true},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
result, err := validator.Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: test.value})
|
||||
if err != nil || result.Approved != test.approved {
|
||||
t.Fatalf("Validate() = %#v, %v; want approved=%t", result, err, test.approved)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorDefersOwnedFailuresAndRegistersPolicy(t *testing.T) {
|
||||
doc := document()
|
||||
result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: dnd.SceneDescriptionList{}})
|
||||
if err != nil || !result.Approved {
|
||||
t.Fatalf("Validate() = %#v, %v; want shape deferral", result, err)
|
||||
}
|
||||
invalidRef := dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{scene("one", 9, dnd.SceneKindNarrative, "Arrival", "The party arrives.")}}
|
||||
result, err = New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: invalidRef})
|
||||
if err != nil || !result.Approved {
|
||||
t.Fatalf("Validate() = %#v, %v; want source-reference deferral", result, err)
|
||||
}
|
||||
registry := pipeline.NewValidatorRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := registry.Spec(Key); !ok {
|
||||
t.Fatalf("registry missing %q", Key)
|
||||
}
|
||||
if got, want := New(Options{}).CheckpointFingerprints(), []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("fingerprints = %#v, want %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func document() *source.SourceDocument {
|
||||
return &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 1, Text: "The party arrives."}, {ID: 2, Text: "Bandits attack."}}}
|
||||
}
|
||||
|
||||
func scene(id string, unit int, kind dnd.SceneKind, title, summary string) dnd.SceneDescription {
|
||||
return dnd.SceneDescription{ID: id, SourceRef: source.SourceRef{SourceID: "session", StartUnitID: unit, EndUnitID: unit}, Kind: kind, Title: title, Summary: summary}
|
||||
}
|
||||
Reference in New Issue
Block a user