Adds SearchCode to gitea.Client and code_search MCP tool for single-repo
code search via GET /api/v1/repos/{owner}/{repo}/search?type=code.
Fan-out placeholder returns ErrValidation (lands in 7.3).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
package gitea_test
|
|
|
|
import (
|
|
"context"
|
|
"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 TestSearchCode(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, "/api/v1/repos/mathias/infra/search", r.URL.Path)
|
|
assert.Equal(t, "SearchCode", r.URL.Query().Get("q"))
|
|
assert.Equal(t, "code", r.URL.Query().Get("type"))
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{
|
|
"data":[{
|
|
"path":"internal/gitea/code_search.go",
|
|
"snippet":"func (c *Client) SearchCode",
|
|
"html_url":"http://gitea.example.com/mathias/infra/src/branch/main/internal/gitea/code_search.go",
|
|
"score":2.5
|
|
}],
|
|
"ok":true
|
|
}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := gitea.NewClient(srv.URL, "tok")
|
|
hits, err := c.SearchCode(context.Background(), "mathias", "infra", "SearchCode", 1, 30)
|
|
require.NoError(t, err)
|
|
require.Len(t, hits, 1)
|
|
assert.Equal(t, "internal/gitea/code_search.go", hits[0].Path)
|
|
assert.Equal(t, "func (c *Client) SearchCode", hits[0].Snippet)
|
|
assert.InDelta(t, 2.5, hits[0].Score, 0.001)
|
|
}
|