reinhardt-testkit 0.2.0-rc.1

Core testing infrastructure for Reinhardt framework (no functional crate dependencies)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Server function assertion helpers.
//!
//! This module provides assertion utilities for testing server function results,
//! including success/error checking, status code validation, and content verification.
//!
//! # Example
//!
//! ```rust,ignore
//! use reinhardt_testkit::server_fn::assertions::{ServerFnResultAssertions, assert_server_fn_returns};
//!
//! let result = my_server_fn(input, &ctx).await;
//!
//! // Using trait methods
//! result.should_be_ok();
//! result.should_have_value(&expected);
//!
//! // Using standalone functions
//! assert_server_fn_returns(&result, &expected);
//! ```

#![cfg(native)]

use std::fmt::Debug;

use http::StatusCode;

/// Extension trait for asserting server function results.
///
/// This trait provides fluent assertion methods for `Result` types
/// returned by server functions.
pub trait ServerFnResultAssertions<T, E> {
	/// Assert that the result is `Ok` and return the value.
	fn should_be_ok(&self) -> &T;

	/// Assert that the result is `Err` and return the error.
	fn should_be_err(&self) -> &E;

	/// Assert that the result is `Ok` and the value equals the expected value.
	fn should_have_value(&self, expected: &T)
	where
		T: PartialEq + Debug;

	/// Assert that the result is `Ok` and the value satisfies a predicate.
	fn should_satisfy<F>(&self, predicate: F)
	where
		F: FnOnce(&T) -> bool;
}

impl<T: Debug, E: Debug> ServerFnResultAssertions<T, E> for Result<T, E> {
	fn should_be_ok(&self) -> &T {
		match self {
			Ok(value) => value,
			Err(e) => panic!("Expected Ok result, but got Err: {:?}", e),
		}
	}

	fn should_be_err(&self) -> &E {
		match self {
			Ok(value) => panic!("Expected Err result, but got Ok: {:?}", value),
			Err(e) => e,
		}
	}

	fn should_have_value(&self, expected: &T)
	where
		T: PartialEq + Debug,
	{
		let actual = self.should_be_ok();
		assert_eq!(
			actual, expected,
			"Expected value {:?}, but got {:?}",
			expected, actual
		);
	}

	fn should_satisfy<F>(&self, predicate: F)
	where
		F: FnOnce(&T) -> bool,
	{
		let value = self.should_be_ok();
		assert!(
			predicate(value),
			"Value {:?} did not satisfy the predicate",
			value
		);
	}
}

/// Extension trait for error assertions.
pub trait ServerFnErrorAssertions<E> {
	/// Assert that the error message contains the specified text.
	fn should_contain_message(&self, expected: &str)
	where
		E: std::fmt::Display;

	/// Assert that the error message matches exactly.
	fn should_have_message(&self, expected: &str)
	where
		E: std::fmt::Display;
}

impl<E: Debug> ServerFnErrorAssertions<E> for E {
	fn should_contain_message(&self, expected: &str)
	where
		E: std::fmt::Display,
	{
		let message = self.to_string();
		assert!(
			message.contains(expected),
			"Expected error message to contain '{}', but got '{}'",
			expected,
			message
		);
	}

	fn should_have_message(&self, expected: &str)
	where
		E: std::fmt::Display,
	{
		let message = self.to_string();
		assert_eq!(
			message, expected,
			"Expected error message '{}', but got '{}'",
			expected, message
		);
	}
}

/// Assert that a server function returns a specific value.
pub fn assert_server_fn_returns<T, E>(result: &Result<T, E>, expected: &T)
where
	T: PartialEq + Debug,
	E: Debug,
{
	result.should_have_value(expected);
}

/// Assert that a server function returns an error.
pub fn assert_server_fn_error<T, E>(result: &Result<T, E>)
where
	T: Debug,
	E: Debug,
{
	let _ = result.should_be_err();
}

/// Assert that a server function error contains the specified message.
pub fn assert_server_fn_error_contains<T, E>(result: &Result<T, E>, message: &str)
where
	T: Debug,
	E: Debug + std::fmt::Display,
{
	let error = result.should_be_err();
	error.should_contain_message(message);
}

