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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
//! Integration tests for notification delivery system
//!
//! This test suite covers end-to-end notification delivery scenarios including:
//! - Multi-channel delivery
//! - Variable interpolation
//! - Error handling and retries
//! - Concurrent delivery (when supported)
//! - File format variations
//! - Schema parsing and serialization

use periplon_sdk::dsl::{
    FileNotificationFormat, NotificationChannel, NotificationContext, NotificationManager,
    NotificationSpec, RetryConfig, SlackMethod,
};
use std::collections::HashMap;

#[tokio::test]
async fn test_simple_console_notification() {
    let manager = NotificationManager::new();
    let context = NotificationContext::new();

    let spec = NotificationSpec::Simple("Test notification message".to_string());

    let result = manager.send(&spec, &context).await;
    assert!(result.is_ok(), "Simple console notification should succeed");
}

#[tokio::test]
async fn test_structured_console_notification() {
    let manager = NotificationManager::new();
    let context = NotificationContext::new();

    let spec = NotificationSpec::Structured {
        message: "Structured test message".to_string(),
        channels: vec![NotificationChannel::Console {
            colored: true,
            timestamp: true,
        }],
        title: Some("Test Title".to_string()),
        priority: None,
        metadata: std::collections::HashMap::new(),
    };

    let result = manager.send(&spec, &context).await;
    assert!(
        result.is_ok(),
        "Structured console notification should succeed"
    );
}

#[tokio::test]
async fn test_variable_interpolation() {
    let context = NotificationContext::new()
        .with_workflow_var("project", "my-project")
        .with_task_var("task_name", "build")
        .with_metadata("status", "success");

    let template =
        "Project ${workflow.project}: Task ${task.task_name} completed with ${metadata.status}";
    let result = context.interpolate(template);

    assert!(result.is_ok());
    assert_eq!(
        result.unwrap(),
        "Project my-project: Task build completed with success"
    );
}

#[tokio::test]
async fn test_variable_interpolation_with_secrets() {
    let context = NotificationContext::new()
        .with_secret("api_key", "secret-key-123")
        .with_workflow_var("env", "production");

    let template = "Deploying to ${workflow.env} with key ${secret.api_key}";
    let result = context.interpolate(template);

    assert!(result.is_ok());
    assert_eq!(
        result.unwrap(),
        "Deploying to production with key secret-key-123"
    );
}

#[tokio::test]
async fn test_unresolved_variable_error() {
    let context = NotificationContext::new().with_workflow_var("known", "value");

    let template = "Known: ${workflow.known}, Unknown: ${workflow.unknown}";
    let result = context.interpolate(template);

    assert!(result.is_err(), "Should error on unresolved variables");
}

#[tokio::test]
async fn test_file_notification_text_format() {
    use tempfile::NamedTempFile;

    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path().to_string_lossy().to_string();

    let manager = NotificationManager::new();
    let context = NotificationContext::new();

    let spec = NotificationSpec::Structured {
        message: "File notification test".to_string(),
        channels: vec![NotificationChannel::File {
            path: file_path.clone(),
            append: false,
            timestamp: false,
            format: periplon_sdk::dsl::FileNotificationFormat::Text,
        }],
        title: None,
        priority: None,
        metadata: std::collections::HashMap::new(),
    };

    let result = manager.send(&spec, &context).await;
    assert!(result.is_ok(), "File notification should succeed");

    // Verify file contents
    let contents = std::fs::read_to_string(&file_path).unwrap();
    assert!(contents.contains("File notification test"));
}

