|
| 1 | +package tools_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +const foreignChangeJSON = ")]}'\n" + `{"_number":123,"project":"core","branch":"main",` + |
| 16 | + `"owner":{"_account_id":7,"username":"alice"}}` |
| 17 | + |
| 18 | +// voteSession wires a fake Gerrit for the vote flow: self-check, change |
| 19 | +// resolve (owned by self), and a recording review POST. |
| 20 | +func voteSession(t *testing.T) (cs *mcp.ClientSession, body *map[string]any) { |
| 21 | + t.Helper() |
| 22 | + |
| 23 | + body = &map[string]any{} |
| 24 | + |
| 25 | + cs = session(t, func(w http.ResponseWriter, r *http.Request) { |
| 26 | + switch { |
| 27 | + case r.URL.Path == "/a/accounts/self": |
| 28 | + _, _ = w.Write([]byte(selfJSON)) |
| 29 | + |
| 30 | + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/review"): |
| 31 | + raw, err := io.ReadAll(r.Body) |
| 32 | + if err != nil { |
| 33 | + t.Errorf("read review body: %v", err) |
| 34 | + |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + if err := json.Unmarshal(raw, body); err != nil { |
| 39 | + t.Errorf("unmarshal review body: %v", err) |
| 40 | + |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + _, _ = w.Write([]byte(")]}'\n{}")) |
| 45 | + |
| 46 | + default: |
| 47 | + _, _ = w.Write([]byte(ownChangeJSON)) |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + return cs, body |
| 52 | +} |
| 53 | + |
| 54 | +func Test_SetVote(t *testing.T) { |
| 55 | + t.Parallel() |
| 56 | + |
| 57 | + t.Run("posts label, value, and message as a review", func(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + |
| 60 | + cs, body := voteSession(t) |
| 61 | + |
| 62 | + out := callTool(t, cs, "set_vote", map[string]any{ |
| 63 | + "change": "123", |
| 64 | + "label": "Code-Review", |
| 65 | + "value": 2, |
| 66 | + "message": "lgtm", |
| 67 | + }) |
| 68 | + |
| 69 | + assert.Contains(t, out, `<vote_set change="123" label="Code-Review" value="2"/>`) |
| 70 | + |
| 71 | + assert.Equal(t, "lgtm", (*body)["message"]) |
| 72 | + assert.Equal(t, map[string]any{"Code-Review": float64(2)}, (*body)["labels"]) |
| 73 | + }) |
| 74 | + |
| 75 | + t.Run("zero value stays on the wire to clear a vote", func(t *testing.T) { |
| 76 | + t.Parallel() |
| 77 | + |
| 78 | + cs, body := voteSession(t) |
| 79 | + |
| 80 | + callTool(t, cs, "set_vote", map[string]any{ |
| 81 | + "change": "123", |
| 82 | + "label": "Code-Review", |
| 83 | + "value": 0, |
| 84 | + }) |
| 85 | + |
| 86 | + assert.Equal(t, map[string]any{"Code-Review": float64(0)}, (*body)["labels"]) |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("empty label refused", func(t *testing.T) { |
| 90 | + t.Parallel() |
| 91 | + |
| 92 | + cs, body := voteSession(t) |
| 93 | + |
| 94 | + res, err := cs.CallTool(t.Context(), &mcp.CallToolParams{ |
| 95 | + Name: "set_vote", |
| 96 | + Arguments: map[string]any{"change": "123", "label": " ", "value": 1}, |
| 97 | + }) |
| 98 | + require.NoError(t, err) |
| 99 | + require.True(t, res.IsError) |
| 100 | + |
| 101 | + assert.Empty(t, *body, "no review may be posted") |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("gerrit label rejection surfaced verbatim", func(t *testing.T) { |
| 105 | + t.Parallel() |
| 106 | + |
| 107 | + cs := session(t, func(w http.ResponseWriter, r *http.Request) { |
| 108 | + switch { |
| 109 | + case r.URL.Path == "/a/accounts/self": |
| 110 | + _, _ = w.Write([]byte(selfJSON)) |
| 111 | + |
| 112 | + case r.Method == http.MethodPost: |
| 113 | + w.WriteHeader(http.StatusBadRequest) |
| 114 | + |
| 115 | + _, _ = w.Write([]byte(`label "Bogus" is not a configured label`)) |
| 116 | + |
| 117 | + default: |
| 118 | + _, _ = w.Write([]byte(ownChangeJSON)) |
| 119 | + } |
| 120 | + }) |
| 121 | + |
| 122 | + res, err := cs.CallTool(t.Context(), &mcp.CallToolParams{ |
| 123 | + Name: "set_vote", |
| 124 | + Arguments: map[string]any{"change": "123", "label": "Bogus", "value": 1}, |
| 125 | + }) |
| 126 | + require.NoError(t, err) |
| 127 | + require.True(t, res.IsError) |
| 128 | + |
| 129 | + text, ok := res.Content[0].(*mcp.TextContent) |
| 130 | + require.True(t, ok) |
| 131 | + assert.Contains(t, text.Text, `label "Bogus" is not a configured label`) |
| 132 | + }) |
| 133 | + |
| 134 | + t.Run("foreign change refused by own-changes restriction", func(t *testing.T) { |
| 135 | + t.Parallel() |
| 136 | + |
| 137 | + posted := false |
| 138 | + |
| 139 | + cs := session(t, func(w http.ResponseWriter, r *http.Request) { |
| 140 | + switch { |
| 141 | + case r.URL.Path == "/a/accounts/self": |
| 142 | + _, _ = w.Write([]byte(selfJSON)) |
| 143 | + |
| 144 | + case r.Method == http.MethodPost: |
| 145 | + posted = true |
| 146 | + |
| 147 | + _, _ = w.Write([]byte(")]}'\n{}")) |
| 148 | + |
| 149 | + default: |
| 150 | + _, _ = w.Write([]byte(foreignChangeJSON)) |
| 151 | + } |
| 152 | + }) |
| 153 | + |
| 154 | + res, err := cs.CallTool(t.Context(), &mcp.CallToolParams{ |
| 155 | + Name: "set_vote", |
| 156 | + Arguments: map[string]any{"change": "123", "label": "Code-Review", "value": -1}, |
| 157 | + }) |
| 158 | + require.NoError(t, err) |
| 159 | + require.True(t, res.IsError) |
| 160 | + |
| 161 | + text, ok := res.Content[0].(*mcp.TextContent) |
| 162 | + require.True(t, ok) |
| 163 | + assert.Contains(t, text.Text, "own-changes") |
| 164 | + |
| 165 | + assert.False(t, posted, "no mutating request may leave the process") |
| 166 | + }) |
| 167 | +} |
0 commit comments