package gitea_test import ( "context" "encoding/json" "io" "net/http" "net/http/httptest" "testing" "gitea.d-ma.be/mathias/gitea-mcp/internal/gitea" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestGetFileContents(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/api/v1/repos/mathias/infra/contents/README.md", r.URL.Path) assert.Equal(t, "main", r.URL.Query().Get("ref")) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"path":"README.md","sha":"deadbeef","size":13,"content":"SGVsbG8sIHdvcmxkIQ==","encoding":"base64"}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") fc, err := c.GetFileContents(context.Background(), "mathias", "infra", "README.md", "main") require.NoError(t, err) assert.Equal(t, "README.md", fc.Path) assert.Equal(t, "deadbeef", fc.Sha) assert.Equal(t, int64(13), fc.Size) assert.Equal(t, "SGVsbG8sIHdvcmxkIQ==", fc.Content) } func TestBranchExistsTrue(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/api/v1/repos/o/r/branches/main", r.URL.Path) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"name":"main","commit":{"id":"abc123","url":"http://example.com"}}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") exists, err := c.BranchExists(context.Background(), "o", "r", "main") require.NoError(t, err) assert.True(t, exists) } func TestBranchExistsFalseOn404(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/api/v1/repos/o/r/branches/nonexistent", r.URL.Path) w.WriteHeader(http.StatusNotFound) _, _ = w.Write([]byte(`{"message":"branch not found"}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") exists, err := c.BranchExists(context.Background(), "o", "r", "nonexistent") require.NoError(t, err) assert.False(t, exists) } func TestCreateBranchSendsPayload(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/branches", 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(`{"name":"feat/x","commit":{"id":"abc","url":"http://example.com"}}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") err := c.CreateBranch(context.Background(), "o", "r", "feat/x", "main") require.NoError(t, err) var payload map[string]string require.NoError(t, json.Unmarshal(captured, &payload)) assert.Equal(t, "feat/x", payload["new_branch_name"]) assert.Equal(t, "main", payload["old_branch_name"]) } func TestUpsertFileSendsPayloadAndDecodesResult(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/contents/p.md", r.URL.Path) assert.Equal(t, http.MethodPut, r.Method) var err error captured, err = io.ReadAll(r.Body) require.NoError(t, err) w.WriteHeader(http.StatusCreated) _, _ = w.Write([]byte(`{"content":{"path":"p.md","sha":"newsha","html_url":"http://example.com/p.md"},"commit":{"sha":"abc","html_url":"http://example.com/commit/abc"}}`)) })) defer srv.Close() c := gitea.NewClient(srv.URL, "tok") result, err := c.UpsertFile(context.Background(), "o", "r", "p.md", gitea.UpsertFileArgs{ Branch: "feat/x", Content: "aGVsbG8=", Message: "add p.md", Sha: "oldsha", }) require.NoError(t, err) var payload map[string]string require.NoError(t, json.Unmarshal(captured, &payload)) assert.Equal(t, "feat/x", payload["branch"]) assert.Equal(t, "aGVsbG8=", payload["content"]) assert.Equal(t, "add p.md", payload["message"]) assert.Equal(t, "oldsha", payload["sha"]) assert.Equal(t, "p.md", result.Content.Path) assert.Equal(t, "newsha", result.Content.Sha) assert.Equal(t, "http://example.com/p.md", result.Content.HTMLURL) assert.Equal(t, "abc", result.Commit.Sha) }