#[tokio::test]
async fn test_file_notification_json_format() {
    use tempfile::NamedTempFile;

    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path().to_string_lossy().to_string();

    let manager = NotificationManager::new();
    let mut metadata = std::collections::HashMap::new();
    metadata.insert("task_id".to_string(), "test-123".to_string());

    let context = NotificationContext::new().with_metadata("task_id", "test-123");

    let spec = NotificationSpec::Structured {
        message: "JSON notification".to_string(),
        channels: vec![NotificationChannel::File {
            path: file_path.clone(),
            append: false,
            timestamp: true,
            format: periplon_sdk::dsl::FileNotificationFormat::Json,
        }],
        title: None,
        priority: None,
        metadata,
    };

    let result = manager.send(&spec, &context).await;
    assert!(result.is_ok(), "JSON file notification should succeed");

    // Verify file contains valid JSON
    let contents = std::fs::read_to_string(&file_path).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&contents).unwrap();
    assert_eq!(parsed["message"], "JSON notification");
}

#[tokio::test]
async fn test_notification_context_builder() {
    let context = NotificationContext::new()
        .with_workflow_var("key1", "value1")
        .with_task_var("key2", "value2")
        .with_agent_var("key3", "value3")
        .with_secret("key4", "value4")
        .with_metadata("key5", "value5");

    assert_eq!(context.workflow_vars.get("key1").unwrap(), "value1");
    assert_eq!(context.task_vars.get("key2").unwrap(), "value2");
    assert_eq!(context.agent_vars.get("key3").unwrap(), "value3");
    assert_eq!(context.secrets.get("key4").unwrap(), "value4");
    assert_eq!(context.metadata.get("key5").unwrap(), "value5");
}

#[tokio::test]
async fn test_multiple_channels_sequential() {
    use tempfile::NamedTempFile;

    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path().to_string_lossy().to_string();

    let manager = NotificationManager::new();
    let context = NotificationContext::new();

    let spec = NotificationSpec::Structured {
        message: "Multi-channel test".to_string(),
        channels: vec![
            NotificationChannel::Console {
                colored: false,
                timestamp: false,
            },
            NotificationChannel::File {
                path: file_path.clone(),
                append: false,
                timestamp: false,
                format: periplon_sdk::dsl::FileNotificationFormat::Text,
            },
        ],
        title: None,
        priority: None,
        metadata: std::collections::HashMap::new(),
    };

    let result = manager.send(&spec, &context).await;
    assert!(result.is_ok(), "Multi-channel notification should succeed");

    // Verify file was written
    let contents = std::fs::read_to_string(&file_path).unwrap();
    assert!(contents.contains("Multi-channel test"));
}

#[tokio::test]
async fn test_placeholder_senders_return_errors() {
    use periplon_sdk::dsl::{EmailSender, NotificationSender};

    let sender = EmailSender::new();
    let context = NotificationContext::new();
    let channel = NotificationChannel::Console {
        colored: false,
        timestamp: false,
    };

    let result = sender.send("test", &channel, &context).await;
    assert!(
        result.is_err(),
        "Email sender should return error (not implemented)"
    );
}

#[tokio::test]
async fn test_notification_manager_has_all_senders() {
    let manager = NotificationManager::new();

    // Verify all expected senders are registered
    assert!(
        manager.has_sender("ntfy"),
        "Manager should have ntfy sender"
    );
    assert!(
        manager.has_sender("slack"),
        "Manager should have slack sender"
    );
    assert!(
        manager.has_sender("discord"),
        "Manager should have discord sender"
    );
    assert!(
        manager.has_sender("console"),
        "Manager should have console sender"
    );
    assert!(
        manager.has_sender("file"),
        "Manager should have file sender"
    );
}

#[test]
fn test_interpolation_all_scopes() {
    let context = NotificationContext::new()
        .with_workflow_var("w_var", "workflow_value")
        .with_task_var("t_var", "task_value")
        .with_agent_var("a_var", "agent_value")
        .with_secret("s_var", "secret_value")
        .with_metadata("m_var", "metadata_value");

    let template = "W:${workflow.w_var} T:${task.t_var} A:${agent.a_var} S:${secret.s_var} M:${metadata.m_var}";
    let result = context.interpolate(template).unwrap();

    assert_eq!(
        result,
        "W:workflow_value T:task_value A:agent_value S:secret_value M:metadata_value"
    );
}

