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
//! CLI Group Management Commands Integration Tests
//!
//! Tests the `periplon-executor group` subcommands:
//! - group list
//! - group install
//! - group update
//! - group validate

#![allow(clippy::unnecessary_unwrap)]

use std::fs;
use std::path::PathBuf;
use std::process::Command;
use tempfile::TempDir;

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

/// Get the path to the periplon-executor binary
fn get_dsl_executor_path() -> PathBuf {
    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    path.push("target");

    // Binary name differs on Windows
    let binary_name = if cfg!(windows) {
        "periplon-executor.exe"
    } else {
        "periplon-executor"
    };

    // Try release first, then debug
    let release_path = path.join("release").join(binary_name);
    if release_path.exists() {
        return release_path;
    }

    path.join("debug").join(binary_name)
}

/// Check if periplon-executor binary exists (built)
fn is_binary_available() -> bool {
    get_dsl_executor_path().exists()
}

/// Create a minimal task YAML for testing
fn create_test_task(dir: &std::path::Path, name: &str, version: &str) -> PathBuf {
    let content = format!(
        r#"apiVersion: "task/v1"
kind: "PredefinedTask"
metadata:
  name: "{}"
  version: "{}"
  description: "Test task"
spec:
  agent_template:
    description: "Test agent"
    tools: ["Read"]
"#,
        name, version
    );

    let file_path = dir.join(format!("{}.task.yaml", name));
    fs::write(&file_path, content).unwrap();
    file_path
}

/// Create a test task group YAML
fn create_test_group(
    dir: &std::path::Path,
    name: &str,
    version: &str,
    tasks: Vec<(&str, &str)>,
) -> PathBuf {
    let tasks_yaml: Vec<String> = tasks
        .iter()
        .map(|(task_name, task_version)| {
            format!(
                r#"  - name: "{}"
    version: "{}""#,
                task_name, task_version
            )
        })
        .collect();

    let content = format!(
        r#"apiVersion: "taskgroup/v1"
kind: "TaskGroup"
metadata:
  name: "{}"
  version: "{}"
  description: "Test group for CLI testing"
spec:
  tasks:
{}
"#,
        name,
        version,
        tasks_yaml.join("\n")
    );

    let file_path = dir.join(format!("{}.taskgroup.yaml", name));
    fs::write(&file_path, content).unwrap();
    file_path
}

// ============================================================================
// SECTION 1: Group List Command Tests
// ============================================================================

#[test]
fn test_group_list_empty_directory() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let groups_dir = temp_dir.path().join("groups");
    fs::create_dir_all(&groups_dir).unwrap();

    let output = Command::new(get_dsl_executor_path())
        .args(["group", "list", "--path", groups_dir.to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

    // Should succeed even with no groups
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("No task groups found") || stdout.contains("groups: []"));
}

#[test]
fn test_group_list_with_groups() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let groups_dir = temp_dir.path().join("groups");
    let tasks_dir = temp_dir.path().join("tasks");
    fs::create_dir_all(&groups_dir).unwrap();
    fs::create_dir_all(&tasks_dir).unwrap();

    // Create test tasks
    create_test_task(&tasks_dir, "task1", "1.0.0");
    create_test_task(&tasks_dir, "task2", "1.0.0");

    // Create test groups
    create_test_group(
        &groups_dir,
        "test-group-1",
        "1.0.0",
        vec![("task1", "1.0.0")],
    );
    create_test_group(
        &groups_dir,
        "test-group-2",
        "2.0.0",
        vec![("task2", "1.0.0")],
    );

    let output = Command::new(get_dsl_executor_path())
        .args(["group", "list", "--path", groups_dir.to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("test-group-1") || stdout.contains("test-group-1@1.0.0"));
    assert!(stdout.contains("test-group-2") || stdout.contains("test-group-2@2.0.0"));
}

#[test]
fn test_group_list_json_output() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let groups_dir = temp_dir.path().join("groups");
    let tasks_dir = temp_dir.path().join("tasks");
    fs::create_dir_all(&groups_dir).unwrap();
    fs::create_dir_all(&tasks_dir).unwrap();

    create_test_task(&tasks_dir, "task1", "1.0.0");
    create_test_group(&groups_dir, "json-test", "1.0.0", vec![("task1", "1.0.0")]);

    let output = Command::new(get_dsl_executor_path())
        .args([
            "group",
            "list",
            "--path",
            groups_dir.to_str().unwrap(),
            "--json",
        ])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should be valid JSON
    let json_result: Result<serde_json::Value, _> = serde_json::from_str(&stdout);
    assert!(json_result.is_ok(), "Output should be valid JSON");

    if let Ok(json) = json_result {
        assert!(json.get("search_paths").is_some());
        assert!(json.get("groups").is_some());
        assert!(json.get("total").is_some());
    }
}

