package gitea_test import ( "context" "encoding/json" "io" "net/http" "net/http/httptest" "testing" "git.d-ma.be/mathias/gitea-mcp/internal/gitea" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestCreateIssue(t *testing.T) { var captured []byte srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/api/v1/repos/o/r/issues", r.URL.Path) assert.Equal(t, http.MethodPost, r.Method) var err error captured, err = io.ReadAll(r.Body) require.NoError(t, err) w.WriteHeader(http.StatusCreated) _, _ = w.Write([]byte(`{"number":42,"title":"x","body":"y","html_url":"http://example.com/issues/42","state":"open"}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") iss, err := c.CreateIssue(context.Background(), "o", "r", gitea.CreateIssueArgs{ Title: "x", Body: "y", }) require.NoError(t, err) var payload map[string]any require.NoError(t, json.Unmarshal(captured, &payload)) assert.Equal(t, "x", payload["title"]) assert.Equal(t, "y", payload["body"]) assert.Equal(t, 42, iss.Number) assert.Equal(t, "x", iss.Title) assert.Equal(t, "y", iss.Body) assert.Equal(t, "http://example.com/issues/42", iss.HTMLURL) assert.Equal(t, "open", iss.State) } func TestGetIssue(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodGet, r.Method) assert.Equal(t, "/api/v1/repos/o/r/issues/42", r.URL.Path) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"number":42,"title":"fix auth","body":"details","state":"open","html_url":"http://example.com/issues/42","created_at":"2026-05-01T00:00:00Z","updated_at":"2026-05-02T00:00:00Z","comments":3}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") iss, err := c.GetIssue(context.Background(), "o", "r", 42) require.NoError(t, err) assert.Equal(t, 42, iss.Number) assert.Equal(t, "fix auth", iss.Title) assert.Equal(t, "open", iss.State) assert.Equal(t, 3, iss.Comments) } func TestGetIssue_NotFound(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) _, _ = w.Write([]byte(`{"message":"issue not found"}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") _, err := c.GetIssue(context.Background(), "o", "r", 999) require.Error(t, err) assert.ErrorIs(t, err, gitea.ErrNotFound) } func TestEditIssue(t *testing.T) { var captured []byte srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodPatch, r.Method) assert.Equal(t, "/api/v1/repos/o/r/issues/42", r.URL.Path) var err error captured, err = io.ReadAll(r.Body) require.NoError(t, err) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"number":42,"title":"new title","body":"new body","state":"open","html_url":"http://example.com/issues/42"}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") title, body := "new title", "new body" iss, err := c.EditIssue(context.Background(), "o", "r", 42, gitea.EditIssueArgs{Title: &title, Body: &body}) require.NoError(t, err) var payload map[string]any require.NoError(t, json.Unmarshal(captured, &payload)) assert.Equal(t, "new title", payload["title"]) assert.Equal(t, "new body", payload["body"]) assert.Equal(t, 42, iss.Number) assert.Equal(t, "new title", iss.Title) } // EditIssue must send only the fields explicitly provided — an omitted field // (nil pointer) is left untouched server-side. func TestEditIssue_PartialPatchOmitsUnsetFields(t *testing.T) { var captured []byte srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var err error captured, err = io.ReadAll(r.Body) require.NoError(t, err) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"number":42,"title":"only title","state":"open"}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") title := "only title" _, err := c.EditIssue(context.Background(), "o", "r", 42, gitea.EditIssueArgs{Title: &title}) require.NoError(t, err) var payload map[string]any require.NoError(t, json.Unmarshal(captured, &payload)) assert.Equal(t, "only title", payload["title"]) _, hasBody := payload["body"] assert.False(t, hasBody, "body must be omitted when not set") } // An explicit empty-string body clears the field — pointer-to-"" is sent, not omitted. func TestEditIssue_EmptyBodyIsSent(t *testing.T) { var captured []byte srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var err error captured, err = io.ReadAll(r.Body) require.NoError(t, err) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"number":42,"body":"","state":"open"}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") body := "" _, err := c.EditIssue(context.Background(), "o", "r", 42, gitea.EditIssueArgs{Body: &body}) require.NoError(t, err) var payload map[string]any require.NoError(t, json.Unmarshal(captured, &payload)) val, hasBody := payload["body"] assert.True(t, hasBody, "explicit empty body must be sent so it can clear the field") assert.Equal(t, "", val) } func TestEditIssue_NotFound(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) _, _ = w.Write([]byte(`{"message":"issue not found"}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") title := "x" _, err := c.EditIssue(context.Background(), "o", "r", 999, gitea.EditIssueArgs{Title: &title}) require.Error(t, err) assert.ErrorIs(t, err, gitea.ErrNotFound) } func TestCreateIssueComment(t *testing.T) { var captured []byte srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/api/v1/repos/o/r/issues/42/comments", r.URL.Path) assert.Equal(t, http.MethodPost, r.Method) var err error captured, err = io.ReadAll(r.Body) require.NoError(t, err) w.WriteHeader(http.StatusCreated) _, _ = w.Write([]byte(`{"id":7,"body":"hello","html_url":"http://example.com/issues/42#comment-7"}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") comment, err := c.CreateIssueComment(context.Background(), "o", "r", 42, "hello") require.NoError(t, err) var payload map[string]any require.NoError(t, json.Unmarshal(captured, &payload)) assert.Equal(t, "hello", payload["body"]) assert.Equal(t, int64(7), comment.ID) assert.Equal(t, "hello", comment.Body) assert.Equal(t, "http://example.com/issues/42#comment-7", comment.HTMLURL) } func TestListIssueComments(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodGet, r.Method) assert.Equal(t, "/api/v1/repos/o/r/issues/42/comments", r.URL.Path) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`[ {"id":1,"body":"first","html_url":"http://example.com/issues/42#comment-1","user":{"login":"alice"},"created_at":"2026-05-01T00:00:00Z","updated_at":"2026-05-01T00:00:00Z"}, {"id":2,"body":"second","html_url":"http://example.com/issues/42#comment-2","user":{"login":"bob"},"created_at":"2026-05-02T00:00:00Z","updated_at":"2026-05-02T00:00:00Z"} ]`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") comments, err := c.ListIssueComments(context.Background(), "o", "r", 42) require.NoError(t, err) require.Len(t, comments, 2) assert.Equal(t, int64(1), comments[0].ID) assert.Equal(t, "first", comments[0].Body) assert.Equal(t, "alice", comments[0].User.Login) assert.Equal(t, "bob", comments[1].User.Login) } func TestListIssueComments_Empty(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`[]`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") comments, err := c.ListIssueComments(context.Background(), "o", "r", 42) require.NoError(t, err) assert.Empty(t, comments) } func TestListIssueComments_NotFound(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) _, _ = w.Write([]byte(`{"message":"issue not found"}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") _, err := c.ListIssueComments(context.Background(), "o", "r", 999) require.Error(t, err) assert.ErrorIs(t, err, gitea.ErrNotFound) }