lspf 1.0.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
//! Connection-owned Notebook synchronization (ADR 0034).
//!
//! [`Notebooks`] holds notebook metadata and ordered cell membership; cell text
//! stays in the connection's [`Documents`](crate::documents::Documents), which
//! is why nothing here touches a rope. Each mutation coordinates both stores
//! before returning to the protocol engine for post-mutation hook dispatch.

use std::collections::HashMap;
use std::sync::{Arc, RwLock};

use gen_lsp_types::{
    DidChangeNotebookDocumentParams, DidCloseNotebookDocumentParams, DidOpenNotebookDocumentParams,
    LspObject, NotebookCell, NotebookDocument, NotebookDocumentCellChanges, TextDocumentItem, Uri,
};

use tracing::debug;

use crate::documents::{Document, DocumentMutationError, Documents};
use crate::error::LspError;
use crate::telemetry::{ConnectionTrace, Resource, ResourceAction};
use crate::uri_key::UriKey;

const NOTEBOOK_COUNT_CAPACITY_EXHAUSTED: &str = "notebook count capacity exhausted";

const NOTEBOOK_NOT_FOUND: &str = "notebook not found";
const CELL_STRUCTURE_OUT_OF_RANGE: &str = "notebook cell structure change is out of range";
const UNKNOWN_CELL_DATA: &str = "notebook cell data change names an unknown cell";

#[derive(Debug, thiserror::Error)]
pub(crate) enum NotebookMutationError {
    #[error(transparent)]
    Protocol(LspError),
    #[error(transparent)]
    Capacity(LspError),
}

impl From<LspError> for NotebookMutationError {
    fn from(error: LspError) -> Self {
        Self::Protocol(error)
    }
}

impl From<DocumentMutationError> for NotebookMutationError {
    fn from(error: DocumentMutationError) -> Self {
        match error {
            DocumentMutationError::Protocol(error) => Self::Protocol(error),
            DocumentMutationError::Capacity(error) => Self::Capacity(error),
        }
    }
}

struct CellOpenRollback {
    uri: Uri,
    previous: Option<Document>,
}

/// A snapshot of one synchronized notebook.
///
/// The snapshot contains notebook metadata and ordered cell membership only.
/// Cell text remains in the connection's [`DocumentsView`](crate::DocumentsView).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Notebook {
    uri: Uri,
    notebook_type: String,
    version: i32,
    metadata: Option<LspObject>,
    cells: Vec<NotebookCell>,
}

impl Notebook {
    fn from_document(document: NotebookDocument) -> Self {
        Self {
            uri: document.uri,
            notebook_type: document.notebook_type,
            version: document.version,
            metadata: document.metadata,
            cells: document.cells,
        }
    }

    /// The notebook URI supplied by the client.
    pub fn uri(&self) -> &Uri {
        &self.uri
    }

    /// The notebook type supplied by the client.
    pub fn notebook_type(&self) -> &str {
        &self.notebook_type
    }

    /// The editor-managed notebook version.
    pub fn version(&self) -> i32 {
        self.version
    }

    /// The client-supplied notebook metadata.
    pub fn metadata(&self) -> Option<&LspObject> {
        self.metadata.as_ref()
    }

    /// The notebook's cells in document order.
    pub fn cells(&self) -> &[NotebookCell] {
        &self.cells
    }
}

#[derive(Debug)]
struct NotebooksInner {
    by_uri: HashMap<UriKey, Notebook>,
    notebook_by_cell: HashMap<UriKey, UriKey>,
    max_notebooks: usize,
}

/// Coordinates Notebook mutations and their cell Documents for one connection.
///
/// The engine serializes mutations and runs hooks only after success. Existing
/// readers can observe intermediate cell mutations; separate view reads do not
/// promise a common revision. New cells consume budgets before old cells close.
#[derive(Debug, Clone)]
pub(crate) struct Notebooks {
    inner: Arc<RwLock<NotebooksInner>>,
    documents: Documents,
    trace: ConnectionTrace,
}

impl Default for Notebooks {
    fn default() -> Self {
        let policy = crate::ResourcePolicy::default();
        let trace = ConnectionTrace::new();
        Self::with_resource_policy(
            Documents::with_resource_policy(policy, trace),
            policy,
            trace,
        )
    }
}