#[tokio::test]
async fn test_file_append_mode() {
    use tempfile::NamedTempFile;
    use tokio::fs;

    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path().to_string_lossy().to_string();

    let manager = NotificationManager::new();
    let context = NotificationContext::new();

    // First write
    let spec1 = NotificationSpec::Structured {
        message: "First message".to_string(),
        channels: vec![NotificationChannel::File {
            path: file_path.clone(),
            append: false,
            timestamp: false,
            format: periplon_sdk::dsl::FileNotificationFormat::Text,
        }],
        title: None,
        priority: None,
        metadata: std::collections::HashMap::new(),
    };

    manager.send(&spec1, &context).await.unwrap();

    // Second write in append mode
    let spec2 = NotificationSpec::Structured {
        message: "Second message".to_string(),
        channels: vec![NotificationChannel::File {
            path: file_path.clone(),
            append: true,
            timestamp: false,
            format: periplon_sdk::dsl::FileNotificationFormat::Text,
        }],
        title: None,
        priority: None,
        metadata: std::collections::HashMap::new(),
    };

    manager.send(&spec2, &context).await.unwrap();

    // Verify both messages are in file
    let contents = fs::read_to_string(&file_path).await.unwrap();
    assert!(contents.contains("First message"));
    assert!(contents.contains("Second message"));
}

// ============================================================================
// Advanced Integration Tests
// ============================================================================

