|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestExtractYAMLValue(t *testing.T) { |
| 8 | + compiler := &Compiler{} |
| 9 | + |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + frontmatter map[string]any |
| 13 | + key string |
| 14 | + expected string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "string value", |
| 18 | + frontmatter: map[string]any{"name": "test-workflow"}, |
| 19 | + key: "name", |
| 20 | + expected: "test-workflow", |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "int value", |
| 24 | + frontmatter: map[string]any{"timeout": 42}, |
| 25 | + key: "timeout", |
| 26 | + expected: "42", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "int64 value", |
| 30 | + frontmatter: map[string]any{"count": int64(12345)}, |
| 31 | + key: "count", |
| 32 | + expected: "12345", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "uint64 value", |
| 36 | + frontmatter: map[string]any{"id": uint64(99999)}, |
| 37 | + key: "id", |
| 38 | + expected: "99999", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "float64 value", |
| 42 | + frontmatter: map[string]any{"version": 3.14}, |
| 43 | + key: "version", |
| 44 | + expected: "3", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "float64 whole number", |
| 48 | + frontmatter: map[string]any{"port": 8080.0}, |
| 49 | + key: "port", |
| 50 | + expected: "8080", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "key not found", |
| 54 | + frontmatter: map[string]any{"name": "test"}, |
| 55 | + key: "missing", |
| 56 | + expected: "", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "empty frontmatter", |
| 60 | + frontmatter: map[string]any{}, |
| 61 | + key: "name", |
| 62 | + expected: "", |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "unsupported type (array)", |
| 66 | + frontmatter: map[string]any{"items": []string{"a", "b"}}, |
| 67 | + key: "items", |
| 68 | + expected: "", |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "unsupported type (map)", |
| 72 | + frontmatter: map[string]any{"config": map[string]string{"key": "value"}}, |
| 73 | + key: "config", |
| 74 | + expected: "", |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for _, tt := range tests { |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + result := compiler.extractYAMLValue(tt.frontmatter, tt.key) |
| 81 | + if result != tt.expected { |
| 82 | + t.Errorf("extractYAMLValue() = %q, want %q", result, tt.expected) |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestExtractFeatures(t *testing.T) { |
| 89 | + compiler := &Compiler{} |
| 90 | + |
| 91 | + tests := []struct { |
| 92 | + name string |
| 93 | + frontmatter map[string]any |
| 94 | + expected map[string]bool |
| 95 | + }{ |
| 96 | + { |
| 97 | + name: "valid features map", |
| 98 | + frontmatter: map[string]any{ |
| 99 | + "features": map[string]any{ |
| 100 | + "feature1": true, |
| 101 | + "feature2": false, |
| 102 | + "feature3": true, |
| 103 | + }, |
| 104 | + }, |
| 105 | + expected: map[string]bool{ |
| 106 | + "feature1": true, |
| 107 | + "feature2": false, |
| 108 | + "feature3": true, |
| 109 | + }, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "features key not present", |
| 113 | + frontmatter: map[string]any{"other": "value"}, |
| 114 | + expected: nil, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "empty frontmatter", |
| 118 | + frontmatter: map[string]any{}, |
| 119 | + expected: nil, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "features is not a map", |
| 123 | + frontmatter: map[string]any{ |
| 124 | + "features": "not a map", |
| 125 | + }, |
| 126 | + expected: nil, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "features map with non-boolean values", |
| 130 | + frontmatter: map[string]any{ |
| 131 | + "features": map[string]any{ |
| 132 | + "feature1": true, |
| 133 | + "feature2": "string value", |
| 134 | + "feature3": 123, |
| 135 | + }, |
| 136 | + }, |
| 137 | + expected: map[string]bool{ |
| 138 | + "feature1": true, |
| 139 | + }, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "empty features map", |
| 143 | + frontmatter: map[string]any{ |
| 144 | + "features": map[string]any{}, |
| 145 | + }, |
| 146 | + expected: map[string]bool{}, |
| 147 | + }, |
| 148 | + } |
| 149 | + |
| 150 | + for _, tt := range tests { |
| 151 | + t.Run(tt.name, func(t *testing.T) { |
| 152 | + result := compiler.extractFeatures(tt.frontmatter) |
| 153 | + |
| 154 | + if result == nil && tt.expected == nil { |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + if (result == nil) != (tt.expected == nil) { |
| 159 | + t.Errorf("extractFeatures() = %v, want %v", result, tt.expected) |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + if len(result) != len(tt.expected) { |
| 164 | + t.Errorf("extractFeatures() length = %d, want %d", len(result), len(tt.expected)) |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + for key, expectedVal := range tt.expected { |
| 169 | + if actualVal, ok := result[key]; !ok || actualVal != expectedVal { |
| 170 | + t.Errorf("extractFeatures()[%q] = %v, want %v", key, actualVal, expectedVal) |
| 171 | + } |
| 172 | + } |
| 173 | + }) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func TestExtractToolsStartupTimeout(t *testing.T) { |
| 178 | + compiler := &Compiler{} |
| 179 | + |
| 180 | + tests := []struct { |
| 181 | + name string |
| 182 | + tools map[string]any |
| 183 | + expected int |
| 184 | + }{ |
| 185 | + { |
| 186 | + name: "int timeout", |
| 187 | + tools: map[string]any{ |
| 188 | + "startup-timeout": 30, |
| 189 | + }, |
| 190 | + expected: 30, |
| 191 | + }, |
| 192 | + { |
| 193 | + name: "int64 timeout", |
| 194 | + tools: map[string]any{ |
| 195 | + "startup-timeout": int64(60), |
| 196 | + }, |
| 197 | + expected: 60, |
| 198 | + }, |
| 199 | + { |
| 200 | + name: "uint timeout", |
| 201 | + tools: map[string]any{ |
| 202 | + "startup-timeout": uint(45), |
| 203 | + }, |
| 204 | + expected: 45, |
| 205 | + }, |
| 206 | + { |
| 207 | + name: "uint64 timeout", |
| 208 | + tools: map[string]any{ |
| 209 | + "startup-timeout": uint64(90), |
| 210 | + }, |
| 211 | + expected: 90, |
| 212 | + }, |
| 213 | + { |
| 214 | + name: "float64 timeout", |
| 215 | + tools: map[string]any{ |
| 216 | + "startup-timeout": 120.0, |
| 217 | + }, |
| 218 | + expected: 120, |
| 219 | + }, |
| 220 | + { |
| 221 | + name: "nil tools", |
| 222 | + tools: nil, |
| 223 | + expected: 0, |
| 224 | + }, |
| 225 | + { |
| 226 | + name: "empty tools map", |
| 227 | + tools: map[string]any{}, |
| 228 | + expected: 0, |
| 229 | + }, |
| 230 | + { |
| 231 | + name: "startup-timeout not present", |
| 232 | + tools: map[string]any{ |
| 233 | + "other-field": "value", |
| 234 | + }, |
| 235 | + expected: 0, |
| 236 | + }, |
| 237 | + { |
| 238 | + name: "invalid type (string)", |
| 239 | + tools: map[string]any{ |
| 240 | + "startup-timeout": "not a number", |
| 241 | + }, |
| 242 | + expected: 0, |
| 243 | + }, |
| 244 | + { |
| 245 | + name: "invalid type (array)", |
| 246 | + tools: map[string]any{ |
| 247 | + "startup-timeout": []int{1, 2, 3}, |
| 248 | + }, |
| 249 | + expected: 0, |
| 250 | + }, |
| 251 | + { |
| 252 | + name: "zero timeout", |
| 253 | + tools: map[string]any{ |
| 254 | + "startup-timeout": 0, |
| 255 | + }, |
| 256 | + expected: 0, |
| 257 | + }, |
| 258 | + } |
| 259 | + |
| 260 | + for _, tt := range tests { |
| 261 | + t.Run(tt.name, func(t *testing.T) { |
| 262 | + result := compiler.extractToolsStartupTimeout(tt.tools) |
| 263 | + if result != tt.expected { |
| 264 | + t.Errorf("extractToolsStartupTimeout() = %d, want %d", result, tt.expected) |
| 265 | + } |
| 266 | + }) |
| 267 | + } |
| 268 | +} |
0 commit comments