package tools_test import ( "context" "encoding/json" "io" "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" ) const issueFixture = `{ "number": 42, "title": "x", "body": "y", "html_url": "http://example.com/issues/42", "state": "open" }` func TestIssueCreateAppliesIdentityFooter(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) var err error captured, err = io.ReadAll(r.Body) require.NoError(t, err) w.WriteHeader(http.StatusCreated) _, _ = w.Write([]byte(issueFixture)) })) defer srv.Close() tool := tools.NewIssueCreate(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"o"})) ctx := callerContext("mathiasbq") _, err := tool.Call(ctx, json.RawMessage(`{"owner":"o","name":"r","title":"x","body":"y"}`)) require.NoError(t, err) var payload map[string]any require.NoError(t, json.Unmarshal(captured, &payload)) body, _ := payload["body"].(string) assert.Contains(t, body, "_Created via git-mcp on behalf of @mathiasbq_") } func TestIssueCreateNoFooterWhenCallerEmpty(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.WriteHeader(http.StatusCreated) _, _ = w.Write([]byte(issueFixture)) })) defer srv.Close() tool := tools.NewIssueCreate(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"o"})) _, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"o","name":"r","title":"x","body":"y"}`)) require.NoError(t, err) var payload map[string]any require.NoError(t, json.Unmarshal(captured, &payload)) body, _ := payload["body"].(string) assert.NotContains(t, body, "_Created via git-mcp on behalf of") } func TestIssueCreateAllowlistRejects(t *testing.T) { tool := tools.NewIssueCreate(gitea.NewClient("http://unused", ""), allowlist.New([]string{"allowed"})) _, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"evil","name":"r","title":"T"}`)) require.Error(t, err) } func TestIssueCreateRequiresTitle(t *testing.T) { tool := tools.NewIssueCreate(gitea.NewClient("http://unused", ""), allowlist.New([]string{"o"})) _, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"o","name":"r","title":""}`)) require.Error(t, err) assert.ErrorIs(t, err, gitea.ErrValidation) }