|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// TestAddCommandRequiresArguments verifies that the add command requires at least one argument |
| 10 | +func TestAddCommandRequiresArguments(t *testing.T) { |
| 11 | + // Save current directory |
| 12 | + originalDir, err := os.Getwd() |
| 13 | + if err != nil { |
| 14 | + t.Fatalf("Failed to get current directory: %v", err) |
| 15 | + } |
| 16 | + defer func() { |
| 17 | + // Restore original directory |
| 18 | + if err := os.Chdir(originalDir); err != nil { |
| 19 | + t.Logf("Warning: Failed to restore directory: %v", err) |
| 20 | + } |
| 21 | + }() |
| 22 | + |
| 23 | + // Create a temporary directory for testing |
| 24 | + tmpDir := t.TempDir() |
| 25 | + if err := os.Chdir(tmpDir); err != nil { |
| 26 | + t.Fatalf("Failed to change to temp dir: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + // Initialize git repo |
| 30 | + if err := os.MkdirAll(".git", 0755); err != nil { |
| 31 | + t.Fatalf("Failed to create .git dir: %v", err) |
| 32 | + } |
| 33 | + |
| 34 | + // Create add command |
| 35 | + validateEngine := func(engine string) error { return nil } |
| 36 | + cmd := NewAddCommand(validateEngine) |
| 37 | + |
| 38 | + // Set up stderr capture |
| 39 | + stderr := &bytes.Buffer{} |
| 40 | + cmd.SetErr(stderr) |
| 41 | + |
| 42 | + // Try to execute without arguments |
| 43 | + cmd.SetArgs([]string{}) |
| 44 | + |
| 45 | + // Execute and expect an error |
| 46 | + err = cmd.Execute() |
| 47 | + if err == nil { |
| 48 | + t.Error("Expected error when calling add without arguments, got nil") |
| 49 | + } |
| 50 | + |
| 51 | + // Verify the error message mentions required arguments |
| 52 | + errMsg := err.Error() |
| 53 | + if errMsg != "requires at least 1 arg(s), only received 0" { |
| 54 | + t.Errorf("Expected 'requires at least 1 arg(s)' error, got: %s", errMsg) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// TestAddCommandWithWorkflow verifies that add command works with a workflow specification |
| 59 | +func TestAddCommandWithWorkflow(t *testing.T) { |
| 60 | + // This test verifies that the command accepts arguments correctly |
| 61 | + // We don't actually execute it since that would require network access |
| 62 | + |
| 63 | + validateEngine := func(engine string) error { return nil } |
| 64 | + cmd := NewAddCommand(validateEngine) |
| 65 | + |
| 66 | + // Verify command accepts arguments |
| 67 | + if cmd.Args == nil { |
| 68 | + t.Error("Add command should have Args validator") |
| 69 | + } |
| 70 | + |
| 71 | + // Test Args validator directly |
| 72 | + argsValidator := cmd.Args |
| 73 | + if argsValidator != nil { |
| 74 | + // Should accept one argument |
| 75 | + if err := argsValidator(cmd, []string{"githubnext/agentics/ci-doctor"}); err != nil { |
| 76 | + t.Errorf("Command should accept one workflow argument, got error: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + // Should accept multiple arguments |
| 80 | + if err := argsValidator(cmd, []string{"githubnext/agentics/ci-doctor", "githubnext/agentics/daily-plan"}); err != nil { |
| 81 | + t.Errorf("Command should accept multiple workflow arguments, got error: %v", err) |
| 82 | + } |
| 83 | + |
| 84 | + // Should reject empty arguments |
| 85 | + if err := argsValidator(cmd, []string{}); err == nil { |
| 86 | + t.Error("Command should reject empty arguments, but no error was returned") |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// TestAddCommandHelpText verifies the help text mentions the new command for creating workflows |
| 92 | +func TestAddCommandHelpText(t *testing.T) { |
| 93 | + validateEngine := func(engine string) error { return nil } |
| 94 | + cmd := NewAddCommand(validateEngine) |
| 95 | + |
| 96 | + // Check that the long description mentions the 'new' command |
| 97 | + longDesc := cmd.Long |
| 98 | + if longDesc == "" { |
| 99 | + t.Error("Add command should have a long description") |
| 100 | + } |
| 101 | + |
| 102 | + // Verify the note about using 'new' command is present |
| 103 | + expectedNote := "Note: To create a new workflow from scratch, use the 'new' command instead." |
| 104 | + if len(longDesc) > 0 { |
| 105 | + found := false |
| 106 | + for i := 0; i <= len(longDesc)-len(expectedNote); i++ { |
| 107 | + if longDesc[i:i+len(expectedNote)] == expectedNote { |
| 108 | + found = true |
| 109 | + break |
| 110 | + } |
| 111 | + } |
| 112 | + if !found { |
| 113 | + t.Errorf("Add command help text should mention 'new' command") |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// TestNewCommandAcceptsOptionalArgument verifies that new command accepts optional workflow name |
| 119 | +func TestNewCommandAcceptsOptionalArgument(t *testing.T) { |
| 120 | + // Note: We can't easily test the actual command defined in main.go from here, |
| 121 | + // but we can document the expected behavior |
| 122 | + |
| 123 | + // The new command should: |
| 124 | + // 1. Accept zero arguments (interactive mode) |
| 125 | + // 2. Accept one argument (template mode with workflow name) |
| 126 | + // 3. Reject more than one argument |
| 127 | + |
| 128 | + // This is enforced by cobra.MaximumNArgs(1) in main.go |
| 129 | +} |
0 commit comments