impl Notebooks {
    #[cfg(test)]
    pub(crate) fn new(documents: Documents) -> Self {
        Self::with_resource_policy(
            documents,
            crate::ResourcePolicy::default(),
            ConnectionTrace::new(),
        )
    }

    pub(crate) fn with_resource_policy(
        documents: Documents,
        policy: crate::ResourcePolicy,
        trace: ConnectionTrace,
    ) -> Self {
        Self {
            inner: Arc::new(RwLock::new(NotebooksInner {
                by_uri: HashMap::new(),
                notebook_by_cell: HashMap::new(),
                max_notebooks: policy.max_notebooks,
            })),
            documents,
            trace,
        }
    }

    /// Open a Notebook and its cell Documents, restoring every cell on refusal.
    pub(crate) fn open(
        &self,
        params: DidOpenNotebookDocumentParams,
    ) -> Result<(), NotebookMutationError> {
        let opened = self.open_cell_text_documents(params.cell_text_documents)?;
        let superseded = match self.insert(params.notebook_document) {
            Ok(superseded) => superseded,
            Err(error) => {
                self.rollback_cell_opens(opened);
                return Err(NotebookMutationError::Capacity(error));
            }
        };
        if let Some(superseded) = superseded {
            debug!(uri = ?superseded.uri(), "re-opening a notebook that was already open");
            self.close_departed_cells(&superseded);
        }
        Ok(())
    }

    /// Close listed and explicitly named cell Documents, even for an unknown Notebook.
    pub(crate) fn close(&self, params: DidCloseNotebookDocumentParams) {
        match self.remove(&params.notebook_document.uri) {
            Some(removed) => {
                for cell in removed.cells() {
                    self.documents.close(&cell.document);
                }
            }
            None => debug!(
                uri = ?params.notebook_document.uri,
                "closing a notebook that was not open"
            ),
        }
        for cell in params.cell_text_documents {
            self.documents.close(&cell.uri);
        }
    }

    /// Apply one complete Notebook change, restoring cell Documents on refusal.
    ///
    /// The order is what makes the whole notification all-or-nothing. Planning
    /// the notebook validates the splice without committing it; every cell text
    /// Document mutation that can be refused runs next, undoing its own work on
    /// refusal; the notebook is committed only once nothing can fail; and the
    /// cells that left the notebook — the one irreversible step — close last.
    pub(crate) fn change(
        &self,
        params: DidChangeNotebookDocumentParams,
    ) -> std::result::Result<(), NotebookMutationError> {
        let planned = self.plan_change(&params)?;
        let closed_cells = match params.change.cells {
            Some(cells) => self.apply_cell_text_documents(cells)?,
            None => Vec::new(),
        };
        // A cell the splice removed loses its Document here even when the peer
        // left it out of `didClose`. Nothing afterwards can name that cell, so
        // trusting `didClose` alone is how a cell text Document leaks.
        if let Some(superseded) = self.commit(planned) {
            self.close_departed_cells(&superseded);
        }
        for uri in closed_cells {
            self.documents.close(&uri);
        }
        Ok(())
    }

    /// Mutate the cell text Documents one cell change names — the structural
    /// change's opens, then every cell's text content — and return the URIs the
    /// peer asked to close, which the caller closes after the notebook commits.
    ///
    /// Any refusal restores what this notification already did: the cells it
    /// opened close again and the cells it edited return to their prior text
    /// and version, so the skipped hook has no partial state to observe.
    fn apply_cell_text_documents(
        &self,
        cells: NotebookDocumentCellChanges,
    ) -> std::result::Result<Vec<Uri>, NotebookMutationError> {
        let (opened_cells, closed_cells) = match cells.structure {
            Some(structure) => (
                structure.did_open.unwrap_or_default(),
                structure.did_close.unwrap_or_default(),
            ),
            None => (Vec::new(), Vec::new()),
        };
        let opened = self.open_cell_text_documents(opened_cells)?;

        // Cell text reuses the document store's incremental change path, so a
        // cell edit behaves exactly like `textDocument/didChange` on the same
        // URI — including its all-or-nothing batch and its byte budget.
        let mut edited = Vec::new();
        for cell in cells.text_content.unwrap_or_default() {
            let previous = self
                .documents
                .get(&cell.document.text_document_identifier.uri);
            match self.documents.apply_changes(
                &cell.document.text_document_identifier.uri,
                cell.document.version,
                cell.changes,
            ) {
                Ok(()) => edited.extend(previous),
                Err(error) => {
                    // A URI can occur more than once, including through an
                    // equivalent spelling. Undo the latest snapshot first so
                    // the original text, version, and accounting win last.
                    for document in edited.into_iter().rev() {
                        self.documents.restore(document);
                    }
                    self.rollback_cell_opens(opened);
                    return Err(error.into());
                }
            }
        }

        Ok(closed_cells.into_iter().map(|cell| cell.uri).collect())
    }

