package tools_test import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" "gitea.d-ma.be/mathias/gitea-mcp/internal/allowlist" "gitea.d-ma.be/mathias/gitea-mcp/internal/gitea" "gitea.d-ma.be/mathias/gitea-mcp/internal/tools" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestFileReadToolWithExplicitRef(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() tool := tools.NewFileRead(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"})) out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"infra","path":"README.md","ref":"main"}`)) require.NoError(t, err) var result map[string]any require.NoError(t, json.Unmarshal(out, &result)) assert.Equal(t, "README.md", result["path"]) assert.Equal(t, "main", result["ref"]) assert.Equal(t, "Hello, world!", result["content"]) } func TestFileReadToolDefaultBranchResolution(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/api/v1/repos/mathias/infra", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"name":"infra","full_name":"mathias/infra","default_branch":"main"}`)) }) mux.HandleFunc("/api/v1/repos/mathias/infra/contents/README.md", func(w http.ResponseWriter, r *http.Request) { 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"}`)) }) srv := httptest.NewServer(mux) defer srv.Close() tool := tools.NewFileRead(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"})) out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"infra","path":"README.md"}`)) require.NoError(t, err) var result map[string]any require.NoError(t, json.Unmarshal(out, &result)) assert.Equal(t, "main", result["ref"]) } func TestFileReadAllowlistRejects(t *testing.T) { tool := tools.NewFileRead(gitea.NewClient("http://unused", ""), allowlist.New([]string{"mathias"})) _, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"evil","name":"infra","path":"README.md"}`)) require.Error(t, err) }