test(mcp): stabilize lsp builtin and doctor checks
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -0,0 +1,96 @@
|
|||||||
|
/// <reference types="bun-types" />
|
||||||
|
|
||||||
|
import { afterEach, describe, expect, it, mock } from "bun:test"
|
||||||
|
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
|
||||||
|
import { tmpdir } from "node:os"
|
||||||
|
import { join } from "node:path"
|
||||||
|
import { clearPluginConfigFileDetectionCache } from "../../../shared/jsonc-parser"
|
||||||
|
|
||||||
|
const originalCwd = process.cwd()
|
||||||
|
const originalOpenCodeConfigDir = process.env.OPENCODE_CONFIG_DIR
|
||||||
|
const temporaryDirectories: string[] = []
|
||||||
|
|
||||||
|
function createTemporaryDirectory(prefix: string): string {
|
||||||
|
const directory = mkdtempSync(join(tmpdir(), prefix))
|
||||||
|
temporaryDirectories.push(directory)
|
||||||
|
return directory
|
||||||
|
}
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
mock.restore()
|
||||||
|
clearPluginConfigFileDetectionCache()
|
||||||
|
process.chdir(originalCwd)
|
||||||
|
|
||||||
|
if (originalOpenCodeConfigDir === undefined) {
|
||||||
|
delete process.env.OPENCODE_CONFIG_DIR
|
||||||
|
} else {
|
||||||
|
process.env.OPENCODE_CONFIG_DIR = originalOpenCodeConfigDir
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const directory of temporaryDirectories.splice(0)) {
|
||||||
|
rmSync(directory, { recursive: true, force: true })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("getInstalledLspServers", () => {
|
||||||
|
it("returns empty when lsp MCP is disabled via config", async () => {
|
||||||
|
// given
|
||||||
|
const userConfigDirectory = createTemporaryDirectory("omo-tools-lsp-user-")
|
||||||
|
const workspaceDirectory = createTemporaryDirectory("omo-tools-lsp-workspace-")
|
||||||
|
const projectConfigDirectory = join(workspaceDirectory, ".opencode")
|
||||||
|
mkdirSync(projectConfigDirectory, { recursive: true })
|
||||||
|
writeFileSync(
|
||||||
|
join(projectConfigDirectory, "oh-my-openagent.json"),
|
||||||
|
JSON.stringify({ disabled_mcps: ["lsp"] }),
|
||||||
|
"utf-8",
|
||||||
|
)
|
||||||
|
process.env.OPENCODE_CONFIG_DIR = userConfigDirectory
|
||||||
|
process.chdir(workspaceDirectory)
|
||||||
|
clearPluginConfigFileDetectionCache()
|
||||||
|
|
||||||
|
const { getInstalledLspServers } = await import(`./tools-lsp?t=${Date.now()}-disabled`)
|
||||||
|
|
||||||
|
// when
|
||||||
|
const servers = getInstalledLspServers()
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(servers).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns bundled lsp server info when MCP is enabled and available", async () => {
|
||||||
|
// given
|
||||||
|
const userConfigDirectory = createTemporaryDirectory("omo-tools-lsp-user-")
|
||||||
|
const workspaceDirectory = createTemporaryDirectory("omo-tools-lsp-enabled-")
|
||||||
|
const lspCliDirectory = join(workspaceDirectory, "vendor", "lsp-tools-mcp", "dist")
|
||||||
|
mkdirSync(lspCliDirectory, { recursive: true })
|
||||||
|
writeFileSync(join(lspCliDirectory, "cli.js"), "#!/usr/bin/env node\n", "utf-8")
|
||||||
|
process.env.OPENCODE_CONFIG_DIR = userConfigDirectory
|
||||||
|
process.chdir(workspaceDirectory)
|
||||||
|
clearPluginConfigFileDetectionCache()
|
||||||
|
|
||||||
|
const { getInstalledLspServers } = await import(`./tools-lsp?t=${Date.now()}-enabled`)
|
||||||
|
|
||||||
|
// when
|
||||||
|
const servers = getInstalledLspServers()
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(servers).toEqual([{ id: "lsp-tools-mcp", extensions: ["*"] }])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns empty when lsp MCP binary is unavailable", async () => {
|
||||||
|
// given
|
||||||
|
const userConfigDirectory = createTemporaryDirectory("omo-tools-lsp-user-")
|
||||||
|
const workspaceDirectory = createTemporaryDirectory("omo-tools-lsp-missing-")
|
||||||
|
process.env.OPENCODE_CONFIG_DIR = userConfigDirectory
|
||||||
|
process.chdir(workspaceDirectory)
|
||||||
|
clearPluginConfigFileDetectionCache()
|
||||||
|
|
||||||
|
const { getInstalledLspServers } = await import(`./tools-lsp?t=${Date.now()}-missing-cli`)
|
||||||
|
|
||||||
|
// when
|
||||||
|
const servers = getInstalledLspServers()
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(servers).toEqual([])
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,9 +1,16 @@
|
|||||||
import { describe, expect, test } from "bun:test"
|
import { afterEach, describe, expect, mock, test } from "bun:test"
|
||||||
import { createBuiltinMcps } from "../index"
|
|
||||||
|
afterEach(() => {
|
||||||
|
mock.restore()
|
||||||
|
})
|
||||||
|
|
||||||
describe("createBuiltinMcps", () => {
|
describe("createBuiltinMcps", () => {
|
||||||
test("should return all MCPs when disabled_mcps is empty", () => {
|
test("should return all MCPs when disabled_mcps is empty", () => {
|
||||||
// given
|
// given
|
||||||
|
mock.module("../lsp", () => ({
|
||||||
|
createLspMcpConfig: () => ({ type: "local", command: ["node", "dist/cli.js", "mcp"], enabled: true }),
|
||||||
|
}))
|
||||||
|
const { createBuiltinMcps } = require("../index") as typeof import("../index")
|
||||||
const disabledMcps: string[] = []
|
const disabledMcps: string[] = []
|
||||||
|
|
||||||
// when
|
// when
|
||||||
@@ -19,6 +26,10 @@ describe("createBuiltinMcps", () => {
|
|||||||
|
|
||||||
test("should filter out disabled MCPs", () => {
|
test("should filter out disabled MCPs", () => {
|
||||||
// given
|
// given
|
||||||
|
mock.module("../lsp", () => ({
|
||||||
|
createLspMcpConfig: () => ({ type: "local", command: ["node", "dist/cli.js", "mcp"], enabled: true }),
|
||||||
|
}))
|
||||||
|
const { createBuiltinMcps } = require("../index") as typeof import("../index")
|
||||||
const disabledMcps = ["websearch"]
|
const disabledMcps = ["websearch"]
|
||||||
|
|
||||||
// when
|
// when
|
||||||
@@ -33,6 +44,10 @@ describe("createBuiltinMcps", () => {
|
|||||||
|
|
||||||
test("should return empty array when all MCPs are disabled", () => {
|
test("should return empty array when all MCPs are disabled", () => {
|
||||||
// given - disable all known MCPs
|
// given - disable all known MCPs
|
||||||
|
mock.module("../lsp", () => ({
|
||||||
|
createLspMcpConfig: () => ({ type: "local", command: ["node", "dist/cli.js", "mcp"], enabled: true }),
|
||||||
|
}))
|
||||||
|
const { createBuiltinMcps } = require("../index") as typeof import("../index")
|
||||||
const disabledMcps = ["websearch", "context7", "grep_app", "lsp"]
|
const disabledMcps = ["websearch", "context7", "grep_app", "lsp"]
|
||||||
|
|
||||||
// when
|
// when
|
||||||
|
|||||||
Reference in New Issue
Block a user