    /// Open every cell text Document, retaining the prior snapshot needed to
    /// roll back this notification.
    ///
    /// A refusal undoes the opens made so far, so a rejected notebook
    /// notification leaves no orphan or overwritten cell text Document behind.
    fn open_cell_text_documents(
        &self,
        cells: Vec<TextDocumentItem>,
    ) -> std::result::Result<Vec<CellOpenRollback>, DocumentMutationError> {
        let mut opened = Vec::new();
        for cell in cells {
            let uri = cell.uri.clone();
            let previous = self.documents.get(&uri);
            if let Err(error) = self.documents.open(cell) {
                self.rollback_cell_opens(opened);
                return Err(error);
            }
            opened.push(CellOpenRollback { uri, previous });
        }
        Ok(opened)
    }

    fn rollback_cell_opens(&self, opened: Vec<CellOpenRollback>) {
        for opened in opened.into_iter().rev() {
            match opened.previous {
                Some(document) => self.documents.restore(document),
                None => {
                    self.documents.close(&opened.uri);
                }
            }
        }
    }

    /// Close the cell text Documents a superseded notebook listed that no
    /// synchronized notebook lists any more.
    fn close_departed_cells(&self, superseded: &Notebook) {
        for cell in superseded.cells() {
            if self.notebook_for_cell(&cell.document).is_none() {
                self.documents.close(&cell.document);
            }
        }
    }

    fn insert(&self, document: NotebookDocument) -> Result<Option<Notebook>, LspError> {
        let notebook = Notebook::from_document(document);
        let notebook_key = UriKey::new(notebook.uri());
        let mut inner = self.inner.write().unwrap();
        if !inner.by_uri.contains_key(&notebook_key) && inner.by_uri.len() >= inner.max_notebooks {
            self.trace.resource_budget(
                Resource::Notebooks,
                ResourceAction::Reject,
                inner.by_uri.len(),
                inner.max_notebooks,
                None,
            );
            return Err(LspError::invalid_request(NOTEBOOK_COUNT_CAPACITY_EXHAUSTED));
        }
        let previous = commit(&mut inner, notebook_key, notebook);
        self.trace.resource_budget(
            Resource::Notebooks,
            ResourceAction::Admit,
            inner.by_uri.len(),
            inner.max_notebooks,
            None,
        );
        Ok(previous)
    }

    /// Install `notebook` as the state for its URI, rebuilding the cell index,
    /// and return the notebook it replaced.
    fn commit(&self, notebook: Notebook) -> Option<Notebook> {
        let notebook_key = UriKey::new(notebook.uri());
        let mut inner = self.inner.write().unwrap();
        let previous = commit(&mut inner, notebook_key, notebook);
        self.trace.resource_budget(
            Resource::Notebooks,
            ResourceAction::Update,
            inner.by_uri.len(),
            inner.max_notebooks,
            None,
        );
        previous
    }

    /// Remove Notebook metadata and its cell index.
    /// `None` means the notebook was never synchronized.
    fn remove(&self, uri: &Uri) -> Option<Notebook> {
        let mut inner = self.inner.write().unwrap();
        let removed = inner.by_uri.remove(&UriKey::new(uri))?;
        for cell in removed.cells() {
            inner.notebook_by_cell.remove(&UriKey::new(&cell.document));
        }
        self.trace.resource_budget(
            Resource::Notebooks,
            ResourceAction::Release,
            inner.by_uri.len(),
            inner.max_notebooks,
            None,
        );
        Some(removed)
    }

    /// Whether `uri` names a synchronized notebook.
    pub(crate) fn contains(&self, uri: &Uri) -> bool {
        self.inner
            .read()
            .unwrap()
            .by_uri
            .contains_key(&UriKey::new(uri))
    }

