periplon 0.2.0

Rust SDK for building multi-agent AI workflows and automation
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! Execution API Integration Tests
//!
//! Comprehensive tests for execution management API covering:
//! - Execution creation and queuing
//! - Execution lifecycle management (queued → running → completed/failed)
//! - Execution status updates and transitions
//! - Execution cancellation
//! - Execution filtering and pagination
//! - Execution logs retrieval
//! - Parent-child execution relationships
//! - Priority handling

#![cfg(feature = "server")]

use chrono::Utc;
use periplon_sdk::dsl::schema::DSLWorkflow;
use periplon_sdk::server::queue::{Job, WorkQueue};
use periplon_sdk::server::storage::{
    Execution, ExecutionFilter, ExecutionLog, ExecutionStatus, ExecutionStorage, WorkflowMetadata,
    WorkflowStorage,
};
use periplon_sdk::testing::{MockQueue, MockStorage};
use serde_json::json;
use std::collections::HashMap;
use std::sync::Arc;
use uuid::Uuid;

// ============================================================================
// Test Helpers
// ============================================================================

fn create_test_workflow(name: &str) -> (DSLWorkflow, WorkflowMetadata) {
    let workflow = DSLWorkflow {
        provider: Default::default(),
        model: None,
        name: name.to_string(),
        version: "1.0.0".to_string(),
        dsl_version: "1.0.0".to_string(),
        cwd: None,
        create_cwd: None,
        secrets: HashMap::new(),
        inputs: HashMap::new(),
        outputs: HashMap::new(),
        agents: HashMap::new(),
        tasks: HashMap::new(),
        workflows: HashMap::new(),
        tools: None,
        communication: None,
        mcp_servers: HashMap::new(),
        subflows: HashMap::new(),
        imports: HashMap::new(),
        notifications: None,
        limits: None,
    };

    let metadata = WorkflowMetadata {
        id: Uuid::new_v4(),
        name: name.to_string(),
        version: "1.0.0".to_string(),
        description: Some("Test workflow for execution".to_string()),
        created_at: Utc::now(),
        updated_at: Utc::now(),
        created_by: Some("test_user".to_string()),
        tags: vec!["test".to_string()],
        is_active: true,
    };

    (workflow, metadata)
}

fn create_test_execution(workflow_id: Uuid, status: ExecutionStatus) -> Execution {
    let status_clone = status.clone();
    Execution {
        id: Uuid::new_v4(),
        workflow_id,
        workflow_version: "1.0.0".to_string(),
        status,
        started_at: if status_clone != ExecutionStatus::Queued {
            Some(Utc::now())
        } else {
            None
        },
        completed_at: if matches!(
            status_clone,
            ExecutionStatus::Completed | ExecutionStatus::Failed | ExecutionStatus::Cancelled
        ) {
            Some(Utc::now())
        } else {
            None
        },
        created_at: Utc::now(),
        triggered_by: Some("test_user".to_string()),
        trigger_type: "manual".to_string(),
        input_params: Some(json!({"key": "value"})),
        result: if status_clone == ExecutionStatus::Completed {
            Some(json!({"result": "success"}))
        } else {
            None
        },
        error: if status_clone == ExecutionStatus::Failed {
            Some("Test error".to_string())
        } else {
            None
        },
        retry_count: 0,
        parent_execution_id: None,
    }
}

async fn setup_test_environment() -> (Arc<MockStorage>, Arc<MockQueue>) {
    let storage = Arc::new(MockStorage::new());
    let queue = Arc::new(MockQueue::new());
    (storage, queue)
}

// ============================================================================
// Execution Creation Tests
// ============================================================================

