|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// TestValidateEngine tests the validateEngine function |
| 9 | +func TestValidateEngine(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + engineID string |
| 13 | + expectError bool |
| 14 | + errorMsg string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "empty engine ID is valid (uses default)", |
| 18 | + engineID: "", |
| 19 | + expectError: false, |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "copilot engine is valid", |
| 23 | + engineID: "copilot", |
| 24 | + expectError: false, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "claude engine is valid", |
| 28 | + engineID: "claude", |
| 29 | + expectError: false, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "codex engine is valid", |
| 33 | + engineID: "codex", |
| 34 | + expectError: false, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "custom engine is valid", |
| 38 | + engineID: "custom", |
| 39 | + expectError: false, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "invalid engine ID", |
| 43 | + engineID: "invalid-engine", |
| 44 | + expectError: true, |
| 45 | + errorMsg: "no engine found matching prefix", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "unknown engine ID", |
| 49 | + engineID: "gpt-7", |
| 50 | + expectError: true, |
| 51 | + errorMsg: "no engine found matching prefix", |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tt := range tests { |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + compiler := NewCompiler(false, "", "") |
| 58 | + err := compiler.validateEngine(tt.engineID) |
| 59 | + |
| 60 | + if tt.expectError && err == nil { |
| 61 | + t.Error("Expected validation to fail but it succeeded") |
| 62 | + } else if !tt.expectError && err != nil { |
| 63 | + t.Errorf("Expected validation to succeed but it failed: %v", err) |
| 64 | + } else if tt.expectError && err != nil && tt.errorMsg != "" { |
| 65 | + if !strings.Contains(err.Error(), tt.errorMsg) { |
| 66 | + t.Errorf("Expected error containing '%s', got '%s'", tt.errorMsg, err.Error()) |
| 67 | + } |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// TestValidateSingleEngineSpecification tests the validateSingleEngineSpecification function |
| 74 | +func TestValidateSingleEngineSpecification(t *testing.T) { |
| 75 | + tests := []struct { |
| 76 | + name string |
| 77 | + mainEngineSetting string |
| 78 | + includedEnginesJSON []string |
| 79 | + expectedEngine string |
| 80 | + expectError bool |
| 81 | + errorMsg string |
| 82 | + }{ |
| 83 | + { |
| 84 | + name: "no engine specified anywhere", |
| 85 | + mainEngineSetting: "", |
| 86 | + includedEnginesJSON: []string{}, |
| 87 | + expectedEngine: "", |
| 88 | + expectError: false, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "engine only in main workflow", |
| 92 | + mainEngineSetting: "copilot", |
| 93 | + includedEnginesJSON: []string{}, |
| 94 | + expectedEngine: "copilot", |
| 95 | + expectError: false, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "engine only in included file (string format)", |
| 99 | + mainEngineSetting: "", |
| 100 | + includedEnginesJSON: []string{`"claude"`}, |
| 101 | + expectedEngine: "claude", |
| 102 | + expectError: false, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "engine only in included file (object format)", |
| 106 | + mainEngineSetting: "", |
| 107 | + includedEnginesJSON: []string{`{"id": "codex", "model": "gpt-4"}`}, |
| 108 | + expectedEngine: "codex", |
| 109 | + expectError: false, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "multiple engines in main and included", |
| 113 | + mainEngineSetting: "copilot", |
| 114 | + includedEnginesJSON: []string{`"claude"`}, |
| 115 | + expectedEngine: "", |
| 116 | + expectError: true, |
| 117 | + errorMsg: "multiple engine fields found", |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "multiple engines in different included files", |
| 121 | + mainEngineSetting: "", |
| 122 | + includedEnginesJSON: []string{`"copilot"`, `"claude"`}, |
| 123 | + expectedEngine: "", |
| 124 | + expectError: true, |
| 125 | + errorMsg: "multiple engine fields found", |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "empty string in main engine setting", |
| 129 | + mainEngineSetting: "", |
| 130 | + includedEnginesJSON: []string{}, |
| 131 | + expectedEngine: "", |
| 132 | + expectError: false, |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "empty strings in included engines are ignored", |
| 136 | + mainEngineSetting: "copilot", |
| 137 | + includedEnginesJSON: []string{"", ""}, |
| 138 | + expectedEngine: "copilot", |
| 139 | + expectError: false, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "invalid JSON in included engine", |
| 143 | + mainEngineSetting: "", |
| 144 | + includedEnginesJSON: []string{`{invalid json}`}, |
| 145 | + expectedEngine: "", |
| 146 | + expectError: true, |
| 147 | + errorMsg: "failed to parse", |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "included engine with invalid object format (no id)", |
| 151 | + mainEngineSetting: "", |
| 152 | + includedEnginesJSON: []string{`{"model": "gpt-4"}`}, |
| 153 | + expectedEngine: "", |
| 154 | + expectError: true, |
| 155 | + errorMsg: "invalid engine configuration", |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "included engine with non-string id", |
| 159 | + mainEngineSetting: "", |
| 160 | + includedEnginesJSON: []string{`{"id": 123}`}, |
| 161 | + expectedEngine: "", |
| 162 | + expectError: true, |
| 163 | + errorMsg: "invalid engine configuration", |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "main engine takes precedence when only non-empty", |
| 167 | + mainEngineSetting: "custom", |
| 168 | + includedEnginesJSON: []string{""}, |
| 169 | + expectedEngine: "custom", |
| 170 | + expectError: false, |
| 171 | + }, |
| 172 | + } |
| 173 | + |
| 174 | + for _, tt := range tests { |
| 175 | + t.Run(tt.name, func(t *testing.T) { |
| 176 | + compiler := NewCompiler(false, "", "") |
| 177 | + result, err := compiler.validateSingleEngineSpecification(tt.mainEngineSetting, tt.includedEnginesJSON) |
| 178 | + |
| 179 | + if tt.expectError && err == nil { |
| 180 | + t.Error("Expected validation to fail but it succeeded") |
| 181 | + } else if !tt.expectError && err != nil { |
| 182 | + t.Errorf("Expected validation to succeed but it failed: %v", err) |
| 183 | + } else if tt.expectError && err != nil && tt.errorMsg != "" { |
| 184 | + if !strings.Contains(err.Error(), tt.errorMsg) { |
| 185 | + t.Errorf("Expected error containing '%s', got '%s'", tt.errorMsg, err.Error()) |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + if !tt.expectError && result != tt.expectedEngine { |
| 190 | + t.Errorf("Expected engine %q, got %q", tt.expectedEngine, result) |
| 191 | + } |
| 192 | + }) |
| 193 | + } |
| 194 | +} |
0 commit comments