package events import ( "testing" "github.com/samcharles93/archie-core/modules/events/internal/mapping" "github.com/samcharles93/archie-core/modules/events/internal/store" ) // TestMappingMismatchIsASkipWithItsReason guards the dispatch contract: an // event that does not match a trigger's mapping starts no run and yields a skip // naming each failed field, so the operator can see why the workflow didn't // start. An event that matches still produces its run input. func TestMappingMismatchIsASkipWithItsReason(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 is skipped", 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}, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { input, mapped, skip, err := applyMapping(store.Event{Body: []byte(tt.body)}, row) if err != nil { t.Fatalf("applyMapping: %v", err) } if tt.wantFailures == nil { if skip != nil || !mapped || len(input) == 0 { t.Fatalf("applyMapping = (%s, %v, %+v), want a run input", input, mapped, skip) } return } if skip == nil { t.Fatalf("applyMapping recorded no skip, want %v", tt.wantFailures) } if skip.Code != skipMappingMismatch || skip.Mapping != "incident" { t.Errorf("skip = %+v, want code %q for mapping incident", skip, skipMappingMismatch) } if len(skip.Failures) != len(tt.wantFailures) { t.Fatalf("failures = %+v, want %+v", skip.Failures, tt.wantFailures) } for i, f := range skip.Failures { if f != tt.wantFailures[i] { t.Errorf("failure %d = %+v, want %+v", i, f, tt.wantFailures[i]) } } if mapped || input != nil { t.Errorf("a skip must carry no run input, got %s", input) } }) } }