    /// Compute the notebook state one `notebookDocument/didChange` produces,
    /// without committing it.
    ///
    /// Planning separately from [`commit`](Self::commit) is what lets the
    /// cell text mutation run in between: a change whose splice is
    /// out of range is refused before either store moves, so the skipped hook
    /// has no partial state to observe.
    ///
    /// The three parts of a cell change are independent and apply in the order
    /// the LSP specifies: metadata, the structural splice, then per-cell data.
    /// Cell text content is not part of the plan; it belongs to the document
    /// store.
    fn plan_change(&self, params: &DidChangeNotebookDocumentParams) -> Result<Notebook, LspError> {
        let mut notebook = self
            .get(&params.notebook_document.uri)
            .ok_or_else(|| LspError::invalid_request(NOTEBOOK_NOT_FOUND))?;
        notebook.version = params.notebook_document.version;
        if params.change.metadata.is_some() {
            notebook.metadata = params.change.metadata.clone();
        }
        let Some(cells) = params.change.cells.as_ref() else {
            return Ok(notebook);
        };
        if let Some(structure) = &cells.structure {
            // The peer supplies the start, the delete count, and the
            // replacement cells; any of the three can name cells this notebook
            // does not have. `start <= start + delete_count`, so bounding the
            // end also bounds the start and the splice range cannot invert.
            let start = structure.array.start as usize;
            let end = start
                .checked_add(structure.array.delete_count as usize)
                .filter(|end| *end <= notebook.cells.len())
                .ok_or_else(|| LspError::invalid_request(CELL_STRUCTURE_OUT_OF_RANGE))?;
            let replacement = structure.array.cells.clone().unwrap_or_default();
            notebook.cells.splice(start..end, replacement);
        }
        if let Some(data) = &cells.data {
            for updated in data {
                let key = UriKey::new(&updated.document);
                let cell = notebook
                    .cells
                    .iter_mut()
                    .find(|cell| UriKey::new(&cell.document) == key)
                    .ok_or_else(|| LspError::invalid_request(UNKNOWN_CELL_DATA))?;
                *cell = updated.clone();
            }
        }
        Ok(notebook)
    }

    /// Release every notebook the connection tracked.
    pub(crate) fn clear(&self) {
        let mut inner = self.inner.write().unwrap();
        inner.by_uri = HashMap::new();
        inner.notebook_by_cell = HashMap::new();
        self.trace.resource_budget(
            Resource::Notebooks,
            ResourceAction::Release,
            0,
            inner.max_notebooks,
            None,
        );
    }

    fn get(&self, uri: &Uri) -> Option<Notebook> {
        self.inner
            .read()
            .unwrap()
            .by_uri
            .get(&UriKey::new(uri))
            .cloned()
    }

    fn notebook_for_cell(&self, cell_uri: &Uri) -> Option<Notebook> {
        let inner = self.inner.read().unwrap();
        let notebook_uri = inner.notebook_by_cell.get(&UriKey::new(cell_uri))?;
        inner.by_uri.get(notebook_uri).cloned()
    }

    pub(crate) fn view(&self) -> NotebooksView {
        NotebooksView {
            notebooks: self.clone(),
        }
    }
}

fn commit(
    inner: &mut NotebooksInner,
    notebook_key: UriKey,
    notebook: Notebook,
) -> Option<Notebook> {
    let previous = inner.by_uri.remove(&notebook_key);
    if let Some(previous) = &previous {
        for cell in previous.cells() {
            inner.notebook_by_cell.remove(&UriKey::new(&cell.document));
        }
    }
    for cell in notebook.cells() {
        inner
            .notebook_by_cell
            .insert(UriKey::new(&cell.document), notebook_key.clone());
    }
    inner.by_uri.insert(notebook_key, notebook);
    previous
}

/// Read-only access to the synchronized notebooks owned by one connection.
///
/// Cheap to clone: every copy observes the same store, while returned
/// [`Notebook`] values are snapshots that user code can inspect but cannot use
/// to mutate connection state.
#[derive(Debug, Clone)]
pub struct NotebooksView {
    notebooks: Notebooks,
}

impl NotebooksView {
    /// Read a notebook snapshot by URI, or `None` if it is not synchronized.
    pub fn get(&self, uri: &Uri) -> Option<Notebook> {
        self.notebooks.get(uri)
    }

    /// Read the notebook containing `cell_uri`, or `None` if it is not a cell
    /// in any synchronized notebook.
    pub fn notebook_for_cell(&self, cell_uri: &Uri) -> Option<Notebook> {
        self.notebooks.notebook_for_cell(cell_uri)
    }
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use std::str::FromStr;

