oxify-model 0.1.0

Data models and types for OxiFY workflows, execution, and configuration
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
498
499
500
501
502
//! Testing Utilities - Helper functions for creating test workflows and mock data
//!
//! This module provides convenient functions to create test workflows, nodes,
//! and analytics data for testing purposes. This is especially useful when
//! writing tests for applications that use oxify-model.
//!
//! # Example
//!
//! ```
//! use oxify_model::test_utils::{create_test_workflow, create_test_analytics};
//!
//! # fn example() {
//! // Create a simple test workflow
//! let workflow = create_test_workflow("test", 5);
//! assert_eq!(workflow.nodes.len(), 5);
//!
//! // Create mock analytics data
//! let analytics = create_test_analytics("test", 100, 0.85);
//! assert_eq!(analytics.execution_stats.total_executions, 100);
//! assert_eq!(analytics.execution_stats.success_rate, 0.85);
//! # }
//! ```

use crate::{
    analytics::{
        AnalyticsPeriod, ExecutionStats, PerformanceMetrics, PeriodType, WorkflowAnalytics,
    },
    execution::{ExecutionContext, ExecutionResult, NodeExecutionResult, NodeMetrics, TokenUsage},
    node::{LlmConfig, LoopConfig, Node, NodeKind},
    workflow::{Workflow, WorkflowMetadata},
    Edge, WorkflowBuilder,
};
use chrono::Utc;
use std::collections::HashMap;
use uuid::Uuid;

/// Create a simple test workflow with the specified number of nodes
///
/// This creates a linear workflow with:
/// - 1 Start node
/// - N-2 LLM nodes (where N is the node_count parameter)
/// - 1 End node
///
/// # Example
///
/// ```
/// use oxify_model::test_utils::create_test_workflow;
///
/// let workflow = create_test_workflow("my_test", 5);
/// assert_eq!(workflow.nodes.len(), 5);
/// ```
pub fn create_test_workflow(name: &str, node_count: usize) -> Workflow {
    let mut builder = WorkflowBuilder::new(name);

    // Start node
    builder = builder.start("Start");

    // Add intermediate LLM nodes
    for i in 1..node_count.saturating_sub(1) {
        let llm_config = LlmConfig {
            provider: "test".to_string(),
            model: "gpt-4".to_string(),
            system_prompt: None,
            prompt_template: format!("Process step {}", i),
            temperature: Some(0.7),
            max_tokens: Some(1000),
            tools: vec![],
            images: vec![],
            extra_params: serde_json::Value::Null,
        };
        builder = builder.llm(format!("LLM_{}", i), llm_config);
    }

    // End node
    builder = builder.end("End");

    builder.build()
}

/// Create a branching test workflow with a switch node
///
/// This creates a workflow with:
/// - 1 Start node
/// - 1 LLM node
/// - 1 Switch node (multi-branch routing)
/// - 1 End node
///
/// # Example
///
/// ```
/// use oxify_model::test_utils::create_branching_workflow;
///
/// let workflow = create_branching_workflow("branching_test");
/// assert!(workflow.nodes.len() >= 4);
/// ```
pub fn create_branching_workflow(name: &str) -> Workflow {
    use crate::node::{SwitchCase, SwitchConfig};

    let mut builder = WorkflowBuilder::new(name);

    // Start node
    builder = builder.start("Start");

    // First LLM node
    let llm_config = LlmConfig {
        provider: "test".to_string(),
        model: "gpt-4".to_string(),
        system_prompt: None,
        prompt_template: "Initial processing".to_string(),
        temperature: Some(0.7),
        max_tokens: Some(1000),
        tools: vec![],
        images: vec![],
        extra_params: serde_json::Value::Null,
    };
    builder = builder.llm("Process", llm_config);

    // Switch branching node
    let switch_config = SwitchConfig {
        switch_on: "{{status}}".to_string(),
        cases: vec![
            SwitchCase {
                match_value: "success".to_string(),
                action: "Process success".to_string(),
            },
            SwitchCase {
                match_value: "error".to_string(),
                action: "Handle error".to_string(),
            },
        ],
        default_case: Some("Default handling".to_string()),
    };
    builder = builder.switch("Router", switch_config);

    // End node
    builder = builder.end("End");

    builder.build()
}