#[tokio::test]
async fn test_retry_logic_with_webhook() {
    // Test retry configuration with webhook (mock will fail)
    let manager = NotificationManager::new();
    let context = NotificationContext::new();

    let retry_config = RetryConfig {
        max_attempts: 2,
        delay_secs: 0, // No delay for testing
        exponential_backoff: false,
    };

    let spec = NotificationSpec::Structured {
        message: "Test retry".to_string(),
        channels: vec![NotificationChannel::Webhook {
            url: "http://invalid-url-for-testing.local".to_string(),
            method: periplon_sdk::dsl::HttpMethod::Post,
            headers: HashMap::new(),
            auth: None,
            body_template: None,
            timeout_secs: Some(1),
            retry: Some(retry_config),
        }],
        title: None,
        priority: None,
        metadata: HashMap::new(),
    };

    // This will fail but shouldn't panic
    let result = manager.send(&spec, &context).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_notification_with_all_variable_scopes() {
    use tempfile::NamedTempFile;

    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path().to_string_lossy().to_string();

    let manager = NotificationManager::new();
    let context = NotificationContext::new()
        .with_workflow_var("workflow_id", "wf-123")
        .with_task_var("task_name", "deploy")
        .with_agent_var("agent_role", "deployer")
        .with_secret("api_key", "secret-xyz")
        .with_metadata("status", "success")
        .with_metadata("duration", "30s");

    let message = r#"
Workflow: ${workflow.workflow_id}
Task: ${task.task_name}
Agent: ${agent.agent_role}
Status: ${metadata.status}
Duration: ${metadata.duration}
"#;

    let spec = NotificationSpec::Structured {
        message: message.to_string(),
        channels: vec![NotificationChannel::File {
            path: file_path.clone(),
            append: false,
            timestamp: false,
            format: FileNotificationFormat::Text,
        }],
        title: None,
        priority: None,
        metadata: HashMap::new(),
    };

    let result = manager.send(&spec, &context).await;
    assert!(result.is_ok());

    let contents = std::fs::read_to_string(&file_path).unwrap();
    assert!(contents.contains("wf-123"));
    assert!(contents.contains("deploy"));
    assert!(contents.contains("deployer"));
    assert!(contents.contains("success"));
    assert!(contents.contains("30s"));
}

#[tokio::test]
async fn test_file_path_interpolation() {
    let context = NotificationContext::new()
        .with_workflow_var("project", "my-app")
        .with_metadata("timestamp", "20250120");

    let manager = NotificationManager::new();

    // Use tempdir for dynamic path
    let temp_dir = tempfile::tempdir().unwrap();
    let base_path = temp_dir.path().to_string_lossy().to_string();

    let spec = NotificationSpec::Structured {
        message: "Test message".to_string(),
        channels: vec![NotificationChannel::File {
            path: format!("{}/notifications.log", base_path),
            append: false,
            timestamp: false,
            format: FileNotificationFormat::Text,
        }],
        title: None,
        priority: None,
        metadata: HashMap::new(),
    };

    let result = manager.send(&spec, &context).await;
    assert!(result.is_ok());

    // Verify file was created
    let log_path = format!("{}/notifications.log", base_path);
    assert!(std::path::Path::new(&log_path).exists());
}

#[tokio::test]
async fn test_json_lines_multiple_notifications() {
    use tempfile::NamedTempFile;

    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path().to_string_lossy().to_string();

    let manager = NotificationManager::new();

    // Send multiple notifications
    for i in 1..=3 {
        let context =
            NotificationContext::new().with_metadata("notification_id", format!("notif-{}", i));

        let spec = NotificationSpec::Structured {
            message: format!("Notification {}", i),
            channels: vec![NotificationChannel::File {
                path: file_path.clone(),
                append: true,
                timestamp: true,
                format: FileNotificationFormat::JsonLines,
            }],
            title: None,
            priority: None,
            metadata: HashMap::new(),
        };

        manager.send(&spec, &context).await.unwrap();
    }

    // Verify all lines are valid JSON
    let contents = std::fs::read_to_string(&file_path).unwrap();
    let lines: Vec<&str> = contents.trim().split('\n').collect();
    assert_eq!(lines.len(), 3);

    for line in lines {
        let parsed: serde_json::Value = serde_json::from_str(line).unwrap();
        assert!(parsed["message"].is_string());
        assert!(parsed["timestamp"].is_string());
    }
}

#[tokio::test]
async fn test_structured_notification_with_metadata() {
    use tempfile::NamedTempFile;

    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path().to_string_lossy().to_string();

    let manager = NotificationManager::new();

    let mut metadata = HashMap::new();
    metadata.insert("environment".to_string(), "production".to_string());
    metadata.insert("version".to_string(), "1.2.3".to_string());

    let context = NotificationContext::new()
        .with_metadata("environment", "production")
        .with_metadata("version", "1.2.3");

    let spec = NotificationSpec::Structured {
        message: "Deployment complete".to_string(),
        channels: vec![NotificationChannel::File {
            path: file_path.clone(),
            append: false,
            timestamp: true,
            format: FileNotificationFormat::Json,
        }],
        title: Some("Deployment Notification".to_string()),
        priority: Some(periplon_sdk::dsl::NotificationPriority::High),
        metadata,
    };

    let result = manager.send(&spec, &context).await;
    assert!(result.is_ok());

    let contents = std::fs::read_to_string(&file_path).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&contents).unwrap();

    assert_eq!(parsed["message"], "Deployment complete");
    assert!(parsed["metadata"].is_object());
    assert_eq!(parsed["metadata"]["environment"], "production");
    assert_eq!(parsed["metadata"]["version"], "1.2.3");
}