    use gen_lsp_types::{
        NotebookCellArrayChange, NotebookCellKind, NotebookDocumentCellChangeStructure,
        NotebookDocumentCellChanges, NotebookDocumentChangeEvent,
        VersionedNotebookDocumentIdentifier,
    };
    use serde_json::json;

    use super::*;

    fn uri(spelling: &str) -> Uri {
        Uri::from_str(spelling).expect("the test URI parses")
    }

    fn cell(spelling: &str) -> NotebookCell {
        NotebookCell::new(NotebookCellKind::Code, uri(spelling), None, None)
    }

    fn object(value: serde_json::Value) -> LspObject {
        serde_json::from_value(value).expect("the test metadata is an object")
    }

    /// A notebook with `cells` already synchronized, plus the store holding it.
    fn opened(cells: Vec<NotebookCell>) -> (Notebooks, Uri) {
        let notebooks = Notebooks::default();
        let notebook_uri = uri("file:///analysis.ipynb");
        notebooks
            .open(open_params(&notebook_uri, 1, cells))
            .unwrap();
        (notebooks, notebook_uri)
    }

    fn open_params(
        uri: &Uri,
        version: i32,
        cells: Vec<NotebookCell>,
    ) -> DidOpenNotebookDocumentParams {
        let cell_text_documents = cells
            .iter()
            .map(|cell| TextDocumentItem {
                uri: cell.document.clone(),
                language_id: "python".into(),
                version: 1,
                text: "one".into(),
            })
            .collect();
        DidOpenNotebookDocumentParams {
            notebook_document: NotebookDocument::new(
                uri.clone(),
                "jupyter-notebook".into(),
                version,
                None,
                cells,
            ),
            cell_text_documents,
        }
    }

    fn close_params(uri: &Uri) -> DidCloseNotebookDocumentParams {
        serde_json::from_value(json!({
            "notebookDocument": {"uri": uri}, "cellTextDocuments": []
        }))
        .unwrap()
    }

    fn with_policy(policy: crate::ResourcePolicy) -> (Notebooks, crate::DocumentsView) {
        let trace = ConnectionTrace::new();
        let documents = Documents::with_resource_policy(policy, trace);
        let view = documents.view();
        (
            Notebooks::with_resource_policy(documents, policy, trace),
            view,
        )
    }

    fn changed_cells(value: serde_json::Value) -> NotebookDocumentCellChanges {
        serde_json::from_value(value).unwrap()
    }

    fn structural(
        start: u32,
        delete_count: u32,
        cells: Vec<NotebookCell>,
    ) -> NotebookDocumentCellChanges {
        NotebookDocumentCellChanges {
            structure: Some(NotebookDocumentCellChangeStructure {
                array: NotebookCellArrayChange::new(start, delete_count, Some(cells)),
                did_open: None,
                did_close: None,
            }),
            data: None,
            text_content: None,
        }
    }

    fn cell_uris(notebook: &Notebook) -> Vec<String> {
        notebook
            .cells()
            .iter()
            .map(|cell| cell.document.as_str().to_string())
            .collect()
    }

    /// One `notebookDocument/didChange` against the test notebook.
    fn change(
        uri: &Uri,
        version: i32,
        metadata: Option<LspObject>,
        cells: Option<NotebookDocumentCellChanges>,
    ) -> DidChangeNotebookDocumentParams {
        DidChangeNotebookDocumentParams::new(
            VersionedNotebookDocumentIdentifier::new(version, uri.clone()),
            NotebookDocumentChangeEvent::new(metadata, cells),
        )
    }

    #[test]
    fn a_splice_inserts_replacement_cells_at_the_start_index() {
        let (notebooks, notebook_uri) = opened(vec![cell("file:///c1"), cell("file:///c2")]);

        notebooks
            .change(change(
                &notebook_uri,
                2,
                None,
                Some(structural(1, 0, vec![cell("file:///c3")])),
            ))
            .expect("an in-range insertion is accepted");
        let updated = notebooks.view().get(&notebook_uri).unwrap();

        assert_eq!(
            cell_uris(&updated),
            ["file:///c1", "file:///c3", "file:///c2"],
            "a zero delete count inserts without removing"
        );
        assert_eq!(updated.version(), 2);
    }

