|
| 1 | +package mcp.code.analysis.server |
| 2 | + |
| 3 | +import io.mockk.* |
| 4 | +import io.modelcontextprotocol.kotlin.sdk.* |
| 5 | +import io.modelcontextprotocol.kotlin.sdk.server.Server as SdkServer |
| 6 | +import io.modelcontextprotocol.kotlin.sdk.server.StdioServerTransport |
| 7 | +import kotlin.test.assertEquals |
| 8 | +import kotlinx.coroutines.runBlocking |
| 9 | +import kotlinx.serialization.json.JsonObject |
| 10 | +import kotlinx.serialization.json.JsonPrimitive |
| 11 | +import mcp.code.analysis.service.RepositoryAnalysisService |
| 12 | +import org.junit.jupiter.api.BeforeEach |
| 13 | +import org.junit.jupiter.api.Disabled |
| 14 | +import org.junit.jupiter.api.Test |
| 15 | +import org.slf4j.Logger |
| 16 | + |
| 17 | +class ServerTest { |
| 18 | + private lateinit var repositoryAnalysisService: RepositoryAnalysisService |
| 19 | + private lateinit var logger: Logger |
| 20 | + private lateinit var server: Server |
| 21 | + private lateinit var mockSdkServer: SdkServer |
| 22 | + |
| 23 | + @BeforeEach |
| 24 | + fun setUp() { |
| 25 | + repositoryAnalysisService = mockk(relaxed = true) |
| 26 | + logger = mockk(relaxed = true) |
| 27 | + mockSdkServer = mockk(relaxed = true) |
| 28 | + server = Server(repositoryAnalysisService, logger) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun `constructor should initialize with default values when not provided`() { |
| 33 | + // Arrange |
| 34 | + val defaultServer = Server() |
| 35 | + val implField = defaultServer.javaClass.getDeclaredField("implementation") |
| 36 | + implField.isAccessible = true |
| 37 | + |
| 38 | + // Act |
| 39 | + val implementation = implField.get(defaultServer) as Implementation |
| 40 | + |
| 41 | + // Assert |
| 42 | + assertEquals("MCP GitHub Code Analysis Server", implementation.name) |
| 43 | + assertEquals("0.1.0", implementation.version) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + @Disabled("Temporarily disabled") |
| 48 | + fun `configureServer should register analyze-repository tool`() { |
| 49 | + // Arrange |
| 50 | + mockkConstructor(SdkServer::class) |
| 51 | + val mockSdkServer = mockk<SdkServer>(relaxed = true) |
| 52 | + every { anyConstructed<SdkServer>() } returns mockSdkServer |
| 53 | + every { mockSdkServer.addTool(any(), any(), any(), any()) } returns mockk() |
| 54 | + |
| 55 | + val configureServerMethod = server.javaClass.getDeclaredMethod("configureServer") |
| 56 | + configureServerMethod.isAccessible = true |
| 57 | + configureServerMethod.invoke(server) |
| 58 | + |
| 59 | + // Act & Assert |
| 60 | + verify { mockSdkServer.addTool(any(), any(), any(), any()) } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun `analyze-repository tool should call repositoryAnalysisService`() = runBlocking { |
| 65 | + // Arrange |
| 66 | + val configureServerMethod = server.javaClass.getDeclaredMethod("configureServer") |
| 67 | + configureServerMethod.isAccessible = true |
| 68 | + |
| 69 | + val handlerCaptor = slot<suspend (CallToolRequest) -> CallToolResult>() |
| 70 | + |
| 71 | + mockkConstructor(SdkServer::class) |
| 72 | + every { anyConstructed<SdkServer>().addTool(any(), any(), any(), capture(handlerCaptor)) } just Runs |
| 73 | + |
| 74 | + configureServerMethod.invoke(server) |
| 75 | + |
| 76 | + val mockRequest = mockk<CallToolRequest>() |
| 77 | + every { mockRequest.arguments } returns |
| 78 | + JsonObject( |
| 79 | + mapOf("repoUrl" to JsonPrimitive("https://github.com/example/repo"), "branch" to JsonPrimitive("main")) |
| 80 | + ) |
| 81 | + |
| 82 | + coEvery { repositoryAnalysisService.analyzeRepository("https://github.com/example/repo", "main") } returns |
| 83 | + "Analysis result" |
| 84 | + |
| 85 | + val result = handlerCaptor.captured(mockRequest) |
| 86 | + |
| 87 | + // Assert & Verify |
| 88 | + assertEquals(false, result.isError) |
| 89 | + assertEquals("Analysis result", (result.content.first() as TextContent).text) |
| 90 | + coVerify { repositoryAnalysisService.analyzeRepository("https://github.com/example/repo", "main") } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + @Disabled("Temporarily disabled") |
| 95 | + fun `runMcpServerUsingStdio should connect transport`() = runBlocking { |
| 96 | + // Arrange |
| 97 | + mockkConstructor(StdioServerTransport::class) |
| 98 | + val mockTransport = mockk<StdioServerTransport>(relaxed = true) |
| 99 | + every { anyConstructed<StdioServerTransport>() } returns mockTransport |
| 100 | + |
| 101 | + mockkConstructor(SdkServer::class) |
| 102 | + val mockSdkServer = mockk<SdkServer>(relaxed = true) |
| 103 | + every { anyConstructed<SdkServer>() } returns mockSdkServer |
| 104 | + coEvery { mockSdkServer.connect(any()) } just Runs |
| 105 | + coEvery { mockSdkServer.onClose(any()) } answers { firstArg<() -> Unit>().invoke() } |
| 106 | + |
| 107 | + // Act |
| 108 | + server.runMcpServerUsingStdio() |
| 109 | + |
| 110 | + // Assert |
| 111 | + coVerify { mockSdkServer.connect(mockTransport) } |
| 112 | + } |
| 113 | +} |
0 commit comments