test-standalone-core-api-server.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/env npx tsx
  2. /**
  3. * Simple Cline gRPC Server
  4. *
  5. * This script provides a minimal way to run the Cline core gRPC service
  6. * without requiring the full installation, while automatically mocking all external services. Simply run:
  7. *
  8. * # One-time setup (generates protobuf files)
  9. * npm run compile-standalone
  10. * npm run test:sca-server
  11. *
  12. * The following components are started automatically:
  13. * 1. HostBridge test server
  14. * 2. ClineApiServerMock (mock implementation of the Cline API)
  15. * 3. AuthServiceMock (activated if E2E_TEST="true")
  16. *
  17. * Environment Variables for Customization:
  18. * PROJECT_ROOT - Override project root directory (default: parent of scripts dir)
  19. * CLINE_DIST_DIR - Override distribution directory (default: PROJECT_ROOT/dist-standalone)
  20. * CLINE_CORE_FILE - Override core file name (default: cline-core.js)
  21. * PROTOBUS_PORT - gRPC server port (default: 26040)
  22. * HOSTBRIDGE_PORT - HostBridge server port (default: 26041)
  23. * WORKSPACE_DIR - Working directory (default: current directory)
  24. * E2E_TEST - Enable E2E test mode (default: true)
  25. * CLINE_ENVIRONMENT - Environment setting (default: local)
  26. *
  27. * Ideal for local development, testing, or lightweight E2E scenarios.
  28. */
  29. import * as fs from "node:fs"
  30. import { mkdtempSync, rmSync } from "node:fs"
  31. import * as os from "node:os"
  32. import { ChildProcess, execSync, spawn } from "child_process"
  33. import * as path from "path"
  34. import { ClineApiServerMock } from "../src/test/e2e/fixtures/server/index"
  35. const PROTOBUS_PORT = process.env.PROTOBUS_PORT || "26040"
  36. const HOSTBRIDGE_PORT = process.env.HOSTBRIDGE_PORT || "26041"
  37. const WORKSPACE_DIR = process.env.WORKSPACE_DIR || process.cwd()
  38. const E2E_TEST = process.env.E2E_TEST || "true"
  39. const CLINE_ENVIRONMENT = process.env.CLINE_ENVIRONMENT || "local"
  40. const USE_C8 = process.env.USE_C8 === "true"
  41. // Locate the standalone build directory and core file with flexible path resolution
  42. const projectRoot = process.env.PROJECT_ROOT || path.resolve(__dirname, "..")
  43. const distDir = process.env.CLINE_DIST_DIR || path.join(projectRoot, "dist-standalone")
  44. const clineCoreFile = process.env.CLINE_CORE_FILE || "cline-core.js"
  45. const coreFile = path.join(distDir, clineCoreFile)
  46. const childProcesses: ChildProcess[] = []
  47. async function main(): Promise<void> {
  48. console.log("Starting Simple Cline gRPC Server...")
  49. console.log(`Project Root: ${projectRoot}`)
  50. console.log(`Workspace: ${WORKSPACE_DIR}`)
  51. console.log(`ProtoBus Port: ${PROTOBUS_PORT}`)
  52. console.log(`HostBridge Port: ${HOSTBRIDGE_PORT}`)
  53. console.log(`Looking for standalone build at: ${coreFile}`)
  54. if (!fs.existsSync(coreFile)) {
  55. console.error(`Standalone build not found at: ${coreFile}`)
  56. console.error("Available environment variables for customization:")
  57. console.error(" PROJECT_ROOT - Override project root directory")
  58. console.error(" CLINE_DIST_DIR - Override distribution directory")
  59. console.error(" CLINE_CORE_FILE - Override core file name")
  60. console.error("")
  61. console.error("To build the standalone version, run: npm run compile-standalone")
  62. process.exit(1)
  63. }
  64. try {
  65. await ClineApiServerMock.startGlobalServer()
  66. console.log("Cline API Server started in-process")
  67. } catch (error) {
  68. console.error("Failed to start Cline API Server:", error)
  69. process.exit(1)
  70. }
  71. const extensionsDir = path.join(distDir, "vsce-extension")
  72. const userDataDir = mkdtempSync(path.join(os.tmpdir(), "vsce"))
  73. const clineTestWorkspace = mkdtempSync(path.join(os.tmpdir(), "cline-test-workspace-"))
  74. console.log("Starting HostBridge test server...")
  75. const hostbridge: ChildProcess = spawn("npx", ["tsx", path.join(__dirname, "test-hostbridge-server.ts")], {
  76. stdio: "pipe",
  77. env: {
  78. ...process.env,
  79. TEST_HOSTBRIDGE_WORKSPACE_DIR: clineTestWorkspace,
  80. HOST_BRIDGE_ADDRESS: `127.0.0.1:${HOSTBRIDGE_PORT}`,
  81. },
  82. })
  83. childProcesses.push(hostbridge)
  84. console.log(`Temp user data dir: ${userDataDir}`)
  85. console.log(`Temp extensions dir: ${extensionsDir}`)
  86. // Extract standalone.zip if needed
  87. const standaloneZipPath = path.join(distDir, "standalone.zip")
  88. if (!fs.existsSync(standaloneZipPath)) {
  89. console.error(`standalone.zip not found at: ${standaloneZipPath}`)
  90. process.exit(1)
  91. }
  92. console.log("Extracting standalone.zip to extensions directory...")
  93. try {
  94. if (!fs.existsSync(extensionsDir)) {
  95. execSync(`unzip -q "${standaloneZipPath}" -d "${extensionsDir}"`, { stdio: "inherit" })
  96. }
  97. console.log(`Successfully extracted standalone.zip to: ${extensionsDir}`)
  98. } catch (error) {
  99. console.error("Failed to extract standalone.zip:", error)
  100. process.exit(1)
  101. }
  102. const covDir = path.join(projectRoot, `coverage/coverage-core-${PROTOBUS_PORT}`)
  103. const baseArgs = ["--enable-source-maps", path.join(distDir, "cline-core.js")]
  104. const spawnArgs = USE_C8 ? ["c8", "--report-dir", covDir, "node", ...baseArgs] : ["node", ...baseArgs]
  105. console.log(`Starting Cline Core Service... (useC8=${USE_C8})`)
  106. const coreService: ChildProcess = spawn("npx", spawnArgs, {
  107. cwd: projectRoot,
  108. env: {
  109. ...process.env,
  110. NODE_PATH: "./node_modules",
  111. DEV_WORKSPACE_FOLDER: WORKSPACE_DIR,
  112. PROTOBUS_ADDRESS: `127.0.0.1:${PROTOBUS_PORT}`,
  113. HOST_BRIDGE_ADDRESS: `localhost:${HOSTBRIDGE_PORT}`,
  114. E2E_TEST,
  115. CLINE_ENVIRONMENT,
  116. CLINE_DIR: userDataDir,
  117. INSTALL_DIR: extensionsDir,
  118. },
  119. stdio: "inherit",
  120. })
  121. childProcesses.push(coreService)
  122. const shutdown = async () => {
  123. console.log("\nShutting down services...")
  124. while (childProcesses.length > 0) {
  125. const child = childProcesses.pop()
  126. if (child && !child.killed) child.kill("SIGINT")
  127. }
  128. await ClineApiServerMock.stopGlobalServer()
  129. try {
  130. rmSync(userDataDir, { recursive: true, force: true })
  131. rmSync(clineTestWorkspace, { recursive: true, force: true })
  132. console.log("Cleaned up temporary directories")
  133. } catch (err) {
  134. console.warn("Failed to cleanup temp directories:", err)
  135. }
  136. process.exit(0)
  137. }
  138. process.on("SIGINT", shutdown)
  139. process.on("SIGTERM", shutdown)
  140. coreService.on("exit", (code) => {
  141. console.log(`Core service exited with code ${code}`)
  142. shutdown()
  143. })
  144. hostbridge.on("exit", (code) => {
  145. console.log(`HostBridge exited with code ${code}`)
  146. shutdown()
  147. })
  148. console.log(`Cline gRPC Server is running on 127.0.0.1:${PROTOBUS_PORT}`)
  149. console.log("Press Ctrl+C to stop")
  150. }
  151. if (require.main === module) {
  152. main().catch((err) => {
  153. console.error("Failed to start simple Cline server:", err)
  154. process.exit(1)
  155. })
  156. }