#[tokio::test]
async fn test_create_execution() {
    let (storage, queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    // Store workflow
    storage.store_workflow(&workflow, &metadata).await.unwrap();

    // Create execution
    let execution = create_test_execution(workflow_id, ExecutionStatus::Queued);
    let execution_id = execution.id;

    storage.store_execution(&execution).await.unwrap();

    // Create job
    let job_payload = json!({
        "workflow": workflow,
        "input_params": {"key": "value"}
    });
    let job = Job::new(workflow_id, execution_id, job_payload);
    queue.enqueue(job).await.unwrap();

    // Verify execution stored
    let retrieved = storage.get_execution(execution_id).await.unwrap();
    assert!(retrieved.is_some());
    let exec = retrieved.unwrap();
    assert_eq!(exec.workflow_id, workflow_id);
    assert_eq!(exec.status, ExecutionStatus::Queued);

    // Verify job queued
    assert_eq!(queue.pending_count(), 1);
}

#[tokio::test]
async fn test_create_execution_with_priority() {
    let (storage, queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    let execution = create_test_execution(workflow_id, ExecutionStatus::Queued);
    let execution_id = execution.id;
    storage.store_execution(&execution).await.unwrap();

    // Create job with priority
    let job = Job::new(workflow_id, execution_id, json!({})).with_priority(10);
    queue.enqueue(job).await.unwrap();

    // Dequeue and verify priority
    let dequeued = queue.dequeue("worker-1").await.unwrap();
    assert!(dequeued.is_some());
    let job = dequeued.unwrap();
    assert_eq!(job.priority, 10);
}

#[tokio::test]
async fn test_create_execution_with_input_params() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    let mut execution = create_test_execution(workflow_id, ExecutionStatus::Queued);
    execution.input_params = Some(json!({
        "environment": "production",
        "debug": false,
        "config": {
            "timeout": 300,
            "retries": 3
        }
    }));

    storage.store_execution(&execution).await.unwrap();

    let retrieved = storage.get_execution(execution.id).await.unwrap().unwrap();
    assert_eq!(retrieved.input_params, execution.input_params);
    assert_eq!(retrieved.input_params.unwrap()["environment"], "production");
}

#[tokio::test]
async fn test_create_child_execution() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    // Create parent execution
    let parent_execution = create_test_execution(workflow_id, ExecutionStatus::Running);
    let parent_id = parent_execution.id;
    storage.store_execution(&parent_execution).await.unwrap();

    // Create child execution
    let mut child_execution = create_test_execution(workflow_id, ExecutionStatus::Queued);
    child_execution.parent_execution_id = Some(parent_id);

    storage.store_execution(&child_execution).await.unwrap();

    let retrieved = storage
        .get_execution(child_execution.id)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(retrieved.parent_execution_id, Some(parent_id));
}

// ============================================================================
// Execution Lifecycle Tests
// ============================================================================

#[tokio::test]
async fn test_execution_lifecycle_queued_to_running() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    // Create queued execution
    let mut execution = create_test_execution(workflow_id, ExecutionStatus::Queued);
    let execution_id = execution.id;
    storage.store_execution(&execution).await.unwrap();

    // Transition to running
    execution.status = ExecutionStatus::Running;
    execution.started_at = Some(Utc::now());
    storage
        .update_execution(execution_id, &execution)
        .await
        .unwrap();

    // Verify status change
    let retrieved = storage.get_execution(execution_id).await.unwrap().unwrap();
    assert_eq!(retrieved.status, ExecutionStatus::Running);
    assert!(retrieved.started_at.is_some());
}

#[tokio::test]
async fn test_execution_lifecycle_running_to_completed() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    let mut execution = create_test_execution(workflow_id, ExecutionStatus::Running);
    let execution_id = execution.id;
    storage.store_execution(&execution).await.unwrap();

    // Transition to completed
    execution.status = ExecutionStatus::Completed;
    execution.completed_at = Some(Utc::now());
    execution.result = Some(json!({"status": "success", "output": "data"}));

    storage
        .update_execution(execution_id, &execution)
        .await
        .unwrap();

    let retrieved = storage.get_execution(execution_id).await.unwrap().unwrap();
    assert_eq!(retrieved.status, ExecutionStatus::Completed);
    assert!(retrieved.completed_at.is_some());
    assert!(retrieved.result.is_some());
}

