When a connector loads with new declarations, the saved workflows that reference it are re-checked against them (via a Recheck hook wired to runs.Engine.RecheckConnector); if any would become invalid, the new declarations are refused and the connector fails to start, so the breakage surfaces at update time instead of mid-run. The previous declarations stay in force. A connector that declares the message or chat_message delivery now has its declared input schema checked against every payload shape core sends on it when it loads; a rejecting schema fails the connector. The delivery names are exported from the connectors module and used by core's senders, so the contract has one definition.
65 lines
2.7 KiB
Go
65 lines
2.7 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|