Files
notarius/internal/modules/dnd/validate/locationoccurrences/shape/validator_test.go

63 lines
2.8 KiB
Go

package shape
import (
"context"
"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 TestValidatorAcceptsEverySupportedKind(t *testing.T) {
kinds := []dnd.LocationOccurrenceKind{dnd.LocationOccurrenceKindVisited, dnd.LocationOccurrenceKindPlanned, dnd.LocationOccurrenceKindRecalled, dnd.LocationOccurrenceKindMentioned}
occurrences := make([]dnd.LocationOccurrence, len(kinds))
for index, kind := range kinds {
occurrences[index] = occurrence(kind)
}
result, err := New(Options{}).Validate(context.Background(), request(dnd.LocationOccurrenceList{Occurrences: occurrences}))
if err != nil || !result.Approved {
t.Fatalf("Validate() = %#v, %v", result, err)
}
}
func TestValidatorRejectsRequiredFieldsAndUnsupportedKindWithoutMutation(t *testing.T) {
value := dnd.LocationOccurrenceList{Occurrences: []dnd.LocationOccurrence{{Name: strings.Repeat("火", 220), Kind: "other", SourceRefs: []source.SourceRef{}}}}
before := value
result, err := New(Options{}).Validate(context.Background(), request(value))
if err != nil || result.Approved || result.ReasonCode != ReasonCode || !strings.Contains(result.Message, "location_id must not be empty") || !strings.Contains(result.Message, "kind is unsupported") {
t.Fatalf("Validate() = %#v, %v", result, err)
}
if !reflect.DeepEqual(value, before) || len(result.Message) > 4096 || !utf8.ValidString(result.Message) {
t.Fatal("validation mutated value or returned unsafe diagnostics")
}
result, err = New(Options{}).Validate(context.Background(), request(dnd.LocationOccurrenceList{}))
if err != nil || result.Approved || !strings.Contains(result.Message, "occurrences must be present") {
t.Fatalf("missing list = %#v, %v", result, err)
}
}
func TestValidatorRegisters(t *testing.T) {
registry := pipeline.NewValidatorRegistry()
if err := Register(registry); err != nil {
t.Fatal(err)
}
if got, ok := registry.Spec(Key); !ok || got != Spec() || got.ExecutionClass != contracts.ExecutionClassDeterministic {
t.Fatalf("registered spec = %#v, ok = %t", got, ok)
}
if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Value != policy {
t.Fatalf("fingerprints = %#v", got)
}
}
func request(value dnd.LocationOccurrenceList) contracts.TypedValidationRequest[dnd.LocationOccurrenceList] {
return contracts.TypedValidationRequest[dnd.LocationOccurrenceList]{Value: value}
}
func occurrence(kind dnd.LocationOccurrenceKind) dnd.LocationOccurrence {
return dnd.LocationOccurrence{LocationID: "location:one", Name: "Moon Gate", Kind: kind, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}}
}