|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +// TestCountPRsWithHiddenOrgs tests that PRs from hidden orgs are not counted |
| 9 | +func TestCountPRsWithHiddenOrgs(t *testing.T) { |
| 10 | + app := &App{ |
| 11 | + incoming: []PR{ |
| 12 | + {Repository: "org1/repo1", NeedsReview: true, UpdatedAt: time.Now()}, |
| 13 | + {Repository: "org2/repo2", NeedsReview: true, UpdatedAt: time.Now()}, |
| 14 | + {Repository: "org3/repo3", NeedsReview: true, UpdatedAt: time.Now()}, |
| 15 | + }, |
| 16 | + outgoing: []PR{ |
| 17 | + {Repository: "org1/repo4", IsBlocked: true, UpdatedAt: time.Now()}, |
| 18 | + {Repository: "org2/repo5", IsBlocked: true, UpdatedAt: time.Now()}, |
| 19 | + }, |
| 20 | + hiddenOrgs: map[string]bool{ |
| 21 | + "org2": true, // Hide org2 |
| 22 | + }, |
| 23 | + hideStaleIncoming: false, |
| 24 | + } |
| 25 | + |
| 26 | + counts := app.countPRs() |
| 27 | + |
| 28 | + // Should only count PRs from org1 and org3, not org2 |
| 29 | + if counts.IncomingTotal != 2 { |
| 30 | + t.Errorf("IncomingTotal = %d, want 2 (org2 should be hidden)", counts.IncomingTotal) |
| 31 | + } |
| 32 | + if counts.IncomingBlocked != 2 { |
| 33 | + t.Errorf("IncomingBlocked = %d, want 2 (org2 should be hidden)", counts.IncomingBlocked) |
| 34 | + } |
| 35 | + if counts.OutgoingTotal != 1 { |
| 36 | + t.Errorf("OutgoingTotal = %d, want 1 (org2 should be hidden)", counts.OutgoingTotal) |
| 37 | + } |
| 38 | + if counts.OutgoingBlocked != 1 { |
| 39 | + t.Errorf("OutgoingBlocked = %d, want 1 (org2 should be hidden)", counts.OutgoingBlocked) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// TestCountPRsWithStalePRs tests that stale PRs are not counted when hideStaleIncoming is true |
| 44 | +func TestCountPRsWithStalePRs(t *testing.T) { |
| 45 | + now := time.Now() |
| 46 | + staleTime := now.Add(-100 * 24 * time.Hour) // 100 days ago |
| 47 | + recentTime := now.Add(-1 * time.Hour) // 1 hour ago |
| 48 | + |
| 49 | + app := &App{ |
| 50 | + incoming: []PR{ |
| 51 | + {Repository: "org1/repo1", NeedsReview: true, UpdatedAt: staleTime}, |
| 52 | + {Repository: "org1/repo2", NeedsReview: true, UpdatedAt: recentTime}, |
| 53 | + {Repository: "org2/repo3", NeedsReview: false, UpdatedAt: staleTime}, |
| 54 | + }, |
| 55 | + outgoing: []PR{ |
| 56 | + {Repository: "org1/repo4", IsBlocked: true, UpdatedAt: staleTime}, |
| 57 | + {Repository: "org1/repo5", IsBlocked: true, UpdatedAt: recentTime}, |
| 58 | + }, |
| 59 | + hiddenOrgs: map[string]bool{}, |
| 60 | + hideStaleIncoming: true, // Hide stale PRs |
| 61 | + } |
| 62 | + |
| 63 | + counts := app.countPRs() |
| 64 | + |
| 65 | + // Should only count recent PRs |
| 66 | + if counts.IncomingTotal != 1 { |
| 67 | + t.Errorf("IncomingTotal = %d, want 1 (stale PRs should be hidden)", counts.IncomingTotal) |
| 68 | + } |
| 69 | + if counts.IncomingBlocked != 1 { |
| 70 | + t.Errorf("IncomingBlocked = %d, want 1 (stale PRs should be hidden)", counts.IncomingBlocked) |
| 71 | + } |
| 72 | + if counts.OutgoingTotal != 1 { |
| 73 | + t.Errorf("OutgoingTotal = %d, want 1 (stale PRs should be hidden)", counts.OutgoingTotal) |
| 74 | + } |
| 75 | + if counts.OutgoingBlocked != 1 { |
| 76 | + t.Errorf("OutgoingBlocked = %d, want 1 (stale PRs should be hidden)", counts.OutgoingBlocked) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// TestCountPRsWithBothFilters tests that both filters work together |
| 81 | +func TestCountPRsWithBothFilters(t *testing.T) { |
| 82 | + now := time.Now() |
| 83 | + staleTime := now.Add(-100 * 24 * time.Hour) |
| 84 | + recentTime := now.Add(-1 * time.Hour) |
| 85 | + |
| 86 | + app := &App{ |
| 87 | + incoming: []PR{ |
| 88 | + {Repository: "org1/repo1", NeedsReview: true, UpdatedAt: recentTime}, // Should be counted |
| 89 | + {Repository: "org2/repo2", NeedsReview: true, UpdatedAt: recentTime}, // Hidden org |
| 90 | + {Repository: "org3/repo3", NeedsReview: true, UpdatedAt: staleTime}, // Stale |
| 91 | + {Repository: "org1/repo4", NeedsReview: false, UpdatedAt: recentTime}, // Not blocked |
| 92 | + }, |
| 93 | + outgoing: []PR{ |
| 94 | + {Repository: "org1/repo5", IsBlocked: true, UpdatedAt: recentTime}, // Should be counted |
| 95 | + {Repository: "org2/repo6", IsBlocked: true, UpdatedAt: recentTime}, // Hidden org |
| 96 | + {Repository: "org3/repo7", IsBlocked: true, UpdatedAt: staleTime}, // Stale |
| 97 | + }, |
| 98 | + hiddenOrgs: map[string]bool{ |
| 99 | + "org2": true, |
| 100 | + }, |
| 101 | + hideStaleIncoming: true, |
| 102 | + } |
| 103 | + |
| 104 | + counts := app.countPRs() |
| 105 | + |
| 106 | + // Should only count org1/repo1 (incoming) and org1/repo5 (outgoing) |
| 107 | + if counts.IncomingTotal != 2 { |
| 108 | + t.Errorf("IncomingTotal = %d, want 2", counts.IncomingTotal) |
| 109 | + } |
| 110 | + if counts.IncomingBlocked != 1 { |
| 111 | + t.Errorf("IncomingBlocked = %d, want 1", counts.IncomingBlocked) |
| 112 | + } |
| 113 | + if counts.OutgoingTotal != 1 { |
| 114 | + t.Errorf("OutgoingTotal = %d, want 1", counts.OutgoingTotal) |
| 115 | + } |
| 116 | + if counts.OutgoingBlocked != 1 { |
| 117 | + t.Errorf("OutgoingBlocked = %d, want 1", counts.OutgoingBlocked) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// TestExtractOrgFromRepo tests the org extraction function |
| 122 | +func TestExtractOrgFromRepo(t *testing.T) { |
| 123 | + tests := []struct { |
| 124 | + repo string |
| 125 | + name string |
| 126 | + want string |
| 127 | + }{ |
| 128 | + { |
| 129 | + name: "standard repo path", |
| 130 | + repo: "microsoft/vscode", |
| 131 | + want: "microsoft", |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "single segment", |
| 135 | + repo: "justarepo", |
| 136 | + want: "justarepo", |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "empty string", |
| 140 | + repo: "", |
| 141 | + want: "", |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "nested path", |
| 145 | + repo: "org/repo/subpath", |
| 146 | + want: "org", |
| 147 | + }, |
| 148 | + } |
| 149 | + |
| 150 | + for _, tt := range tests { |
| 151 | + t.Run(tt.name, func(t *testing.T) { |
| 152 | + if got := extractOrgFromRepo(tt.repo); got != tt.want { |
| 153 | + t.Errorf("extractOrgFromRepo(%q) = %q, want %q", tt.repo, got, tt.want) |
| 154 | + } |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
0 commit comments