package p.studio.app.mcp; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import p.studio.core.project.ProjectAccessRegistry; import p.studio.core.session.DefaultAutomationSession; import p.studio.app.operation.ExecutorOperationExecutor; import p.studio.api.automation.*; import java.nio.file.Path; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; import static org.junit.jupiter.api.Assertions.*; class McpConnectionGuideTest { @TempDir Path root; @Test void exportsLiveProjectDescriptorWithoutGrantAndTracksRestartWithoutRetainingSecrets() throws Exception { var mapper = new ObjectMapper(); var projects = new ProjectAccessRegistry(); try (var workers = Executors.newSingleThreadExecutor(); var server = new LocalMcpServer(mapper, new McpToolRegistry(new McpToolCatalog(mapper), mapper, Map.of()))) { var authority = new DefaultAutomationSession(root, new ExecutorOperationExecutor(workers)); var registration = projects.register(root, authority::enterManual, () -> !authority.snapshot().closed(), authority); var guide = new McpConnectionGuide(server, projects, mapper); assertTrue(guide.endpoint().isEmpty()); assertThrows(IllegalStateException.class, () -> guide.configuration(root)); var notifications = new AtomicInteger(); var subscription = guide.subscribe(notifications::incrementAndGet); server.start(); assertEquals(2, notifications.get()); var descriptor = mapper.readTree(guide.configuration(root)); assertEquals(server.connection().endpoint().toString(), descriptor.path("url").asText()); assertEquals(root.toRealPath().toString(), descriptor.path("projectRoot").asText()); // Do not put bearer credentials into assertion diagnostics. assertTrue(server.connection().authorizationHeader().equals(descriptor.path("headers").path("Authorization").asText())); assertFalse(guide.toString().contains(descriptor.path("headers").path("Authorization").asText())); assertEquals(ControlState.MANUAL, authority.snapshot().state()); assertNull(authority.snapshot().task()); for (var instruction : List.of("studio_project_context", "studio_reference_read", "studio_control_acquire", "studio_control_release", "studio_language_query", "studio_build_start", "clientRequestId", "read-only", "restart", "native file writes")) assertTrue(descriptor.path("instructions").asText().contains(instruction)); var old = server.connection(); server.close(); assertEquals(3, notifications.get()); assertTrue(guide.endpoint().isEmpty()); server.start(); assertEquals(4, notifications.get()); var fresh = mapper.readTree(guide.configuration(root)); assertTrue(fresh.path("headers").path("Authorization").asText().equals(server.connection().authorizationHeader())); assertFalse(fresh.path("headers").path("Authorization").asText().equals(old.authorizationHeader())); assertThrows(AutomationException.class, () -> guide.configuration(root.resolve("unopened"))); authority.closeAsync().toCompletableFuture().join(); assertThrows(AutomationException.class, () -> guide.configuration(root)); registration.close(); subscription.close(); server.close(); assertEquals(4, notifications.get()); } } }