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