#[test]
fn test_notification_priority_serialization() {
    use periplon_sdk::dsl::NotificationPriority;

    let priorities = vec![
        (NotificationPriority::Low, "low"),
        (NotificationPriority::Normal, "normal"),
        (NotificationPriority::High, "high"),
        (NotificationPriority::Critical, "critical"),
    ];

    for (priority, expected_str) in priorities {
        let json = serde_json::to_string(&priority).unwrap();
        assert_eq!(json, format!(r#""{}""#, expected_str));

        let deserialized: NotificationPriority = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, priority);
    }
}

#[test]
fn test_file_notification_format_serialization() {
    let formats = vec![
        (FileNotificationFormat::Text, "text"),
        (FileNotificationFormat::Json, "json"),
        (FileNotificationFormat::JsonLines, "jsonlines"),
    ];

    for (format, expected_str) in formats {
        let json = serde_json::to_string(&format).unwrap();
        assert_eq!(json, format!(r#""{}""#, expected_str));

        let deserialized: FileNotificationFormat = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, format);
    }
}

#[test]
fn test_slack_channel_deserialization() {
    let json = r##"{
        "type": "slack",
        "credential": "${secret.slack_webhook}",
        "channel": "#deployments",
        "method": "webhook",
        "attachments": [
            {
                "text": "Deployment succeeded",
                "color": "good",
                "fields": [
                    {
                        "title": "Environment",
                        "value": "Production",
                        "short": true
                    }
                ]
            }
        ]
    }"##;

    let channel: NotificationChannel = serde_json::from_str(json).unwrap();

    match channel {
        NotificationChannel::Slack {
            credential,
            channel: ch,
            method,
            attachments,
        } => {
            assert_eq!(credential, "${secret.slack_webhook}");
            assert_eq!(ch, "#deployments");
            assert_eq!(method, SlackMethod::Webhook);
            assert_eq!(attachments.len(), 1);
            assert_eq!(attachments[0].text, "Deployment succeeded");
            assert_eq!(attachments[0].color, Some("good".to_string()));
            assert_eq!(attachments[0].fields.len(), 1);
        }
        _ => panic!("Expected Slack channel"),
    }
}

#[test]
fn test_discord_channel_deserialization() {
    let json = r#"{
        "type": "discord",
        "webhook_url": "https://discord.com/api/webhooks/123/abc",
        "username": "DeployBot",
        "tts": false,
        "embed": {
            "title": "Deployment Status",
            "description": "Production deployment completed",
            "color": 65280,
            "fields": [
                {
                    "name": "Version",
                    "value": "1.2.3",
                    "inline": true
                }
            ],
            "footer": "Automated notification"
        }
    }"#;

    let channel: NotificationChannel = serde_json::from_str(json).unwrap();

    match channel {
        NotificationChannel::Discord {
            webhook_url,
            username,
            tts,
            embed,
            ..
        } => {
            assert_eq!(webhook_url, "https://discord.com/api/webhooks/123/abc");
            assert_eq!(username, Some("DeployBot".to_string()));
            assert!(!tts);
            assert!(embed.is_some());

            let embed = embed.unwrap();
            assert_eq!(embed.title, Some("Deployment Status".to_string()));
            assert_eq!(embed.color, Some(65280));
            assert_eq!(embed.fields.len(), 1);
        }
        _ => panic!("Expected Discord channel"),
    }
}

#[test]
fn test_ntfy_channel_deserialization() {
    let json = r#"{
        "type": "ntfy",
        "server": "https://ntfy.sh",
        "topic": "my-topic",
        "title": "Alert",
        "priority": 4,
        "tags": ["warning", "urgent"],
        "markdown": true
    }"#;

    let channel: NotificationChannel = serde_json::from_str(json).unwrap();

    match channel {
        NotificationChannel::Ntfy {
            server,
            topic,
            title,
            priority,
            tags,
            markdown,
            ..
        } => {
            assert_eq!(server, "https://ntfy.sh");
            assert_eq!(topic, "my-topic");
            assert_eq!(title, Some("Alert".to_string()));
            assert_eq!(priority, Some(4));
            assert_eq!(tags.len(), 2);
            assert!(tags.contains(&"warning".to_string()));
            assert!(markdown);
        }
        _ => panic!("Expected Ntfy channel"),
    }
}

#[test]
fn test_notification_spec_yaml_parsing() {
    // Test simple string notification
    let yaml = r#""Task completed successfully""#;
    let spec: NotificationSpec = serde_yaml::from_str(yaml).unwrap();
    match spec {
        NotificationSpec::Simple(msg) => assert_eq!(msg, "Task completed successfully"),
        _ => panic!("Expected Simple variant"),
    }

    // Test structured notification
    let yaml = r#"
message: "Build completed"
title: "CI/CD Notification"
priority: high
channels:
  - type: console
    colored: true
    timestamp: true
"#;
    let spec: NotificationSpec = serde_yaml::from_str(yaml).unwrap();
    match spec {
        NotificationSpec::Structured {
            message,
            title,
            priority,
            channels,
            ..
        } => {
            assert_eq!(message, "Build completed");
            assert_eq!(title, Some("CI/CD Notification".to_string()));
            assert_eq!(
                priority,
                Some(periplon_sdk::dsl::NotificationPriority::High)
            );
            assert_eq!(channels.len(), 1);
        }
        _ => panic!("Expected Structured variant"),
    }
}

