Add NPC normalization and identity validation
This commit is contained in:
84
internal/modules/dnd/validate/npcs/identity/validator.go
Normal file
84
internal/modules/dnd/validate/npcs/identity/validator.go
Normal file
@@ -0,0 +1,84 @@
|
||||
// Package identity validates the stable identity invariants of D&D NPC lists.
|
||||
package identity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
domainidentity "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/npcs/diagnostics"
|
||||
npcshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/npcs/shape"
|
||||
)
|
||||
|
||||
const (
|
||||
Key = "normalize/dnd/npcs/identity"
|
||||
ReasonCode = "invalid_npc_identity"
|
||||
policy = domainidentity.Policy
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.NPCList] = (*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.NPCList]) (contracts.ValidationResult, error) {
|
||||
if err := npcshape.Validate(req.Value); err != nil {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
identityIssues := domainidentity.ValidateRegistry(req.Value.NPCs)
|
||||
if len(identityIssues) == 0 {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
|
||||
issues := make([]string, len(identityIssues))
|
||||
for index, issue := range identityIssues {
|
||||
location := fmt.Sprintf("npcs[%d]", issue.RecordIndex)
|
||||
if issue.AliasIndex >= 0 {
|
||||
location += fmt.Sprintf(".aliases[%d]", issue.AliasIndex)
|
||||
}
|
||||
issues[index] = fmt.Sprintf("%s %s: %s", location, issue.Code, diagnostics.Quote(issue.Value))
|
||||
}
|
||||
return contracts.ValidationResult{
|
||||
Approved: false,
|
||||
ReasonCode: ReasonCode,
|
||||
Message: diagnostics.Aggregate("invalid NPC identity", issues),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
return pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic}
|
||||
}
|
||||
|
||||
func Register(registry *pipeline.ValidatorRegistry) error {
|
||||
return pipeline.RegisterTypedValidatorBuilder(registry, dnd.NPCListKind, Spec(), validateOptions, func(request pipeline.BuildRequest) (contracts.TypedValidator[dnd.NPCList], 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 }
|
||||
107
internal/modules/dnd/validate/npcs/identity/validator_test.go
Normal file
107
internal/modules/dnd/validate/npcs/identity/validator_test.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
|
||||
"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 TestValidatorContractAndRegistration(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)
|
||||
}
|
||||
if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Name != "policy" || got[0].Value != policy {
|
||||
t.Fatalf("fingerprints = %#v, want identity policy", got)
|
||||
}
|
||||
|
||||
want := pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic}
|
||||
if got := Spec(); !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("Spec() = %#v, want %#v", got, want)
|
||||
}
|
||||
registry := pipeline.NewValidatorRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
if registered, ok := registry.Spec(Key); !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(), "validator registry") {
|
||||
t.Fatalf("Register(nil) error = %v, want registry error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorDefersShapeAndRejectsIdentityIssues(t *testing.T) {
|
||||
validator := New(Options{})
|
||||
shapeInvalid := dnd.NPCList{NPCs: []dnd.NPC{{Name: "missing description"}}}
|
||||
result, err := validator.Validate(context.Background(), validationRequest(shapeInvalid))
|
||||
if err != nil || !result.Approved {
|
||||
t.Fatalf("shape-invalid result = %#v, error = %v, want deferred approval", result, err)
|
||||
}
|
||||
|
||||
value := validNPCList(2)
|
||||
value.NPCs[0].ID = "not-an-id"
|
||||
value.NPCs[1].Aliases = []string{"Shared Alias"}
|
||||
value.NPCs[0].Aliases = []string{"Shared Alias"}
|
||||
result, err = validator.Validate(context.Background(), validationRequest(value))
|
||||
if err != nil || result.Approved || result.ReasonCode != ReasonCode {
|
||||
t.Fatalf("identity result = %#v, error = %v, want rejection", result, err)
|
||||
}
|
||||
for _, want := range []string{"invalid_id", "alias_owned_by_multiple_records", "npcs[0]", "npcs[1]"} {
|
||||
if !strings.Contains(result.Message, want) {
|
||||
t.Fatalf("identity message %q missing %q", result.Message, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorBoundsUnicodeDiagnostics(t *testing.T) {
|
||||
value := validNPCList(30)
|
||||
for index := range value.NPCs {
|
||||
value.NPCs[index].ID = fmt.Sprintf("bad-%d", index)
|
||||
value.NPCs[index].Name = strings.Repeat("火", 140) + fmt.Sprintf("-%d", index)
|
||||
}
|
||||
result, err := New(Options{}).Validate(context.Background(), validationRequest(value))
|
||||
if err != nil || result.Approved {
|
||||
t.Fatalf("bounded result = %#v, error = %v, want rejection", result, err)
|
||||
}
|
||||
if !utf8.ValidString(result.Message) || len([]rune(result.Message)) == 0 || len([]byte(result.Message)) > 4096 {
|
||||
t.Fatalf("bounded message has invalid size/encoding: bytes=%d message=%q", len([]byte(result.Message)), result.Message)
|
||||
}
|
||||
if !strings.Contains(result.Message, "additional issue(s) omitted") || strings.Contains(result.Message, strings.Repeat("火", 140)) {
|
||||
t.Fatalf("bounded message = %q, want omission and truncation", result.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func validationRequest(value dnd.NPCList) contracts.TypedValidationRequest[dnd.NPCList] {
|
||||
return contracts.TypedValidationRequest[dnd.NPCList]{Value: value}
|
||||
}
|
||||
|
||||
func validNPCList(count int) dnd.NPCList {
|
||||
value := dnd.NPCList{NPCs: make([]dnd.NPC, count)}
|
||||
for index := range value.NPCs {
|
||||
name := fmt.Sprintf("NPC %d", index)
|
||||
value.NPCs[index] = dnd.NPC{
|
||||
ID: "npc:sha256:0000000000000000000000000000000000000000000000000000000000000000",
|
||||
Name: name,
|
||||
Aliases: []string{},
|
||||
Description: "description",
|
||||
Relationships: []dnd.NPCRelationship{},
|
||||
SourceRefs: []source.SourceRef{sourceRefForTest()},
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func sourceRefForTest() source.SourceRef {
|
||||
return source.SourceRef{SourceID: "source", StartUnitID: 1, EndUnitID: 1}
|
||||
}
|
||||
Reference in New Issue
Block a user