|
| 1 | +package tools_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "dev.gaijin.team/go/go-gerrit-mcp/internal/config" |
| 13 | +) |
| 14 | + |
| 15 | +// Errors and empty results must carry what the agent needs to recover: |
| 16 | +// proposals when the valid set is known, the active scope when it silently |
| 17 | +// narrows a query, and the real patch-set range when a revision guess misses. |
| 18 | + |
| 19 | +func Test_SearchChanges_Boundaries(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + t.Run("negative start refused before any request", func(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + requested := false |
| 26 | + |
| 27 | + cs := fakeGerritFlag(t, ")]}'\n[]", &requested) |
| 28 | + |
| 29 | + res, err := cs.CallTool(t.Context(), &mcp.CallToolParams{ |
| 30 | + Name: "search_changes", |
| 31 | + Arguments: map[string]any{"query": "status:open", "start": -2}, |
| 32 | + }) |
| 33 | + require.NoError(t, err) |
| 34 | + require.True(t, res.IsError) |
| 35 | + assert.Contains(t, errorText(t, res), "start must be zero or positive") |
| 36 | + assert.False(t, requested, "no query may leave the process") |
| 37 | + }) |
| 38 | + |
| 39 | + t.Run("empty query without scope refused with a starting point", func(t *testing.T) { |
| 40 | + t.Parallel() |
| 41 | + |
| 42 | + requested := false |
| 43 | + |
| 44 | + cs := fakeGerritFlag(t, ")]}'\n[]", &requested) |
| 45 | + |
| 46 | + res, err := cs.CallTool(t.Context(), &mcp.CallToolParams{ |
| 47 | + Name: "search_changes", |
| 48 | + Arguments: map[string]any{"query": " "}, |
| 49 | + }) |
| 50 | + require.NoError(t, err) |
| 51 | + require.True(t, res.IsError) |
| 52 | + assert.Contains(t, errorText(t, res), "status:open") |
| 53 | + assert.False(t, requested, "no query may leave the process") |
| 54 | + }) |
| 55 | + |
| 56 | + t.Run("empty query with scope browses the scope", func(t *testing.T) { |
| 57 | + t.Parallel() |
| 58 | + |
| 59 | + cs, lastURL := scopedGerrit(t, ")]}'\n[]") |
| 60 | + |
| 61 | + out := callTool(t, cs, "search_changes", map[string]any{"query": ""}) |
| 62 | + |
| 63 | + assert.Equal(t, `<changes query="" scope="core" start="0" count="0" more="false"/>`, out) |
| 64 | + assert.Contains(t, *lastURL, "project:core", "scope clause must reach the wire") |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("scoped result names the scope", func(t *testing.T) { |
| 68 | + t.Parallel() |
| 69 | + |
| 70 | + cs, _ := scopedGerrit(t, ")]}'\n[]") |
| 71 | + |
| 72 | + out := callTool(t, cs, "search_changes", map[string]any{"query": "project:elsewhere"}) |
| 73 | + |
| 74 | + assert.Equal(t, |
| 75 | + `<changes query="project:elsewhere" scope="core" start="0" count="0" more="false"/>`, |
| 76 | + out, |
| 77 | + "the scope attribute is what tells the agent an empty page may mean out-of-scope", |
| 78 | + ) |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +func Test_GetFileDiff_UnknownFile(t *testing.T) { |
| 83 | + t.Parallel() |
| 84 | + |
| 85 | + const emptyDiffJSON = ")]}'\n" + `{"change_type":"MODIFIED","content":[]}` |
| 86 | + |
| 87 | + cs := session(t, func(w http.ResponseWriter, r *http.Request) { |
| 88 | + switch { |
| 89 | + case r.URL.Path == "/a/accounts/self": |
| 90 | + _, _ = w.Write([]byte(selfJSON)) |
| 91 | + case strings.HasSuffix(r.URL.Path, "/diff"): |
| 92 | + _, _ = w.Write([]byte(emptyDiffJSON)) |
| 93 | + case strings.HasSuffix(r.URL.Path, "/files/"): |
| 94 | + _, _ = w.Write([]byte(revisionFilesJSON)) |
| 95 | + default: |
| 96 | + _, _ = w.Write([]byte(ownChangeJSON)) |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + res, err := cs.CallTool(t.Context(), &mcp.CallToolParams{ |
| 101 | + Name: "get_file_diff", |
| 102 | + Arguments: map[string]any{"change": "123", "file": "core/scaner.go"}, |
| 103 | + }) |
| 104 | + require.NoError(t, err) |
| 105 | + require.True(t, res.IsError, "Gerrit's empty diff for an absent file must not render as success") |
| 106 | + |
| 107 | + golden(t, "error-diff-unknown-file", errorText(t, res)) |
| 108 | +} |
| 109 | + |
| 110 | +func Test_ListChangeFiles_RevisionNotFound(t *testing.T) { |
| 111 | + t.Parallel() |
| 112 | + |
| 113 | + const allRevisionsJSON = ")]}'\n" + `{"_number":123,"project":"core",` + |
| 114 | + `"current_revision":"sha3",` + |
| 115 | + `"revisions":{"sha1":{"_number":1},"sha2":{"_number":2},"sha3":{"_number":3}}}` |
| 116 | + |
| 117 | + cs := session(t, func(w http.ResponseWriter, r *http.Request) { |
| 118 | + switch { |
| 119 | + case r.URL.Path == "/a/accounts/self": |
| 120 | + _, _ = w.Write([]byte(selfJSON)) |
| 121 | + case strings.Contains(r.URL.Path, "/revisions/99/"): |
| 122 | + w.WriteHeader(http.StatusNotFound) |
| 123 | + |
| 124 | + _, _ = w.Write([]byte("Not found: 99")) |
| 125 | + |
| 126 | + default: |
| 127 | + _, _ = w.Write([]byte(allRevisionsJSON)) |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + res, err := cs.CallTool(t.Context(), &mcp.CallToolParams{ |
| 132 | + Name: "list_change_files", |
| 133 | + Arguments: map[string]any{"change": "123", "revision": "99"}, |
| 134 | + }) |
| 135 | + require.NoError(t, err) |
| 136 | + require.True(t, res.IsError) |
| 137 | + |
| 138 | + text := errorText(t, res) |
| 139 | + assert.Contains(t, text, "Not found: 99") |
| 140 | + assert.Contains(t, text, "valid_patch_sets=1-3") |
| 141 | + assert.Contains(t, text, "current_patch_set=3") |
| 142 | +} |
| 143 | + |
| 144 | +// fakeGerritFlag is fakeGerrit with a flag reporting whether any API request |
| 145 | +// beyond the startup self-check reached the fake. |
| 146 | +func fakeGerritFlag(t *testing.T, fixture string, requested *bool) *mcp.ClientSession { |
| 147 | + t.Helper() |
| 148 | + |
| 149 | + return session(t, func(w http.ResponseWriter, r *http.Request) { |
| 150 | + if r.URL.Path == "/a/accounts/self" { |
| 151 | + _, _ = w.Write([]byte(selfJSON)) |
| 152 | + |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + *requested = true |
| 157 | + |
| 158 | + _, _ = w.Write([]byte(fixture)) |
| 159 | + }) |
| 160 | +} |
| 161 | + |
| 162 | +// scopedGerrit is fakeGerrit with the project allowlist set to "core". |
| 163 | +func scopedGerrit(t *testing.T, fixture string) (*mcp.ClientSession, *string) { |
| 164 | + t.Helper() |
| 165 | + |
| 166 | + lastURL := new(string) |
| 167 | + |
| 168 | + cs := session(t, func(w http.ResponseWriter, r *http.Request) { |
| 169 | + if r.URL.Path == "/a/accounts/self" { |
| 170 | + _, _ = w.Write([]byte(selfJSON)) |
| 171 | + |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + *lastURL = r.URL.String() |
| 176 | + |
| 177 | + _, _ = w.Write([]byte(fixture)) |
| 178 | + }, func(cfg *config.Config) { cfg.Projects = []string{"core"} }) |
| 179 | + |
| 180 | + return cs, lastURL |
| 181 | +} |
0 commit comments