acton-htmx 1.0.0-beta.7

Opinionated Rust web framework for HTMX applications
Documentation
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
//! Testing utilities for background jobs
//!
//! This module provides utilities for testing job execution, including:
//! - `TestJobQueue` - In-memory job queue for unit tests
//! - Job execution assertions
//! - Retry behavior tests
//! - Mock job implementations

use crate::jobs::{Job, JobError, JobResult};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use std::time::Duration;

/// In-memory job queue for testing
///
/// Provides a simple job queue implementation for unit tests without requiring
/// Redis or the full job agent infrastructure.
///
/// # Example
///
/// ```rust
/// use acton_htmx::jobs::testing::{TestJobQueue, TestJob};
/// use acton_htmx::jobs::Job;
///
/// #[tokio::test]
/// async fn test_job_execution() {
///     let queue = TestJobQueue::new();
///
///     let job = TestJob::new("test".to_string(), true);
///     queue.enqueue(job.clone());
///
///     assert_eq!(queue.len(), 1);
///
///     let executed = queue.execute_next().await;
///     assert!(executed.is_ok());
///     assert_eq!(queue.len(), 0);
/// }
/// ```
#[derive(Clone)]
pub struct TestJobQueue {
    jobs: Arc<Mutex<VecDeque<Box<dyn JobWrapper>>>>,
    completed: Arc<Mutex<Vec<String>>>,
    failed: Arc<Mutex<Vec<(String, String)>>>,
}

impl Default for TestJobQueue {
    fn default() -> Self {
        Self::new()
    }
}

impl TestJobQueue {
    /// Create a new test job queue
    #[must_use]
    pub fn new() -> Self {
        Self {
            jobs: Arc::new(Mutex::new(VecDeque::new())),
            completed: Arc::new(Mutex::new(Vec::new())),
            failed: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Enqueue a job for execution
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned
    pub fn enqueue<J: Job + Clone + Send + Sync + 'static>(&self, job: J) {
        let wrapper = Box::new(TypedJobWrapper { job });
        self.jobs.lock().unwrap().push_back(wrapper);
    }

    /// Execute the next job in the queue
    ///
    /// Returns `None` if the queue is empty.
    ///
    /// # Errors
    ///
    /// Returns error if job execution fails
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned
    pub async fn execute_next(&self) -> Option<Result<(), JobError>> {
        let job = self.jobs.lock().unwrap().pop_front()?;

        let job_name = job.name();
        match job.execute_boxed().await {
            Ok(()) => {
                self.completed.lock().unwrap().push(job_name);
                Some(Ok(()))
            }
            Err(e) => {
                self.failed
                    .lock()
                    .unwrap()
                    .push((job_name, e.to_string()));
                Some(Err(e))
            }
        }
    }

    /// Execute all jobs in the queue
    ///
    /// # Errors
    ///
    /// Returns the first error encountered during execution
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned
    pub async fn execute_all(&self) -> Result<(), JobError> {
        while let Some(result) = self.execute_next().await {
            result?;
        }
        Ok(())
    }

    /// Get the number of pending jobs
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned
    #[must_use]
    pub fn len(&self) -> usize {
        self.jobs.lock().unwrap().len()
    }

    /// Check if the queue is empty
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.jobs.lock().unwrap().is_empty()
    }

    /// Get the number of completed jobs
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned
    #[must_use]
    pub fn completed_count(&self) -> usize {
        self.completed.lock().unwrap().len()
    }

    /// Get the number of failed jobs
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned
    #[must_use]
    pub fn failed_count(&self) -> usize {
        self.failed.lock().unwrap().len()
    }

    /// Get the list of completed job names
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned
    #[must_use]
    pub fn completed_jobs(&self) -> Vec<String> {
        self.completed.lock().unwrap().clone()
    }

    /// Get the list of failed jobs with error messages
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned
    #[must_use]
    pub fn failed_jobs(&self) -> Vec<(String, String)> {
        self.failed.lock().unwrap().clone()
    }

    /// Clear all completed and failed job history
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned
    pub fn clear_history(&self) {
        self.completed.lock().unwrap().clear();
        self.failed.lock().unwrap().clear();
    }
}

/// Trait for type-erased job execution
trait JobWrapper: Send {
    fn execute_boxed(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = JobResult<()>> + Send + '_>>;
    fn name(&self) -> String;
}

/// Typed wrapper for jobs
struct TypedJobWrapper<J: Job> {
    job: J,
}

impl<J: Job + Send + Sync> JobWrapper for TypedJobWrapper<J> {
    fn execute_boxed(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = JobResult<()>> + Send + '_>> {
        Box::pin(async move {
            let ctx = crate::jobs::JobContext::new();
            self.job.execute(&ctx).await?;
            Ok(())
        })
    }

    fn name(&self) -> String {
        std::any::type_name::<J>().to_string()
    }
}

/// Test job implementation for testing
///
/// A simple job that can be configured to succeed or fail, useful for testing
/// job queue behavior, retry logic, and error handling.
///
/// # Example
///
/// ```rust
/// use acton_htmx::jobs::testing::TestJob;
/// use acton_htmx::jobs::Job;
///
/// #[tokio::test]
/// async fn test_successful_job() {
///     let job = TestJob::new("test".to_string(), true);
///     let result = job.execute().await;
///     assert!(result.is_ok());
/// }
///
/// #[tokio::test]
/// async fn test_failing_job() {
///     let job = TestJob::new("test".to_string(), false);
///     let result = job.execute().await;
///     assert!(result.is_err());
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestJob {
    /// Job identifier
    pub id: String,
    /// Whether the job should succeed (true) or fail (false)
    pub should_succeed: bool,
    /// Optional delay in milliseconds before execution completes
    #[serde(default)]
    pub delay_ms: Option<u64>,
}

impl TestJob {
    /// Create a new test job
    #[must_use]
    pub const fn new(id: String, should_succeed: bool) -> Self {
        Self {
            id,
            should_succeed,
            delay_ms: None,
        }
    }