#[tokio::test]
async fn test_concurrent_file_writes() {
    use tempfile::NamedTempFile;
    use tokio::fs;

    // Send multiple notifications to different files to avoid conflicts
    let mut tasks = vec![];

    for i in 1..=5 {
        let temp_file_i = NamedTempFile::new().unwrap();
        let file_path_i = temp_file_i.path().to_string_lossy().to_string();

        let manager_clone = NotificationManager::new();
        let context = NotificationContext::new().with_metadata("id", format!("{}", i));

        let spec = NotificationSpec::Structured {
            message: format!("Concurrent notification {}", i),
            channels: vec![NotificationChannel::File {
                path: file_path_i.clone(),
                append: false,
                timestamp: false,
                format: FileNotificationFormat::Text,
            }],
            title: None,
            priority: None,
            metadata: HashMap::new(),
        };

        let task = tokio::spawn(async move {
            manager_clone.send(&spec, &context).await.unwrap();
            file_path_i
        });

        tasks.push(task);
    }

    // Wait for all tasks
    let results = futures::future::join_all(tasks).await;

    // Verify all files were written
    for result in results {
        let path = result.unwrap();
        let contents = fs::read_to_string(&path).await.unwrap();
        assert!(contents.contains("Concurrent notification"));
    }
}

#[tokio::test]
async fn test_error_on_missing_file_parent_dir() {
    let manager = NotificationManager::new();
    let context = NotificationContext::new();

    let spec = NotificationSpec::Structured {
        message: "Test".to_string(),
        channels: vec![NotificationChannel::File {
            path: "/nonexistent/path/to/file.log".to_string(),
            append: false,
            timestamp: false,
            format: FileNotificationFormat::Text,
        }],
        title: None,
        priority: None,
        metadata: HashMap::new(),
    };

    let result = manager.send(&spec, &context).await;
    assert!(result.is_err());
}

#[test]
fn test_notification_context_clone() {
    let context = NotificationContext::new()
        .with_workflow_var("key", "value")
        .with_task_var("task", "test");

    let cloned = context.clone();

    assert_eq!(context.workflow_vars, cloned.workflow_vars);
    assert_eq!(context.task_vars, cloned.task_vars);
}

#[test]
fn test_notification_manager_custom_sender() {
    use async_trait::async_trait;
    use periplon_sdk::dsl::{NotificationResult, NotificationSender};

    struct CustomSender;

    #[async_trait]
    impl NotificationSender for CustomSender {
        async fn send(
            &self,
            _message: &str,
            _channel: &NotificationChannel,
            _context: &NotificationContext,
        ) -> NotificationResult<()> {
            Ok(())
        }

        fn channel_name(&self) -> &str {
            "custom"
        }
    }

    let mut manager = NotificationManager::new();
    manager.register_sender("custom".to_string(), Box::new(CustomSender));

    assert!(manager.has_sender("custom"));
}

#[tokio::test]
async fn test_notification_with_empty_message() {
    let manager = NotificationManager::new();
    let context = NotificationContext::new();

    let spec = NotificationSpec::Simple("".to_string());

    // Should succeed with empty message
    let result = manager.send(&spec, &context).await;
    assert!(result.is_ok());
}

#[test]
fn test_interpolation_with_unicode() {
    let context = NotificationContext::new()
        .with_workflow_var("emoji", "")
        .with_task_var("lang", "日本語");

    let result = context
        .interpolate("Status: ${workflow.emoji} Language: ${task.lang}")
        .unwrap();
    assert_eq!(result, "Status: ✅ Language: 日本語");
}