#[tokio::test]
async fn test_execution_lifecycle_running_to_failed() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    let mut execution = create_test_execution(workflow_id, ExecutionStatus::Running);
    let execution_id = execution.id;
    storage.store_execution(&execution).await.unwrap();

    // Transition to failed
    execution.status = ExecutionStatus::Failed;
    execution.completed_at = Some(Utc::now());
    execution.error = Some("Task execution failed".to_string());

    storage
        .update_execution(execution_id, &execution)
        .await
        .unwrap();

    let retrieved = storage.get_execution(execution_id).await.unwrap().unwrap();
    assert_eq!(retrieved.status, ExecutionStatus::Failed);
    assert!(retrieved.completed_at.is_some());
    assert_eq!(retrieved.error, Some("Task execution failed".to_string()));
}

#[tokio::test]
async fn test_execution_cancellation() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    // Create running execution
    let mut execution = create_test_execution(workflow_id, ExecutionStatus::Running);
    let execution_id = execution.id;
    storage.store_execution(&execution).await.unwrap();

    // Cancel execution
    execution.status = ExecutionStatus::Cancelled;
    execution.completed_at = Some(Utc::now());

    storage
        .update_execution(execution_id, &execution)
        .await
        .unwrap();

    let retrieved = storage.get_execution(execution_id).await.unwrap().unwrap();
    assert_eq!(retrieved.status, ExecutionStatus::Cancelled);
    assert!(retrieved.completed_at.is_some());
}

#[tokio::test]
async fn test_execution_retry_increment() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    let mut execution = create_test_execution(workflow_id, ExecutionStatus::Failed);
    let execution_id = execution.id;
    storage.store_execution(&execution).await.unwrap();

    // Increment retry count
    execution.retry_count += 1;
    execution.status = ExecutionStatus::Queued; // Re-queue for retry

    storage
        .update_execution(execution_id, &execution)
        .await
        .unwrap();

    let retrieved = storage.get_execution(execution_id).await.unwrap().unwrap();
    assert_eq!(retrieved.retry_count, 1);
    assert_eq!(retrieved.status, ExecutionStatus::Queued);
}

// ============================================================================
// Execution Listing and Filtering Tests
// ============================================================================

#[tokio::test]
async fn test_list_all_executions() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    // Create multiple executions
    for i in 0..5 {
        let status = match i % 3 {
            0 => ExecutionStatus::Queued,
            1 => ExecutionStatus::Running,
            _ => ExecutionStatus::Completed,
        };
        let execution = create_test_execution(workflow_id, status);
        storage.store_execution(&execution).await.unwrap();
    }

    let filter = ExecutionFilter::default();
    let executions = storage.list_executions(&filter).await.unwrap();
    assert_eq!(executions.len(), 5);
}

#[tokio::test]
async fn test_filter_executions_by_workflow() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow1, metadata1) = create_test_workflow("workflow-1");
    let (workflow2, metadata2) = create_test_workflow("workflow-2");
    let workflow_id1 = metadata1.id;
    let workflow_id2 = metadata2.id;

    storage
        .store_workflow(&workflow1, &metadata1)
        .await
        .unwrap();
    storage
        .store_workflow(&workflow2, &metadata2)
        .await
        .unwrap();

    // Create executions for both workflows
    for _ in 0..3 {
        let execution = create_test_execution(workflow_id1, ExecutionStatus::Queued);
        storage.store_execution(&execution).await.unwrap();
    }
    for _ in 0..2 {
        let execution = create_test_execution(workflow_id2, ExecutionStatus::Running);
        storage.store_execution(&execution).await.unwrap();
    }

    let filter = ExecutionFilter {
        workflow_id: Some(workflow_id1),
        ..Default::default()
    };
    let executions = storage.list_executions(&filter).await.unwrap();
    assert_eq!(executions.len(), 3);
    assert!(executions.iter().all(|e| e.workflow_id == workflow_id1));
}

#[tokio::test]
async fn test_filter_executions_by_status() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    // Create executions with different statuses
    for _ in 0..2 {
        let execution = create_test_execution(workflow_id, ExecutionStatus::Running);
        storage.store_execution(&execution).await.unwrap();
    }
    for _ in 0..3 {
        let execution = create_test_execution(workflow_id, ExecutionStatus::Completed);
        storage.store_execution(&execution).await.unwrap();
    }

    let filter = ExecutionFilter {
        status: Some(ExecutionStatus::Completed),
        ..Default::default()
    };
    let executions = storage.list_executions(&filter).await.unwrap();
    assert_eq!(executions.len(), 3);
    assert!(executions
        .iter()
        .all(|e| e.status == ExecutionStatus::Completed));
}