/// Create mock analytics data for testing
///
/// # Arguments
///
/// * `workflow_name` - Name of the workflow
/// * `total_executions` - Total number of executions
/// * `success_rate` - Success rate (0.0 to 1.0)
///
/// # Example
///
/// ```
/// use oxify_model::test_utils::create_test_analytics;
///
/// let analytics = create_test_analytics("test", 100, 0.90);
/// assert_eq!(analytics.execution_stats.total_executions, 100);
/// assert_eq!(analytics.execution_stats.success_rate, 0.90);
/// ```
pub fn create_test_analytics(
    workflow_name: &str,
    total_executions: u64,
    success_rate: f64,
) -> WorkflowAnalytics {
    let successful = (total_executions as f64 * success_rate) as u64;
    let failed = total_executions - successful;

    WorkflowAnalytics {
        workflow_id: Uuid::new_v4(),
        workflow_name: workflow_name.to_string(),
        period: AnalyticsPeriod {
            start: Utc::now(),
            end: Utc::now(),
            period_type: PeriodType::Daily,
        },
        execution_stats: ExecutionStats {
            total_executions,
            successful_executions: successful,
            failed_executions: failed,
            cancelled_executions: 0,
            success_rate,
            failure_rate: 1.0 - success_rate,
            executions_per_hour: total_executions as f64 / 24.0,
        },
        performance_metrics: PerformanceMetrics {
            avg_duration_ms: 1500.0,
            p50_duration_ms: 1200,
            p95_duration_ms: 3000,
            p99_duration_ms: 4500,
            min_duration_ms: 500,
            max_duration_ms: 5000,
            total_tokens: total_executions * 1000,
            avg_tokens: 1000.0,
            total_cost_usd: total_executions as f64 * 0.01,
            avg_cost_usd: 0.01,
        },
        node_analytics: vec![],
        error_patterns: vec![],
        updated_at: Utc::now(),
    }
}

/// Create a test execution context with the given workflow ID
///
/// # Example
///
/// ```
/// use oxify_model::test_utils::create_test_execution_context;
///
/// let context = create_test_execution_context();
/// assert_eq!(context.state, oxify_model::ExecutionState::Running);
/// ```
pub fn create_test_execution_context() -> ExecutionContext {
    ExecutionContext::new(Uuid::new_v4())
}

/// Create a test node execution result with success status
///
/// # Example
///
/// ```
/// use oxify_model::test_utils::create_test_node_result;
///
/// let result = create_test_node_result(true);
/// assert!(result.completed_at.is_some());
/// ```
pub fn create_test_node_result(success: bool) -> NodeExecutionResult {
    let result = NodeExecutionResult::new();

    if success {
        result
            .with_metrics(NodeMetrics {
                duration_ms: Some(100),
                token_usage: Some(TokenUsage {
                    input_tokens: 50,
                    output_tokens: 100,
                    total_tokens: 150,
                    cached_tokens: None,
                }),
                cost_usd: Some(0.001),
                api_calls: 1,
                bytes_transferred: 1024,
                memory_bytes: None,
                custom: HashMap::new(),
            })
            .complete(ExecutionResult::Success(
                serde_json::json!({"status": "success"}),
            ))
    } else {
        result.complete(ExecutionResult::Failure("Test error".to_string()))
    }
}

/// Create a test workflow metadata with the given name
///
/// # Example
///
/// ```
/// use oxify_model::test_utils::create_test_metadata;
///
/// let metadata = create_test_metadata("my_workflow", "1.0.0");
/// assert_eq!(metadata.name, "my_workflow");
/// assert_eq!(metadata.version, "1.0.0");
/// ```
pub fn create_test_metadata(name: &str, version: &str) -> WorkflowMetadata {
    WorkflowMetadata {
        id: Uuid::new_v4(),
        name: name.to_string(),
        description: Some(format!("Test workflow: {}", name)),
        version: version.to_string(),
        created_at: Utc::now(),
        updated_at: Utc::now(),
        tags: vec!["test".to_string()],
        parent_id: None,
        change_description: None,
        schedule: None,
    }
}

/// Create a simple LLM node for testing
///
/// # Example
///
/// ```
/// use oxify_model::test_utils::create_test_llm_node;
///
/// let node = create_test_llm_node("test_node", "Test prompt");
/// assert_eq!(node.name, "test_node");
/// ```
pub fn create_test_llm_node(name: &str, prompt: &str) -> Node {
    Node {
        id: Uuid::new_v4(),
        name: name.to_string(),
        kind: NodeKind::LLM(LlmConfig {
            provider: "test".to_string(),
            model: "gpt-4".to_string(),
            system_prompt: None,
            prompt_template: prompt.to_string(),
            temperature: Some(0.7),
            max_tokens: Some(1000),
            tools: vec![],
            images: vec![],
            extra_params: serde_json::Value::Null,
        }),
        position: None,
        retry_config: None,
        timeout_config: None,
    }
}

/// Create a test edge connecting two nodes
///
/// # Example
///
/// ```
/// use oxify_model::test_utils::create_test_edge;
/// use uuid::Uuid;
///
/// let from = Uuid::new_v4();
/// let to = Uuid::new_v4();
/// let edge = create_test_edge(from, to, Some("success"));
/// assert_eq!(edge.from, from);
/// assert_eq!(edge.to, to);
/// ```
pub fn create_test_edge(from: Uuid, to: Uuid, label: Option<&str>) -> Edge {
    Edge {
        id: Uuid::new_v4(),
        from,
        to,
        label: label.map(String::from),
        condition: None,
    }
}

