|
| 1 | +// Package tool contains integration tests for the tool command |
| 2 | +// |
| 3 | +// Test Assumptions and Limitations: |
| 4 | +// 1. These tests are integration tests that execute the CLI commands as external processes |
| 5 | +// 2. Tests use the --dry-run flag when possible to avoid actual resource creation |
| 6 | +// 3. Tests are designed to be resilient to different environments (CI, local dev) |
| 7 | +// 4. The tests focus on command execution rather than specific output validation |
| 8 | +// 5. Some tests may pass even if the underlying functionality has issues, as they |
| 9 | +// primarily test that the command doesn't panic or crash |
| 10 | +package tool |
| 11 | + |
| 12 | +import ( |
| 13 | + "os" |
| 14 | + "os/exec" |
| 15 | + "strings" |
| 16 | + "testing" |
| 17 | +) |
| 18 | + |
| 19 | +// TestToolCreate tests the tool create command |
| 20 | +func TestToolCreate(t *testing.T) { |
| 21 | + // Create a valid YAML file for testing |
| 22 | + validYAML := `--- |
| 23 | +apiVersion: maestro/v1alpha1 |
| 24 | +kind: MCPTool |
| 25 | +metadata: |
| 26 | + name: test-tool |
| 27 | +spec: |
| 28 | + description: "Test tool for unit tests" |
| 29 | + parameters: |
| 30 | + - name: param1 |
| 31 | + description: "A test parameter" |
| 32 | + required: true |
| 33 | + type: string |
| 34 | + returns: |
| 35 | + description: "Test return value" |
| 36 | + type: string |
| 37 | +` |
| 38 | + |
| 39 | + tempFile := createTempFile(t, "valid-tool-*.yaml", validYAML) |
| 40 | + defer os.Remove(tempFile) |
| 41 | + |
| 42 | + cmd := exec.Command("../../../maestro", "tool", "create", tempFile, "--dry-run") |
| 43 | + output, err := cmd.CombinedOutput() |
| 44 | + |
| 45 | + outputStr := string(output) |
| 46 | + |
| 47 | + // This test is expected to fail if no MCP server is running |
| 48 | + if err != nil { |
| 49 | + // Check if the error is due to MCP server not being available |
| 50 | + if strings.Contains(outputStr, "MCP server could not be reached") { |
| 51 | + t.Logf("Test skipped: No MCP server running (expected): %s", outputStr) |
| 52 | + return |
| 53 | + } |
| 54 | + t.Fatalf("Tool create command failed with unexpected error: %v, output: %s", err, string(output)) |
| 55 | + } |
| 56 | + |
| 57 | + if !strings.Contains(outputStr, "Creating MCP tools from YAML configuration") { |
| 58 | + t.Errorf("Should show MCP tools creation message, got: %s", outputStr) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// TestToolCreateWithNonExistentFile tests with non-existent file |
| 63 | +func TestToolCreateWithNonExistentFile(t *testing.T) { |
| 64 | + cmd := exec.Command("../../../maestro", "tool", "create", "nonexistent.yaml") |
| 65 | + output, err := cmd.CombinedOutput() |
| 66 | + |
| 67 | + outputStr := string(output) |
| 68 | + |
| 69 | + // Should fail with non-existent file |
| 70 | + if err == nil { |
| 71 | + t.Error("Tool create command should fail with non-existent file") |
| 72 | + } |
| 73 | + |
| 74 | + if !strings.Contains(outputStr, "no such file or directory") { |
| 75 | + t.Errorf("Error message should mention file not found, got: %s", outputStr) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// TestToolCreateWithInvalidYAML tests with invalid YAML |
| 80 | +func TestToolCreateWithInvalidYAML(t *testing.T) { |
| 81 | + // Create an invalid YAML file |
| 82 | + invalidYAML := `--- |
| 83 | +apiVersion: maestro/v1alpha1 |
| 84 | +kind: MCPTool |
| 85 | +metadata: |
| 86 | + name: test-tool |
| 87 | +spec: |
| 88 | + description: "Test tool with invalid YAML |
| 89 | + parameters: |
| 90 | + - name: param1 |
| 91 | + description: "A test parameter" |
| 92 | + required: true |
| 93 | + type: string |
| 94 | +` |
| 95 | + |
| 96 | + tempFile := createTempFile(t, "invalid-tool-*.yaml", invalidYAML) |
| 97 | + defer os.Remove(tempFile) |
| 98 | + |
| 99 | + cmd := exec.Command("../../../maestro", "tool", "create", tempFile) |
| 100 | + output, err := cmd.CombinedOutput() |
| 101 | + |
| 102 | + outputStr := string(output) |
| 103 | + |
| 104 | + // Should fail with invalid YAML |
| 105 | + if err == nil { |
| 106 | + t.Error("Tool create command should fail with invalid YAML") |
| 107 | + } |
| 108 | + |
| 109 | + if !strings.Contains(outputStr, "no valid YAML documents found") { |
| 110 | + t.Errorf("Error message should mention YAML parsing error, got: %s", outputStr) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// Helper function to create a temporary file with content |
| 115 | +func createTempFile(t *testing.T, pattern string, content string) string { |
| 116 | + tmpfile, err := os.CreateTemp("", pattern) |
| 117 | + if err != nil { |
| 118 | + t.Fatalf("Failed to create temp file: %v", err) |
| 119 | + } |
| 120 | + |
| 121 | + if _, err := tmpfile.Write([]byte(content)); err != nil { |
| 122 | + t.Fatalf("Failed to write to temp file: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + if err := tmpfile.Close(); err != nil { |
| 126 | + t.Fatalf("Failed to close temp file: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + return tmpfile.Name() |
| 130 | +} |
| 131 | + |
| 132 | +// Made with Bob |
0 commit comments