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 TestListLabels(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/labels", r.URL.Path) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`[ {"id":1,"name":"bug","color":"ee0701"}, {"id":2,"name":"enhancement","color":"84b6eb"} ]`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") labels, err := c.ListLabels(context.Background(), "o", "r") require.NoError(t, err) require.Len(t, labels, 2) assert.Equal(t, int64(1), labels[0].ID) assert.Equal(t, "bug", labels[0].Name) assert.Equal(t, "ee0701", labels[0].Color) assert.Equal(t, "enhancement", labels[1].Name) } func TestListLabels_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") labels, err := c.ListLabels(context.Background(), "o", "r") require.NoError(t, err) assert.Empty(t, labels) } func TestListLabels_NotFound(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) _, _ = w.Write([]byte(`{"message":"repo not found"}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") _, err := c.ListLabels(context.Background(), "o", "r") require.Error(t, err) assert.ErrorIs(t, err, gitea.ErrNotFound) } func TestAddIssueLabels(t *testing.T) { var captured []byte srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodPost, r.Method) assert.Equal(t, "/api/v1/repos/o/r/issues/42/labels", 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(`[ {"id":1,"name":"bug","color":"ee0701"}, {"id":3,"name":"priority","color":"00ff00"} ]`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") labels, err := c.AddIssueLabels(context.Background(), "o", "r", 42, []int64{3}) require.NoError(t, err) var payload map[string]any require.NoError(t, json.Unmarshal(captured, &payload)) ids, ok := payload["labels"].([]any) require.True(t, ok) require.Len(t, ids, 1) assert.Equal(t, float64(3), ids[0]) require.Len(t, labels, 2) assert.Equal(t, "bug", labels[0].Name) assert.Equal(t, "priority", labels[1].Name) } func TestAddIssueLabels_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.AddIssueLabels(context.Background(), "o", "r", 999, []int64{1}) require.Error(t, err) assert.ErrorIs(t, err, gitea.ErrNotFound) }