client.test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { describe, expect, test, beforeEach } from "bun:test"
  2. import path from "path"
  3. import { LSPClient } from "../../src/lsp/client"
  4. import { LSPServer } from "../../src/lsp/server"
  5. import { Instance } from "../../src/project/instance"
  6. import { Log } from "../../src/util/log"
  7. // Minimal fake LSP server that speaks JSON-RPC over stdio
  8. function spawnFakeServer() {
  9. const { spawn } = require("child_process")
  10. const serverPath = path.join(__dirname, "../fixture/lsp/fake-lsp-server.js")
  11. return {
  12. process: spawn(process.execPath, [serverPath], {
  13. stdio: "pipe",
  14. }),
  15. }
  16. }
  17. describe("LSPClient interop", () => {
  18. beforeEach(async () => {
  19. await Log.init({ print: true })
  20. })
  21. test("handles workspace/workspaceFolders request", async () => {
  22. const handle = spawnFakeServer() as any
  23. const client = await Instance.provide({
  24. directory: process.cwd(),
  25. fn: () =>
  26. LSPClient.create({
  27. serverID: "fake",
  28. server: handle as unknown as LSPServer.Handle,
  29. root: process.cwd(),
  30. }),
  31. })
  32. await client.connection.sendNotification("test/trigger", {
  33. method: "workspace/workspaceFolders",
  34. })
  35. await new Promise((r) => setTimeout(r, 100))
  36. expect(client.connection).toBeDefined()
  37. await client.shutdown()
  38. })
  39. test("handles client/registerCapability request", async () => {
  40. const handle = spawnFakeServer() as any
  41. const client = await Instance.provide({
  42. directory: process.cwd(),
  43. fn: () =>
  44. LSPClient.create({
  45. serverID: "fake",
  46. server: handle as unknown as LSPServer.Handle,
  47. root: process.cwd(),
  48. }),
  49. })
  50. await client.connection.sendNotification("test/trigger", {
  51. method: "client/registerCapability",
  52. })
  53. await new Promise((r) => setTimeout(r, 100))
  54. expect(client.connection).toBeDefined()
  55. await client.shutdown()
  56. })
  57. test("handles client/unregisterCapability request", async () => {
  58. const handle = spawnFakeServer() as any
  59. const client = await Instance.provide({
  60. directory: process.cwd(),
  61. fn: () =>
  62. LSPClient.create({
  63. serverID: "fake",
  64. server: handle as unknown as LSPServer.Handle,
  65. root: process.cwd(),
  66. }),
  67. })
  68. await client.connection.sendNotification("test/trigger", {
  69. method: "client/unregisterCapability",
  70. })
  71. await new Promise((r) => setTimeout(r, 100))
  72. expect(client.connection).toBeDefined()
  73. await client.shutdown()
  74. })
  75. })