Files

45 lines
1.3 KiB
Go

package natsrpc
import (
"encoding/json"
"errors"
"fmt"
"testing"
"github.com/samcharles93/archie-core/internal/forge"
)
// TestEnvelopeKeepsSentinel guards that a forge sentinel survives the NATS
// envelope: a caller that matches it with errors.Is on the receiving side must
// get the same answer as on the sending side, with the sender's text kept.
func TestEnvelopeKeepsSentinel(t *testing.T) {
codes := map[string]error{"blocked_by_dependencies": forge.ErrBlockedByDependencies}
tests := []struct {
name string
err error
want bool
}{
{name: "sentinel wrapped", err: fmt.Errorf("close issue: %w", forge.ErrBlockedByDependencies), want: true},
{name: "plain error", err: errors.New("forge unreachable"), want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(NewEnvelopeWith(tt.err, codes))
if err != nil {
t.Fatalf("marshal envelope: %v", err)
}
var env Envelope
if err := json.Unmarshal(data, &env); err != nil {
t.Fatalf("unmarshal envelope: %v", err)
}
got := env.ErrWith(codes)
if errors.Is(got, forge.ErrBlockedByDependencies) != tt.want {
t.Errorf("errors.Is(%v, sentinel) = %t, want %t", got, !tt.want, tt.want)
}
if got.Error() != tt.err.Error() {
t.Errorf("text = %q, want %q", got.Error(), tt.err.Error())
}
})
}
}