@@ -55,3 +55,182 @@ impl From<reqwest::Error> for WorkerError {
5555 }
5656 }
5757}
58+
59+ #[ cfg( test) ]
60+ mod tests {
61+ use super :: * ;
62+ use std:: error:: Error ;
63+
64+ #[ test]
65+ fn test_health_check_failed_display ( ) {
66+ let error = WorkerError :: HealthCheckFailed {
67+ url : "http://worker1:8080" . to_string ( ) ,
68+ reason : "Connection refused" . to_string ( ) ,
69+ } ;
70+ assert_eq ! (
71+ error. to_string( ) ,
72+ "Health check failed for worker http://worker1:8080: Connection refused"
73+ ) ;
74+ }
75+
76+ #[ test]
77+ fn test_worker_not_found_display ( ) {
78+ let error = WorkerError :: WorkerNotFound {
79+ url : "http://worker2:8080" . to_string ( ) ,
80+ } ;
81+ assert_eq ! ( error. to_string( ) , "Worker not found: http://worker2:8080" ) ;
82+ }
83+
84+ #[ test]
85+ fn test_invalid_configuration_display ( ) {
86+ let error = WorkerError :: InvalidConfiguration {
87+ message : "Missing port number" . to_string ( ) ,
88+ } ;
89+ assert_eq ! (
90+ error. to_string( ) ,
91+ "Invalid worker configuration: Missing port number"
92+ ) ;
93+ }
94+
95+ #[ test]
96+ fn test_network_error_display ( ) {
97+ let error = WorkerError :: NetworkError {
98+ url : "http://worker3:8080" . to_string ( ) ,
99+ error : "Timeout after 30s" . to_string ( ) ,
100+ } ;
101+ assert_eq ! (
102+ error. to_string( ) ,
103+ "Network error for worker http://worker3:8080: Timeout after 30s"
104+ ) ;
105+ }
106+
107+ #[ test]
108+ fn test_worker_at_capacity_display ( ) {
109+ let error = WorkerError :: WorkerAtCapacity {
110+ url : "http://worker4:8080" . to_string ( ) ,
111+ } ;
112+ assert_eq ! ( error. to_string( ) , "Worker at capacity: http://worker4:8080" ) ;
113+ }
114+
115+ #[ test]
116+ fn test_worker_error_implements_std_error ( ) {
117+ let error = WorkerError :: WorkerNotFound {
118+ url : "http://test" . to_string ( ) ,
119+ } ;
120+ // Verify it implements Error trait
121+ let _: & dyn Error = & error;
122+ assert ! ( error. source( ) . is_none( ) ) ;
123+ }
124+
125+ #[ test]
126+ fn test_error_send_sync ( ) {
127+ fn assert_send_sync < T : Send + Sync > ( ) { }
128+ assert_send_sync :: < WorkerError > ( ) ;
129+ }
130+
131+ #[ test]
132+ fn test_worker_result_type_alias ( ) {
133+ // Test Ok variant
134+ let result: WorkerResult < i32 > = Ok ( 42 ) ;
135+ assert ! ( result. is_ok( ) ) ;
136+ assert_eq ! ( result. unwrap( ) , 42 ) ;
137+
138+ // Test Err variant
139+ let error = WorkerError :: WorkerNotFound {
140+ url : "test" . to_string ( ) ,
141+ } ;
142+ let result: WorkerResult < i32 > = Err ( error) ;
143+ assert ! ( result. is_err( ) ) ;
144+ }
145+
146+ #[ test]
147+ fn test_empty_url_handling ( ) {
148+ // Test empty URLs in error variants
149+ let error1 = WorkerError :: HealthCheckFailed {
150+ url : "" . to_string ( ) ,
151+ reason : "No connection" . to_string ( ) ,
152+ } ;
153+ assert_eq ! (
154+ error1. to_string( ) ,
155+ "Health check failed for worker : No connection"
156+ ) ;
157+
158+ let error2 = WorkerError :: NetworkError {
159+ url : "" . to_string ( ) ,
160+ error : "DNS failure" . to_string ( ) ,
161+ } ;
162+ assert_eq ! ( error2. to_string( ) , "Network error for worker : DNS failure" ) ;
163+
164+ let error3 = WorkerError :: WorkerNotFound {
165+ url : "" . to_string ( ) ,
166+ } ;
167+ assert_eq ! ( error3. to_string( ) , "Worker not found: " ) ;
168+ }
169+
170+ #[ test]
171+ fn test_special_characters_in_messages ( ) {
172+ // Test with special characters
173+ let error = WorkerError :: InvalidConfiguration {
174+ message : "Invalid JSON: {\" error\" : \" test\" }" . to_string ( ) ,
175+ } ;
176+ assert_eq ! (
177+ error. to_string( ) ,
178+ "Invalid worker configuration: Invalid JSON: {\" error\" : \" test\" }"
179+ ) ;
180+
181+ // Test with unicode
182+ let error2 = WorkerError :: HealthCheckFailed {
183+ url : "http://测试:8080" . to_string ( ) ,
184+ reason : "连接被拒绝" . to_string ( ) ,
185+ } ;
186+ assert_eq ! (
187+ error2. to_string( ) ,
188+ "Health check failed for worker http://测试:8080: 连接被拒绝"
189+ ) ;
190+ }
191+
192+ #[ test]
193+ fn test_very_long_error_messages ( ) {
194+ let long_message = "A" . repeat ( 10000 ) ;
195+ let error = WorkerError :: InvalidConfiguration {
196+ message : long_message. clone ( ) ,
197+ } ;
198+ let display = error. to_string ( ) ;
199+ assert ! ( display. contains( & long_message) ) ;
200+ assert_eq ! (
201+ display. len( ) ,
202+ "Invalid worker configuration: " . len( ) + long_message. len( )
203+ ) ;
204+ }
205+
206+ // Mock reqwest error for testing conversion
207+ #[ test]
208+ fn test_reqwest_error_conversion ( ) {
209+ // Test that NetworkError is the correct variant
210+ let network_error = WorkerError :: NetworkError {
211+ url : "http://example.com" . to_string ( ) ,
212+ error : "connection timeout" . to_string ( ) ,
213+ } ;
214+
215+ match network_error {
216+ WorkerError :: NetworkError { url, error } => {
217+ assert_eq ! ( url, "http://example.com" ) ;
218+ assert_eq ! ( error, "connection timeout" ) ;
219+ }
220+ _ => panic ! ( "Expected NetworkError variant" ) ,
221+ }
222+ }
223+
224+ #[ test]
225+ fn test_error_equality ( ) {
226+ // WorkerError doesn't implement PartialEq, but we can test that
227+ // the same error construction produces the same display output
228+ let error1 = WorkerError :: WorkerNotFound {
229+ url : "http://test" . to_string ( ) ,
230+ } ;
231+ let error2 = WorkerError :: WorkerNotFound {
232+ url : "http://test" . to_string ( ) ,
233+ } ;
234+ assert_eq ! ( error1. to_string( ) , error2. to_string( ) ) ;
235+ }
236+ }
0 commit comments