/// Create a batch of test workflows with different configurations
///
/// This creates a collection of workflows for testing batch operations.
///
/// # Arguments
///
/// * `count` - Number of workflows to create
/// * `nodes_per_workflow` - Number of nodes in each workflow
///
/// # Example
///
/// ```
/// use oxify_model::test_utils::create_test_workflow_batch;
///
/// let workflows = create_test_workflow_batch(3, 5);
/// assert_eq!(workflows.len(), 3);
/// assert_eq!(workflows[0].nodes.len(), 5);
/// ```
pub fn create_test_workflow_batch(count: usize, nodes_per_workflow: usize) -> Vec<Workflow> {
    (0..count)
        .map(|i| create_test_workflow(&format!("test_workflow_{}", i), nodes_per_workflow))
        .collect()
}

/// Create a workflow with a loop for testing iteration
///
/// # Example
///
/// ```
/// use oxify_model::test_utils::create_loop_workflow;
/// use oxify_model::LoopConfig;
///
/// let workflow = create_loop_workflow("loop_test");
/// assert!(!workflow.nodes.is_empty());
/// ```
pub fn create_loop_workflow(name: &str) -> Workflow {
    use crate::node::LoopType;

    let mut builder = WorkflowBuilder::new(name);

    let loop_config = LoopConfig {
        loop_type: LoopType::ForEach {
            collection_path: "{{items}}".to_string(),
            item_variable: "item".to_string(),
            index_variable: None,
            body_expression: "process item".to_string(),
            parallel: false,
            max_concurrency: None,
        },
        max_iterations: 10,
    };

    builder = builder
        .start("Start")
        .loop_node("Loop", loop_config)
        .end("End");

    builder.build()
}

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

    #[test]
    fn test_create_test_workflow() {
        let workflow = create_test_workflow("test", 5);
        assert_eq!(workflow.metadata.name, "test");
        assert_eq!(workflow.nodes.len(), 5);
        assert!(!workflow.edges.is_empty());
    }

    #[test]
    fn test_create_test_workflow_minimum() {
        let workflow = create_test_workflow("minimal", 2);
        assert_eq!(workflow.nodes.len(), 2);
    }

    #[test]
    fn test_create_branching_workflow() {
        let workflow = create_branching_workflow("branch");
        assert!(workflow.nodes.len() >= 4);
    }

    #[test]
    fn test_create_test_analytics() {
        let analytics = create_test_analytics("test", 100, 0.85);
        assert_eq!(analytics.execution_stats.total_executions, 100);
        assert_eq!(analytics.execution_stats.success_rate, 0.85);
        assert_eq!(analytics.execution_stats.successful_executions, 85);
        assert_eq!(analytics.execution_stats.failed_executions, 15);
    }

    #[test]
    fn test_create_test_execution_context() {
        let context = create_test_execution_context();
        assert_eq!(context.state, ExecutionState::Running);
    }

    #[test]
    fn test_create_test_node_result_success() {
        let result = create_test_node_result(true);
        assert!(result.completed_at.is_some());
        assert!(matches!(result.result, ExecutionResult::Success(_)));
        assert!(result.metrics.is_some());
    }

    #[test]
    fn test_create_test_node_result_failure() {
        let result = create_test_node_result(false);
        assert!(result.completed_at.is_some());
        assert!(matches!(result.result, ExecutionResult::Failure(_)));
    }

    #[test]
    fn test_create_test_metadata() {
        let metadata = create_test_metadata("my_workflow", "1.0.0");
        assert_eq!(metadata.name, "my_workflow");
        assert_eq!(metadata.version, "1.0.0");
        assert!(metadata.description.is_some());
        assert!(!metadata.tags.is_empty());
    }

    #[test]
    fn test_create_test_llm_node() {
        let node = create_test_llm_node("test", "prompt");
        assert_eq!(node.name, "test");
        match node.kind {
            NodeKind::LLM(config) => {
                assert_eq!(config.prompt_template, "prompt");
            }
            _ => panic!("Expected LLM node"),
        }
    }

    #[test]
    fn test_create_test_edge() {
        let from = Uuid::new_v4();
        let to = Uuid::new_v4();
        let edge = create_test_edge(from, to, Some("success"));
        assert_eq!(edge.from, from);
        assert_eq!(edge.to, to);
        assert_eq!(edge.label, Some("success".to_string()));
    }

    #[test]
    fn test_create_test_workflow_batch() {
        let workflows = create_test_workflow_batch(3, 5);
        assert_eq!(workflows.len(), 3);
        for workflow in workflows {
            assert_eq!(workflow.nodes.len(), 5);
        }
    }

    #[test]
    fn test_create_loop_workflow() {
        let workflow = create_loop_workflow("loop");
        assert!(!workflow.nodes.is_empty());
        assert_eq!(workflow.metadata.name, "loop");
    }

    #[test]
    fn test_analytics_calculations() {
        let analytics = create_test_analytics("test", 200, 0.75);
        assert_eq!(analytics.execution_stats.successful_executions, 150);
        assert_eq!(analytics.execution_stats.failed_executions, 50);
        assert_eq!(analytics.execution_stats.failure_rate, 0.25);
    }
}