    #[test]
    fn a_splice_replaces_the_deleted_range() {
        let (notebooks, notebook_uri) = opened(vec![
            cell("file:///c1"),
            cell("file:///c2"),
            cell("file:///c3"),
        ]);

        notebooks
            .change(change(
                &notebook_uri,
                2,
                None,
                Some(structural(0, 2, vec![cell("file:///c4")])),
            ))
            .expect("an in-range replacement is accepted");
        let updated = notebooks.view().get(&notebook_uri).unwrap();

        assert_eq!(cell_uris(&updated), ["file:///c4", "file:///c3"]);
    }

    #[test]
    fn an_out_of_range_splice_is_a_protocol_error_rather_than_a_panic() {
        let (notebooks, notebook_uri) = opened(vec![cell("file:///c1"), cell("file:///c2")]);

        for (start, delete_count) in [(3, 0), (0, 3), (1, 2), (u32::MAX, u32::MAX)] {
            let error = notebooks
                .change(change(
                    &notebook_uri,
                    2,
                    None,
                    Some(structural(start, delete_count, Vec::new())),
                ))
                .expect_err("an out-of-range splice is refused");
            assert!(
                error.to_string().contains(CELL_STRUCTURE_OUT_OF_RANGE),
                "start {start} delete_count {delete_count} reports the range error: {error}"
            );
        }
    }

    #[test]
    fn changing_an_unsynchronized_notebook_is_a_protocol_error() {
        let (notebooks, _) = opened(vec![cell("file:///c1")]);

        let error = notebooks
            .change(change(&uri("file:///other.ipynb"), 2, None, None))
            .expect_err("an unknown notebook is refused");

        assert!(error.to_string().contains(NOTEBOOK_NOT_FOUND));
    }

    #[test]
    fn metadata_and_cell_data_changes_apply_independently() {
        let (notebooks, notebook_uri) = opened(vec![cell("file:///c1"), cell("file:///c2")]);
        let updated_cell = NotebookCell::new(
            NotebookCellKind::Markup,
            uri("file:///c2"),
            Some(object(json!({ "collapsed": true }))),
            None,
        );

        notebooks
            .change(change(
                &notebook_uri,
                2,
                Some(object(json!({ "kernel": "python3" }))),
                Some(NotebookDocumentCellChanges {
                    structure: None,
                    data: Some(vec![updated_cell.clone()]),
                    text_content: None,
                }),
            ))
            .expect("a data-only change is accepted");
        let updated = notebooks.view().get(&notebook_uri).unwrap();

        assert_eq!(
            updated.metadata(),
            Some(&object(json!({ "kernel": "python3" })))
        );
        assert_eq!(updated.cells(), [cell("file:///c1"), updated_cell]);
    }

    #[test]
    fn cell_data_for_an_unknown_cell_is_a_protocol_error() {
        let (notebooks, notebook_uri) = opened(vec![cell("file:///c1")]);

        let error = notebooks
            .change(change(
                &notebook_uri,
                2,
                None,
                Some(NotebookDocumentCellChanges {
                    structure: None,
                    data: Some(vec![cell("file:///absent")]),
                    text_content: None,
                }),
            ))
            .expect_err("data for a cell outside the notebook is refused");

        assert!(error.to_string().contains(UNKNOWN_CELL_DATA));
    }

    #[test]
    fn closing_a_notebook_forgets_its_cells() {
        let (notebooks, notebook_uri) = opened(vec![cell("file:///c1")]);
        notebooks.close(close_params(&notebook_uri));
        assert!(notebooks.view().get(&notebook_uri).is_none());
        assert!(
            notebooks
                .view()
                .notebook_for_cell(&uri("file:///c1"))
                .is_none()
        );
        notebooks.close(close_params(&notebook_uri));
        assert!(notebooks.view().get(&notebook_uri).is_none());
    }

    #[test]
    fn reopening_a_notebook_replaces_its_cell_index() {
        let (notebooks, notebook_uri) = opened(vec![cell("file:///c1")]);
        notebooks
            .open(open_params(&notebook_uri, 2, vec![cell("file:///c2")]))
            .unwrap();
        let updated = notebooks.view().get(&notebook_uri).unwrap();
        assert_eq!(updated.version(), 2);
        assert_eq!(cell_uris(&updated), ["file:///c2"]);
        assert!(
            notebooks
                .view()
                .notebook_for_cell(&uri("file:///c1"))
                .is_none()
        );
        assert!(
            notebooks
                .view()
                .notebook_for_cell(&uri("file:///c2"))
                .is_some()
        );
    }

