use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowExecution {
pub workflow_id: String,
pub run_id: String,
pub workflow_type: String,
pub attempt: i32,
pub namespace: String,
pub task_queue: String,
}
impl WorkflowExecution {
pub fn new(
workflow_id: String,
run_id: String,
workflow_type: String,
namespace: String,
task_queue: String,
) -> Self {
Self {
workflow_id,
run_id,
workflow_type,
attempt: 1,
namespace,
task_queue,
}
}
pub fn with_attempt(mut self, attempt: i32) -> Self {
self.attempt = attempt;
self
}
pub fn is_first_attempt(&self) -> bool {
self.attempt == 1
}
pub fn is_retry(&self) -> bool {
self.attempt > 1
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_workflow_execution_creation() {
let execution = WorkflowExecution {
workflow_id: "wf-123".to_string(),
run_id: "run-456".to_string(),
workflow_type: "TestWorkflow".to_string(),
attempt: 1,
namespace: "default".to_string(),
task_queue: "test-queue".to_string(),
};
assert_eq!(execution.workflow_id, "wf-123");
assert_eq!(execution.run_id, "run-456");
assert_eq!(execution.workflow_type, "TestWorkflow");
assert_eq!(execution.attempt, 1);
assert!(execution.is_first_attempt());
assert!(!execution.is_retry());
}
#[test]
fn test_workflow_execution_new() {
let execution = WorkflowExecution::new(
"wf-123".to_string(),
"run-456".to_string(),
"TestWorkflow".to_string(),
"production".to_string(),
"critical".to_string(),
);
assert_eq!(execution.workflow_id, "wf-123");
assert_eq!(execution.run_id, "run-456");
assert_eq!(execution.workflow_type, "TestWorkflow");
assert_eq!(execution.namespace, "production");
assert_eq!(execution.task_queue, "critical");
assert_eq!(execution.attempt, 1);
}
#[test]
fn test_workflow_execution_with_attempt() {
let execution = WorkflowExecution::new(
"wf-123".to_string(),
"run-456".to_string(),
"TestWorkflow".to_string(),
"default".to_string(),
"test".to_string(),
)
.with_attempt(3);
assert_eq!(execution.attempt, 3);
assert!(!execution.is_first_attempt());
assert!(execution.is_retry());
}
#[test]
fn test_serialization() {
let execution = WorkflowExecution::new(
"wf-123".to_string(),
"run-456".to_string(),
"TestWorkflow".to_string(),
"default".to_string(),
"test".to_string(),
);
let json = serde_json::to_string(&execution).unwrap();
assert!(json.contains("wf-123"));
assert!(json.contains("run-456"));
let deserialized: WorkflowExecution = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.workflow_id, execution.workflow_id);
assert_eq!(deserialized.run_id, execution.run_id);
}
}