forge-core 0.9.0

Core types and traits for the Forge framework
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
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
496
497
//! Mock dispatchers for testing job and workflow dispatch.
//!
//! Provides mock implementations that record dispatched jobs and workflows
//! for verification in tests.

#![allow(clippy::unwrap_used, clippy::indexing_slicing)]

use std::sync::RwLock;

use chrono::{DateTime, Utc};
use uuid::Uuid;

use crate::error::{ForgeError, Result};
use crate::job::JobStatus;
use crate::workflow::WorkflowStatus;

/// Record of a dispatched job.
#[derive(Debug, Clone)]
pub struct DispatchedJob {
    /// Job ID.
    pub id: Uuid,
    /// Job type name.
    pub job_type: String,
    /// Job arguments (serialized).
    pub args: serde_json::Value,
    /// When the job was dispatched.
    pub dispatched_at: DateTime<Utc>,
    /// Current status (for test simulation).
    pub status: JobStatus,
    /// Cancellation reason (if any).
    pub cancel_reason: Option<String>,
}

/// Record of a started workflow.
#[derive(Debug, Clone)]
pub struct StartedWorkflow {
    /// Run ID.
    pub run_id: Uuid,
    /// Workflow name.
    pub workflow_name: String,
    /// Input (serialized).
    pub input: serde_json::Value,
    /// When the workflow was started.
    pub started_at: DateTime<Utc>,
    /// Current status.
    pub status: WorkflowStatus,
}

/// Mock job dispatcher for testing.
///
/// Records dispatched jobs for later verification.
///
/// # Example
///
/// ```ignore
/// let dispatch = MockJobDispatch::new();
/// dispatch.dispatch("send_email", json!({"to": "test@example.com"})).await?;
///
/// dispatch.assert_dispatched("send_email");
/// dispatch.assert_dispatched_with("send_email", |args| {
///     args["to"] == "test@example.com"
/// });
/// ```
pub struct MockJobDispatch {
    jobs: RwLock<Vec<DispatchedJob>>,
}

impl MockJobDispatch {
    /// Create a new mock job dispatcher.
    pub fn new() -> Self {
        Self {
            jobs: RwLock::new(Vec::new()),
        }
    }

    /// Dispatch a job (records for later verification).
    pub async fn dispatch<T: serde::Serialize>(&self, job_type: &str, args: T) -> Result<Uuid> {
        let id = Uuid::new_v4();
        let args_json =
            serde_json::to_value(args).map_err(|e| ForgeError::Serialization(e.to_string()))?;

        let job = DispatchedJob {
            id,
            job_type: job_type.to_string(),
            args: args_json,
            dispatched_at: Utc::now(),
            status: JobStatus::Pending,
            cancel_reason: None,
        };

        self.jobs.write().expect("jobs lock poisoned").push(job);
        Ok(id)
    }

    /// Get all dispatched jobs.
    pub fn dispatched_jobs(&self) -> Vec<DispatchedJob> {
        self.jobs.read().expect("jobs lock poisoned").clone()
    }

    /// Get jobs of a specific type.
    pub fn jobs_of_type(&self, job_type: &str) -> Vec<DispatchedJob> {
        self.jobs
            .read()
            .expect("jobs lock poisoned")
            .iter()
            .filter(|j| j.job_type == job_type)
            .cloned()
            .collect()
    }

    /// Assert that a job type was dispatched.
    pub fn assert_dispatched(&self, job_type: &str) {
        let jobs = self.jobs.read().expect("jobs lock poisoned");
        let found = jobs.iter().any(|j| j.job_type == job_type);
        assert!(
            found,
            "Expected job '{}' to be dispatched, but it wasn't. Dispatched jobs: {:?}",
            job_type,
            jobs.iter().map(|j| &j.job_type).collect::<Vec<_>>()
        );
    }

    /// Assert that a job was dispatched with matching arguments.
    pub fn assert_dispatched_with<F>(&self, job_type: &str, predicate: F)
    where
        F: Fn(&serde_json::Value) -> bool,
    {
        let jobs = self.jobs.read().expect("jobs lock poisoned");
        let found = jobs
            .iter()
            .any(|j| j.job_type == job_type && predicate(&j.args));
        assert!(
            found,
            "Expected job '{}' with matching args to be dispatched",
            job_type
        );
    }

    /// Assert that a job type was not dispatched.
    pub fn assert_not_dispatched(&self, job_type: &str) {
        let jobs = self.jobs.read().expect("jobs lock poisoned");
        let found = jobs.iter().any(|j| j.job_type == job_type);
        assert!(
            !found,
            "Expected job '{}' NOT to be dispatched, but it was",
            job_type
        );
    }

    /// Assert that a specific number of jobs were dispatched.
    pub fn assert_dispatch_count(&self, job_type: &str, expected: usize) {
        let jobs = self.jobs.read().expect("jobs lock poisoned");
        let count = jobs.iter().filter(|j| j.job_type == job_type).count();
        assert_eq!(
            count, expected,
            "Expected {} dispatches of '{}', but found {}",
            expected, job_type, count
        );
    }