    #[test]
    fn a_full_document_count_rejects_a_new_cell_before_closing_the_old_one() {
        let (notebooks, documents) = with_policy(crate::ResourcePolicy {
            max_documents: 1,
            ..crate::ResourcePolicy::default()
        });
        let notebook_uri = uri("file:///analysis.ipynb");
        notebooks
            .open(open_params(&notebook_uri, 1, vec![cell("file:///c1")]))
            .unwrap();
        let before = notebooks.view().get(&notebook_uri).unwrap();
        let error = notebooks.change(change(&notebook_uri, 2, None, Some(changed_cells(json!({
            "structure": {
                "array": {"start": 0, "deleteCount": 1, "cells": [{"kind": 2, "document": "file:///c2"}]},
                "didOpen": [{"uri": "file:///c2", "languageId": "python", "version": 1, "text": "two"}],
                "didClose": [{"uri": "file:///c1"}]
            }
        }))))).unwrap_err();
        assert!(matches!(error, NotebookMutationError::Capacity(_)));
        assert_eq!(notebooks.view().get(&notebook_uri), Some(before));
        assert_eq!(documents.get(&uri("file:///c1")).unwrap().text(), "one");
        assert!(documents.get(&uri("file:///c2")).is_none());
        notebooks.close(close_params(&notebook_uri));
        assert!(documents.get(&uri("file:///c1")).is_none());
        notebooks
            .open(open_params(&notebook_uri, 3, vec![cell("file:///c2")]))
            .unwrap();
        assert_eq!(documents.get(&uri("file:///c2")).unwrap().text(), "one");
    }

    #[test]
    fn rejected_edits_restore_text_version_identity_and_byte_capacity() {
        for repeated in ["file:///c1", "file:///c%31"] {
            let (notebooks, documents) = with_policy(crate::ResourcePolicy {
                max_document_bytes: 6,
                ..crate::ResourcePolicy::default()
            });
            let notebook_uri = uri("file:///analysis.ipynb");
            notebooks
                .open(open_params(
                    &notebook_uri,
                    1,
                    vec![cell("file:///c1"), cell("file:///c2")],
                ))
                .unwrap();
            let before = notebooks.view().get(&notebook_uri).unwrap();
            let error = notebooks.change(change(&notebook_uri, 2, None, Some(changed_cells(json!({
                "textContent": [
                    {"document": {"uri": "file:///c1", "version": 2}, "changes": [{"text": "x"}]},
                    {"document": {"uri": repeated, "version": 3}, "changes": [{"text": "yy"}]},
                    {"document": {"uri": "file:///c2", "version": 2}, "changes": [{"text": "too long"}]}
                ]
            }))))).unwrap_err();
            assert!(matches!(error, NotebookMutationError::Capacity(_)));
            assert_eq!(notebooks.view().get(&notebook_uri), Some(before));
            for spelling in ["file:///c1", "file:///c2"] {
                let document = documents.get(&uri(spelling)).unwrap();
                assert_eq!(document.text(), "one");
                assert_eq!(document.version(), Some(1));
                assert_eq!(document.uri(), &uri(spelling));
            }
            // A reopen exactly fills the byte budget, then one extra byte
            // must still be refused. Both overcount and undercount are visible.
            notebooks
                .open(open_params(
                    &notebook_uri,
                    3,
                    vec![cell("file:///c1"), cell("file:///c2")],
                ))
                .unwrap();
            let too_large = change(
                &notebook_uri,
                4,
                None,
                Some(changed_cells(json!({
                    "textContent": [{"document": {"uri": "file:///c2", "version": 4}, "changes": [{"text": "four"}]}]
                }))),
            );
            assert!(matches!(
                notebooks.change(too_large),
                Err(NotebookMutationError::Capacity(_))
            ));
        }
    }

