package events import ( "reflect" "testing" "github.com/samcharles93/archie-core/modules/events/internal/mapping" "github.com/samcharles93/archie-core/modules/events/internal/store" ) // TestMappingMismatchIsAFailureWithItsReason guards the dispatch contract: an // event that does not match a trigger's mapping starts no run and records a // failure naming each failed field and the value found there, so the operator // can see why the workflow didn't start. An event that matches still produces // its run input. func TestMappingMismatchIsAFailureWithItsReason(t *testing.T) { row := store.Mapping{ Name: "incident", Fields: []byte(`[ {"name":"id","path":"incident.id","type":"string","required":true}, {"name":"severity","path":"incident.severity","type":"number","required":true} ]`), } tests := []struct { name string body string wantFailures []mapping.Failure }{ {name: "a matching event dispatches", body: `{"incident":{"id":"a1","severity":3}}`}, { name: "a missing field fails", body: `{"incident":{"id":"a1"}}`, wantFailures: []mapping.Failure{ {FieldName: "severity", Path: "incident.severity", Reason: mapping.ReasonMissing}, }, }, { name: "every failed field is listed", body: `{"incident":{"id":null,"severity":"high"}}`, wantFailures: []mapping.Failure{ {FieldName: "id", Path: "incident.id", Reason: mapping.ReasonNull}, {FieldName: "severity", Path: "incident.severity", Reason: mapping.ReasonTypeMismatch, Value: "high"}, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { input, mapped, failure, err := applyMapping(store.Event{Body: []byte(tt.body)}, row) if err != nil { t.Fatalf("applyMapping: %v", err) } if tt.wantFailures == nil { if failure != nil || !mapped || len(input) == 0 { t.Fatalf("applyMapping = (%s, %v, %+v), want a run input", input, mapped, failure) } return } if failure == nil { t.Fatalf("applyMapping recorded no failure, want %v", tt.wantFailures) } if failure.outcome != store.OutcomeFailed || failure.Code != reasonMappingMismatch || failure.Mapping != "incident" { t.Errorf("failure = %+v, want outcome %q, code %q for mapping incident", failure, store.OutcomeFailed, reasonMappingMismatch) } if !reflect.DeepEqual(failure.Failures, tt.wantFailures) { t.Fatalf("failures = %+v, want %+v", failure.Failures, tt.wantFailures) } if mapped || input != nil { t.Errorf("a failure must carry no run input, got %s", input) } }) } }