<commit_before>package reviewdog

import (
	"context"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"os"
	"testing"

	"github.com/kylelemons/godebug/pretty"
	"github.com/xanzy/go-gitlab"
)

const noGitlabTokenSkipTestMes = "skipping test (requires actual Personal access tokens. export REVIEWDOG_TEST_GITHUB_API_TOKEN=<GitLab Personal Access Token>)"

func setupGitLabClient() *gitlab.Client {
	token := os.Getenv("REVIEWDOG_TEST_GITLAB_API_TOKEN")
	if token == "" {
		return nil
	}
	cli := gitlab.NewClient(nil, token)
	cli.SetBaseURL("https://gitlab.com/api/v4")
	return cli
}

func TestGitLabMergeRequest_Post(t *testing.T) {
	t.Skip("skipping test which post comments actually")
	client := setupGitLabClient()
	if client == nil {
		t.Skip(noGitlabTokenSkipTestMes)
	}

	// https://gitlab.com/nakatanakatana/reviewdog/merge_requests/1
	owner := "nakatanakatana"
	repo := "reviewdog"
	pr := 1
	sha := "bc328521a974c23acb24e8ebf51c1c2dcdb4fe6a"

	g, err := NewGitLabMergeReqest(client, owner, repo, pr, sha)
	if err != nil {
		t.Fatal(err)
	}
	comment := &Comment{
		CheckResult: &CheckResult{
			Path: "diff.go",
			Lnum: 22,
		},
		LnumDiff: 11,
		Body:     "[reviewdog] test",
	}
	// https://gitlab.com/nakatanakatana/reviewdog/merge_requests/1
	if err := g.Post(context.Background(), comment); err != nil {
		t.Error(err)
	}
	if err := g.Flush(context.Background()); err != nil {
		t.Error(err)
	}
}

func TestGitLabMergeRequest_Diff(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test which contains actual API requests in short mode")
	}
	client := setupGitLabClient()
	if client == nil {
		t.Skip(notokenSkipTestMes)
	}

	want := `diff --git a/diff.go b/diff.go
index 496d0d8..f06d633 100644
--- a/diff.go
+++ b/diff.go
@@ -13,14 +13,15 @@ type DiffString struct {
 	strip int
 }
 
-func NewDiffString(diff string, strip int) DiffService {
-	return &DiffString{b: []byte(diff), strip: strip}
-}
 
 func (d *DiffString) Diff(_ context.Context) ([]byte, error) {
 	return d.b, nil
 }
 
+func NewDiffString(diff string, strip int) DiffService {
+	return &DiffString{b: []byte(diff), strip: strip}
+}
+
 func (d *DiffString) Strip() int {
 	return d.strip
 }
`

	// https://gitlab.com/nakatanakatana/reviewdog/merge_requests/1
	owner := "nakatanakatana"
	repo := "reviewdog"
	pr := 1
	g, err := NewGitLabMergeReqest(client, owner, repo, pr, "")
	if err != nil {
		t.Fatal(err)
	}
	b, err := g.Diff(context.Background())
	if err != nil {
		t.Fatal(err)
	}
	if got := string(b); got != want {
		t.Errorf("got:\n%v\nwant:\n%v", got, want)
	}
}

func TestGitLabMergeRequest_comment(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test which contains actual API requests in short mode")
	}
	client := setupGitLabClient()
	if client == nil {
		t.Skip(notokenSkipTestMes)
	}
	// https://gitlab.com/nakatanakatana/reviewdog/merge_requests/1
	owner := "nakatanakatana"
	repo := "reviewdog"
	pr := 1
	g, err := NewGitLabMergeReqest(client, owner, repo, pr, "")
	if err != nil {
		t.Fatal(err)
	}
	comments, err := g.comment(context.Background())
	if err != nil {
		t.Fatal(err)
	}
	for _, c := range comments {
		t.Log("---")
		t.Log(c.Note)
		t.Log(c.Path)
		t.Log(c.Line)
	}
}