#[test]
fn test_group_list_verbose() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let groups_dir = temp_dir.path().join("groups");
    let tasks_dir = temp_dir.path().join("tasks");
    fs::create_dir_all(&groups_dir).unwrap();
    fs::create_dir_all(&tasks_dir).unwrap();

    create_test_task(&tasks_dir, "task1", "1.0.0");
    create_test_group(
        &groups_dir,
        "verbose-test",
        "1.0.0",
        vec![("task1", "1.0.0")],
    );

    let output = Command::new(get_dsl_executor_path())
        .args([
            "group",
            "list",
            "--path",
            groups_dir.to_str().unwrap(),
            "--verbose",
        ])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Verbose mode should show more details like task count
    assert!(stdout.contains("task") || stdout.contains("Task"));
}

// ============================================================================
// SECTION 2: Group Validate Command Tests
// ============================================================================

#[test]
fn test_group_validate_valid_group() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let tasks_dir = temp_dir.path().join("tasks");
    fs::create_dir_all(&tasks_dir).unwrap();

    // Create task first
    create_test_task(&tasks_dir, "valid-task", "1.0.0");

    // Create group file
    let group_file = create_test_group(
        temp_dir.path(),
        "valid-group",
        "1.0.0",
        vec![("valid-task", "1.0.0")],
    );

    // Set up task path environment (if needed by loader)
    let output = Command::new(get_dsl_executor_path())
        .args(["group", "validate", group_file.to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    // The command might fail if tasks aren't in the standard paths,
    // but it should at least parse the group file structure
    if !output.status.success() {
        // Check if it's a "task not found" error, which means parsing succeeded
        assert!(
            stderr.contains("not found") || stderr.contains("Task"),
            "Should fail with task not found, not a parse error"
        );
    } else {
        assert!(stdout.contains("valid") || stdout.contains("Task group is valid"));
    }
}

#[test]
fn test_group_validate_invalid_yaml() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let temp_dir = TempDir::new().unwrap();

    // Create invalid YAML file
    let invalid_file = temp_dir.path().join("invalid.taskgroup.yaml");
    fs::write(
        &invalid_file,
        r#"
apiVersion: "taskgroup/v1"
kind: "TaskGroup"
metadata:
  name: "invalid"
  version: "1.0.0"
spec:
  # Missing tasks field - invalid!
"#,
    )
    .unwrap();

    let output = Command::new(get_dsl_executor_path())
        .args(["group", "validate", invalid_file.to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

    // Should fail
    assert!(!output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("Error") || stderr.contains("error"));
}

#[test]
fn test_group_validate_json_output() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let group_file = create_test_group(
        temp_dir.path(),
        "json-validate",
        "1.0.0",
        vec![("some-task", "1.0.0")],
    );

    let output = Command::new(get_dsl_executor_path())
        .args(["group", "validate", group_file.to_str().unwrap(), "--json"])
        .output()
        .expect("Failed to execute command");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Even if validation fails, JSON output should be valid JSON
    if !stdout.is_empty() {
        let json_result: Result<serde_json::Value, _> = serde_json::from_str(&stdout);
        if json_result.is_ok() {
            let json = json_result.unwrap();
            assert!(json.get("valid").is_some());
            assert!(json.get("group_name").is_some());
            assert!(json.get("group_version").is_some());
        }
    }
}

// ============================================================================
// SECTION 3: Group Install Command Tests
// ============================================================================

#[test]
fn test_group_install_invalid_reference() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let output = Command::new(get_dsl_executor_path())
        .args(["group", "install", "invalid-ref-format"])
        .output()
        .expect("Failed to execute command");

    // Should fail with invalid reference
    assert!(!output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("Invalid") || stderr.contains("Error"));
}

#[test]
fn test_group_install_nonexistent_group() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let output = Command::new(get_dsl_executor_path())
        .args(["group", "install", "nonexistent-group@1.0.0"])
        .output()
        .expect("Failed to execute command");

    // Should fail - group doesn't exist
    assert!(!output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("not found") || stderr.contains("Error"));
}

// ============================================================================
// SECTION 4: Group Update Command Tests
// ============================================================================

#[test]
fn test_group_update_all() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    // Update all groups (even if none exist, should not fail)
    let output = Command::new(get_dsl_executor_path())
        .args(["group", "update"])
        .output()
        .expect("Failed to execute command");

    // Should succeed (or at least not crash)
    // Note: May succeed or fail depending on whether standard paths have groups
    let _stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Just verify the command runs without panicking
    assert!(
        output.status.success() || stderr.contains("Error"),
        "Command should either succeed or fail gracefully"
    );
}

