Skip to content

Add unit tests for the LSP - #105

Merged
Rigidity merged 2 commits into
mainfrom
lsp-tests
Oct 12, 2026
Merged

Rigidity merged 2 commits into
mainfrom
lsp-tests

Conversation

@Rigidity

@Rigidity Rigidity commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #63

Summary

  • Library/binary split. rue-lsp is now a library (Backend) plus a small binary that runs it over stdio, so the server can be tested.
  • Test harness (src/tests/mod.rs). It writes a temporary project to disk, opens each file in the server, and records the cursor from a $0 marker. Results are rendered as text and compared with expect-test snapshots.
  • Tests (53). They cover:
    • hover: bindings, parameters, functions, constants, modules, aliases, builtin types, structs (including spread fields), fields
    • go to definition and references, including across files and file modules
    • completion: locals, partial identifiers, types, struct, pair and atom fields, struct initializers, module exports
    • diagnostics: updates on change, warnings, save reverting to the file on disk, errors reported across files
    • formatting, including document_end with CRLF and UTF-16, and fuzzy_match
    • an end-to-end JSON-RPC session through LspService

The snapshots record how the server behaves today, so later refactors will show up as snapshot diffs.

Fixes found by the tests

  • Completion at the very start of a file panicked (index - 1 underflow).
  • on_change panicked on non-file URIs, invalid Rue.toml files, and missing entrypoints. It now returns no diagnostics in those cases.
  • Scope::symbol_names() / type_names() iterated a HashMap, so completion order was different on every run. They now use IndexMap (insertion order).

Behaviour the snapshots record but this PR doesn't change

  • Builtin functions show {unresolved} as their type in completions.
  • Functions and constants hover as let name: type.
  • Generic arguments are dropped in hover, e.g. ...rest: List instead of List<Int>.
  • No completions are offered in an empty type position (fn main(value: $0)).
  • Go to definition on a declaration returns its references.
  • Cursor positions are converted using character counts and byte slicing rather than UTF-16, so non-ASCII text before the cursor can give wrong positions.

Test plan

  • cargo test --workspace --all-features
  • cargo clippy --workspace --all-features --all-targets (no warnings)
  • cargo fmt --check, cargo machete
  • Ran the LSP tests 8 times in a row; no flaky results

Note

Low Risk
Mostly test infrastructure and defensive error handling in the LSP; no security or data-path changes, with small behavior fixes for panics and completion ordering.

Overview
Adds a broad LSP test suite by turning rue-lsp into a library + thin stdio binary: Backend and server logic live in lib.rs, and main.rs only wires up LspService.

Introduces a TestProject harness (temp Rue.toml project, $0 cursor markers, expect-test snapshots) plus JSON-RPC end-to-end coverage via LspService. Tests lock in hover, go-to-definition/references, completion, diagnostics, formatting, and protocol behavior.

Production fixes bundled with the tests: completion no longer uses index - 1 (avoids panics at BOF); on_change_impl returns empty diagnostics instead of panicking on non-file URIs, bad manifests, or failed compiles; Scope symbol/type name maps use IndexMap so completion order is stable across runs.

Reviewed by Cursor Bugbot for commit 7b97d26. Bugbot is set up for automated code reviews on this repo. Configure here.

symbol_names() and type_names() iterated a HashMap, so completion results
(and glob imports) came out in a different order on every run.
Split rue-lsp into a library and a thin binary so the Backend can be
tested, and add tests covering hover, go to definition, references,
completion, diagnostics, formatting, and an end-to-end JSON-RPC session.

Fix crashes found along the way:
- Completion at the start of a file underflowed when checking the
  previous character.
- on_change panicked on non-file URIs, invalid Rue.toml files, and
  missing entrypoints.
@Rigidity Rigidity mentioned this pull request Oct 12, 2026
2 tasks done
@Rigidity
Rigidity added this pull request to stack #107 October 12, 2026 22:56
@Rigidity
Rigidity merged commit ce5d480 into main Oct 12, 2026
2 checks passed
@Rigidity
Rigidity deleted the lsp-tests branch October 12, 2026 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unit test LSP

1 participant