    #[test]
    fn notebook_capacity_refusal_undoes_repeated_opens_and_new_documents() {
        let (notebooks, documents) = with_policy(crate::ResourcePolicy {
            max_notebooks: 1,
            max_documents: 2,
            max_document_bytes: 6,
            ..crate::ResourcePolicy::default()
        });
        let notebook_uri = uri("file:///analysis.ipynb");
        notebooks
            .open(open_params(&notebook_uri, 1, vec![cell("file:///c1")]))
            .unwrap();
        let other = uri("file:///other.ipynb");
        let mut rejected = open_params(
            &other,
            1,
            vec![cell("file:///c1"), cell("file:///c%31"), cell("file:///c2")],
        );
        rejected.cell_text_documents[0].text = "x".into();
        rejected.cell_text_documents[0].version = 2;
        rejected.cell_text_documents[1].text = "yy".into();
        rejected.cell_text_documents[1].version = 3;
        assert!(matches!(
            notebooks.open(rejected),
            Err(NotebookMutationError::Capacity(_))
        ));
        assert!(notebooks.view().get(&other).is_none());
        assert!(documents.get(&uri("file:///c2")).is_none());
        let restored = documents.get(&uri("file:///c%31")).unwrap();
        assert_eq!(restored.uri(), &uri("file:///c1"));
        assert_eq!(restored.text(), "one");
        assert_eq!(restored.version(), Some(1));
        assert_eq!(
            notebooks
                .view()
                .notebook_for_cell(&uri("file:///c1"))
                .unwrap()
                .uri(),
            &notebook_uri
        );
        notebooks
            .open(open_params(
                &notebook_uri,
                2,
                vec![cell("file:///c1"), cell("file:///c2")],
            ))
            .unwrap();
        assert_eq!(documents.get(&uri("file:///c2")).unwrap().text(), "one");
    }

    #[test]
    fn protocol_refusal_undoes_cell_opens_and_prior_text_edits() {
        let (notebooks, documents) = with_policy(crate::ResourcePolicy::default());
        let notebook_uri = uri("file:///analysis.ipynb");
        notebooks
            .open(open_params(&notebook_uri, 1, vec![cell("file:///c1")]))
            .unwrap();
        let before = notebooks.view().get(&notebook_uri).unwrap();
        let error = notebooks.change(change(&notebook_uri, 2, None, Some(changed_cells(json!({
            "structure": {
                "array": {"start": 1, "deleteCount": 0, "cells": [{"kind": 2, "document": "file:///c2"}]},
                "didOpen": [{"uri": "file:///c2", "languageId": "python", "version": 1, "text": "two"}]
            },
            "textContent": [
                {"document": {"uri": "file:///c1", "version": 2}, "changes": [{"text": "changed"}]},
                {"document": {"uri": "file:///absent", "version": 2}, "changes": [{"text": "invalid"}]}
            ]
        }))))).unwrap_err();
        assert!(matches!(error, NotebookMutationError::Protocol(_)));
        assert_eq!(notebooks.view().get(&notebook_uri), Some(before));
        assert!(documents.get(&uri("file:///c2")).is_none());
        let restored = documents.get(&uri("file:///c1")).unwrap();
        assert_eq!(restored.text(), "one");
        assert_eq!(restored.version(), Some(1));
    }

    #[test]
    fn reopens_and_closes_release_cell_documents_with_membership() {
        let (notebooks, documents) = with_policy(crate::ResourcePolicy::default());
        let notebook_uri = uri("file:///analysis.ipynb");
        notebooks
            .open(open_params(
                &notebook_uri,
                1,
                vec![cell("file:///c1"), cell("file:///c2")],
            ))
            .unwrap();
        notebooks
            .open(open_params(
                &notebook_uri,
                2,
                vec![cell("file:///c2"), cell("file:///c3")],
            ))
            .unwrap();
        assert!(documents.get(&uri("file:///c1")).is_none());
        assert!(documents.get(&uri("file:///c2")).is_some());
        assert!(documents.get(&uri("file:///c3")).is_some());
        let close = serde_json::from_value(json!({
            "notebookDocument": {"uri": "file:///unknown.ipynb"},
            "cellTextDocuments": [{"uri": "file:///c3"}]
        }))
        .unwrap();
        notebooks.close(close);
        assert!(documents.get(&uri("file:///c3")).is_none());
        notebooks.close(close_params(&notebook_uri));
        assert!(documents.get(&uri("file:///c2")).is_none());
        assert!(notebooks.view().get(&notebook_uri).is_none());
    }

    #[test]
    fn clear_releases_every_notebook_and_cell() {
        let (notebooks, notebook_uri) = opened(vec![cell("file:///c1")]);

        notebooks.clear();

        assert!(notebooks.view().get(&notebook_uri).is_none());
        assert!(
            notebooks
                .view()
                .notebook_for_cell(&uri("file:///c1"))
                .is_none()
        );
    }
}