#[test]
fn test_group_update_json_output() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let output = Command::new(get_dsl_executor_path())
        .args(["group", "update", "--json"])
        .output()
        .expect("Failed to execute command");

    if output.status.success() {
        let stdout = String::from_utf8_lossy(&output.stdout);

        // Should be valid JSON
        let json_result: Result<serde_json::Value, _> = serde_json::from_str(&stdout);
        if json_result.is_ok() {
            let json = json_result.unwrap();
            assert!(json.get("success").is_some());
            assert!(json.get("updated_count").is_some());
        }
    }
}

#[test]
fn test_group_update_force() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let output = Command::new(get_dsl_executor_path())
        .args(["group", "update", "--force"])
        .output()
        .expect("Failed to execute command");

    // Force flag should be accepted
    // Success depends on whether groups are available
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should not have argument parsing errors
    assert!(
        !stderr.contains("unexpected argument") && !stderr.contains("unrecognized"),
        "Force flag should be recognized"
    );
}

// ============================================================================
// SECTION 5: Command Help Tests
// ============================================================================

#[test]
fn test_group_command_help() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let output = Command::new(get_dsl_executor_path())
        .args(["group", "--help"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("list"));
    assert!(stdout.contains("install"));
    assert!(stdout.contains("update"));
    assert!(stdout.contains("validate"));
}

#[test]
fn test_group_list_help() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let output = Command::new(get_dsl_executor_path())
        .args(["group", "list", "--help"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("verbose") || stdout.contains("--verbose"));
    assert!(stdout.contains("json") || stdout.contains("--json"));
    assert!(stdout.contains("path") || stdout.contains("--path"));
}

#[test]
fn test_group_validate_help() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let output = Command::new(get_dsl_executor_path())
        .args(["group", "validate", "--help"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("GROUP_FILE") || stdout.contains("group-file"));
    assert!(stdout.contains("verbose") || stdout.contains("--verbose"));
}

// ============================================================================
// SECTION 6: Integration Tests
// ============================================================================

#[test]
fn test_group_workflow_list_validate_sequence() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let groups_dir = temp_dir.path().join("groups");
    let tasks_dir = temp_dir.path().join("tasks");
    fs::create_dir_all(&groups_dir).unwrap();
    fs::create_dir_all(&tasks_dir).unwrap();

    // Create task
    create_test_task(&tasks_dir, "integration-task", "1.0.0");

    // Create group
    let group_file = create_test_group(
        &groups_dir,
        "integration-group",
        "1.0.0",
        vec![("integration-task", "1.0.0")],
    );

    // Step 1: List groups
    let list_output = Command::new(get_dsl_executor_path())
        .args(["group", "list", "--path", groups_dir.to_str().unwrap()])
        .output()
        .expect("Failed to list groups");

    assert!(list_output.status.success());
    let list_stdout = String::from_utf8_lossy(&list_output.stdout);
    assert!(list_stdout.contains("integration-group"));

    // Step 2: Validate the group
    let validate_output = Command::new(get_dsl_executor_path())
        .args(["group", "validate", group_file.to_str().unwrap()])
        .output()
        .expect("Failed to validate group");

    // Validation might fail if task not in standard path, but should parse correctly
    let validate_stderr = String::from_utf8_lossy(&validate_output.stderr);

    // Should not have YAML parsing errors
    assert!(
        !validate_stderr.contains("YAML") && !validate_stderr.contains("parse"),
        "Should not have YAML parsing errors"
    );
}

#[test]
fn test_multiple_groups_discovery() {
    if !is_binary_available() {
        eprintln!("Skipping test: periplon-executor binary not built");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let groups_dir = temp_dir.path().join("groups");
    let tasks_dir = temp_dir.path().join("tasks");
    fs::create_dir_all(&groups_dir).unwrap();
    fs::create_dir_all(&tasks_dir).unwrap();

    // Create multiple tasks
    create_test_task(&tasks_dir, "task-a", "1.0.0");
    create_test_task(&tasks_dir, "task-b", "1.0.0");
    create_test_task(&tasks_dir, "task-c", "1.0.0");

    // Create multiple groups
    create_test_group(
        &groups_dir,
        "group-alpha",
        "1.0.0",
        vec![("task-a", "1.0.0")],
    );
    create_test_group(
        &groups_dir,
        "group-beta",
        "2.0.0",
        vec![("task-b", "1.0.0")],
    );
    create_test_group(
        &groups_dir,
        "group-gamma",
        "3.0.0",
        vec![("task-c", "1.0.0")],
    );

    let output = Command::new(get_dsl_executor_path())
        .args(["group", "list", "--path", groups_dir.to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should discover all three groups
    let has_alpha = stdout.contains("group-alpha") || stdout.contains("group-alpha@1.0.0");
    let has_beta = stdout.contains("group-beta") || stdout.contains("group-beta@2.0.0");
    let has_gamma = stdout.contains("group-gamma") || stdout.contains("group-gamma@3.0.0");

    assert!(has_alpha, "Should find group-alpha");
    assert!(has_beta, "Should find group-beta");
    assert!(has_gamma, "Should find group-gamma");
}