|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestMapToolConfig_GetAny(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + config MapToolConfig |
| 12 | + key string |
| 13 | + wantValue any |
| 14 | + wantOk bool |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "existing string key", |
| 18 | + config: MapToolConfig{ |
| 19 | + "name": "test-server", |
| 20 | + }, |
| 21 | + key: "name", |
| 22 | + wantValue: "test-server", |
| 23 | + wantOk: true, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "existing number key", |
| 27 | + config: MapToolConfig{ |
| 28 | + "port": 8080, |
| 29 | + }, |
| 30 | + key: "port", |
| 31 | + wantValue: 8080, |
| 32 | + wantOk: true, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "existing boolean key", |
| 36 | + config: MapToolConfig{ |
| 37 | + "enabled": true, |
| 38 | + }, |
| 39 | + key: "enabled", |
| 40 | + wantValue: true, |
| 41 | + wantOk: true, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "existing array key", |
| 45 | + config: MapToolConfig{ |
| 46 | + "items": []string{"a", "b", "c"}, |
| 47 | + }, |
| 48 | + key: "items", |
| 49 | + wantValue: []string{"a", "b", "c"}, |
| 50 | + wantOk: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "existing object key", |
| 54 | + config: MapToolConfig{ |
| 55 | + "nested": map[string]any{"key": "value"}, |
| 56 | + }, |
| 57 | + key: "nested", |
| 58 | + wantValue: map[string]any{"key": "value"}, |
| 59 | + wantOk: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "non-existent key", |
| 63 | + config: MapToolConfig{ |
| 64 | + "foo": "bar", |
| 65 | + }, |
| 66 | + key: "missing", |
| 67 | + wantValue: nil, |
| 68 | + wantOk: false, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "empty config", |
| 72 | + config: MapToolConfig{}, |
| 73 | + key: "anything", |
| 74 | + wantValue: nil, |
| 75 | + wantOk: false, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "nil value", |
| 79 | + config: MapToolConfig{ |
| 80 | + "nullable": nil, |
| 81 | + }, |
| 82 | + key: "nullable", |
| 83 | + wantValue: nil, |
| 84 | + wantOk: true, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + for _, tt := range tests { |
| 89 | + t.Run(tt.name, func(t *testing.T) { |
| 90 | + gotValue, gotOk := tt.config.GetAny(tt.key) |
| 91 | + |
| 92 | + if gotOk != tt.wantOk { |
| 93 | + t.Errorf("GetAny() gotOk = %v, want %v", gotOk, tt.wantOk) |
| 94 | + } |
| 95 | + |
| 96 | + // For non-existent keys, value should be nil |
| 97 | + if !tt.wantOk { |
| 98 | + if gotValue != nil { |
| 99 | + t.Errorf("GetAny() gotValue = %v, want nil for non-existent key", gotValue) |
| 100 | + } |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + // For existing keys, compare values based on type |
| 105 | + switch wantVal := tt.wantValue.(type) { |
| 106 | + case string: |
| 107 | + if gotVal, ok := gotValue.(string); !ok || gotVal != wantVal { |
| 108 | + t.Errorf("GetAny() gotValue = %v, want %v", gotValue, tt.wantValue) |
| 109 | + } |
| 110 | + case int: |
| 111 | + if gotVal, ok := gotValue.(int); !ok || gotVal != wantVal { |
| 112 | + t.Errorf("GetAny() gotValue = %v, want %v", gotValue, tt.wantValue) |
| 113 | + } |
| 114 | + case bool: |
| 115 | + if gotVal, ok := gotValue.(bool); !ok || gotVal != wantVal { |
| 116 | + t.Errorf("GetAny() gotValue = %v, want %v", gotValue, tt.wantValue) |
| 117 | + } |
| 118 | + case nil: |
| 119 | + if gotValue != nil { |
| 120 | + t.Errorf("GetAny() gotValue = %v, want nil", gotValue) |
| 121 | + } |
| 122 | + default: |
| 123 | + // For complex types like arrays and objects, just verify they exist |
| 124 | + if gotValue == nil { |
| 125 | + t.Errorf("GetAny() gotValue = nil, want non-nil value") |
| 126 | + } |
| 127 | + } |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func TestGetTypeString(t *testing.T) { |
| 133 | + tests := []struct { |
| 134 | + name string |
| 135 | + value any |
| 136 | + want string |
| 137 | + }{ |
| 138 | + { |
| 139 | + name: "nil value", |
| 140 | + value: nil, |
| 141 | + want: "null", |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "int value", |
| 145 | + value: 42, |
| 146 | + want: "number", |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "int64 value", |
| 150 | + value: int64(100), |
| 151 | + want: "number", |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "float64 value", |
| 155 | + value: 3.14, |
| 156 | + want: "number", |
| 157 | + }, |
| 158 | + { |
| 159 | + name: "float32 value", |
| 160 | + value: float32(2.71), |
| 161 | + want: "number", |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "boolean true", |
| 165 | + value: true, |
| 166 | + want: "boolean", |
| 167 | + }, |
| 168 | + { |
| 169 | + name: "boolean false", |
| 170 | + value: false, |
| 171 | + want: "boolean", |
| 172 | + }, |
| 173 | + { |
| 174 | + name: "string value", |
| 175 | + value: "hello world", |
| 176 | + want: "string", |
| 177 | + }, |
| 178 | + { |
| 179 | + name: "empty string", |
| 180 | + value: "", |
| 181 | + want: "string", |
| 182 | + }, |
| 183 | + { |
| 184 | + name: "object (map[string]any)", |
| 185 | + value: map[string]any{ |
| 186 | + "key": "value", |
| 187 | + }, |
| 188 | + want: "object", |
| 189 | + }, |
| 190 | + { |
| 191 | + name: "empty object", |
| 192 | + value: map[string]any{}, |
| 193 | + want: "object", |
| 194 | + }, |
| 195 | + { |
| 196 | + name: "array of strings", |
| 197 | + value: []string{"a", "b", "c"}, |
| 198 | + want: "array", |
| 199 | + }, |
| 200 | + { |
| 201 | + name: "array of ints", |
| 202 | + value: []int{1, 2, 3}, |
| 203 | + want: "array", |
| 204 | + }, |
| 205 | + { |
| 206 | + name: "array of any", |
| 207 | + value: []any{"mixed", 123, true}, |
| 208 | + want: "array", |
| 209 | + }, |
| 210 | + { |
| 211 | + name: "empty array", |
| 212 | + value: []string{}, |
| 213 | + want: "array", |
| 214 | + }, |
| 215 | + { |
| 216 | + name: "array of objects", |
| 217 | + value: []map[string]any{{"key": "value"}}, |
| 218 | + want: "array", |
| 219 | + }, |
| 220 | + { |
| 221 | + name: "unknown type (struct)", |
| 222 | + value: struct { |
| 223 | + Name string |
| 224 | + }{Name: "test"}, |
| 225 | + want: "unknown", |
| 226 | + }, |
| 227 | + } |
| 228 | + |
| 229 | + for _, tt := range tests { |
| 230 | + t.Run(tt.name, func(t *testing.T) { |
| 231 | + got := getTypeString(tt.value) |
| 232 | + if got != tt.want { |
| 233 | + t.Errorf("getTypeString(%v) = %v, want %v", tt.value, got, tt.want) |
| 234 | + } |
| 235 | + }) |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +func TestWriteArgsToYAMLInline(t *testing.T) { |
| 240 | + tests := []struct { |
| 241 | + name string |
| 242 | + args []string |
| 243 | + want string |
| 244 | + }{ |
| 245 | + { |
| 246 | + name: "no args", |
| 247 | + args: []string{}, |
| 248 | + want: "", |
| 249 | + }, |
| 250 | + { |
| 251 | + name: "single simple arg", |
| 252 | + args: []string{"--verbose"}, |
| 253 | + want: `, "--verbose"`, |
| 254 | + }, |
| 255 | + { |
| 256 | + name: "multiple simple args", |
| 257 | + args: []string{"--verbose", "--debug"}, |
| 258 | + want: `, "--verbose", "--debug"`, |
| 259 | + }, |
| 260 | + { |
| 261 | + name: "args with spaces", |
| 262 | + args: []string{"--message", "hello world"}, |
| 263 | + want: `, "--message", "hello world"`, |
| 264 | + }, |
| 265 | + { |
| 266 | + name: "args with quotes", |
| 267 | + args: []string{"--text", `say "hello"`}, |
| 268 | + want: `, "--text", "say \"hello\""`, |
| 269 | + }, |
| 270 | + { |
| 271 | + name: "args with special characters", |
| 272 | + args: []string{"--path", "/tmp/test\n\t"}, |
| 273 | + want: `, "--path", "/tmp/test\n\t"`, |
| 274 | + }, |
| 275 | + { |
| 276 | + name: "args with backslashes", |
| 277 | + args: []string{"--path", `C:\Windows\System32`}, |
| 278 | + want: `, "--path", "C:\\Windows\\System32"`, |
| 279 | + }, |
| 280 | + { |
| 281 | + name: "empty string arg", |
| 282 | + args: []string{""}, |
| 283 | + want: `, ""`, |
| 284 | + }, |
| 285 | + { |
| 286 | + name: "unicode args", |
| 287 | + args: []string{"--text", "Hello 世界 🌍"}, |
| 288 | + want: `, "--text", "Hello 世界 🌍"`, |
| 289 | + }, |
| 290 | + } |
| 291 | + |
| 292 | + for _, tt := range tests { |
| 293 | + t.Run(tt.name, func(t *testing.T) { |
| 294 | + var builder strings.Builder |
| 295 | + writeArgsToYAMLInline(&builder, tt.args) |
| 296 | + got := builder.String() |
| 297 | + if got != tt.want { |
| 298 | + t.Errorf("writeArgsToYAMLInline() = %q, want %q", got, tt.want) |
| 299 | + } |
| 300 | + }) |
| 301 | + } |
| 302 | +} |
0 commit comments