|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/githubnext/gh-aw/pkg/constants" |
| 10 | +) |
| 11 | + |
| 12 | +func TestLoadWorkflowMCPConfigs(t *testing.T) { |
| 13 | + // Create a temporary directory for test workflows |
| 14 | + tmpDir := t.TempDir() |
| 15 | + workflowsDir := filepath.Join(tmpDir, constants.GetWorkflowDir()) |
| 16 | + err := os.MkdirAll(workflowsDir, 0755) |
| 17 | + if err != nil { |
| 18 | + t.Fatalf("Failed to create test directory: %v", err) |
| 19 | + } |
| 20 | + |
| 21 | + // Create a test workflow with MCP servers |
| 22 | + testWorkflowContent := `--- |
| 23 | +on: |
| 24 | + workflow_dispatch: |
| 25 | +
|
| 26 | +permissions: read-all |
| 27 | +
|
| 28 | +safe-outputs: |
| 29 | + create-issue: |
| 30 | + title-prefix: "[Test] " |
| 31 | +
|
| 32 | +tools: |
| 33 | + github: |
| 34 | + mcp: |
| 35 | + type: stdio |
| 36 | + command: "npx" |
| 37 | + args: ["@github/github-mcp-server"] |
| 38 | + allowed: ["create_issue"] |
| 39 | +
|
| 40 | +mcp-servers: |
| 41 | + test-server: |
| 42 | + type: stdio |
| 43 | + command: "node" |
| 44 | + args: ["test-server.js"] |
| 45 | + allowed: ["test_tool_1", "test_tool_2"] |
| 46 | +
|
| 47 | +--- |
| 48 | +
|
| 49 | +# Test Workflow |
| 50 | +This is a test workflow.` |
| 51 | + |
| 52 | + testWorkflowPath := filepath.Join(workflowsDir, "test-workflow.md") |
| 53 | + err = os.WriteFile(testWorkflowPath, []byte(testWorkflowContent), 0644) |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("Failed to create test workflow file: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + t.Run("load_workflow_with_no_filter", func(t *testing.T) { |
| 59 | + workflowData, mcpConfigs, err := loadWorkflowMCPConfigs(testWorkflowPath, "") |
| 60 | + if err != nil { |
| 61 | + t.Errorf("loadWorkflowMCPConfigs() error = %v", err) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + if workflowData == nil { |
| 66 | + t.Error("Expected workflowData to be non-nil") |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + if workflowData.Frontmatter == nil { |
| 71 | + t.Error("Expected workflowData.Frontmatter to be non-nil") |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + // Should find multiple MCP servers (github, test-server, safe-outputs) |
| 76 | + if len(mcpConfigs) < 2 { |
| 77 | + t.Errorf("Expected at least 2 MCP servers, got %d", len(mcpConfigs)) |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + t.Run("load_workflow_with_server_filter", func(t *testing.T) { |
| 82 | + workflowData, mcpConfigs, err := loadWorkflowMCPConfigs(testWorkflowPath, "github") |
| 83 | + if err != nil { |
| 84 | + t.Errorf("loadWorkflowMCPConfigs() error = %v", err) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + if workflowData == nil { |
| 89 | + t.Error("Expected workflowData to be non-nil") |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + // Should find only the github MCP server |
| 94 | + found := false |
| 95 | + for _, config := range mcpConfigs { |
| 96 | + if strings.EqualFold(config.Name, "github") { |
| 97 | + found = true |
| 98 | + break |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if !found { |
| 103 | + t.Error("Expected to find 'github' MCP server with filter") |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + t.Run("load_nonexistent_workflow", func(t *testing.T) { |
| 108 | + nonexistentPath := filepath.Join(workflowsDir, "nonexistent.md") |
| 109 | + _, _, err := loadWorkflowMCPConfigs(nonexistentPath, "") |
| 110 | + if err == nil { |
| 111 | + t.Error("Expected error for nonexistent workflow, got nil") |
| 112 | + } |
| 113 | + if !strings.Contains(err.Error(), "failed to read workflow file") { |
| 114 | + t.Errorf("Expected 'failed to read workflow file' error, got: %v", err) |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + t.Run("load_workflow_with_invalid_frontmatter", func(t *testing.T) { |
| 119 | + invalidWorkflowContent := `--- |
| 120 | +invalid: yaml: content: [ |
| 121 | +--- |
| 122 | +# Invalid Workflow` |
| 123 | + |
| 124 | + invalidWorkflowPath := filepath.Join(workflowsDir, "invalid-workflow.md") |
| 125 | + err = os.WriteFile(invalidWorkflowPath, []byte(invalidWorkflowContent), 0644) |
| 126 | + if err != nil { |
| 127 | + t.Fatalf("Failed to create invalid workflow file: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + _, _, err = loadWorkflowMCPConfigs(invalidWorkflowPath, "") |
| 131 | + if err == nil { |
| 132 | + t.Error("Expected error for invalid frontmatter, got nil") |
| 133 | + } |
| 134 | + if !strings.Contains(err.Error(), "failed to parse workflow file") { |
| 135 | + t.Errorf("Expected 'failed to parse workflow file' error, got: %v", err) |
| 136 | + } |
| 137 | + }) |
| 138 | + |
| 139 | + t.Run("load_workflow_without_mcp_servers", func(t *testing.T) { |
| 140 | + noMCPWorkflowContent := `--- |
| 141 | +on: |
| 142 | + push: |
| 143 | +permissions: read-all |
| 144 | +--- |
| 145 | +# No MCP Workflow` |
| 146 | + |
| 147 | + noMCPWorkflowPath := filepath.Join(workflowsDir, "no-mcp-workflow.md") |
| 148 | + err = os.WriteFile(noMCPWorkflowPath, []byte(noMCPWorkflowContent), 0644) |
| 149 | + if err != nil { |
| 150 | + t.Fatalf("Failed to create no-MCP workflow file: %v", err) |
| 151 | + } |
| 152 | + |
| 153 | + workflowData, mcpConfigs, err := loadWorkflowMCPConfigs(noMCPWorkflowPath, "") |
| 154 | + if err != nil { |
| 155 | + t.Errorf("loadWorkflowMCPConfigs() error = %v", err) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + if workflowData == nil { |
| 160 | + t.Error("Expected workflowData to be non-nil") |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + // Should return empty list, not error |
| 165 | + if len(mcpConfigs) != 0 { |
| 166 | + t.Errorf("Expected 0 MCP servers, got %d", len(mcpConfigs)) |
| 167 | + } |
| 168 | + }) |
| 169 | +} |
0 commit comments