func TestGitLabPullRequest_Post_Flush_review_api(t *testing.T) {
	apiCalled := 0
	mux := http.NewServeMux()
	mux.HandleFunc("/projects/o/r/merge_requests/14/commits", func(w http.ResponseWriter, r *http.Request) {
		apiCalled++
		if r.Method != "GET" {
			t.Errorf("unexpected access: %v %v", r.Method, r.URL)
		}
		cs := []*gitlab.Commit{
			{
				ID:      "0123456789abcdef",
				ShortID: "012345678",
			},
		}
		if err := json.NewEncoder(w).Encode(cs); err != nil {
			t.Fatal(err)
		}
	})
	mux.HandleFunc("/projects/o/r/repository/commits/0123456789abcdef/comments", func(w http.ResponseWriter, r *http.Request) {
		apiCalled++
		if r.Method != "GET" {
			t.Errorf("unexpected access: %v %v", r.Method, r.URL)
		}
		cs := []*gitlab.CommitComment{
			{
				Path: "notExistFile.go",
				Line: 1,
				Note: bodyPrefix + "\nalready commented",
			},
		}
		if err := json.NewEncoder(w).Encode(cs); err != nil {
			t.Fatal(err)
		}
	})
	mux.HandleFunc("/projects/o/r/repository/commits/sha/comments", func(w http.ResponseWriter, r *http.Request) {
		apiCalled++
		if r.Method != "POST" {
			t.Errorf("unexpected access: %v %v", r.Method, r.URL)<commit_msg>test: fix GitLab API URL<commit_after>package reviewdog

import (
	"context"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"os"
	"testing"

	"github.com/kylelemons/godebug/pretty"
	"github.com/xanzy/go-gitlab"
)

const noGitlabTokenSkipTestMes = "skipping test (requires actual Personal access tokens. export REVIEWDOG_TEST_GITHUB_API_TOKEN=<GitLab Personal Access Token>)"

func setupGitLabClient() *gitlab.Client {
	token := os.Getenv("REVIEWDOG_TEST_GITLAB_API_TOKEN")
	if token == "" {
		return nil
	}
	cli := gitlab.NewClient(nil, token)
	cli.SetBaseURL("https://gitlab.com/api/v4")
	return cli
}

func TestGitLabMergeRequest_Post(t *testing.T) {
	t.Skip("skipping test which post comments actually")
	client := setupGitLabClient()
	if client == nil {
		t.Skip(noGitlabTokenSkipTestMes)
	}

	// https://gitlab.com/nakatanakatana/reviewdog/merge_requests/1
	owner := "nakatanakatana"
	repo := "reviewdog"
	pr := 1
	sha := "bc328521a974c23acb24e8ebf51c1c2dcdb4fe6a"

	g, err := NewGitLabMergeReqest(client, owner, repo, pr, sha)
	if err != nil {
		t.Fatal(err)
	}
	comment := &Comment{
		CheckResult: &CheckResult{
			Path: "diff.go",
			Lnum: 22,
		},
		LnumDiff: 11,
		Body:     "[reviewdog] test",
	}
	// https://gitlab.com/nakatanakatana/reviewdog/merge_requests/1
	if err := g.Post(context.Background(), comment); err != nil {
		t.Error(err)
	}
	if err := g.Flush(context.Background()); err != nil {
		t.Error(err)
	}
}

func TestGitLabMergeRequest_Diff(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test which contains actual API requests in short mode")
	}
	client := setupGitLabClient()
	if client == nil {
		t.Skip(notokenSkipTestMes)
	}

	want := `diff --git a/diff.go b/diff.go
index 496d0d8..f06d633 100644
--- a/diff.go
+++ b/diff.go
@@ -13,14 +13,15 @@ type DiffString struct {
 	strip int
 }
 
-func NewDiffString(diff string, strip int) DiffService {
-	return &DiffString{b: []byte(diff), strip: strip}
-}
 
 func (d *DiffString) Diff(_ context.Context) ([]byte, error) {
 	return d.b, nil
 }
 
+func NewDiffString(diff string, strip int) DiffService {
+	return &DiffString{b: []byte(diff), strip: strip}
+}
+
 func (d *DiffString) Strip() int {
 	return d.strip
 }
`

	// https://gitlab.com/nakatanakatana/reviewdog/merge_requests/1
	owner := "nakatanakatana"
	repo := "reviewdog"
	pr := 1
	g, err := NewGitLabMergeReqest(client, owner, repo, pr, "")
	if err != nil {
		t.Fatal(err)
	}
	b, err := g.Diff(context.Background())
	if err != nil {
		t.Fatal(err)
	}
	if got := string(b); got != want {
		t.Errorf("got:\n%v\nwant:\n%v", got, want)
	}
}

func TestGitLabMergeRequest_comment(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test which contains actual API requests in short mode")
	}
	client := setupGitLabClient()
	if client == nil {
		t.Skip(notokenSkipTestMes)
	}
	// https://gitlab.com/nakatanakatana/reviewdog/merge_requests/1
	owner := "nakatanakatana"
	repo := "reviewdog"
	pr := 1
	g, err := NewGitLabMergeReqest(client, owner, repo, pr, "")
	if err != nil {
		t.Fatal(err)
	}
	comments, err := g.comment(context.Background())
	if err != nil {
		t.Fatal(err)
	}
	for _, c := range comments {
		t.Log("---")
		t.Log(c.Note)
		t.Log(c.Path)
		t.Log(c.Line)
	}
}

func TestGitLabPullRequest_Post_Flush_review_api(t *testing.T) {
	apiCalled := 0
	mux := http.NewServeMux()
	mux.HandleFunc("/api/v4/projects/o/r/merge_requests/14/commits", func(w http.ResponseWriter, r *http.Request) {
		apiCalled++
		if r.Method != "GET" {
			t.Errorf("unexpected access: %v %v", r.Method, r.URL)
		}
		cs := []*gitlab.Commit{
			{
				ID:      "0123456789abcdef",
				ShortID: "012345678",
			},
		}
		if err := json.NewEncoder(w).Encode(cs); err != nil {
			t.Fatal(err)
		}
	})
	mux.HandleFunc("/api/v4/projects/o/r/repository/commits/0123456789abcdef/comments", func(w http.ResponseWriter, r *http.Request) {
		apiCalled++
		if r.Method != "GET" {
			t.Errorf("unexpected access: %v %v", r.Method, r.URL)
		}
		cs := []*gitlab.CommitComment{
			{
				Path: "notExistFile.go",
				Line: 1,
				Note: bodyPrefix + "\nalready commented",
			},
		}
		if err := json.NewEncoder(w).Encode(cs); err != nil {
			t.Fatal(err)
		}
	})
	mux.HandleFunc("/api/v4/projects/o/r/repository/commits/sha/comments", func(w http.ResponseWriter, r *http.Request) {
		apiCalled++
		if r.Method != "POST" {
			t.Errorf("unexpected access: %v %v", r.Method, r.URL)
{'fraction_non_alphanumeric': 0.16536964980544747, 'fraction_numerical': 0.020714122224765392, 'mean_word_length': 3.8308457711442787, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3460478', 'n_tokens_mistral': 3769, 'n_tokens_neox': 3432, 'n_words': 691}