Files
openide/plugins/mcp-server/test/com/intellij/mcpserver/McpJsonGenerationTest.kt
Artem.Bukhonov efe41f6d8d [MCP Server] Add advanced tests for all toolsets. Improve transport tests
(cherry picked from commit bcb3c215ddcccfad3311ed5163503175e54254a4)

IJ-CR-166188

GitOrigin-RevId: 0a16d89676ee5f7d9f80688805bd4312d7f20ccd
2025-06-19 17:22:45 +00:00

66 lines
2.3 KiB
Kotlin

package com.intellij.mcpserver
import com.intellij.mcpserver.stdio.IJ_MCP_SERVER_PORT
import com.intellij.mcpserver.stdio.IJ_MCP_SERVER_PROJECT_PATH
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
class McpJsonGenerationTest {
@Test
fun test_createStdioMcpServerJsonConfiguration() {
val port = 12345
val projectPath = "/test/project/path"
val jsonConfig = createStdioMcpServerJsonConfiguration(port, projectPath)
// Verify the JSON structure
Assertions.assertNotNull(jsonConfig["command"])
Assertions.assertNotNull(jsonConfig["args"])
Assertions.assertNotNull(jsonConfig["env"])
// Verify command is a java executable
val command = jsonConfig["command"]?.jsonPrimitive?.content
assertTrue(command?.endsWith("java") == true, "Command should be java executable")
// Verify environment variables
val env = jsonConfig["env"]?.jsonObject!!
Assertions.assertNotNull(env)
Assertions.assertEquals(port.toString(), env[IJ_MCP_SERVER_PORT]?.jsonPrimitive?.content)
Assertions.assertEquals(projectPath, env[IJ_MCP_SERVER_PROJECT_PATH]?.jsonPrimitive?.content)
}
@Test
fun test_createStdioMcpServerJsonConfiguration_withoutProjectPath() {
val port = 12345
val jsonConfig = createStdioMcpServerJsonConfiguration(port, null)
// Verify the JSON structure
Assertions.assertNotNull(jsonConfig["command"])
Assertions.assertNotNull(jsonConfig["args"])
Assertions.assertNotNull(jsonConfig["env"])
Assertions.assertEquals("stdio", jsonConfig["type"]?.jsonPrimitive?.content)
// Verify environment variables
val env = jsonConfig["env"]?.jsonObject!!
Assertions.assertNotNull(env)
Assertions.assertEquals(port.toString(), env[IJ_MCP_SERVER_PORT]?.jsonPrimitive?.content)
// Project path should not be present when null
assertTrue(env[IJ_MCP_SERVER_PROJECT_PATH] == null)
}
@Test
fun test_createSseServerJsonEntry() {
val port = 8080
val jsonConfig = createSseServerJsonEntry(port)
// Verify the JSON structure
Assertions.assertEquals("sse", jsonConfig["type"]?.jsonPrimitive?.content)
Assertions.assertEquals("http://localhost:$port/sse", jsonConfig["url"]?.jsonPrimitive?.content)
}
}