/// Assert that a validation error occurred for a specific field.
///
/// This checks if the error message mentions the specified field name,
/// which is a common pattern for validation errors.
pub fn assert_validation_error<T, E>(result: &Result<T, E>, field: &str)
where
	T: Debug,
	E: Debug + std::fmt::Display,
{
	let error = result.should_be_err();
	let message = error.to_string();
	assert!(
		message.to_lowercase().contains(&field.to_lowercase()),
		"Expected validation error for field '{}', but got: {}",
		field,
		message
	);
}

/// Assert multiple validation errors occurred for the specified fields.
pub fn assert_validation_errors<T, E>(result: &Result<T, E>, fields: &[&str])
where
	T: Debug,
	E: Debug + std::fmt::Display,
{
	let error = result.should_be_err();
	let message = error.to_string().to_lowercase();

	for field in fields {
		assert!(
			message.contains(&field.to_lowercase()),
			"Expected validation error for field '{}', but it was not found in: {}",
			field,
			error
		);
	}
}

/// Response assertion builder for server functions that return HTTP-like responses.
#[derive(Debug)]
pub struct ResponseAssertion<T> {
	value: T,
	status: Option<StatusCode>,
}

impl<T> ResponseAssertion<T> {
	/// Create a new response assertion.
	pub fn new(value: T) -> Self {
		Self {
			value,
			status: None,
		}
	}

	/// Set the expected status code.
	pub fn with_status(mut self, status: StatusCode) -> Self {
		self.status = Some(status);
		self
	}

	/// Get the underlying value.
	pub fn value(&self) -> &T {
		&self.value
	}

	/// Consume and return the value.
	pub fn into_value(self) -> T {
		self.value
	}
}

/// Trait for types that can provide a status code.
pub trait HasStatusCode {
	/// Get the status code.
	fn status_code(&self) -> StatusCode;
}

impl<T: HasStatusCode> ResponseAssertion<T> {
	/// Assert that the status code matches.
	pub fn should_have_status(&self, expected: StatusCode) {
		let actual = self.value.status_code();
		assert_eq!(
			actual, expected,
			"Expected status code {:?}, but got {:?}",
			expected, actual
		);
	}

	/// Assert that the response is successful (2xx).
	pub fn should_be_success(&self) {
		let status = self.value.status_code();
		assert!(
			status.is_success(),
			"Expected successful response, but got {:?}",
			status
		);
	}

	/// Assert that the response is a client error (4xx).
	pub fn should_be_client_error(&self) {
		let status = self.value.status_code();
		assert!(
			status.is_client_error(),
			"Expected client error response, but got {:?}",
			status
		);
	}

	/// Assert that the response is a server error (5xx).
	pub fn should_be_server_error(&self) {
		let status = self.value.status_code();
		assert!(
			status.is_server_error(),
			"Expected server error response, but got {:?}",
			status
		);
	}
}

/// Macro for creating server function test cases.
///
/// This macro provides a convenient way to define multiple test cases
/// for a server function with different inputs and expected outputs.
///
/// # Example
///
/// ```rust,ignore
/// server_fn_test_cases! {
///     my_server_fn,
///     // Test case name, input, expected pattern
///     test_success: (Input { value: 1 }, Ok(_)),
///     test_error: (Input { value: -1 }, Err(_)),
/// }
/// ```
#[macro_export]
macro_rules! server_fn_test_cases {
	($fn_name:path, $($test_name:ident: ($input:expr, $expected:pat)),* $(,)?) => {
		$(
			#[rstest::rstest]
			#[tokio::test]
			async fn $test_name(
				// Fixture injected by rstest; may not be directly used in test body
				#[allow(unused_variables)]
				server_fn_context: $crate::server_fn::ServerFnTestEnv,
			) {
				let result = $fn_name($input, &server_fn_context).await;
				assert!(
					matches!(result, $expected),
					"Expected {:?} to match pattern {}",
					result,
					stringify!($expected)
				);
			}
		)*
	};
}

/// Assertion module for common HTTP status code checks.
pub mod assert_status {
	use super::*;

	/// Assert OK (200) status.
	pub fn ok<T: HasStatusCode>(response: &T) {
		let status = response.status_code();
		assert_eq!(
			status,
			StatusCode::OK,
			"Expected 200 OK, but got {:?}",
			status
		);
	}

	/// Assert Created (201) status.
	pub fn created<T: HasStatusCode>(response: &T) {
		let status = response.status_code();
		assert_eq!(
			status,
			StatusCode::CREATED,
			"Expected 201 Created, but got {:?}",
			status
		);
	}

