130 lines
6.2 KiB
Go
130 lines
6.2 KiB
Go
package invariants
|
|
|
|
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"
|
|
normalizeitemevents "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/normalize/itemevents"
|
|
)
|
|
|
|
func TestValidatorApprovesNormalizerOutput(t *testing.T) {
|
|
doc := invariantDocument()
|
|
input := dnd.ItemEventList{Events: []dnd.ItemEvent{
|
|
{Name: " Coin ", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 20, EndUnitID: 20}}},
|
|
{Name: "Arrow", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}}},
|
|
}}
|
|
normalized, err := normalizeitemevents.New(normalizeitemevents.Options{}).Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.ItemEventList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.ItemEventList]{Value: input}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.ItemEventList]{Source: doc, Value: normalized.Value})
|
|
if err != nil || !result.Approved {
|
|
t.Fatalf("Validate() = %#v, %v; want approval", result, err)
|
|
}
|
|
}
|
|
|
|
func TestValidateRejectsOwnedNormalizedInvariants(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
mutate func(*dnd.ItemEventList)
|
|
want string
|
|
}{
|
|
{name: "name whitespace", mutate: func(value *dnd.ItemEventList) { value.Events[0].Name = " Coin " }, want: ".name is not display-normalized"},
|
|
{name: "from whitespace", mutate: func(value *dnd.ItemEventList) {
|
|
value.Events[0].Kind = dnd.ItemEventKindLost
|
|
value.Events[0].From = " Aria "
|
|
}, want: ".from is not display-normalized"},
|
|
{name: "to whitespace", mutate: func(value *dnd.ItemEventList) {
|
|
value.Events[0].Kind = dnd.ItemEventKindAcquired
|
|
value.Events[0].To = " Aria "
|
|
}, want: ".to is not display-normalized"},
|
|
{name: "reference order", mutate: func(value *dnd.ItemEventList) {
|
|
value.Events[0].SourceRefs = []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}, {SourceID: "session", StartUnitID: 10, EndUnitID: 10}}
|
|
}, want: "not in canonical order"},
|
|
{name: "duplicate reference", mutate: func(value *dnd.ItemEventList) {
|
|
value.Events[0].SourceRefs = append(value.Events[0].SourceRefs, value.Events[0].SourceRefs[0])
|
|
}, want: "duplicates the previous reference"},
|
|
{name: "list order", mutate: func(value *dnd.ItemEventList) { value.Events = []dnd.ItemEvent{value.Events[1], value.Events[0]} }, want: "out of canonical order"},
|
|
{name: "duplicate event", mutate: func(value *dnd.ItemEventList) { value.Events = append(value.Events, value.Events[0]) }, want: "duplicates item event"},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
value := normalizedList()
|
|
test.mutate(&value)
|
|
err := Validate(invariantDocument(), value)
|
|
if err == nil || !strings.Contains(err.Error(), test.want) {
|
|
t.Fatalf("Validate() error = %v, want %q", err, test.want)
|
|
}
|
|
result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.ItemEventList]{Source: invariantDocument(), Value: value})
|
|
if err != nil || result.Approved || result.ReasonCode != ReasonCode {
|
|
t.Fatalf("validator result = %#v, %v", result, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateUsesSourceDocumentOrderAndDefersEarlierFailures(t *testing.T) {
|
|
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 30}, {ID: 10}}}
|
|
value := dnd.ItemEventList{Events: []dnd.ItemEvent{{
|
|
Name: "Coin", Kind: dnd.ItemEventKindDiscovered,
|
|
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 rejection", err)
|
|
}
|
|
shapeInvalid := normalizedList()
|
|
shapeInvalid.Events[0].Name = " "
|
|
if err := Validate(invariantDocument(), shapeInvalid); err != nil {
|
|
t.Fatalf("shape failure must be deferred, got %v", err)
|
|
}
|
|
sourceInvalid := normalizedList()
|
|
sourceInvalid.Events[0].SourceRefs[0].StartUnitID = 999
|
|
if err := Validate(invariantDocument(), sourceInvalid); err != nil {
|
|
t.Fatalf("source-reference failure must be deferred, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidatorContractAndBoundedDiagnostics(t *testing.T) {
|
|
value := normalizedList()
|
|
value.Events = make([]dnd.ItemEvent, 30)
|
|
for index := range value.Events {
|
|
value.Events[index] = normalizedList().Events[0]
|
|
value.Events[index].Name = strings.Repeat("火", 300)
|
|
}
|
|
result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.ItemEventList]{Source: invariantDocument(), Value: value})
|
|
if err != nil || result.Approved || !utf8.ValidString(result.Message) || len([]byte(result.Message)) > 4096 || !strings.Contains(result.Message, "additional issue(s) omitted") {
|
|
t.Fatalf("bounded result = %#v, %v", result, err)
|
|
}
|
|
if got := New(Options{}).CheckpointFingerprints(); !reflect.DeepEqual(got, []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}) {
|
|
t.Fatalf("CheckpointFingerprints() = %#v", got)
|
|
}
|
|
if spec := Spec(); spec.Key != Key || spec.ExecutionClass != contracts.ExecutionClassDeterministic {
|
|
t.Fatalf("Spec() = %#v", spec)
|
|
}
|
|
registry := pipeline.NewValidatorRegistry()
|
|
if err := Register(registry); err != nil {
|
|
t.Fatalf("Register() error = %v", err)
|
|
}
|
|
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
|
|
t.Fatal("DecodeOptions() accepted unknown option")
|
|
}
|
|
}
|
|
|
|
func normalizedList() dnd.ItemEventList {
|
|
return dnd.ItemEventList{Events: []dnd.ItemEvent{
|
|
{Name: "Arrow", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 10}}},
|
|
{Name: "Coin", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}}},
|
|
}}
|
|
}
|
|
|
|
func invariantDocument() *source.SourceDocument {
|
|
return &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 10}, {ID: 20}}}
|
|
}
|