    /// Clear all recorded jobs.
    pub fn clear(&self) {
        self.jobs.write().expect("jobs lock poisoned").clear();
    }

    /// Mark a job as completed (for test simulation).
    pub fn complete_job(&self, job_id: Uuid) {
        let mut jobs = self.jobs.write().expect("jobs lock poisoned");
        if let Some(job) = jobs.iter_mut().find(|j| j.id == job_id) {
            job.status = JobStatus::Completed;
        }
    }

    /// Mark a job as failed (for test simulation).
    pub fn fail_job(&self, job_id: Uuid) {
        let mut jobs = self.jobs.write().expect("jobs lock poisoned");
        if let Some(job) = jobs.iter_mut().find(|j| j.id == job_id) {
            job.status = JobStatus::Failed;
        }
    }

    /// Mark a job as cancelled (for test simulation).
    pub fn cancel_job(&self, job_id: Uuid, reason: Option<String>) {
        let mut jobs = self.jobs.write().expect("jobs lock poisoned");
        if let Some(job) = jobs.iter_mut().find(|j| j.id == job_id) {
            job.status = JobStatus::Cancelled;
            job.cancel_reason = reason;
        }
    }
}

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

impl crate::function::JobDispatch for MockJobDispatch {
    fn get_info(&self, _job_type: &str) -> Option<crate::job::JobInfo> {
        None
    }

    fn dispatch_by_name(
        &self,
        job_type: &str,
        args: serde_json::Value,
        _owner_subject: Option<String>,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Uuid>> + Send + '_>> {
        let job_type = job_type.to_string();
        Box::pin(async move { self.dispatch(&job_type, args).await })
    }

    fn cancel(
        &self,
        job_id: Uuid,
        reason: Option<String>,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<bool>> + Send + '_>> {
        Box::pin(async move {
            self.cancel_job(job_id, reason);
            Ok(true)
        })
    }
}

/// Mock workflow dispatcher for testing.
///
/// Records started workflows for later verification.
///
/// # Example
///
/// ```ignore
/// let dispatch = MockWorkflowDispatch::new();
/// dispatch.start("onboarding", json!({"user_id": "123"})).await?;
///
/// dispatch.assert_started("onboarding");
/// ```
pub struct MockWorkflowDispatch {
    workflows: RwLock<Vec<StartedWorkflow>>,
}

impl MockWorkflowDispatch {
    /// Create a new mock workflow dispatcher.
    pub fn new() -> Self {
        Self {
            workflows: RwLock::new(Vec::new()),
        }
    }

    /// Start a workflow (records for later verification).
    pub async fn start<T: serde::Serialize>(&self, workflow_name: &str, input: T) -> Result<Uuid> {
        let run_id = Uuid::new_v4();
        let input_json =
            serde_json::to_value(input).map_err(|e| ForgeError::Serialization(e.to_string()))?;

        let workflow = StartedWorkflow {
            run_id,
            workflow_name: workflow_name.to_string(),
            input: input_json,
            started_at: Utc::now(),
            status: WorkflowStatus::Created,
        };

        self.workflows
            .write()
            .expect("workflows lock poisoned")
            .push(workflow);
        Ok(run_id)
    }

    /// Get all started workflows.
    pub fn started_workflows(&self) -> Vec<StartedWorkflow> {
        self.workflows
            .read()
            .expect("workflows lock poisoned")
            .clone()
    }

    /// Get workflows of a specific name.
    pub fn workflows_named(&self, name: &str) -> Vec<StartedWorkflow> {
        self.workflows
            .read()
            .expect("workflows lock poisoned")
            .iter()
            .filter(|w| w.workflow_name == name)
            .cloned()
            .collect()
    }

    /// Assert that a workflow was started.
    pub fn assert_started(&self, workflow_name: &str) {
        let workflows = self.workflows.read().expect("workflows lock poisoned");
        let found = workflows.iter().any(|w| w.workflow_name == workflow_name);
        assert!(
            found,
            "Expected workflow '{}' to be started, but it wasn't. Started workflows: {:?}",
            workflow_name,
            workflows
                .iter()
                .map(|w| &w.workflow_name)
                .collect::<Vec<_>>()
        );
    }

    /// Assert that a workflow was started with matching input.
    pub fn assert_started_with<F>(&self, workflow_name: &str, predicate: F)
    where
        F: Fn(&serde_json::Value) -> bool,
    {
        let workflows = self.workflows.read().expect("workflows lock poisoned");
        let found = workflows
            .iter()
            .any(|w| w.workflow_name == workflow_name && predicate(&w.input));
        assert!(
            found,
            "Expected workflow '{}' with matching input to be started",
            workflow_name
        );
    }