#[tokio::test]
async fn test_filter_executions_by_triggered_by() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    // Create executions with different triggers
    for _ in 0..3 {
        let mut execution = create_test_execution(workflow_id, ExecutionStatus::Queued);
        execution.triggered_by = Some("user1".to_string());
        storage.store_execution(&execution).await.unwrap();
    }
    for _ in 0..2 {
        let mut execution = create_test_execution(workflow_id, ExecutionStatus::Running);
        execution.triggered_by = Some("user2".to_string());
        storage.store_execution(&execution).await.unwrap();
    }

    let filter = ExecutionFilter {
        triggered_by: Some("user1".to_string()),
        ..Default::default()
    };
    let executions = storage.list_executions(&filter).await.unwrap();
    assert_eq!(executions.len(), 3);
    assert!(executions
        .iter()
        .all(|e| e.triggered_by.as_ref() == Some(&"user1".to_string())));
}

#[tokio::test]
async fn test_execution_pagination() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    // Create 10 executions
    for _ in 0..10 {
        let execution = create_test_execution(workflow_id, ExecutionStatus::Queued);
        storage.store_execution(&execution).await.unwrap();
    }

    // Test limit
    let filter = ExecutionFilter {
        limit: Some(5),
        ..Default::default()
    };
    let executions = storage.list_executions(&filter).await.unwrap();
    assert_eq!(executions.len(), 5);
}

// ============================================================================
// Execution Log Tests
// ============================================================================

#[tokio::test]
async fn test_store_and_retrieve_execution_logs() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    let execution = create_test_execution(workflow_id, ExecutionStatus::Running);
    let execution_id = execution.id;
    storage.store_execution(&execution).await.unwrap();

    // Store logs
    for i in 0..5 {
        let log = ExecutionLog {
            id: None,
            execution_id,
            task_execution_id: None,
            timestamp: Utc::now(),
            level: "INFO".to_string(),
            message: format!("Log message {}", i),
            metadata: None,
        };
        storage.store_execution_log(&log).await.unwrap();
    }

    // Retrieve logs
    let logs = storage
        .get_execution_logs(execution_id, None)
        .await
        .unwrap();
    assert_eq!(logs.len(), 5);
}

#[tokio::test]
async fn test_execution_logs_with_different_levels() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    let execution = create_test_execution(workflow_id, ExecutionStatus::Running);
    let execution_id = execution.id;
    storage.store_execution(&execution).await.unwrap();

    let levels = vec!["DEBUG", "INFO", "WARN", "ERROR"];
    for level in levels {
        let log = ExecutionLog {
            id: None,
            execution_id,
            task_execution_id: None,
            timestamp: Utc::now(),
            level: level.to_string(),
            message: format!("Test {} message", level),
            metadata: None,
        };
        storage.store_execution_log(&log).await.unwrap();
    }

    let logs = storage
        .get_execution_logs(execution_id, None)
        .await
        .unwrap();
    assert_eq!(logs.len(), 4);
    assert!(logs.iter().any(|l| l.level == "DEBUG"));
    assert!(logs.iter().any(|l| l.level == "ERROR"));
}

#[tokio::test]
async fn test_execution_logs_with_metadata() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    let execution = create_test_execution(workflow_id, ExecutionStatus::Running);
    let execution_id = execution.id;
    storage.store_execution(&execution).await.unwrap();

    let log = ExecutionLog {
        id: None,
        execution_id,
        task_execution_id: None,
        timestamp: Utc::now(),
        level: "INFO".to_string(),
        message: "Processing task".to_string(),
        metadata: Some(json!({
            "task_id": "task_1",
            "duration_ms": 1500,
            "memory_mb": 128
        })),
    };
    storage.store_execution_log(&log).await.unwrap();

    let logs = storage
        .get_execution_logs(execution_id, None)
        .await
        .unwrap();
    assert_eq!(logs.len(), 1);
    assert!(logs[0].metadata.is_some());
    assert_eq!(logs[0].metadata.as_ref().unwrap()["task_id"], "task_1");
}

