2026-02-05 10:36:21 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
|
|
"github.com/host-uk/core/pkg/jobrunner"
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-08 23:15:41 +00:00
|
|
|
func TestDismissReviews_Match_Good(t *testing.T) {
|
|
|
|
|
h := NewDismissReviewsHandler(nil)
|
2026-02-05 10:36:21 +00:00
|
|
|
sig := &jobrunner.PipelineSignal{
|
|
|
|
|
PRState: "OPEN",
|
|
|
|
|
ThreadsTotal: 4,
|
|
|
|
|
ThreadsResolved: 2,
|
|
|
|
|
}
|
|
|
|
|
assert.True(t, h.Match(sig))
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 23:15:41 +00:00
|
|
|
func TestDismissReviews_Match_Bad_AllResolved(t *testing.T) {
|
|
|
|
|
h := NewDismissReviewsHandler(nil)
|
2026-02-05 10:36:21 +00:00
|
|
|
sig := &jobrunner.PipelineSignal{
|
|
|
|
|
PRState: "OPEN",
|
|
|
|
|
ThreadsTotal: 3,
|
|
|
|
|
ThreadsResolved: 3,
|
|
|
|
|
}
|
|
|
|
|
assert.False(t, h.Match(sig))
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 23:15:41 +00:00
|
|
|
func TestDismissReviews_Execute_Good(t *testing.T) {
|
2026-02-05 10:36:21 +00:00
|
|
|
callCount := 0
|
|
|
|
|
|
2026-02-08 23:15:41 +00:00
|
|
|
srv := httptest.NewServer(withVersion(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2026-02-05 10:36:21 +00:00
|
|
|
callCount++
|
2026-02-08 23:15:41 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2026-02-05 10:36:21 +00:00
|
|
|
|
2026-02-08 23:15:41 +00:00
|
|
|
// ListPullReviews (GET)
|
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
|
reviews := []map[string]any{
|
|
|
|
|
{
|
|
|
|
|
"id": 1, "state": "REQUEST_CHANGES", "dismissed": false, "stale": true,
|
|
|
|
|
"body": "fix this", "commit_id": "abc123",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 2, "state": "APPROVED", "dismissed": false, "stale": false,
|
|
|
|
|
"body": "looks good", "commit_id": "abc123",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 3, "state": "REQUEST_CHANGES", "dismissed": false, "stale": true,
|
|
|
|
|
"body": "needs work", "commit_id": "abc123",
|
|
|
|
|
},
|
2026-02-05 10:36:21 +00:00
|
|
|
}
|
2026-02-08 23:15:41 +00:00
|
|
|
_ = json.NewEncoder(w).Encode(reviews)
|
2026-02-05 10:36:21 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 23:15:41 +00:00
|
|
|
// DismissPullReview (POST to dismissals endpoint)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
})))
|
2026-02-05 10:36:21 +00:00
|
|
|
defer srv.Close()
|
|
|
|
|
|
2026-02-08 23:15:41 +00:00
|
|
|
client := newTestForgeClient(t, srv.URL)
|
|
|
|
|
|
|
|
|
|
h := NewDismissReviewsHandler(client)
|
2026-02-05 10:36:21 +00:00
|
|
|
sig := &jobrunner.PipelineSignal{
|
|
|
|
|
RepoOwner: "host-uk",
|
|
|
|
|
RepoName: "core-admin",
|
|
|
|
|
PRNumber: 33,
|
|
|
|
|
PRState: "OPEN",
|
|
|
|
|
ThreadsTotal: 3,
|
|
|
|
|
ThreadsResolved: 1,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := h.Execute(context.Background(), sig)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
assert.True(t, result.Success)
|
2026-02-08 23:15:41 +00:00
|
|
|
assert.Equal(t, "dismiss_reviews", result.Action)
|
2026-02-05 10:36:21 +00:00
|
|
|
assert.Equal(t, "host-uk", result.RepoOwner)
|
|
|
|
|
assert.Equal(t, "core-admin", result.RepoName)
|
|
|
|
|
assert.Equal(t, 33, result.PRNumber)
|
|
|
|
|
|
2026-02-08 23:15:41 +00:00
|
|
|
// 1 list + 2 dismiss (reviews #1 and #3 are stale REQUEST_CHANGES)
|
2026-02-05 10:36:21 +00:00
|
|
|
assert.Equal(t, 3, callCount)
|
|
|
|
|
}
|