    /// Assert that a workflow was not started.
    pub fn assert_not_started(&self, workflow_name: &str) {
        let workflows = self.workflows.read().expect("workflows lock poisoned");
        let found = workflows.iter().any(|w| w.workflow_name == workflow_name);
        assert!(
            !found,
            "Expected workflow '{}' NOT to be started, but it was",
            workflow_name
        );
    }

    /// Assert that a specific number of workflows were started.
    pub fn assert_start_count(&self, workflow_name: &str, expected: usize) {
        let workflows = self.workflows.read().expect("workflows lock poisoned");
        let count = workflows
            .iter()
            .filter(|w| w.workflow_name == workflow_name)
            .count();
        assert_eq!(
            count, expected,
            "Expected {} starts of '{}', but found {}",
            expected, workflow_name, count
        );
    }

    /// Clear all recorded workflows.
    pub fn clear(&self) {
        self.workflows
            .write()
            .expect("workflows lock poisoned")
            .clear();
    }

    /// Mark a workflow as completed (for test simulation).
    pub fn complete_workflow(&self, run_id: Uuid) {
        let mut workflows = self.workflows.write().expect("workflows lock poisoned");
        if let Some(workflow) = workflows.iter_mut().find(|w| w.run_id == run_id) {
            workflow.status = WorkflowStatus::Completed;
        }
    }

    /// Mark a workflow as failed (for test simulation).
    pub fn fail_workflow(&self, run_id: Uuid) {
        let mut workflows = self.workflows.write().expect("workflows lock poisoned");
        if let Some(workflow) = workflows.iter_mut().find(|w| w.run_id == run_id) {
            workflow.status = WorkflowStatus::Failed;
        }
    }
}

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

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

    #[tokio::test]
    async fn test_mock_job_dispatch() {
        let dispatch = MockJobDispatch::new();

        let job_id = dispatch
            .dispatch("send_email", serde_json::json!({"to": "test@example.com"}))
            .await
            .unwrap();

        assert!(!job_id.is_nil());
        dispatch.assert_dispatched("send_email");
        dispatch.assert_not_dispatched("other_job");
    }

    #[tokio::test]
    async fn test_job_dispatch_with_args() {
        let dispatch = MockJobDispatch::new();

        dispatch
            .dispatch("send_email", serde_json::json!({"to": "test@example.com"}))
            .await
            .unwrap();

        dispatch.assert_dispatched_with("send_email", |args| args["to"] == "test@example.com");
    }

    #[tokio::test]
    async fn test_job_dispatch_count() {
        let dispatch = MockJobDispatch::new();

        dispatch
            .dispatch("job_a", serde_json::json!({}))
            .await
            .unwrap();
        dispatch
            .dispatch("job_b", serde_json::json!({}))
            .await
            .unwrap();
        dispatch
            .dispatch("job_a", serde_json::json!({}))
            .await
            .unwrap();

        dispatch.assert_dispatch_count("job_a", 2);
        dispatch.assert_dispatch_count("job_b", 1);
    }

    #[tokio::test]
    async fn test_mock_workflow_dispatch() {
        let dispatch = MockWorkflowDispatch::new();

        let run_id = dispatch
            .start("onboarding", serde_json::json!({"user_id": "123"}))
            .await
            .unwrap();

        assert!(!run_id.is_nil());
        dispatch.assert_started("onboarding");
        dispatch.assert_not_started("other_workflow");
    }

    #[tokio::test]
    async fn test_workflow_dispatch_with_input() {
        let dispatch = MockWorkflowDispatch::new();

        dispatch
            .start("onboarding", serde_json::json!({"user_id": "123"}))
            .await
            .unwrap();

        dispatch.assert_started_with("onboarding", |input| input["user_id"] == "123");
    }

    #[tokio::test]
    async fn test_clear() {
        let dispatch = MockJobDispatch::new();
        dispatch
            .dispatch("test", serde_json::json!({}))
            .await
            .unwrap();

        assert_eq!(dispatch.dispatched_jobs().len(), 1);
        dispatch.clear();
        assert_eq!(dispatch.dispatched_jobs().len(), 0);
    }

    #[tokio::test]
    async fn test_job_status_simulation() {
        let dispatch = MockJobDispatch::new();
        let job_id = dispatch
            .dispatch("test", serde_json::json!({}))
            .await
            .unwrap();

        let jobs = dispatch.dispatched_jobs();
        assert_eq!(jobs[0].status, JobStatus::Pending);

        dispatch.complete_job(job_id);

        let jobs = dispatch.dispatched_jobs();
        assert_eq!(jobs[0].status, JobStatus::Completed);
    }

    #[tokio::test]
    async fn test_job_cancel_simulation() {
        let dispatch = MockJobDispatch::new();
        let job_id = dispatch
            .dispatch("test", serde_json::json!({}))
            .await
            .unwrap();

        dispatch.cancel_job(job_id, Some("user request".to_string()));

        let jobs = dispatch.dispatched_jobs();
        assert_eq!(jobs[0].status, JobStatus::Cancelled);
        assert_eq!(jobs[0].cancel_reason.as_deref(), Some("user request"));
    }
}