// ============================================================================
// Concurrent Execution Tests
// ============================================================================

#[tokio::test]
async fn test_concurrent_execution_creation() {
    let storage = Arc::new(MockStorage::new());
    let queue = Arc::new(MockQueue::new());
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    // Spawn multiple tasks creating executions concurrently
    let mut handles = vec![];
    for _ in 0..10 {
        let storage_clone = storage.clone();
        let queue_clone = queue.clone();
        let wf_id = workflow_id;
        let handle = tokio::spawn(async move {
            let execution = create_test_execution(wf_id, ExecutionStatus::Queued);
            let execution_id = execution.id;
            storage_clone.store_execution(&execution).await.unwrap();

            let job = Job::new(wf_id, execution_id, json!({}));
            queue_clone.enqueue(job).await.unwrap();
        });
        handles.push(handle);
    }

    // Wait for all to complete
    for handle in handles {
        handle.await.unwrap();
    }

    // Verify all stored
    assert_eq!(storage.execution_count(), 10);
    assert_eq!(queue.pending_count(), 10);
}

#[tokio::test]
async fn test_execution_state_consistency() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    let execution = create_test_execution(workflow_id, ExecutionStatus::Queued);
    let execution_id = execution.id;
    storage.store_execution(&execution).await.unwrap();

    // Multiple concurrent updates
    let storage_clone1 = Arc::clone(&Arc::new(storage.clone()));
    let storage_clone2 = Arc::clone(&Arc::new(storage.clone()));

    let handle1 = tokio::spawn(async move {
        let mut exec = storage_clone1
            .get_execution(execution_id)
            .await
            .unwrap()
            .unwrap();
        exec.status = ExecutionStatus::Running;
        storage_clone1
            .update_execution(execution_id, &exec)
            .await
            .unwrap();
    });

    let handle2 = tokio::spawn(async move {
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        let mut exec = storage_clone2
            .get_execution(execution_id)
            .await
            .unwrap()
            .unwrap();
        exec.status = ExecutionStatus::Completed;
        storage_clone2
            .update_execution(execution_id, &exec)
            .await
            .unwrap();
    });

    handle1.await.unwrap();
    handle2.await.unwrap();

    // Final state should be from last update
    let final_exec = storage.get_execution(execution_id).await.unwrap().unwrap();
    assert_eq!(final_exec.status, ExecutionStatus::Completed);
}

// ============================================================================
// Error Handling Tests
// ============================================================================

#[tokio::test]
async fn test_execution_not_found() {
    let (storage, _queue) = setup_test_environment().await;
    let non_existent_id = Uuid::new_v4();

    let result = storage.get_execution(non_existent_id).await.unwrap();
    assert!(result.is_none());
}

#[tokio::test]
async fn test_storage_failure_handling() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    // Configure storage to fail
    storage.fail_store();

    let execution = create_test_execution(workflow_id, ExecutionStatus::Queued);
    let result = storage.store_execution(&execution).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_delete_execution_with_logs() {
    let (storage, _queue) = setup_test_environment().await;
    let (workflow, metadata) = create_test_workflow("test-workflow");
    let workflow_id = metadata.id;

    storage.store_workflow(&workflow, &metadata).await.unwrap();

    let execution = create_test_execution(workflow_id, ExecutionStatus::Running);
    let execution_id = execution.id;
    storage.store_execution(&execution).await.unwrap();

    // Store logs
    for i in 0..3 {
        let log = ExecutionLog {
            id: None,
            execution_id,
            task_execution_id: None,
            timestamp: Utc::now(),
            level: "INFO".to_string(),
            message: format!("Log {}", i),
            metadata: None,
        };
        storage.store_execution_log(&log).await.unwrap();
    }

    // Delete execution (should cascade to logs)
    storage.delete_execution(execution_id).await.unwrap();

    // Verify execution deleted
    assert!(storage.get_execution(execution_id).await.unwrap().is_none());

    // Verify logs deleted
    let logs = storage
        .get_execution_logs(execution_id, None)
        .await
        .unwrap();
    assert_eq!(logs.len(), 0);
}