| 1 | package basic |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "net/url" |
| 8 | "os" |
| 9 | "reflect" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "text/template" |
| 14 | |
| 15 | "github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 16 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 17 | ) |
| 18 | |
| 19 | // mockConfig implements types.ServiceConfig. |
| 20 | type mockConfig struct { |
| 21 | Host string `default:"localhost" key:"host"` |
| 22 | Port int `default:"8080" key:"port" required:"true"` |
| 23 | url *url.URL |
| 24 | } |
| 25 | |
| 26 | // mockServiceConfig is a test implementation of Service. |
| 27 | type mockServiceConfig struct { |
| 28 | Config *mockConfig |
| 29 | } |
| 30 | |
| 31 | // mockServiceNilConfig is a test service with nil Config field (simulating uninitialized service). |
| 32 | type mockServiceNilConfig struct { |
| 33 | Config *mockConfig // nil by default |
| 34 | } |
| 35 | |
| 36 | // mockServiceWithoutConfig is a test service without a Config field. |
| 37 | type mockServiceWithoutConfig struct{} |
| 38 | |
| 39 | func (m *mockConfig) Enums() map[string]types.EnumFormatter { |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | // ConfigQueryResolver methods. |
| 44 | func (m *mockConfig) Get(key string) (string, error) { |
| 45 | switch strings.ToLower(key) { |
| 46 | case "host": |
| 47 | return m.Host, nil |
| 48 | case "port": |
| 49 | return strconv.Itoa(m.Port), nil |
| 50 | default: |
| 51 | return "", fmt.Errorf("unknown key: %s", key) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func (m *mockConfig) GetPropValue() (string, error) { |
| 56 | // Minimal implementation for testing |
| 57 | return fmt.Sprintf("%s:%d", m.Host, m.Port), nil |
| 58 | } |
| 59 | |
| 60 | func (m *mockConfig) GetURL() *url.URL { |
| 61 | if m.url == nil { |
| 62 | u, _ := url.Parse("mock://url") |
| 63 | m.url = u |
| 64 | } |
| 65 | |
| 66 | return m.url |
| 67 | } |
| 68 | |
| 69 | func (m *mockConfig) QueryFields() []string { |
| 70 | return []string{"host", "port"} |
| 71 | } |
| 72 | |
| 73 | func (m *mockConfig) Set(key, value string) error { |
| 74 | switch strings.ToLower(key) { |
| 75 | case "host": |
| 76 | m.Host = value |
| 77 | |
| 78 | return nil |
| 79 | case "port": |
| 80 | port, err := strconv.Atoi(value) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | |
| 85 | m.Port = port |
| 86 | |
| 87 | return nil |
| 88 | default: |
| 89 | return fmt.Errorf("unknown key: %s", key) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // ConfigProp methods. |
| 94 | func (m *mockConfig) SetFromProp(propValue string) error { |
| 95 | // Minimal implementation for testing; typically parses propValue |
| 96 | parts := strings.SplitN(propValue, ":", 2) |
| 97 | if len(parts) == 2 { |
| 98 | m.Host = parts[0] |
| 99 | |
| 100 | port, err := strconv.Atoi(parts[1]) |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | |
| 105 | m.Port = port |
| 106 | } |
| 107 | |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | func (m *mockConfig) SetLogger(_ types.StdLogger) { |
| 112 | // Minimal implementation, no-op |
| 113 | } |
| 114 | |
| 115 | func (m *mockConfig) SetTemplateFile(_, _ string) error { |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | func (m *mockConfig) SetTemplateString(_, _ string) error { |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | func (m *mockConfig) SetURL(u *url.URL) error { |
| 124 | m.url = u |
| 125 | |
| 126 | return nil |
| 127 | } |
| 128 | |
| 129 | func (m *mockServiceConfig) GetID() string { |
| 130 | return "mockID" |
| 131 | } |
| 132 | |
| 133 | func (m *mockServiceConfig) GetTemplate(_ string) (*template.Template, bool) { |
| 134 | return nil, false |
| 135 | } |
| 136 | |
| 137 | func (m *mockServiceConfig) Initialize(_ *url.URL, _ types.StdLogger) error { |
| 138 | return nil |
| 139 | } |
| 140 | |
| 141 | func (m *mockServiceConfig) Send(_ string, _ *types.Params) error { |
| 142 | return nil |
| 143 | } |
| 144 | |
| 145 | func (m *mockServiceConfig) SetLogger(_ types.StdLogger) {} |
| 146 | |
| 147 | func (m *mockServiceConfig) SetTemplateFile(_, _ string) error { |
| 148 | return nil |
| 149 | } |
| 150 | |
| 151 | func (m *mockServiceConfig) SetTemplateString(_, _ string) error { |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | func (m *mockServiceNilConfig) GetID() string { |
| 156 | return "mockNilConfig" |
| 157 | } |
| 158 | |
| 159 | func (m *mockServiceNilConfig) GetTemplate(_ string) (*template.Template, bool) { |
| 160 | return nil, false |
| 161 | } |
| 162 | |
| 163 | func (m *mockServiceNilConfig) Initialize(_ *url.URL, _ types.StdLogger) error { |
| 164 | return nil |
| 165 | } |
| 166 | |
| 167 | func (m *mockServiceNilConfig) Send(_ string, _ *types.Params) error { |
| 168 | return nil |
| 169 | } |
| 170 | |
| 171 | func (m *mockServiceNilConfig) SetLogger(_ types.StdLogger) {} |
| 172 | |
| 173 | func (m *mockServiceNilConfig) SetTemplateFile(_, _ string) error { |
| 174 | return nil |
| 175 | } |
| 176 | |
| 177 | func (m *mockServiceNilConfig) SetTemplateString(_, _ string) error { |
| 178 | return nil |
| 179 | } |
| 180 | |
| 181 | func (m *mockServiceWithoutConfig) GetID() string { |
| 182 | return "mockNoConfig" |
| 183 | } |
| 184 | |
| 185 | func (m *mockServiceWithoutConfig) GetTemplate(_ string) (*template.Template, bool) { |
| 186 | return nil, false |
| 187 | } |
| 188 | |
| 189 | func (m *mockServiceWithoutConfig) Initialize(_ *url.URL, _ types.StdLogger) error { |
| 190 | return nil |
| 191 | } |
| 192 | |
| 193 | func (m *mockServiceWithoutConfig) Send(_ string, _ *types.Params) error { |
| 194 | return nil |
| 195 | } |
| 196 | |
| 197 | func (m *mockServiceWithoutConfig) SetLogger(_ types.StdLogger) {} |
| 198 | |
| 199 | func (m *mockServiceWithoutConfig) SetTemplateFile(_, _ string) error { |
| 200 | return nil |
| 201 | } |
| 202 | |
| 203 | func (m *mockServiceWithoutConfig) SetTemplateString(_, _ string) error { |
| 204 | return nil |
| 205 | } |
| 206 | |
| 207 | func TestGenerator_Generate(t *testing.T) { |
| 208 | t.Parallel() |
| 209 | |
| 210 | tests := []struct { |
| 211 | name string |
| 212 | props map[string]string |
| 213 | input string |
| 214 | want types.ServiceConfig |
| 215 | wantErr bool |
| 216 | }{ |
| 217 | { |
| 218 | name: "successful generation with defaults", |
| 219 | props: map[string]string{}, |
| 220 | input: "\n8080\n", |
| 221 | want: &mockConfig{ |
| 222 | Host: "localhost", |
| 223 | Port: 8080, |
| 224 | }, |
| 225 | wantErr: false, |
| 226 | }, |
| 227 | { |
| 228 | name: "successful generation with props", |
| 229 | props: map[string]string{"host": "example.com", "port": "9090"}, |
| 230 | input: "", |
| 231 | want: &mockConfig{ |
| 232 | Host: "example.com", |
| 233 | Port: 9090, |
| 234 | }, |
| 235 | wantErr: false, |
| 236 | }, |
| 237 | { |
| 238 | name: "error_on_invalid_port", |
| 239 | props: map[string]string{}, |
| 240 | input: "\ninvalid\n", |
| 241 | want: nil, |
| 242 | wantErr: true, |
| 243 | }, |
| 244 | } |
| 245 | |
| 246 | for _, tt := range tests { |
| 247 | t.Run(tt.name, func(t *testing.T) { |
| 248 | t.Parallel() |
| 249 | |
| 250 | // Set up pipe for stdin simulation |
| 251 | r, w, err := os.Pipe() |
| 252 | if err != nil { |
| 253 | t.Fatal(err) |
| 254 | } |
| 255 | |
| 256 | // Use dependency injection instead of global os.Stdin manipulation |
| 257 | g := &Generator{Input: r} |
| 258 | |
| 259 | defer func() { |
| 260 | _ = w.Close() |
| 261 | _ = r.Close() |
| 262 | }() |
| 263 | |
| 264 | // Write input to the pipe |
| 265 | _, err = w.WriteString(tt.input) |
| 266 | if err != nil { |
| 267 | t.Fatal(err) |
| 268 | } |
| 269 | |
| 270 | _ = w.Close() |
| 271 | |
| 272 | service := newMockServiceConfig() |
| 273 | |
| 274 | got, err := g.Generate(service, tt.props, nil) |
| 275 | if (err != nil) != tt.wantErr { |
| 276 | t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr) |
| 277 | |
| 278 | return |
| 279 | } |
| 280 | |
| 281 | if !tt.wantErr && !reflect.DeepEqual(got, tt.want) { |
| 282 | t.Errorf("Generate() = %+v, want %+v", got, tt.want) |
| 283 | } |
| 284 | }) |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | func TestGenerator_promptUserForFields(t *testing.T) { |
| 289 | t.Parallel() |
| 290 | |
| 291 | tests := []struct { |
| 292 | name string |
| 293 | config reflect.Value |
| 294 | props map[string]string |
| 295 | input string |
| 296 | wantErr bool |
| 297 | }{ |
| 298 | { |
| 299 | name: "valid input with defaults", |
| 300 | config: reflect.ValueOf(newMockServiceConfig().Config), // Pass *mockConfig |
| 301 | props: map[string]string{}, |
| 302 | input: "\n8080\n", |
| 303 | wantErr: false, |
| 304 | }, |
| 305 | { |
| 306 | name: "valid props", |
| 307 | config: reflect.ValueOf(newMockServiceConfig().Config), // Pass *mockConfig |
| 308 | props: map[string]string{"host": "test.com", "port": "1234"}, |
| 309 | input: "", |
| 310 | wantErr: false, |
| 311 | }, |
| 312 | { |
| 313 | name: "invalid config type", |
| 314 | config: reflect.ValueOf("not a config"), |
| 315 | props: map[string]string{}, |
| 316 | input: "", |
| 317 | wantErr: true, |
| 318 | }, |
| 319 | } |
| 320 | |
| 321 | for _, tt := range tests { |
| 322 | t.Run(tt.name, func(t *testing.T) { |
| 323 | t.Parallel() |
| 324 | |
| 325 | g := &Generator{} |
| 326 | scanner := bufio.NewScanner(strings.NewReader(tt.input)) |
| 327 | |
| 328 | err := g.promptUserForFields(tt.config, tt.props, scanner) |
| 329 | if (err != nil) != tt.wantErr { |
| 330 | t.Errorf("promptUserForFields() error = %v, wantErr %v", err, tt.wantErr) |
| 331 | } |
| 332 | |
| 333 | if err == nil && tt.config.Kind() == reflect.Pointer && |
| 334 | tt.config.Type().Elem().Kind() == reflect.Struct { |
| 335 | got := tt.config.Interface().(*mockConfig) |
| 336 | if tt.props["host"] != "" && got.Host != tt.props["host"] { |
| 337 | t.Errorf("promptUserForFields() host = %v, want %v", got.Host, tt.props["host"]) |
| 338 | } |
| 339 | |
| 340 | if tt.props["port"] != "" { |
| 341 | wantPort := atoiOrZero(tt.props["port"]) |
| 342 | if got.Port != wantPort { |
| 343 | t.Errorf("promptUserForFields() port = %v, want %v", got.Port, wantPort) |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | }) |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | func TestGenerator_getInputValue(t *testing.T) { |
| 352 | t.Parallel() |
| 353 | |
| 354 | tests := []struct { |
| 355 | name string |
| 356 | field *format.FieldInfo |
| 357 | propKey string |
| 358 | props map[string]string |
| 359 | input string |
| 360 | want string |
| 361 | wantErr bool |
| 362 | }{ |
| 363 | { |
| 364 | name: "from props", |
| 365 | field: &format.FieldInfo{Name: "Host"}, |
| 366 | propKey: "host", |
| 367 | props: map[string]string{"host": "example.com"}, |
| 368 | input: "", |
| 369 | want: "example.com", |
| 370 | wantErr: false, |
| 371 | }, |
| 372 | { |
| 373 | name: "from user input", |
| 374 | field: &format.FieldInfo{Name: "Port", Type: reflect.TypeFor[int]()}, // Add Type |
| 375 | propKey: "port", |
| 376 | props: map[string]string{}, |
| 377 | input: "8080\n", |
| 378 | want: "8080", |
| 379 | wantErr: false, |
| 380 | }, |
| 381 | { |
| 382 | name: "default value", |
| 383 | field: &format.FieldInfo{Name: "Host", DefaultValue: "localhost"}, |
| 384 | propKey: "host", |
| 385 | props: map[string]string{}, |
| 386 | input: "\n", |
| 387 | want: "localhost", |
| 388 | wantErr: false, |
| 389 | }, |
| 390 | } |
| 391 | |
| 392 | for _, tt := range tests { |
| 393 | t.Run(tt.name, func(t *testing.T) { |
| 394 | t.Parallel() |
| 395 | |
| 396 | g := &Generator{} |
| 397 | scanner := bufio.NewScanner(strings.NewReader(tt.input)) |
| 398 | consumed := make(map[string]struct{}) |
| 399 | |
| 400 | got, err := g.getInputValue(tt.field, tt.propKey, tt.props, consumed, scanner) |
| 401 | if (err != nil) != tt.wantErr { |
| 402 | t.Errorf("getInputValue() error = %v, wantErr %v", err, tt.wantErr) |
| 403 | |
| 404 | return |
| 405 | } |
| 406 | |
| 407 | if got != tt.want { |
| 408 | t.Errorf("getInputValue() = %v, want %v", got, tt.want) |
| 409 | } |
| 410 | |
| 411 | // Verify that props from input are marked as consumed |
| 412 | if _, wasConsumed := consumed[tt.propKey]; tt.props[tt.propKey] != "" && !wasConsumed { |
| 413 | t.Errorf("getInputValue() did not mark prop as consumed") |
| 414 | } |
| 415 | }) |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | func TestGenerator_formatPrompt(t *testing.T) { |
| 420 | t.Parallel() |
| 421 | |
| 422 | tests := []struct { |
| 423 | name string |
| 424 | field *format.FieldInfo |
| 425 | want string |
| 426 | }{ |
| 427 | { |
| 428 | name: "field with default", |
| 429 | field: &format.FieldInfo{Name: "Host", DefaultValue: "localhost"}, |
| 430 | want: "Host[localhost]: ", |
| 431 | }, |
| 432 | { |
| 433 | name: "field without default", |
| 434 | field: &format.FieldInfo{Name: "Port"}, |
| 435 | want: "Port: ", |
| 436 | }, |
| 437 | } |
| 438 | |
| 439 | for _, tt := range tests { |
| 440 | t.Run(tt.name, func(t *testing.T) { |
| 441 | t.Parallel() |
| 442 | |
| 443 | g := &Generator{} |
| 444 | |
| 445 | got := g.formatPrompt(tt.field) |
| 446 | if got != tt.want { |
| 447 | t.Errorf("formatPrompt() = %q, want %q", got, tt.want) |
| 448 | } |
| 449 | }) |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | func TestGenerator_setFieldValue(t *testing.T) { |
| 454 | t.Parallel() |
| 455 | |
| 456 | tests := []struct { |
| 457 | name string |
| 458 | config reflect.Value |
| 459 | field *format.FieldInfo |
| 460 | inputValue string |
| 461 | want bool |
| 462 | wantErr bool |
| 463 | }{ |
| 464 | { |
| 465 | name: "valid value", |
| 466 | config: reflect.ValueOf(newMockServiceConfig().Config).Elem(), |
| 467 | field: &format.FieldInfo{Name: "Port", Type: reflect.TypeFor[int](), Required: true}, |
| 468 | inputValue: "8080", |
| 469 | want: true, |
| 470 | wantErr: false, |
| 471 | }, |
| 472 | { |
| 473 | name: "required field empty", |
| 474 | config: reflect.ValueOf(newMockServiceConfig().Config).Elem(), |
| 475 | field: &format.FieldInfo{Name: "Port", Type: reflect.TypeFor[int](), Required: true}, |
| 476 | inputValue: "", |
| 477 | want: false, |
| 478 | wantErr: false, |
| 479 | }, |
| 480 | { |
| 481 | name: "invalid value", |
| 482 | config: reflect.ValueOf(newMockServiceConfig().Config).Elem(), |
| 483 | field: &format.FieldInfo{Name: "Port", Type: reflect.TypeFor[int]()}, |
| 484 | inputValue: "invalid", |
| 485 | want: false, |
| 486 | wantErr: true, |
| 487 | }, |
| 488 | } |
| 489 | |
| 490 | for _, tt := range tests { |
| 491 | t.Run(tt.name, func(t *testing.T) { |
| 492 | t.Parallel() |
| 493 | |
| 494 | g := &Generator{} |
| 495 | |
| 496 | got, err := g.setFieldValue(tt.config, tt.field, tt.inputValue) |
| 497 | if (err != nil) != tt.wantErr { |
| 498 | t.Errorf("setFieldValue() error = %v, wantErr %v", err, tt.wantErr) |
| 499 | |
| 500 | return |
| 501 | } |
| 502 | |
| 503 | if got != tt.want { |
| 504 | t.Errorf("setFieldValue() = %v, want %v", got, tt.want) |
| 505 | } |
| 506 | |
| 507 | if got && !tt.wantErr { |
| 508 | if tt.field.Name == "Port" { |
| 509 | wantPort := atoiOrZero(tt.inputValue) |
| 510 | if gotPort := tt.config.FieldByName("Port").Int(); int(gotPort) != wantPort { |
| 511 | t.Errorf("setFieldValue() set Port = %v, want %v", gotPort, wantPort) |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | }) |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | func TestGenerator_printError(t *testing.T) { |
| 520 | t.Parallel() |
| 521 | |
| 522 | tests := []struct { |
| 523 | name string |
| 524 | fieldName string |
| 525 | errorMsg string |
| 526 | }{ |
| 527 | { |
| 528 | name: "basic error", |
| 529 | fieldName: "Port", |
| 530 | errorMsg: "invalid format", |
| 531 | }, |
| 532 | } |
| 533 | |
| 534 | for _, tt := range tests { |
| 535 | t.Run(tt.name, func(t *testing.T) { |
| 536 | t.Parallel() |
| 537 | |
| 538 | g := &Generator{} |
| 539 | |
| 540 | g.printError(tt.fieldName, tt.errorMsg) |
| 541 | }) |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | func TestGenerator_printInvalidType(t *testing.T) { |
| 546 | t.Parallel() |
| 547 | |
| 548 | tests := []struct { |
| 549 | name string |
| 550 | fieldName string |
| 551 | typeName string |
| 552 | }{ |
| 553 | { |
| 554 | name: "invalid type", |
| 555 | fieldName: "Port", |
| 556 | typeName: "int", |
| 557 | }, |
| 558 | } |
| 559 | |
| 560 | for _, tt := range tests { |
| 561 | t.Run(tt.name, func(t *testing.T) { |
| 562 | t.Parallel() |
| 563 | |
| 564 | g := &Generator{} |
| 565 | |
| 566 | g.printInvalidType(tt.fieldName, tt.typeName) |
| 567 | }) |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | // atoiOrZero converts a string to an int, returning 0 on error. |
| 572 | func atoiOrZero(s string) int { |
| 573 | i, _ := strconv.Atoi(s) |
| 574 | |
| 575 | return i |
| 576 | } |
| 577 | |
| 578 | // newMockServiceConfig creates a new mockServiceConfig with an initialized Config. |
| 579 | func newMockServiceConfig() *mockServiceConfig { |
| 580 | return &mockServiceConfig{ |
| 581 | Config: &mockConfig{}, |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | // TestGenerator_NilConfigInitialization tests that a service with nil Config gets properly initialized. |
| 586 | // This is a regression test for the nil config fix in basic.go:45-53. |
| 587 | func TestGenerator_NilConfigInitialization(t *testing.T) { |
| 588 | t.Parallel() |
| 589 | |
| 590 | testCases := []struct { |
| 591 | name string |
| 592 | service types.Service |
| 593 | props map[string]string |
| 594 | input string |
| 595 | want *mockConfig |
| 596 | wantErr bool |
| 597 | }{ |
| 598 | { |
| 599 | name: "nil config gets initialized with defaults", |
| 600 | service: &mockServiceNilConfig{Config: nil}, |
| 601 | props: map[string]string{}, |
| 602 | input: "\n8080\n", |
| 603 | want: &mockConfig{ |
| 604 | Host: "localhost", |
| 605 | Port: 8080, |
| 606 | }, |
| 607 | wantErr: false, |
| 608 | }, |
| 609 | { |
| 610 | name: "nil config gets initialized with provided props", |
| 611 | service: &mockServiceNilConfig{Config: nil}, |
| 612 | props: map[string]string{"host": "example.com", "port": "9090"}, |
| 613 | input: "", |
| 614 | want: &mockConfig{ |
| 615 | Host: "example.com", |
| 616 | Port: 9090, |
| 617 | }, |
| 618 | wantErr: false, |
| 619 | }, |
| 620 | { |
| 621 | name: "nil config error on invalid input", |
| 622 | service: &mockServiceNilConfig{Config: nil}, |
| 623 | props: map[string]string{}, |
| 624 | input: "\ninvalid\n", |
| 625 | want: nil, |
| 626 | wantErr: true, |
| 627 | }, |
| 628 | } |
| 629 | |
| 630 | for _, tt := range testCases { |
| 631 | t.Run(tt.name, func(t *testing.T) { |
| 632 | t.Parallel() |
| 633 | |
| 634 | // Set up pipe for stdin simulation |
| 635 | r, w, err := os.Pipe() |
| 636 | if err != nil { |
| 637 | t.Fatal(err) |
| 638 | } |
| 639 | |
| 640 | g := &Generator{Input: r} |
| 641 | |
| 642 | defer func() { |
| 643 | _ = w.Close() |
| 644 | _ = r.Close() |
| 645 | }() |
| 646 | |
| 647 | // Write input to the pipe |
| 648 | _, err = w.WriteString(tt.input) |
| 649 | if err != nil { |
| 650 | t.Fatal(err) |
| 651 | } |
| 652 | |
| 653 | _ = w.Close() |
| 654 | |
| 655 | got, err := g.Generate(tt.service, tt.props, nil) |
| 656 | if (err != nil) != tt.wantErr { |
| 657 | t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr) |
| 658 | |
| 659 | return |
| 660 | } |
| 661 | |
| 662 | if !tt.wantErr { |
| 663 | gotConfig, ok := got.(*mockConfig) |
| 664 | if !ok { |
| 665 | t.Errorf("Generate() returned type %T, want *mockConfig", got) |
| 666 | |
| 667 | return |
| 668 | } |
| 669 | |
| 670 | // Verify the config was initialized (not nil) |
| 671 | if gotConfig == nil { |
| 672 | t.Error("Generate() returned nil config, expected initialized config") |
| 673 | |
| 674 | return |
| 675 | } |
| 676 | |
| 677 | if gotConfig.Host != tt.want.Host { |
| 678 | t.Errorf("Generate() host = %v, want %v", gotConfig.Host, tt.want.Host) |
| 679 | } |
| 680 | |
| 681 | if gotConfig.Port != tt.want.Port { |
| 682 | t.Errorf("Generate() port = %v, want %v", gotConfig.Port, tt.want.Port) |
| 683 | } |
| 684 | |
| 685 | // Also verify that the service's internal Config field was initialized |
| 686 | if mockSvc, ok := tt.service.(*mockServiceNilConfig); ok { |
| 687 | if mockSvc.Config == nil { |
| 688 | t.Error("Generate() left service.Config nil, expected initialized config") |
| 689 | } |
| 690 | |
| 691 | // Optionally verify Host/Port match |
| 692 | if mockSvc.Config != nil && mockSvc.Config.Host != tt.want.Host { |
| 693 | t.Errorf("service.Config.Host = %v, want %v", mockSvc.Config.Host, tt.want.Host) |
| 694 | } |
| 695 | |
| 696 | if mockSvc.Config != nil && mockSvc.Config.Port != tt.want.Port { |
| 697 | t.Errorf("service.Config.Port = %v, want %v", mockSvc.Config.Port, tt.want.Port) |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | }) |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | // TestGenerator_DefaultsAndPropsOverrideExistingConfig tests that provided props |
| 706 | // override the default values in an existing config. |
| 707 | func TestGenerator_DefaultsAndPropsOverrideExistingConfig(t *testing.T) { |
| 708 | t.Parallel() |
| 709 | |
| 710 | testCases := []struct { |
| 711 | name string |
| 712 | service *mockServiceConfig |
| 713 | props map[string]string |
| 714 | input string |
| 715 | want *mockConfig |
| 716 | wantErr bool |
| 717 | }{ |
| 718 | { |
| 719 | name: "props override defaults", |
| 720 | service: &mockServiceConfig{Config: &mockConfig{Host: "existing.com", Port: 3000}}, |
| 721 | props: map[string]string{"port": "9000"}, |
| 722 | input: "", |
| 723 | want: &mockConfig{ |
| 724 | Host: "localhost", // defaults are used when not in props |
| 725 | Port: 9000, |
| 726 | }, |
| 727 | wantErr: false, |
| 728 | }, |
| 729 | { |
| 730 | name: "user input overrides defaults", |
| 731 | service: &mockServiceConfig{Config: &mockConfig{Host: "original.com", Port: 4000}}, |
| 732 | props: map[string]string{}, |
| 733 | input: "\n5000\n", |
| 734 | want: &mockConfig{ |
| 735 | Host: "localhost", // defaults are used when not in props |
| 736 | Port: 5000, |
| 737 | }, |
| 738 | wantErr: false, |
| 739 | }, |
| 740 | } |
| 741 | |
| 742 | for _, tt := range testCases { |
| 743 | t.Run(tt.name, func(t *testing.T) { |
| 744 | t.Parallel() |
| 745 | |
| 746 | // Set up pipe for stdin simulation |
| 747 | r, w, err := os.Pipe() |
| 748 | if err != nil { |
| 749 | t.Fatal(err) |
| 750 | } |
| 751 | |
| 752 | g := &Generator{Input: r} |
| 753 | |
| 754 | defer func() { |
| 755 | _ = w.Close() |
| 756 | _ = r.Close() |
| 757 | }() |
| 758 | |
| 759 | // Write input to the pipe |
| 760 | _, err = w.WriteString(tt.input) |
| 761 | if err != nil { |
| 762 | t.Fatal(err) |
| 763 | } |
| 764 | |
| 765 | _ = w.Close() |
| 766 | |
| 767 | got, err := g.Generate(tt.service, tt.props, nil) |
| 768 | if (err != nil) != tt.wantErr { |
| 769 | t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr) |
| 770 | |
| 771 | return |
| 772 | } |
| 773 | |
| 774 | if !tt.wantErr { |
| 775 | gotConfig, ok := got.(*mockConfig) |
| 776 | if !ok { |
| 777 | t.Errorf("Generate() returned type %T, want *mockConfig", got) |
| 778 | |
| 779 | return |
| 780 | } |
| 781 | |
| 782 | if gotConfig.Host != tt.want.Host { |
| 783 | t.Errorf("Generate() host = %v, want %v", gotConfig.Host, tt.want.Host) |
| 784 | } |
| 785 | |
| 786 | if gotConfig.Port != tt.want.Port { |
| 787 | t.Errorf("Generate() port = %v, want %v", gotConfig.Port, tt.want.Port) |
| 788 | } |
| 789 | } |
| 790 | }) |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | // TestGenerator_ServiceWithoutConfigField tests that services without a Config field |
| 795 | // return ErrInvalidConfigField. |
| 796 | func TestGenerator_ServiceWithoutConfigField(t *testing.T) { |
| 797 | t.Parallel() |
| 798 | |
| 799 | // Set up pipe for stdin simulation |
| 800 | r, w, err := os.Pipe() |
| 801 | if err != nil { |
| 802 | t.Fatal(err) |
| 803 | } |
| 804 | |
| 805 | g := &Generator{Input: r} |
| 806 | |
| 807 | defer func() { |
| 808 | _ = w.Close() |
| 809 | _ = r.Close() |
| 810 | }() |
| 811 | |
| 812 | // Write empty input to the pipe |
| 813 | _, err = w.WriteString("") |
| 814 | if err != nil { |
| 815 | t.Fatal(err) |
| 816 | } |
| 817 | |
| 818 | _ = w.Close() |
| 819 | |
| 820 | // Service without Config field should return ErrInvalidConfigField |
| 821 | _, err = g.Generate(&mockServiceWithoutConfig{}, map[string]string{}, nil) |
| 822 | if err == nil { |
| 823 | t.Error("Generate() expected error for service without Config field") |
| 824 | |
| 825 | return |
| 826 | } |
| 827 | |
| 828 | if !errors.Is(err, ErrInvalidConfigField) { |
| 829 | t.Errorf("Generate() error = %v, want ErrInvalidConfigField", err) |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | // TestGenerator_IgnoresInvalidFieldNames tests that invalid/unknown field names |
| 834 | // in props are silently ignored and do not cause errors. |
| 835 | func TestGenerator_IgnoresInvalidFieldNames(t *testing.T) { |
| 836 | t.Parallel() |
| 837 | |
| 838 | // Set up pipe for stdin simulation |
| 839 | r, w, err := os.Pipe() |
| 840 | if err != nil { |
| 841 | t.Fatal(err) |
| 842 | } |
| 843 | |
| 844 | g := &Generator{Input: r} |
| 845 | |
| 846 | defer func() { |
| 847 | _ = w.Close() |
| 848 | _ = r.Close() |
| 849 | }() |
| 850 | |
| 851 | // Write input to the pipe |
| 852 | _, err = w.WriteString("\n8080\n") |
| 853 | if err != nil { |
| 854 | t.Fatal(err) |
| 855 | } |
| 856 | |
| 857 | _ = w.Close() |
| 858 | |
| 859 | // Props with invalid field name should not cause an error, |
| 860 | // it should just be ignored (unknown props are silently ignored) |
| 861 | service := newMockServiceConfig() |
| 862 | |
| 863 | got, err := g.Generate(service, map[string]string{"invalid_field": "value"}, nil) |
| 864 | if err != nil { |
| 865 | t.Errorf("Generate() error = %v, wantErr false", err) |
| 866 | |
| 867 | return |
| 868 | } |
| 869 | |
| 870 | // Verify that the config was still generated with defaults |
| 871 | gotConfig, ok := got.(*mockConfig) |
| 872 | if !ok { |
| 873 | t.Errorf("Generate() returned type %T, want *mockConfig", got) |
| 874 | |
| 875 | return |
| 876 | } |
| 877 | |
| 878 | if gotConfig.Host != "localhost" { |
| 879 | t.Errorf("Generate() host = %v, want localhost (default)", gotConfig.Host) |
| 880 | } |
| 881 | |
| 882 | if gotConfig.Port != 8080 { |
| 883 | t.Errorf("Generate() port = %v, want 8080", gotConfig.Port) |
| 884 | } |
| 885 | } |