Debian Codegraph @

pkg/services/push/mqtt/mqtt_errors_test.go

raw
1 package mqtt
2
3 import (
4 ginkgo "github.com/onsi/ginkgo/v2"
5 gomega "github.com/onsi/gomega"
6 )
7
8 var _ = ginkgo.Describe("MQTT Errors", func() {
9 ginkgo.Describe("PublishError", func() {
10 ginkgo.Context("Error method", func() {
11 ginkgo.It("should format error with reason code only", func() {
12 err := PublishError{
13 ReasonCode: 0x80,
14 ReasonString: "",
15 }
16 gomega.Expect(err.Error()).To(gomega.Equal("MQTT publish failed: reason code 0x80"))
17 })
18
19 ginkgo.It("should format error with reason code and string", func() {
20 err := PublishError{
21 ReasonCode: 0x81,
22 ReasonString: "Client identifier not valid",
23 }
24 gomega.Expect(err.Error()).
25 To(gomega.Equal("MQTT publish failed: reason code 0x81 - Client identifier not valid"))
26 })
27
28 ginkgo.It("should format success code", func() {
29 err := PublishError{
30 ReasonCode: 0x00,
31 ReasonString: "Success",
32 }
33 gomega.Expect(err.Error()).
34 To(gomega.Equal("MQTT publish failed: reason code 0x00 - Success"))
35 })
36
37 ginkgo.It("should format maximum reason code", func() {
38 err := PublishError{
39 ReasonCode: 0xFF,
40 ReasonString: "",
41 }
42 gomega.Expect(err.Error()).To(gomega.Equal("MQTT publish failed: reason code 0xFF"))
43 })
44
45 ginkgo.It("should format with single digit hex code", func() {
46 err := PublishError{
47 ReasonCode: 0x01,
48 ReasonString: "",
49 }
50 gomega.Expect(err.Error()).To(gomega.Equal("MQTT publish failed: reason code 0x01"))
51 })
52
53 ginkgo.It("should format with reason string containing special characters", func() {
54 err := PublishError{
55 ReasonCode: 0x8C,
56 ReasonString: "Topic Name invalid: /test/# (wildcards not allowed)",
57 }
58 gomega.Expect(err.Error()).
59 To(gomega.Equal("MQTT publish failed: reason code 0x8C - Topic Name invalid: /test/# (wildcards not allowed)"))
60 })
61
62 ginkgo.It("should format with empty reason string as code only", func() {
63 err := PublishError{
64 ReasonCode: 0x10,
65 ReasonString: "",
66 }
67 gomega.Expect(err.Error()).To(gomega.Equal("MQTT publish failed: reason code 0x10"))
68 })
69
70 ginkgo.It("should format with whitespace-only reason string", func() {
71 err := PublishError{
72 ReasonCode: 0x20,
73 ReasonString: " ",
74 }
75 gomega.Expect(err.Error()).
76 To(gomega.Equal("MQTT publish failed: reason code 0x20 - "))
77 })
78 })
79 })
80
81 ginkgo.Describe("IsFailureCode", func() {
82 ginkgo.It("should return false for success code 0x00", func() {
83 gomega.Expect(IsFailureCode(0x00)).To(gomega.BeFalse())
84 })
85
86 ginkgo.It("should return false for warning codes below 0x80", func() {
87 testCases := []struct {
88 code byte
89 expected bool
90 }{
91 {0x00, false}, // Success
92 {0x01, false}, // Granted QoS 1
93 {0x02, false}, // Granted QoS 2
94 {0x10, false}, // Normal disconnect
95 {0x7F, false}, // Highest non-failure code
96 }
97
98 for _, tc := range testCases {
99 gomega.Expect(IsFailureCode(tc.code)).To(gomega.Equal(tc.expected),
100 "IsFailureCode(0x%02X) should return %v", tc.code, tc.expected)
101 }
102 })
103
104 ginkgo.It("should return true for failure codes 0x80 and above", func() {
105 testCases := []struct {
106 code byte
107 expected bool
108 }{
109 {0x80, true}, // Unspecified error
110 {0x81, true}, // Malformed packet
111 {0x82, true}, // Protocol error
112 {0x87, true}, // Not authorized
113 {0x8C, true}, // Topic Name invalid
114 {0x90, true}, // Topic filter invalid
115 {0xFF, true}, // Highest failure code
116 }
117
118 for _, tc := range testCases {
119 gomega.Expect(IsFailureCode(tc.code)).To(gomega.Equal(tc.expected),
120 "IsFailureCode(0x%02X) should return %v", tc.code, tc.expected)
121 }
122 })
123
124 ginkgo.It("should return false for boundary code 0x7F", func() {
125 gomega.Expect(IsFailureCode(0x7F)).To(gomega.BeFalse())
126 })
127
128 ginkgo.It("should return true for boundary code 0x80", func() {
129 gomega.Expect(IsFailureCode(0x80)).To(gomega.BeTrue())
130 })
131 })
132
133 ginkgo.Describe("Sentinel Errors", func() {
134 ginkgo.It("should match ErrPublishTimeout", func() {
135 gomega.Expect(ErrPublishTimeout.Error()).To(gomega.Equal("publish timeout exceeded"))
136 })
137
138 ginkgo.It("should match ErrConnectionNotInitialized", func() {
139 gomega.Expect(ErrConnectionNotInitialized.Error()).
140 To(gomega.Equal("MQTT connection manager not initialized"))
141 })
142
143 ginkgo.It("should be usable with errors.Is for ErrPublishTimeout", func() {
144 err := ErrPublishTimeout
145 gomega.Expect(err).To(gomega.Equal(ErrPublishTimeout))
146 })
147
148 ginkgo.It("should be usable with errors.Is for ErrConnectionNotInitialized", func() {
149 err := ErrConnectionNotInitialized
150 gomega.Expect(err).To(gomega.Equal(ErrConnectionNotInitialized))
151 })
152
153 ginkgo.It("should have distinct sentinel errors", func() {
154 gomega.Expect(ErrPublishTimeout).ToNot(gomega.Equal(ErrConnectionNotInitialized))
155 })
156 })
157 })