package github import ( "bytes" "context" "encoding/json" "fmt" "net/http" "net/http/httptest" "testing" "time" "github.com/gin-gonic/gin" "github.com/kandev/kandev/internal/common/logger" ) // stubClient implements Client with no-op defaults; override fields as needed. type stubClient struct { getPRFunc func(ctx context.Context, owner, repo string, number int) (*PR, error) mergePRFn func(ctx context.Context, owner, repo string, number int, mergeMethod string) error } func (s *stubClient) IsAuthenticated(context.Context) (bool, error) { return true, nil } func (s *stubClient) GetAuthenticatedUser(context.Context) (string, error) { return "test-user", nil } func (s *stubClient) GetPR(ctx context.Context, owner, repo string, number int) (*PR, error) { if s.getPRFunc != nil { return s.getPRFunc(ctx, owner, repo, number) } return nil, fmt.Errorf("not implemented") } func (s *stubClient) FindPRByBranch(context.Context, string, string, string) (*PR, error) { return nil, nil } func (s *stubClient) ListAuthoredPRs(context.Context, string, string) ([]*PR, error) { return nil, nil } func (s *stubClient) SearchPRs(context.Context, string, string) ([]*PR, error) { return nil, nil } func (s *stubClient) SearchPRsPaged(context.Context, string, string, int, int) (*PRSearchPage, error) { return &PRSearchPage{PRs: []*PR{}}, nil } func (s *stubClient) ListReviewRequestedPRs(context.Context, string, string, string) ([]*PR, error) { return nil, nil } func (s *stubClient) ListUserOrgs(context.Context) ([]GitHubOrg, error) { return nil, nil } func (s *stubClient) SearchOrgRepos(context.Context, string, string, int) ([]GitHubRepo, error) { return nil, nil } func (s *stubClient) ListPRReviews(context.Context, string, string, int) ([]PRReview, error) { return nil, nil } func (s *stubClient) ListPRComments(context.Context, string, string, int, *time.Time) ([]PRComment, error) { return nil, nil } func (s *stubClient) ListCheckRuns(context.Context, string, string, string) ([]CheckRun, error) { return nil, nil } func (s *stubClient) GetPRFeedback(context.Context, string, string, int) (*PRFeedback, error) { return nil, nil } func (s *stubClient) ListPRFiles(context.Context, string, string, int) ([]PRFile, error) { return nil, nil } func (s *stubClient) ListPRCommits(context.Context, string, string, int) ([]PRCommitInfo, error) { return nil, nil } func (s *stubClient) SubmitReview(context.Context, string, string, int, string, string) error { return nil } func (s *stubClient) MergePR(ctx context.Context, owner, repo string, number int, mergeMethod string) error { if s.mergePRFn != nil { return s.mergePRFn(ctx, owner, repo, number, mergeMethod) } return nil } func (s *stubClient) ListRepoBranches(context.Context, string, string) ([]RepoBranch, error) { return nil, nil } func (s *stubClient) ListIssues(context.Context, string, string) ([]*Issue, error) { return nil, nil } func (s *stubClient) ListIssuesPaged(context.Context, string, string, int, int) (*IssueSearchPage, error) { return &IssueSearchPage{Issues: []*Issue{}}, nil } func (s *stubClient) GetIssueState(context.Context, string, string, int) (string, error) { return defaultPRState, nil } func (s *stubClient) GetPRStatus(context.Context, string, string, int) (*PRStatus, error) { return nil, nil } func newControllerTestLogger() *logger.Logger { log, _ := logger.NewLogger(logger.LoggingConfig{ Level: "error", Format: "json", }) return log } func setupControllerTest(client Client) (*gin.Engine, *Controller) { gin.SetMode(gin.TestMode) log := newControllerTestLogger() svc := NewService(client, "pat", nil, nil, nil, log) ctrl := NewController(svc, log) router := gin.New() ctrl.RegisterHTTPRoutes(router) return router, ctrl } func TestHttpGetPRInfo_Success(t *testing.T) { sc := &stubClient{ getPRFunc: func(_ context.Context, owner, repo string, number int) (*PR, error) { if owner != "acme" || repo != "widget" || number != 42 { t.Errorf("unexpected params: %s/%s#%d", owner, repo, number) } return &PR{Number: 42, Title: "feat: add widget"}, nil }, } router, _ := setupControllerTest(sc) req := httptest.NewRequest(http.MethodGet, "/api/v1/github/prs/acme/widget/42/info", nil) w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String()) } var pr PR if err := json.NewDecoder(w.Body).Decode(&pr); err != nil { t.Fatalf("failed to decode response: %v", err) } if pr.Number != 42 { t.Errorf("expected PR number 42, got %d", pr.Number) } if pr.Title != "feat: add widget" { t.Errorf("expected title 'feat: add widget', got %q", pr.Title) } } func TestHttpGetPRInfo_InvalidNumber(t *testing.T) { router, _ := setupControllerTest(&stubClient{}) req := httptest.NewRequest(http.MethodGet, "/api/v1/github/prs/acme/widget/abc/info", nil) w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != http.StatusBadRequest { t.Fatalf("expected 400, got %d", w.Code) } } func TestHttpGetPRInfo_ServiceError(t *testing.T) { sc := &stubClient{ getPRFunc: func(context.Context, string, string, int) (*PR, error) { return nil, fmt.Errorf("not found") }, } router, _ := setupControllerTest(sc) req := httptest.NewRequest(http.MethodGet, "/api/v1/github/prs/acme/widget/99/info", nil) w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != http.StatusInternalServerError { t.Fatalf("expected 500, got %d", w.Code) } } func TestHttpMergePR_Success(t *testing.T) { var called struct { owner string repo string number int mergeMethod string } sc := &stubClient{ mergePRFn: func(_ context.Context, owner, repo string, number int, mergeMethod string) error { called.owner = owner called.repo = repo called.number = number called.mergeMethod = mergeMethod return nil }, } router, _ := setupControllerTest(sc) body := bytes.NewBufferString(`{"merge_method":"squash"}`) req := httptest.NewRequest(http.MethodPut, "/api/v1/github/prs/acme/widget/42/merge", body) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String()) } if called.owner != "acme" || called.repo != "widget" || called.number != 42 || called.mergeMethod != "squash" { t.Errorf("unexpected MergePR args: %+v", called) } } func TestHttpMergePR_InvalidMethod(t *testing.T) { router, _ := setupControllerTest(&stubClient{}) body := bytes.NewBufferString(`{"merge_method":"bogus"}`) req := httptest.NewRequest(http.MethodPut, "/api/v1/github/prs/acme/widget/42/merge", body) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != http.StatusBadRequest { t.Fatalf("expected 400, got %d", w.Code) } } func TestHttpMergePR_EmptyBody_UsesDefaultMethod(t *testing.T) { var gotMethod string sc := &stubClient{ mergePRFn: func(_ context.Context, _, _ string, _ int, mergeMethod string) error { gotMethod = mergeMethod return nil }, } router, _ := setupControllerTest(sc) req := httptest.NewRequest(http.MethodPut, "/api/v1/github/prs/acme/widget/42/merge", nil) w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String()) } if gotMethod != "" { t.Errorf("expected empty merge_method, got %q", gotMethod) } } func TestHttpMergePR_MalformedJSON(t *testing.T) { router, _ := setupControllerTest(&stubClient{}) // Truncated JSON: parser fails with a non-EOF error, which must now // surface as 400 rather than silently falling through to the default // merge method. body := bytes.NewBufferString(`{"merge_method":"squash"`) req := httptest.NewRequest(http.MethodPut, "/api/v1/github/prs/acme/widget/42/merge", body) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != http.StatusBadRequest { t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String()) } } func TestHttpMergePR_NoClient_Returns503(t *testing.T) { gin.SetMode(gin.TestMode) log := newControllerTestLogger() // nil client triggers ErrNoClient via Service.MergePR. svc := NewService(nil, "none", nil, nil, nil, log) ctrl := NewController(svc, log) router := gin.New() ctrl.RegisterHTTPRoutes(router) req := httptest.NewRequest(http.MethodPut, "/api/v1/github/prs/acme/widget/42/merge", nil) w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != http.StatusServiceUnavailable { t.Fatalf("expected 503, got %d: %s", w.Code, w.Body.String()) } } func TestHttpMergePR_Conflict(t *testing.T) { sc := &stubClient{ mergePRFn: func(context.Context, string, string, int, string) error { return &GitHubAPIError{StatusCode: http.StatusMethodNotAllowed, Endpoint: "/merge", Body: "not mergeable"} }, } router, _ := setupControllerTest(sc) body := bytes.NewBufferString(`{"merge_method":"merge"}`) req := httptest.NewRequest(http.MethodPut, "/api/v1/github/prs/acme/widget/42/merge", body) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != http.StatusConflict { t.Fatalf("expected 409, got %d: %s", w.Code, w.Body.String()) } }