	/// Assert No Content (204) status.
	pub fn no_content<T: HasStatusCode>(response: &T) {
		let status = response.status_code();
		assert_eq!(
			status,
			StatusCode::NO_CONTENT,
			"Expected 204 No Content, but got {:?}",
			status
		);
	}

	/// Assert Bad Request (400) status.
	pub fn bad_request<T: HasStatusCode>(response: &T) {
		let status = response.status_code();
		assert_eq!(
			status,
			StatusCode::BAD_REQUEST,
			"Expected 400 Bad Request, but got {:?}",
			status
		);
	}

	/// Assert Unauthorized (401) status.
	pub fn unauthorized<T: HasStatusCode>(response: &T) {
		let status = response.status_code();
		assert_eq!(
			status,
			StatusCode::UNAUTHORIZED,
			"Expected 401 Unauthorized, but got {:?}",
			status
		);
	}

	/// Assert Forbidden (403) status.
	pub fn forbidden<T: HasStatusCode>(response: &T) {
		let status = response.status_code();
		assert_eq!(
			status,
			StatusCode::FORBIDDEN,
			"Expected 403 Forbidden, but got {:?}",
			status
		);
	}

	/// Assert Not Found (404) status.
	pub fn not_found<T: HasStatusCode>(response: &T) {
		let status = response.status_code();
		assert_eq!(
			status,
			StatusCode::NOT_FOUND,
			"Expected 404 Not Found, but got {:?}",
			status
		);
	}

	/// Assert Conflict (409) status.
	pub fn conflict<T: HasStatusCode>(response: &T) {
		let status = response.status_code();
		assert_eq!(
			status,
			StatusCode::CONFLICT,
			"Expected 409 Conflict, but got {:?}",
			status
		);
	}

	/// Assert Unprocessable Entity (422) status.
	pub fn unprocessable_entity<T: HasStatusCode>(response: &T) {
		let status = response.status_code();
		assert_eq!(
			status,
			StatusCode::UNPROCESSABLE_ENTITY,
			"Expected 422 Unprocessable Entity, but got {:?}",
			status
		);
	}

	/// Assert Internal Server Error (500) status.
	pub fn internal_error<T: HasStatusCode>(response: &T) {
		let status = response.status_code();
		assert_eq!(
			status,
			StatusCode::INTERNAL_SERVER_ERROR,
			"Expected 500 Internal Server Error, but got {:?}",
			status
		);
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_should_be_ok() {
		let result: Result<i32, &str> = Ok(42);
		assert_eq!(*result.should_be_ok(), 42);
	}

	#[test]
	#[should_panic(expected = "Expected Ok result")]
	fn test_should_be_ok_panics_on_err() {
		let result: Result<i32, &str> = Err("error");
		result.should_be_ok();
	}

	#[test]
	fn test_should_be_err() {
		let result: Result<i32, &str> = Err("error");
		assert_eq!(*result.should_be_err(), "error");
	}

	#[test]
	#[should_panic(expected = "Expected Err result")]
	fn test_should_be_err_panics_on_ok() {
		let result: Result<i32, &str> = Ok(42);
		result.should_be_err();
	}

	#[test]
	fn test_should_have_value() {
		let result: Result<i32, &str> = Ok(42);
		result.should_have_value(&42);
	}

	#[test]
	fn test_should_satisfy() {
		let result: Result<i32, &str> = Ok(42);
		result.should_satisfy(|v| *v > 0);
	}

	#[test]
	fn test_assert_server_fn_returns() {
		let result: Result<i32, &str> = Ok(42);
		assert_server_fn_returns(&result, &42);
	}

	#[test]
	fn test_assert_server_fn_error_contains() {
		let result: Result<i32, String> = Err("Validation failed: email is required".to_string());
		assert_server_fn_error_contains(&result, "email");
	}

	#[test]
	fn test_assert_validation_error() {
		let result: Result<i32, String> = Err("Field 'username' is required".to_string());
		assert_validation_error(&result, "username");
	}

	#[test]
	fn test_assert_validation_errors() {
		let result: Result<i32, String> =
			Err("Validation errors: username is required, email is invalid".to_string());
		assert_validation_errors(&result, &["username", "email"]);
	}
}