lsp.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { afterEach, describe, expect } from "bun:test"
  2. import { Effect, Layer } from "effect"
  3. import path from "path"
  4. import { Agent } from "../../src/agent/agent"
  5. import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
  6. import { AppFileSystem } from "@opencode-ai/core/filesystem"
  7. import { LSP } from "@/lsp/lsp"
  8. import { Permission } from "../../src/permission"
  9. import { Instance } from "../../src/project/instance"
  10. import { MessageID, SessionID } from "../../src/session/schema"
  11. import { Tool } from "@/tool/tool"
  12. import { Truncate } from "@/tool/truncate"
  13. import { LspTool } from "../../src/tool/lsp"
  14. import { disposeAllInstances, provideTmpdirInstance } from "../fixture/fixture"
  15. import { testEffect } from "../lib/effect"
  16. afterEach(async () => {
  17. await disposeAllInstances()
  18. })
  19. const ctx = {
  20. sessionID: SessionID.make("ses_test"),
  21. messageID: MessageID.make("msg_test"),
  22. callID: "",
  23. agent: "build",
  24. abort: AbortSignal.any([]),
  25. messages: [],
  26. metadata: () => Effect.void,
  27. ask: () => Effect.void,
  28. }
  29. const workspaceSymbolQueries: string[] = []
  30. const lsp = Layer.succeed(
  31. LSP.Service,
  32. LSP.Service.of({
  33. init: () => Effect.void,
  34. status: () => Effect.succeed([]),
  35. hasClients: () => Effect.succeed(true),
  36. touchFile: () => Effect.void,
  37. diagnostics: () => Effect.succeed({}),
  38. hover: () => Effect.succeed([]),
  39. definition: () => Effect.succeed([]),
  40. references: () => Effect.succeed([]),
  41. implementation: () => Effect.succeed([]),
  42. documentSymbol: () => Effect.succeed([]),
  43. workspaceSymbol: (query) =>
  44. Effect.sync(() => {
  45. workspaceSymbolQueries.push(query)
  46. return []
  47. }),
  48. prepareCallHierarchy: () => Effect.succeed([]),
  49. incomingCalls: () => Effect.succeed([]),
  50. outgoingCalls: () => Effect.succeed([]),
  51. }),
  52. )
  53. const it = testEffect(
  54. Layer.mergeAll(
  55. Agent.defaultLayer,
  56. AppFileSystem.defaultLayer,
  57. CrossSpawnSpawner.defaultLayer,
  58. Truncate.defaultLayer,
  59. lsp,
  60. ),
  61. )
  62. const init = Effect.fn("LspToolTest.init")(function* () {
  63. const info = yield* LspTool
  64. return yield* info.init()
  65. })
  66. const run = Effect.fn("LspToolTest.run")(function* (
  67. args: Tool.InferParameters<typeof LspTool>,
  68. next: Tool.Context = ctx,
  69. ) {
  70. const tool = yield* init()
  71. return yield* tool.execute(args, next)
  72. })
  73. const put = Effect.fn("LspToolTest.put")(function* (file: string) {
  74. const fs = yield* AppFileSystem.Service
  75. yield* fs.writeWithDirs(file, "export const x = 1\n")
  76. })
  77. const asks = () => {
  78. const items: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  79. return {
  80. items,
  81. next: {
  82. ...ctx,
  83. ask: (req: Omit<Permission.Request, "id" | "sessionID" | "tool">) =>
  84. Effect.sync(() => {
  85. items.push(req)
  86. }),
  87. },
  88. }
  89. }
  90. describe("tool.lsp", () => {
  91. describe("permission metadata", () => {
  92. it.live("keeps cursor details for position-based operations", () =>
  93. provideTmpdirInstance(
  94. (dir) =>
  95. Effect.gen(function* () {
  96. const file = path.join(dir, "test.ts")
  97. yield* put(file)
  98. const { items, next } = asks()
  99. const result = yield* run({ operation: "goToDefinition", filePath: file, line: 3, character: 7 }, next)
  100. const req = items.find((item) => item.permission === "lsp")
  101. expect(req).toBeDefined()
  102. expect(req!.metadata).toEqual({
  103. operation: "goToDefinition",
  104. filePath: file,
  105. line: 3,
  106. character: 7,
  107. })
  108. expect(result.title).toBe("goToDefinition test.ts:3:7")
  109. }),
  110. { git: true },
  111. ),
  112. )
  113. it.live("omits cursor details for documentSymbol", () =>
  114. provideTmpdirInstance(
  115. (dir) =>
  116. Effect.gen(function* () {
  117. const file = path.join(dir, "test.ts")
  118. yield* put(file)
  119. const { items, next } = asks()
  120. const result = yield* run({ operation: "documentSymbol", filePath: file, line: 3, character: 7 }, next)
  121. const req = items.find((item) => item.permission === "lsp")
  122. expect(req).toBeDefined()
  123. expect(req!.metadata).toEqual({
  124. operation: "documentSymbol",
  125. filePath: file,
  126. })
  127. expect(result.title).toBe("documentSymbol test.ts")
  128. }),
  129. { git: true },
  130. ),
  131. )
  132. it.live("omits file and cursor details for workspaceSymbol", () =>
  133. provideTmpdirInstance(
  134. (dir) =>
  135. Effect.gen(function* () {
  136. workspaceSymbolQueries.length = 0
  137. const file = path.join(dir, "test.ts")
  138. yield* put(file)
  139. const { items, next } = asks()
  140. const result = yield* run({ operation: "workspaceSymbol", filePath: file, line: 3, character: 7 }, next)
  141. const req = items.find((item) => item.permission === "lsp")
  142. expect(req).toBeDefined()
  143. expect(req!.metadata).toEqual({
  144. operation: "workspaceSymbol",
  145. })
  146. expect(result.title).toBe("workspaceSymbol")
  147. }),
  148. { git: true },
  149. ),
  150. )
  151. it.live("passes workspaceSymbol query to LSP", () =>
  152. provideTmpdirInstance(
  153. (dir) =>
  154. Effect.gen(function* () {
  155. workspaceSymbolQueries.length = 0
  156. const file = path.join(dir, "test.ts")
  157. yield* put(file)
  158. yield* run({ operation: "workspaceSymbol", filePath: file, line: 3, character: 7, query: "TestSymbol" })
  159. yield* run({ operation: "workspaceSymbol", filePath: file, line: 3, character: 7 })
  160. expect(workspaceSymbolQueries).toEqual(["TestSymbol", ""])
  161. }),
  162. { git: true },
  163. ),
  164. )
  165. })
  166. })