lspf 0.9.1

A Rust framework for building extensible LSP language servers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! Typed outgoing notification helpers on `ClientHandle` (issue #102).
//!
//! These tests exercise the helpers over an in-memory transport, capturing the
//! connection's `ClientHandle` from inside a handler and observing the exact wire
//! method and parameter shape against the fixtures under `tests/fixtures/`.

use std::borrow::Cow;
use std::fmt;
use std::str::FromStr;
use std::sync::{Arc, Mutex, OnceLock};

use bytes::Bytes;
use lspf::types::notification::{DidCloseTextDocument, DidOpenTextDocument};
use lspf::types::request::Request;
use lspf::types::{
    Diagnostic, DiagnosticSeverity, DidCloseTextDocumentParams, DidOpenTextDocumentParams,
    LogMessageParams, LogTraceParams, MessageType, NumberOrString, Position, ProgressParams,
    ProgressParamsValue, PublishDiagnosticsParams, Range, ShowMessageParams, Uri, WorkDoneProgress,
    WorkDoneProgressBegin,
};
use lspf::{
    CancellationToken, ClientError, ClientHandle, LspError, RawMessage, RequestId, Server,
    ServerContext, TelemetryEventParams, Transport, TransportError, TransportReader,
    TransportWriter,
};
use serde_json::json;
use tokio::sync::mpsc;

const PUBLISH_DIAGNOSTICS_FIXTURE: &str =
    include_str!("fixtures/publish_diagnostics_notification.json");
const SHOW_MESSAGE_FIXTURE: &str = include_str!("fixtures/show_message_notification.json");
const LOG_MESSAGE_FIXTURE: &str = include_str!("fixtures/log_message_notification.json");
const LOG_TRACE_FIXTURE: &str = include_str!("fixtures/log_trace_notification.json");
const TELEMETRY_EVENT_OBJECT_FIXTURE: &str = include_str!("fixtures/telemetry_event_object.json");
const TELEMETRY_EVENT_ARRAY_FIXTURE: &str = include_str!("fixtures/telemetry_event_array.json");
const PROGRESS_FIXTURE: &str = include_str!("fixtures/progress_notification.json");

// --- Registrations -----------------------------------------------------------

struct AppState {
    client: Arc<Mutex<Option<ClientHandle>>>,
}

enum CaptureClient {}

impl Request for CaptureClient {
    type Params = String;
    type Result = String;
    const METHOD: &'static str = "test/capture-client";
}

enum TraceLevel {}

impl Request for TraceLevel {
    type Params = String;
    type Result = String;
    const METHOD: &'static str = "test/trace-level";
}

enum CtxPublish {}

impl Request for CtxPublish {
    type Params = PublishDiagnosticsParams;
    type Result = String;
    const METHOD: &'static str = "test/ctx-publish";
}

async fn capture_client(
    state: Arc<AppState>,
    ctx: ServerContext,
    _params: String,
    _ct: CancellationToken,
) -> Result<String, LspError> {
    *state.client.lock().unwrap() = Some(ctx.client());
    Ok("captured".to_string())
}

async fn trace_level(
    _state: Arc<AppState>,
    ctx: ServerContext,
    _params: String,
    _ct: CancellationToken,
) -> Result<String, LspError> {
    let level = match ctx.workspace().trace() {
        lspf::types::TraceValue::Off => "off",
        lspf::types::TraceValue::Messages => "messages",
        lspf::types::TraceValue::Verbose => "verbose",
    };
    Ok(level.to_string())
}

async fn ctx_publish(
    _state: Arc<AppState>,
    ctx: ServerContext,
    params: PublishDiagnosticsParams,
    _ct: CancellationToken,
) -> Result<String, LspError> {
    // The legacy convenience surface propagates enqueue failures instead of
    // swallowing them.
    ctx.publish_diagnostics(params)
        .map_err(LspError::internal)?;
    Ok("published".to_string())
}

async fn noop_open(_state: Arc<AppState>, _ctx: ServerContext, _params: DidOpenTextDocumentParams) {
}

async fn noop_close(
    _state: Arc<AppState>,
    _ctx: ServerContext,
    _params: DidCloseTextDocumentParams,
) {
}

fn server(state: &Arc<Mutex<Option<ClientHandle>>>) -> Server<AppState> {
    Server::builder(AppState {
        client: Arc::clone(state),
    })
    .request::<CaptureClient, _, _>(capture_client)
    .request::<TraceLevel, _, _>(trace_level)
    .request::<CtxPublish, _, _>(ctx_publish)
    .notification::<DidOpenTextDocument, _, _>(noop_open)
    .notification::<DidCloseTextDocument, _, _>(noop_close)
    .build()
    .expect("the outgoing-notification server builds")
}

// --- Harness -----------------------------------------------------------------

struct ChannelTransport {
    in_rx: mpsc::UnboundedReceiver<RawMessage>,
    out_tx: mpsc::UnboundedSender<RawMessage>,
}

struct ChannelReader(mpsc::UnboundedReceiver<RawMessage>);
struct ChannelWriter(mpsc::UnboundedSender<RawMessage>);

impl Transport for ChannelTransport {
    type Reader = ChannelReader;
    type Writer = ChannelWriter;

    fn split(self) -> (Self::Reader, Self::Writer) {
        (ChannelReader(self.in_rx), ChannelWriter(self.out_tx))
    }
}

impl TransportReader for ChannelReader {
    async fn recv(&mut self) -> Result<RawMessage, TransportError> {
        self.0.recv().await.ok_or(TransportError::Closed)
    }
}

impl TransportWriter for ChannelWriter {
    async fn send(&mut self, message: RawMessage) -> Result<(), TransportError> {
        self.0.send(message).map_err(|_| TransportError::Closed)
    }

    async fn shutdown(self) -> Result<(), TransportError> {
        Ok(())
    }
}

struct Session {
    in_tx: mpsc::UnboundedSender<RawMessage>,
    out_rx: mpsc::UnboundedReceiver<RawMessage>,
    serve: tokio::task::JoinHandle<lspf::Result<lspf::Outcome>>,
}

fn request(id: i32, method: &'static str, params: serde_json::Value) -> RawMessage {
    RawMessage::Request {
        id: RequestId::Number(id),
        method: Cow::Borrowed(method),
        params: Bytes::from(serde_json::to_vec(&params).unwrap()),
    }
}

fn notification(method: &'static str, params: serde_json::Value) -> RawMessage {
    RawMessage::Notification {
        method: Cow::Borrowed(method),
        params: Bytes::from(serde_json::to_vec(&params).unwrap()),
    }
}

fn exit() -> RawMessage {
    RawMessage::Notification {
        method: Cow::Borrowed("exit"),
        params: Bytes::from_static(b"null"),
    }
}

async fn receive(outgoing: &mut mpsc::UnboundedReceiver<RawMessage>) -> RawMessage {
    tokio::time::timeout(std::time::Duration::from_secs(2), outgoing.recv())
        .await
        .expect("server output before watchdog timeout")
        .expect("server output channel remains open")
}

async fn start(server: Server<AppState>) -> Session {
    let (in_tx, in_rx) = mpsc::unbounded_channel();
    let (out_tx, out_rx) = mpsc::unbounded_channel();
    let serve = tokio::spawn(server.serve(ChannelTransport { in_rx, out_tx }));
    Session {
        in_tx,
        out_rx,
        serve,
    }
}

/// Send a request and await its correlated response, synchronizing the test
/// with the read loop: every notification sent before the request has been
/// processed by the time the response arrives.
async fn request_and_sync(session: &mut Session, message: RawMessage) -> RawMessage {
    let id = message.id().cloned().expect("a request carries an id");
    session.in_tx.send(message).unwrap();
    let response = receive(&mut session.out_rx).await;
    assert_eq!(response.id(), Some(&id));
    response
}

fn result_string(response: &RawMessage) -> String {
    match response {
        RawMessage::Response {
            result: Ok(bytes), ..
        } => serde_json::from_slice(bytes).expect("the result decodes"),
        other => panic!("expected a success response, got {other:?}"),
    }
}

/// The connection's current trace level, observed through a handler's
/// workspace — proof of what `ClientHandle::log_trace` gates on.
async fn trace_level_probe(session: &mut Session, id: i32) -> String {
    let response = request_and_sync(session, request(id, TraceLevel::METHOD, json!("probe"))).await;
    result_string(&response)
}

async fn set_trace(session: &mut Session, id: i32, value: &str) {
    session
        .in_tx
        .send(notification("$/setTrace", json!({ "value": value })))
        .unwrap();
    assert_eq!(trace_level_probe(session, id).await, value);
}

/// A session that ran initialize and captured its connection `ClientHandle`.
async fn initialized_session() -> (Session, Arc<Mutex<Option<ClientHandle>>>) {
    let captured = Arc::new(Mutex::new(None));
    let mut session = start(server(&captured)).await;
    request_and_sync(
        &mut session,
        request(
            1,
            "initialize",
            json!({ "processId": null, "rootUri": null, "capabilities": {} }),
        ),
    )
    .await;
    request_and_sync(&mut session, request(2, CaptureClient::METHOD, json!("x"))).await;
    (session, captured)
}

fn take_client(captured: &Arc<Mutex<Option<ClientHandle>>>) -> ClientHandle {
    captured
        .lock()
        .unwrap()
        .clone()
        .expect("the handler captured its connection ClientHandle")
}

async fn finish(session: Session) {
    session.in_tx.send(exit()).unwrap();
    session
        .serve
        .await
        .expect("serve task did not panic")
        .expect("serve ended cleanly");
}

fn assert_wire_fixture(message: &RawMessage, fixture: &str) {
    let RawMessage::Notification { method, params } = message else {
        panic!("expected a notification, got {message:?}")
    };
    let wire = json!({
        "method": method.as_ref(),
        "params": serde_json::from_slice::<serde_json::Value>(params)
            .expect("the params are valid JSON"),
    });
    let expected: serde_json::Value =
        serde_json::from_str(fixture).expect("the fixture is valid JSON");
    assert_eq!(wire, expected, "the wire shape must match the fixture");
}

// --- Params used across tests ------------------------------------------------

fn diagnostics_params() -> PublishDiagnosticsParams {
    PublishDiagnosticsParams {
        uri: Uri::from_str("file:///diagnostics.rs").expect("the URI parses"),
        diagnostics: vec![Diagnostic {
            range: Range::new(Position::new(0, 0), Position::new(0, 5)),
            severity: Some(DiagnosticSeverity::ERROR),
            source: Some("lspf-test".to_string()),
            message: "boom".to_string(),
            ..Diagnostic::default()
        }],
        version: Some(7),
    }
}

fn show_message_params() -> ShowMessageParams {
    ShowMessageParams {
        typ: MessageType::WARNING,
        message: "disk almost full".to_string(),
    }
}

fn log_message_params() -> LogMessageParams {
    LogMessageParams {
        typ: MessageType::INFO,
        message: "indexed 42 files".to_string(),
    }
}

fn log_trace_params() -> LogTraceParams {
    LogTraceParams {
        message: "resolved import".to_string(),
        verbose: Some("walked the import graph".to_string()),
    }
}

fn telemetry_object_params() -> TelemetryEventParams {
    TelemetryEventParams::from(
        json!({"eventName": "build", "durationMs": 42})
            .as_object()
            .expect("the telemetry payload is an object")
            .clone(),
    )
}

fn telemetry_array_params() -> TelemetryEventParams {
    TelemetryEventParams::from(
        json!(["build", 42])
            .as_array()
            .expect("the telemetry payload is an array")
            .clone(),
    )
}

fn progress_params() -> ProgressParams {
    ProgressParams {
        token: NumberOrString::String("build-1".to_string()),
        value: ProgressParamsValue::WorkDone(WorkDoneProgress::Begin(WorkDoneProgressBegin {
            title: "Building".to_string(),
            cancellable: None,
            message: Some("crates".to_string()),
            percentage: Some(10),
        })),
    }
}

// --- Tests -------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn publish_diagnostics_matches_the_wire_fixture_and_preserves_the_version() {
    let (mut session, captured) = initialized_session().await;
    let client = take_client(&captured);

    client
        .publish_diagnostics(diagnostics_params())
        .expect("a successful enqueue returns Ok(())");

    assert_wire_fixture(
        &receive(&mut session.out_rx).await,
        PUBLISH_DIAGNOSTICS_FIXTURE,
    );
    finish(session).await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn context_publish_diagnostics_returns_the_enqueue_result() {
    let (mut session, _captured) = initialized_session().await;

    // The notification is enqueued before the handler's response, and the
    // outbound channel is FIFO.
    session
        .in_tx
        .send(request(
            3,
            CtxPublish::METHOD,
            serde_json::to_value(diagnostics_params()).unwrap(),
        ))
        .unwrap();
    let notification = receive(&mut session.out_rx).await;
    assert_wire_fixture(&notification, PUBLISH_DIAGNOSTICS_FIXTURE);
    let response = receive(&mut session.out_rx).await;
    assert_eq!(response.id(), Some(&RequestId::Number(3)));
    assert_eq!(result_string(&response), "published");
    finish(session).await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn show_message_matches_the_wire_fixture() {
    let (mut session, captured) = initialized_session().await;
    let client = take_client(&captured);

    client
        .show_message(show_message_params())
        .expect("a successful enqueue returns Ok(())");

    assert_wire_fixture(&receive(&mut session.out_rx).await, SHOW_MESSAGE_FIXTURE);
    finish(session).await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn log_message_matches_the_wire_fixture() {
    let (mut session, captured) = initialized_session().await;
    let client = take_client(&captured);

    client
        .log_message(log_message_params())
        .expect("a successful enqueue returns Ok(())");

    assert_wire_fixture(&receive(&mut session.out_rx).await, LOG_MESSAGE_FIXTURE);
    finish(session).await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn telemetry_event_matches_the_wire_fixtures() {
    let (mut session, captured) = initialized_session().await;
    let client = take_client(&captured);

    client
        .telemetry_event(telemetry_object_params())
        .expect("an object payload enqueues");
    assert_wire_fixture(
        &receive(&mut session.out_rx).await,
        TELEMETRY_EVENT_OBJECT_FIXTURE,
    );

    client
        .telemetry_event(telemetry_array_params())
        .expect("an array payload enqueues");
    assert_wire_fixture(
        &receive(&mut session.out_rx).await,
        TELEMETRY_EVENT_ARRAY_FIXTURE,
    );

    finish(session).await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn progress_matches_the_wire_fixture() {
    let (mut session, captured) = initialized_session().await;
    let client = take_client(&captured);

    client
        .progress(progress_params())
        .expect("a successful enqueue returns Ok(())");

    assert_wire_fixture(&receive(&mut session.out_rx).await, PROGRESS_FIXTURE);
    finish(session).await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn log_trace_gates_on_the_connection_trace_level_without_changing_it() {
    let (mut session, captured) = initialized_session().await;
    let client = take_client(&captured);

    // The initial level is Off: nothing is enqueued, and the no-op is Ok.
    client
        .log_trace(log_trace_params())
        .expect("an Off trace level is a silent no-op");
    assert!(
        session.out_rx.try_recv().is_err(),
        "an Off trace level enqueues nothing"
    );

    // Messages sends the params as provided.
    set_trace(&mut session, 3, "messages").await;
    client
        .log_trace(log_trace_params())
        .expect("a Messages trace level enqueues");
    assert_wire_fixture(&receive(&mut session.out_rx).await, LOG_TRACE_FIXTURE);
    // Sending never changes the level.
    assert_eq!(trace_level_probe(&mut session, 4).await, "messages");

    // Verbose sends too, still without changing the level.
    set_trace(&mut session, 5, "verbose").await;
    client
        .log_trace(log_trace_params())
        .expect("a Verbose trace level enqueues");
    assert_wire_fixture(&receive(&mut session.out_rx).await, LOG_TRACE_FIXTURE);
    assert_eq!(trace_level_probe(&mut session, 6).await, "verbose");

    finish(session).await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn every_helper_reports_a_closed_queue_as_client_error() {
    let (mut session, captured) = initialized_session().await;
    let client = take_client(&captured);
    // Raise the level so log_trace attempts an enqueue instead of no-oping.
    set_trace(&mut session, 3, "messages").await;
    finish(session).await;

    assert!(matches!(
        client.publish_diagnostics(diagnostics_params()),
        Err(ClientError::OutboundClosed)
    ));
    assert!(matches!(
        client.show_message(show_message_params()),
        Err(ClientError::OutboundClosed)
    ));
    assert!(matches!(
        client.log_message(log_message_params()),
        Err(ClientError::OutboundClosed)
    ));
    assert!(matches!(
        client.log_trace(log_trace_params()),
        Err(ClientError::OutboundClosed)
    ));
    assert!(matches!(
        client.telemetry_event(telemetry_object_params()),
        Err(ClientError::OutboundClosed)
    ));
    assert!(matches!(
        client.progress(progress_params()),
        Err(ClientError::OutboundClosed)
    ));
}

/// Captures `tracing` events with their rendered fields, so tests can assert
/// what the helpers emit locally.
#[derive(Clone, Default)]
struct EventCapture {
    events: Arc<Mutex<Vec<(tracing::Level, String)>>>,
}

static TRACING_INTEREST: OnceLock<tracing::Dispatch> = OnceLock::new();

fn keep_tracing_interest() {
    // Callsite interest is cached process-wide, while scoped subscribers are
    // thread-local. Keep one dispatch alive for this test binary so parallel
    // tests cannot leave the queue callsites permanently disabled.
    TRACING_INTEREST.get_or_init(|| tracing::Dispatch::new(tracing_subscriber::registry()));
}

struct FieldVisitor(String);

impl tracing::field::Visit for FieldVisitor {
    fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn fmt::Debug) {
        if !self.0.is_empty() {
            self.0.push(' ');
        }
        self.0.push_str(&format!("{}={value:?}", field.name()));
    }
}

impl<S> tracing_subscriber::Layer<S> for EventCapture
where
    S: tracing::Subscriber,
{
    fn on_event(
        &self,
        event: &tracing::Event<'_>,
        _context: tracing_subscriber::layer::Context<'_, S>,
    ) {
        let mut visitor = FieldVisitor(String::new());
        event.record(&mut visitor);
        self.events
            .lock()
            .unwrap()
            .push((*event.metadata().level(), visitor.0));
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn helper_failures_emit_a_tracing_event_without_suppressing_the_error() {
    use tracing_subscriber::layer::SubscriberExt;

    keep_tracing_interest();
    let (mut session, captured) = initialized_session().await;
    let client = take_client(&captured);
    let capture = EventCapture::default();

    // A successful log message records only queue accounting locally; its
    // protocol payload is not duplicated into the tracing stream.
    tracing::subscriber::with_default(tracing_subscriber::registry().with(capture.clone()), || {
        client
            .log_message(log_message_params())
            .expect("a successful enqueue returns Ok(())");
    });
    {
        let events = capture.events.lock().unwrap();
        assert!(events.iter().any(|(level, fields)| {
            *level == tracing::Level::TRACE
                && fields.contains("resource=\"outbound_queue\"")
                && fields.contains("resource_action=\"admit\"")
        }));
        assert!(
            events
                .iter()
                .all(|(_, fields)| !fields.contains("indexed 42 files")),
            "log_message must not echo the payload into local tracing; got {events:?}"
        );
    }
    assert_wire_fixture(&receive(&mut session.out_rx).await, LOG_MESSAGE_FIXTURE);

    // A failed enqueue emits a tracing event naming the wire method, and the
    // error is still returned to the caller.
    finish(session).await;
    let result = tracing::subscriber::with_default(
        tracing_subscriber::registry().with(capture.clone()),
        || client.show_message(show_message_params()),
    );
    assert!(matches!(result, Err(ClientError::OutboundClosed)));
    let events = capture.events.lock().unwrap();
    assert!(
        events
            .iter()
            .any(|(level, fields)| *level == tracing::Level::WARN
                && fields.contains("window/showMessage")),
        "a failed helper emits a WARN event naming the method; got {events:?}"
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn did_close_never_emits_an_automatic_publish_diagnostics() {
    let captured = Arc::new(Mutex::new(None));
    let mut session = start(server(&captured)).await;
    request_and_sync(
        &mut session,
        request(
            1,
            "initialize",
            json!({ "processId": null, "rootUri": null, "capabilities": {} }),
        ),
    )
    .await;

    session
        .in_tx
        .send(notification(
            "textDocument/didOpen",
            json!({
                "textDocument": {
                    "uri": "file:///watched.rs",
                    "languageId": "rust",
                    "version": 1,
                    "text": "fn main() {}"
                }
            }),
        ))
        .unwrap();
    session
        .in_tx
        .send(notification(
            "textDocument/didClose",
            json!({ "textDocument": { "uri": "file:///watched.rs" } }),
        ))
        .unwrap();
    // The probe synchronizes with the read loop: open and close are fully
    // processed before the response comes back.
    request_and_sync(&mut session, request(2, CaptureClient::METHOD, json!("x"))).await;

    session.in_tx.send(exit()).unwrap();
    session
        .serve
        .await
        .expect("serve task did not panic")
        .expect("serve ended cleanly");

    while let Ok(message) = session.out_rx.try_recv() {
        assert!(
            !matches!(&message, RawMessage::Notification { method, .. } if method == "textDocument/publishDiagnostics"),
            "didClose must not emit an automatic publishDiagnostics"
        );
    }
}