package connectors import ( "encoding/json/jsontext" "strings" "testing" ) // TestCoreDeliveriesGuardWhatCoreSends guards the load-time check of a // connector's declared message and chat_message schemas: a declaration that // rejects a payload core sends is refused, so the mismatch surfaces when the // connector loads rather than when a delivery is mid-flight. A connector that // declares neither delivery, or declares one that accepts core's payloads, is // fine. func TestCoreDeliveriesGuardWhatCoreSends(t *testing.T) { accepting := jsontext.Value(`{"type":"object","properties":{"title":{"type":"string"},"body":{"type":"string"},"approval_request_id":{"type":"string"}},"required":["title"]}`) tests := []struct { name string deliveries []Capability want string }{ { name: "a message schema that accepts core's payloads passes", deliveries: []Capability{ {Name: DeliveryMessage, Schema: accepting}, {Name: DeliveryChatMessage, Schema: jsontext.Value(`{"type":"object","properties":{"chat_id":{"type":"string"},"text":{"type":"string"}},"required":["chat_id","text"]}`)}, }, }, { name: "a message schema requiring a field core never sends is refused", deliveries: []Capability{{Name: DeliveryMessage, Schema: jsontext.Value(`{"type":"object","required":["to"]}`)}}, want: `delivery "message" doesn't accept what core sends`, }, { name: "a message schema with a wrong field type is refused", deliveries: []Capability{{Name: DeliveryMessage, Schema: jsontext.Value(`{"type":"object","properties":{"title":{"type":"integer"}}}`)}}, want: `delivery "message" doesn't accept what core sends`, }, { name: "a chat_message schema that rejects core's payload is refused", deliveries: []Capability{{Name: DeliveryChatMessage, Schema: jsontext.Value(`{"type":"object","properties":{"chat_id":{"type":"string"},"text":{"type":"integer"}},"required":["chat_id","text"]}`)}}, want: `delivery "chat_message" doesn't accept what core sends`, }, { name: "an empty schema accepts anything", deliveries: []Capability{{Name: DeliveryMessage, Schema: jsontext.Value(`{}`)}}, }, { name: "a connector that declares neither core delivery is not checked", deliveries: []Capability{{Name: "webhook", Schema: jsontext.Value(`{"type":"object","required":["url"]}`)}}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := checkCoreDeliveries(Capabilities{Deliveries: tt.deliveries}) switch { case tt.want == "" && err != nil: t.Fatalf("checkCoreDeliveries: %v", err) case tt.want != "" && (err == nil || !strings.Contains(err.Error(), tt.want)): t.Fatalf("checkCoreDeliveries error = %v, want %q", err, tt.want) } }) } }