| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package integration |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"net/http/httptest" |
| 11 |
"os" |
| 12 |
"os/exec" |
| 13 |
"strings" |
| 14 |
"sync" |
| 15 |
"sync/atomic" |
| 16 |
"testing" |
| 17 |
|
| 18 |
"github.com/modelcontextprotocol/go-sdk/mcp" |
| 19 |
"golang.org/x/tools/gopls/internal/protocol" |
| 20 |
"golang.org/x/tools/gopls/internal/test/integration/fake" |
| 21 |
"golang.org/x/tools/internal/jsonrpc2/servertest" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
type Env struct { |
| 29 |
TB testing.TB |
| 30 |
Ctx context.Context |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
Sandbox *fake.Sandbox |
| 35 |
Server servertest.Connector |
| 36 |
|
| 37 |
|
| 38 |
Editor *fake.Editor |
| 39 |
|
| 40 |
Awaiter *Awaiter |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
MCPServer *httptest.Server |
| 45 |
MCPSession *mcp.ClientSession |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
var nextAwaiterRegistration atomic.Uint64 |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
type Awaiter struct { |
| 60 |
workdir *fake.Workdir |
| 61 |
|
| 62 |
mu sync.Mutex |
| 63 |
|
| 64 |
state State |
| 65 |
waiters map[uint64]*condition |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
docCollectors map[uint64][]*protocol.ShowDocumentParams |
| 70 |
messageCollectors map[uint64][]*protocol.ShowMessageParams |
| 71 |
} |
| 72 |
|
| 73 |
func NewAwaiter(workdir *fake.Workdir) *Awaiter { |
| 74 |
return &Awaiter{ |
| 75 |
workdir: workdir, |
| 76 |
state: State{ |
| 77 |
diagnostics: make(map[string]*protocol.PublishDiagnosticsParams), |
| 78 |
work: make(map[protocol.ProgressToken]*workProgress), |
| 79 |
startedWork: make(map[string]uint64), |
| 80 |
completedWork: make(map[string]uint64), |
| 81 |
}, |
| 82 |
waiters: make(map[uint64]*condition), |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func (a *Awaiter) Hooks() fake.ClientHooks { |
| 88 |
return fake.ClientHooks{ |
| 89 |
OnDiagnostics: a.onDiagnostics, |
| 90 |
OnLogMessage: a.onLogMessage, |
| 91 |
OnWorkDoneProgressCreate: a.onWorkDoneProgressCreate, |
| 92 |
OnProgress: a.onProgress, |
| 93 |
OnShowDocument: a.onShowDocument, |
| 94 |
OnShowMessage: a.onShowMessage, |
| 95 |
OnShowMessageRequest: a.onShowMessageRequest, |
| 96 |
OnRegisterCapability: a.onRegisterCapability, |
| 97 |
OnUnregisterCapability: a.onUnregisterCapability, |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
type State struct { |
| 103 |
|
| 104 |
diagnostics map[string]*protocol.PublishDiagnosticsParams |
| 105 |
logs []*protocol.LogMessageParams |
| 106 |
showDocument []*protocol.ShowDocumentParams |
| 107 |
showMessage []*protocol.ShowMessageParams |
| 108 |
showMessageRequest []*protocol.ShowMessageRequestParams |
| 109 |
|
| 110 |
registrations []*protocol.RegistrationParams |
| 111 |
registeredCapabilities map[string]protocol.Registration |
| 112 |
unregistrations []*protocol.UnregistrationParams |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
work map[protocol.ProgressToken]*workProgress |
| 117 |
startedWork map[string]uint64 |
| 118 |
completedWork map[string]uint64 |
| 119 |
} |
| 120 |
|
| 121 |
type workProgress struct { |
| 122 |
title, msg, endMsg string |
| 123 |
percent float64 |
| 124 |
complete bool |
| 125 |
} |
| 126 |
|
| 127 |
type awaitResult struct { |
| 128 |
verdict Verdict |
| 129 |
reason string |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
type condition struct { |
| 135 |
expectation Expectation |
| 136 |
verdict chan awaitResult |
| 137 |
} |
| 138 |
|
| 139 |
func (a *Awaiter) onDiagnostics(_ context.Context, d *protocol.PublishDiagnosticsParams) error { |
| 140 |
a.mu.Lock() |
| 141 |
defer a.mu.Unlock() |
| 142 |
|
| 143 |
pth := a.workdir.URIToPath(d.URI) |
| 144 |
a.state.diagnostics[pth] = d |
| 145 |
a.checkConditionsLocked() |
| 146 |
return nil |
| 147 |
} |
| 148 |
|
| 149 |
func (a *Awaiter) onShowDocument(_ context.Context, params *protocol.ShowDocumentParams) error { |
| 150 |
a.mu.Lock() |
| 151 |
defer a.mu.Unlock() |
| 152 |
|
| 153 |
|
| 154 |
for id, s := range a.docCollectors { |
| 155 |
a.docCollectors[id] = append(s, params) |
| 156 |
} |
| 157 |
|
| 158 |
a.state.showDocument = append(a.state.showDocument, params) |
| 159 |
a.checkConditionsLocked() |
| 160 |
return nil |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
func (a *Awaiter) ListenToShownDocuments() func() []*protocol.ShowDocumentParams { |
| 168 |
id := nextAwaiterRegistration.Add(1) |
| 169 |
|
| 170 |
a.mu.Lock() |
| 171 |
defer a.mu.Unlock() |
| 172 |
|
| 173 |
if a.docCollectors == nil { |
| 174 |
a.docCollectors = make(map[uint64][]*protocol.ShowDocumentParams) |
| 175 |
} |
| 176 |
a.docCollectors[id] = nil |
| 177 |
|
| 178 |
return func() []*protocol.ShowDocumentParams { |
| 179 |
a.mu.Lock() |
| 180 |
defer a.mu.Unlock() |
| 181 |
params := a.docCollectors[id] |
| 182 |
delete(a.docCollectors, id) |
| 183 |
return params |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
func (a *Awaiter) onShowMessage(_ context.Context, params *protocol.ShowMessageParams) error { |
| 188 |
a.mu.Lock() |
| 189 |
defer a.mu.Unlock() |
| 190 |
|
| 191 |
|
| 192 |
for id, s := range a.messageCollectors { |
| 193 |
a.messageCollectors[id] = append(s, params) |
| 194 |
} |
| 195 |
|
| 196 |
a.state.showMessage = append(a.state.showMessage, params) |
| 197 |
a.checkConditionsLocked() |
| 198 |
return nil |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
func (a *Awaiter) ListenToShownMessages() func() []*protocol.ShowMessageParams { |
| 210 |
id := nextAwaiterRegistration.Add(1) |
| 211 |
|
| 212 |
a.mu.Lock() |
| 213 |
defer a.mu.Unlock() |
| 214 |
|
| 215 |
if a.messageCollectors == nil { |
| 216 |
a.messageCollectors = make(map[uint64][]*protocol.ShowMessageParams) |
| 217 |
} |
| 218 |
a.messageCollectors[id] = nil |
| 219 |
|
| 220 |
return func() []*protocol.ShowMessageParams { |
| 221 |
a.mu.Lock() |
| 222 |
defer a.mu.Unlock() |
| 223 |
params := a.messageCollectors[id] |
| 224 |
delete(a.messageCollectors, id) |
| 225 |
return params |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
func (a *Awaiter) onShowMessageRequest(_ context.Context, m *protocol.ShowMessageRequestParams) error { |
| 230 |
a.mu.Lock() |
| 231 |
defer a.mu.Unlock() |
| 232 |
|
| 233 |
a.state.showMessageRequest = append(a.state.showMessageRequest, m) |
| 234 |
a.checkConditionsLocked() |
| 235 |
return nil |
| 236 |
} |
| 237 |
|
| 238 |
func (a *Awaiter) onLogMessage(_ context.Context, m *protocol.LogMessageParams) error { |
| 239 |
a.mu.Lock() |
| 240 |
defer a.mu.Unlock() |
| 241 |
|
| 242 |
a.state.logs = append(a.state.logs, m) |
| 243 |
a.checkConditionsLocked() |
| 244 |
return nil |
| 245 |
} |
| 246 |
|
| 247 |
func (a *Awaiter) onWorkDoneProgressCreate(_ context.Context, m *protocol.WorkDoneProgressCreateParams) error { |
| 248 |
a.mu.Lock() |
| 249 |
defer a.mu.Unlock() |
| 250 |
|
| 251 |
a.state.work[m.Token] = &workProgress{} |
| 252 |
return nil |
| 253 |
} |
| 254 |
|
| 255 |
func (a *Awaiter) onProgress(_ context.Context, m *protocol.ProgressParams) error { |
| 256 |
a.mu.Lock() |
| 257 |
defer a.mu.Unlock() |
| 258 |
work, ok := a.state.work[m.Token] |
| 259 |
if !ok { |
| 260 |
panic(fmt.Sprintf("got progress report for unknown report %v: %v", m.Token, m)) |
| 261 |
} |
| 262 |
v := m.Value.(map[string]any) |
| 263 |
switch kind := v["kind"]; kind { |
| 264 |
case "begin": |
| 265 |
work.title = v["title"].(string) |
| 266 |
a.state.startedWork[work.title]++ |
| 267 |
if msg, ok := v["message"]; ok { |
| 268 |
work.msg = msg.(string) |
| 269 |
} |
| 270 |
case "report": |
| 271 |
if pct, ok := v["percentage"]; ok { |
| 272 |
work.percent = pct.(float64) |
| 273 |
} |
| 274 |
if msg, ok := v["message"]; ok { |
| 275 |
work.msg = msg.(string) |
| 276 |
} |
| 277 |
case "end": |
| 278 |
work.complete = true |
| 279 |
a.state.completedWork[work.title]++ |
| 280 |
if msg, ok := v["message"]; ok { |
| 281 |
work.endMsg = msg.(string) |
| 282 |
} |
| 283 |
} |
| 284 |
a.checkConditionsLocked() |
| 285 |
return nil |
| 286 |
} |
| 287 |
|
| 288 |
func (a *Awaiter) onRegisterCapability(_ context.Context, m *protocol.RegistrationParams) error { |
| 289 |
a.mu.Lock() |
| 290 |
defer a.mu.Unlock() |
| 291 |
|
| 292 |
a.state.registrations = append(a.state.registrations, m) |
| 293 |
if a.state.registeredCapabilities == nil { |
| 294 |
a.state.registeredCapabilities = make(map[string]protocol.Registration) |
| 295 |
} |
| 296 |
for _, reg := range m.Registrations { |
| 297 |
a.state.registeredCapabilities[reg.Method] = reg |
| 298 |
} |
| 299 |
a.checkConditionsLocked() |
| 300 |
return nil |
| 301 |
} |
| 302 |
|
| 303 |
func (a *Awaiter) onUnregisterCapability(_ context.Context, m *protocol.UnregistrationParams) error { |
| 304 |
a.mu.Lock() |
| 305 |
defer a.mu.Unlock() |
| 306 |
|
| 307 |
a.state.unregistrations = append(a.state.unregistrations, m) |
| 308 |
a.checkConditionsLocked() |
| 309 |
return nil |
| 310 |
} |
| 311 |
|
| 312 |
func (a *Awaiter) checkConditionsLocked() { |
| 313 |
for id, condition := range a.waiters { |
| 314 |
if v, why := condition.expectation.Check(a.state); v != Unmet { |
| 315 |
delete(a.waiters, id) |
| 316 |
condition.verdict <- awaitResult{v, why} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
func (e *Env) Await(expectations ...Expectation) { |
| 328 |
e.TB.Helper() |
| 329 |
if err := e.Awaiter.Await(e.Ctx, AllOf(expectations...)); err != nil { |
| 330 |
e.TB.Fatal(err) |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
func (e *Env) OnceMet(pre Expectation, mustMeets ...Expectation) { |
| 338 |
e.TB.Helper() |
| 339 |
e.Await(OnceMet(pre, AllOf(mustMeets...))) |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
func (a *Awaiter) Await(ctx context.Context, expectation Expectation) error { |
| 345 |
a.mu.Lock() |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
switch verdict, why := expectation.Check(a.state); verdict { |
| 350 |
case Met: |
| 351 |
a.mu.Unlock() |
| 352 |
return nil |
| 353 |
case Unmeetable: |
| 354 |
err := fmt.Errorf("unmeetable expectation:\n%s\nreason:\n%s", indent(expectation.Description), indent(why)) |
| 355 |
a.mu.Unlock() |
| 356 |
return err |
| 357 |
} |
| 358 |
cond := &condition{ |
| 359 |
expectation: expectation, |
| 360 |
verdict: make(chan awaitResult), |
| 361 |
} |
| 362 |
a.waiters[nextAwaiterRegistration.Add(1)] = cond |
| 363 |
a.mu.Unlock() |
| 364 |
|
| 365 |
var err error |
| 366 |
select { |
| 367 |
case <-ctx.Done(): |
| 368 |
err = ctx.Err() |
| 369 |
case res := <-cond.verdict: |
| 370 |
if res.verdict != Met { |
| 371 |
err = fmt.Errorf("the following condition is %s:\n%s\nreason:\n%s", |
| 372 |
res.verdict, indent(expectation.Description), indent(res.reason)) |
| 373 |
} |
| 374 |
} |
| 375 |
return err |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
func indent(msg string) string { |
| 380 |
const prefix = " " |
| 381 |
return prefix + strings.ReplaceAll(msg, "\n", "\n"+prefix) |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
func CleanModCache(t *testing.T, modcache string) { |
| 394 |
cmd := exec.Command("go", "clean", "-modcache") |
| 395 |
cmd.Env = append(os.Environ(), "GOMODCACHE="+modcache, "GOTOOLCHAIN=local") |
| 396 |
if output, err := cmd.CombinedOutput(); err != nil { |
| 397 |
t.Errorf("cleaning modcache: %v\noutput:\n%s", err, string(output)) |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
func CodeActionByKind(actions []protocol.CodeAction, kind protocol.CodeActionKind) (*protocol.CodeAction, error) { |
| 403 |
for _, act := range actions { |
| 404 |
if act.Kind == kind { |
| 405 |
return &act, nil |
| 406 |
} |
| 407 |
} |
| 408 |
return nil, fmt.Errorf("can't find action with kind %s, only %#v", kind, actions) |
| 409 |
} |