    /// Create a test job with a delay
    #[must_use]
    pub const fn with_delay(id: String, should_succeed: bool, delay_ms: u64) -> Self {
        Self {
            id,
            should_succeed,
            delay_ms: Some(delay_ms),
        }
    }
}

#[async_trait]
impl Job for TestJob {
    type Result = String;

    async fn execute(&self, _ctx: &crate::jobs::JobContext) -> JobResult<Self::Result> {
        if let Some(delay) = self.delay_ms {
            tokio::time::sleep(Duration::from_millis(delay)).await;
        }

        if self.should_succeed {
            Ok(format!("Success: {}", self.id))
        } else {
            Err(JobError::ExecutionFailed(format!(
                "Intentional failure: {}",
                self.id
            )))
        }
    }

    fn max_retries(&self) -> u32 {
        3
    }

    fn timeout(&self) -> Duration {
        Duration::from_secs(30)
    }

    fn priority(&self) -> i32 {
        128
    }
}

/// Assert that a job executes successfully
///
/// # Panics
///
/// Panics if the job fails to execute
///
/// # Example
///
/// ```rust
/// use acton_htmx::jobs::testing::{assert_job_succeeds, TestJob};
///
/// #[tokio::test]
/// async fn test_job_success() {
///     let job = TestJob::new("test".to_string(), true);
///     assert_job_succeeds(job).await;
/// }
/// ```
pub async fn assert_job_succeeds<J: Job>(job: J)
where
    J::Result: std::fmt::Debug,
{
    let ctx = crate::jobs::JobContext::new();
    let result = job.execute(&ctx).await;
    assert!(
        result.is_ok(),
        "Job should succeed but failed with: {:?}",
        result.unwrap_err()
    );
}

/// Assert that a job fails with an error
///
/// # Panics
///
/// Panics if the job succeeds
///
/// # Example
///
/// ```rust
/// use acton_htmx::jobs::testing::{assert_job_fails, TestJob};
///
/// #[tokio::test]
/// async fn test_job_failure() {
///     let job = TestJob::new("test".to_string(), false);
///     assert_job_fails(job).await;
/// }
/// ```
pub async fn assert_job_fails<J: Job>(job: J) {
    let ctx = crate::jobs::JobContext::new();
    let result = job.execute(&ctx).await;
    assert!(result.is_err(), "Job should fail but succeeded");
}

/// Assert that a job completes within a timeout
///
/// # Panics
///
/// Panics if the job takes longer than the specified timeout
///
/// # Example
///
/// ```rust
/// use acton_htmx::jobs::testing::{assert_job_completes_within, TestJob};
/// use std::time::Duration;
///
/// #[tokio::test]
/// async fn test_job_timeout() {
///     let job = TestJob::with_delay("test".to_string(), true, 100);
///     assert_job_completes_within(job, Duration::from_millis(200)).await;
/// }
/// ```
pub async fn assert_job_completes_within<J: Job>(job: J, timeout: Duration) {
    let ctx = crate::jobs::JobContext::new();
    let result = tokio::time::timeout(timeout, job.execute(&ctx)).await;
    assert!(
        result.is_ok(),
        "Job should complete within {timeout:?} but timed out"
    );
}

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

    #[tokio::test]
    async fn test_queue_enqueue_and_execute() {
        let queue = TestJobQueue::new();
        let job = TestJob::new("test1".to_string(), true);

        queue.enqueue(job);
        assert_eq!(queue.len(), 1);

        let result = queue.execute_next().await;
        assert!(result.is_some());
        assert!(result.unwrap().is_ok());
        assert_eq!(queue.len(), 0);
        assert_eq!(queue.completed_count(), 1);
    }

    #[tokio::test]
    async fn test_queue_failed_job() {
        let queue = TestJobQueue::new();
        let job = TestJob::new("test1".to_string(), false);

        queue.enqueue(job);
        let result = queue.execute_next().await;

        assert!(result.is_some());
        assert!(result.unwrap().is_err());
        assert_eq!(queue.failed_count(), 1);
    }

    #[tokio::test]
    async fn test_queue_execute_all() {
        let queue = TestJobQueue::new();

        queue.enqueue(TestJob::new("test1".to_string(), true));
        queue.enqueue(TestJob::new("test2".to_string(), true));
        queue.enqueue(TestJob::new("test3".to_string(), true));

        let result = queue.execute_all().await;
        assert!(result.is_ok());
        assert_eq!(queue.completed_count(), 3);
    }

    #[tokio::test]
    async fn test_successful_job() {
        let job = TestJob::new("test".to_string(), true);
        assert_job_succeeds(job).await;
    }

    #[tokio::test]
    async fn test_failing_job() {
        let job = TestJob::new("test".to_string(), false);
        assert_job_fails(job).await;
    }

    #[tokio::test]
    async fn test_job_with_delay() {
        let job = TestJob::with_delay("test".to_string(), true, 50);
        assert_job_completes_within(job, Duration::from_millis(100)).await;
    }
}