|
6 | 6 | "os" |
7 | 7 | "testing" |
8 | 8 |
|
| 9 | + "github.com/codeGROOVE-dev/prx/pkg/prx" |
9 | 10 | "github.com/codeGROOVE-dev/slacker/pkg/bot/cache" |
| 11 | + "github.com/codeGROOVE-dev/turnclient/pkg/turn" |
10 | 12 | "github.com/slack-go/slack" |
11 | 13 | ) |
12 | 14 |
|
@@ -309,3 +311,234 @@ func TestThreadCache_Set(t *testing.T) { |
309 | 311 | t.Errorf("expected channel ID %s, got %s", threadInfo.ChannelID, retrieved.ChannelID) |
310 | 312 | } |
311 | 313 | } |
| 314 | + |
| 315 | +func TestShouldPostThread(t *testing.T) { |
| 316 | + coord := &Coordinator{} |
| 317 | + |
| 318 | + tests := []struct { |
| 319 | + name string |
| 320 | + when string |
| 321 | + checkResult *turn.CheckResponse |
| 322 | + wantPost bool |
| 323 | + wantReasonPart string // Part of the reason string to check for |
| 324 | + }{ |
| 325 | + { |
| 326 | + name: "immediate mode always posts", |
| 327 | + when: "immediate", |
| 328 | + checkResult: &turn.CheckResponse{ |
| 329 | + PullRequest: prx.PullRequest{ |
| 330 | + State: "open", |
| 331 | + }, |
| 332 | + }, |
| 333 | + wantPost: true, |
| 334 | + wantReasonPart: "immediate_mode", |
| 335 | + }, |
| 336 | + { |
| 337 | + name: "merged PR always posts regardless of when", |
| 338 | + when: "passing", |
| 339 | + checkResult: &turn.CheckResponse{ |
| 340 | + PullRequest: prx.PullRequest{ |
| 341 | + State: "closed", |
| 342 | + Merged: true, |
| 343 | + }, |
| 344 | + }, |
| 345 | + wantPost: true, |
| 346 | + wantReasonPart: "pr_merged", |
| 347 | + }, |
| 348 | + { |
| 349 | + name: "closed PR always posts regardless of when", |
| 350 | + when: "passing", |
| 351 | + checkResult: &turn.CheckResponse{ |
| 352 | + PullRequest: prx.PullRequest{ |
| 353 | + State: "closed", |
| 354 | + Merged: false, |
| 355 | + }, |
| 356 | + }, |
| 357 | + wantPost: true, |
| 358 | + wantReasonPart: "pr_closed", |
| 359 | + }, |
| 360 | + { |
| 361 | + name: "assigned: posts when has assignees", |
| 362 | + when: "assigned", |
| 363 | + checkResult: &turn.CheckResponse{ |
| 364 | + PullRequest: prx.PullRequest{ |
| 365 | + State: "open", |
| 366 | + Assignees: []string{"user1", "user2"}, |
| 367 | + }, |
| 368 | + }, |
| 369 | + wantPost: true, |
| 370 | + wantReasonPart: "has_2_assignees", |
| 371 | + }, |
| 372 | + { |
| 373 | + name: "assigned: does not post when no assignees", |
| 374 | + when: "assigned", |
| 375 | + checkResult: &turn.CheckResponse{ |
| 376 | + PullRequest: prx.PullRequest{ |
| 377 | + State: "open", |
| 378 | + Assignees: []string{}, |
| 379 | + }, |
| 380 | + }, |
| 381 | + wantPost: false, |
| 382 | + wantReasonPart: "no_assignees", |
| 383 | + }, |
| 384 | + { |
| 385 | + name: "blocked: posts when users are blocked", |
| 386 | + when: "blocked", |
| 387 | + checkResult: &turn.CheckResponse{ |
| 388 | + PullRequest: prx.PullRequest{ |
| 389 | + State: "open", |
| 390 | + }, |
| 391 | + Analysis: turn.Analysis{ |
| 392 | + NextAction: map[string]turn.Action{ |
| 393 | + "user1": {Kind: "review"}, |
| 394 | + "user2": {Kind: "approve"}, |
| 395 | + }, |
| 396 | + }, |
| 397 | + }, |
| 398 | + wantPost: true, |
| 399 | + wantReasonPart: "blocked_on_2_users", |
| 400 | + }, |
| 401 | + { |
| 402 | + name: "blocked: does not post when no users blocked", |
| 403 | + when: "blocked", |
| 404 | + checkResult: &turn.CheckResponse{ |
| 405 | + PullRequest: prx.PullRequest{ |
| 406 | + State: "open", |
| 407 | + }, |
| 408 | + Analysis: turn.Analysis{ |
| 409 | + NextAction: map[string]turn.Action{}, |
| 410 | + }, |
| 411 | + }, |
| 412 | + wantPost: false, |
| 413 | + wantReasonPart: "not_blocked_yet", |
| 414 | + }, |
| 415 | + { |
| 416 | + name: "blocked: ignores _system sentinel", |
| 417 | + when: "blocked", |
| 418 | + checkResult: &turn.CheckResponse{ |
| 419 | + PullRequest: prx.PullRequest{ |
| 420 | + State: "open", |
| 421 | + }, |
| 422 | + Analysis: turn.Analysis{ |
| 423 | + NextAction: map[string]turn.Action{ |
| 424 | + "_system": {Kind: "processing"}, |
| 425 | + }, |
| 426 | + }, |
| 427 | + }, |
| 428 | + wantPost: false, |
| 429 | + wantReasonPart: "not_blocked_yet", |
| 430 | + }, |
| 431 | + { |
| 432 | + name: "passing: posts when in review state", |
| 433 | + when: "passing", |
| 434 | + checkResult: &turn.CheckResponse{ |
| 435 | + PullRequest: prx.PullRequest{ |
| 436 | + State: "open", |
| 437 | + }, |
| 438 | + Analysis: turn.Analysis{ |
| 439 | + WorkflowState: string(turn.StateAssignedWaitingForReview), |
| 440 | + }, |
| 441 | + }, |
| 442 | + wantPost: true, |
| 443 | + wantReasonPart: "workflow_state", |
| 444 | + }, |
| 445 | + { |
| 446 | + name: "passing: does not post when tests pending", |
| 447 | + when: "passing", |
| 448 | + checkResult: &turn.CheckResponse{ |
| 449 | + PullRequest: prx.PullRequest{ |
| 450 | + State: "open", |
| 451 | + }, |
| 452 | + Analysis: turn.Analysis{ |
| 453 | + WorkflowState: string(turn.StatePublishedWaitingForTests), |
| 454 | + }, |
| 455 | + }, |
| 456 | + wantPost: false, |
| 457 | + wantReasonPart: "waiting_for", |
| 458 | + }, |
| 459 | + { |
| 460 | + name: "passing: uses fallback when workflow state unknown and tests passing", |
| 461 | + when: "passing", |
| 462 | + checkResult: &turn.CheckResponse{ |
| 463 | + PullRequest: prx.PullRequest{ |
| 464 | + State: "open", |
| 465 | + }, |
| 466 | + Analysis: turn.Analysis{ |
| 467 | + WorkflowState: "unknown_state", |
| 468 | + Checks: turn.Checks{ |
| 469 | + Passing: 5, |
| 470 | + Failing: 0, |
| 471 | + Pending: 0, |
| 472 | + Waiting: 0, |
| 473 | + }, |
| 474 | + }, |
| 475 | + }, |
| 476 | + wantPost: true, |
| 477 | + wantReasonPart: "tests_passed_fallback", |
| 478 | + }, |
| 479 | + { |
| 480 | + name: "passing: uses fallback when tests failing", |
| 481 | + when: "passing", |
| 482 | + checkResult: &turn.CheckResponse{ |
| 483 | + PullRequest: prx.PullRequest{ |
| 484 | + State: "open", |
| 485 | + }, |
| 486 | + Analysis: turn.Analysis{ |
| 487 | + WorkflowState: "unknown_state", |
| 488 | + Checks: turn.Checks{ |
| 489 | + Passing: 3, |
| 490 | + Failing: 2, |
| 491 | + }, |
| 492 | + }, |
| 493 | + }, |
| 494 | + wantPost: false, |
| 495 | + wantReasonPart: "tests_failing", |
| 496 | + }, |
| 497 | + { |
| 498 | + name: "nil check result returns false", |
| 499 | + when: "passing", |
| 500 | + checkResult: nil, |
| 501 | + wantPost: false, |
| 502 | + wantReasonPart: "no_check_result", |
| 503 | + }, |
| 504 | + { |
| 505 | + name: "invalid when value defaults to immediate", |
| 506 | + when: "invalid_value", |
| 507 | + checkResult: &turn.CheckResponse{ |
| 508 | + PullRequest: prx.PullRequest{ |
| 509 | + State: "open", |
| 510 | + }, |
| 511 | + }, |
| 512 | + wantPost: true, |
| 513 | + wantReasonPart: "invalid_config", |
| 514 | + }, |
| 515 | + } |
| 516 | + |
| 517 | + for _, tt := range tests { |
| 518 | + t.Run(tt.name, func(t *testing.T) { |
| 519 | + gotPost, gotReason := coord.shouldPostThread(tt.checkResult, tt.when) |
| 520 | + |
| 521 | + if gotPost != tt.wantPost { |
| 522 | + t.Errorf("shouldPostThread() gotPost = %v, wantPost %v", gotPost, tt.wantPost) |
| 523 | + } |
| 524 | + |
| 525 | + if tt.wantReasonPart != "" && !contains(gotReason, tt.wantReasonPart) { |
| 526 | + t.Errorf("shouldPostThread() reason = %q, want to contain %q", gotReason, tt.wantReasonPart) |
| 527 | + } |
| 528 | + }) |
| 529 | + } |
| 530 | +} |
| 531 | + |
| 532 | +// Helper function to check if a string contains a substring |
| 533 | +func contains(s, substr string) bool { |
| 534 | + return len(s) >= len(substr) && (s == substr || len(s) > len(substr) && (s[:len(substr)] == substr || s[len(s)-len(substr):] == substr || containsMiddle(s, substr))) |
| 535 | +} |
| 536 | + |
| 537 | +func containsMiddle(s, substr string) bool { |
| 538 | + for i := 0; i <= len(s)-len(substr); i++ { |
| 539 | + if s[i:i+len(substr)] == substr { |
| 540 | + return true |
| 541 | + } |
| 542 | + } |
| 543 | + return false |
| 544 | +} |
0 commit comments