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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 { mkdtempSync, rmSync } from "node:fs"
  30. import * as os from "node:os"
  31. import { ChildProcess, execSync, spawn } from "child_process"
  32. import * as fs from "fs"
  33. import * as path from "path"
  34. import { ClineApiServerMock } from "../src/test/e2e/fixtures/server/index"
  35. // Configuration
  36. const PROTOBUS_PORT = process.env.PROTOBUS_PORT || "26040"
  37. const HOSTBRIDGE_PORT = process.env.HOSTBRIDGE_PORT || "26041"
  38. const WORKSPACE_DIR = process.env.WORKSPACE_DIR || process.cwd()
  39. const E2E_TEST = process.env.E2E_TEST || "true"
  40. const CLINE_ENVIRONMENT = process.env.CLINE_ENVIRONMENT || "local"
  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. async function main(): Promise<void> {
  47. console.log("Starting Simple Cline gRPC Server...")
  48. console.log(`Workspace: ${WORKSPACE_DIR}`)
  49. console.log(`ProtoBus Port: ${PROTOBUS_PORT}`)
  50. console.log(`HostBridge Port: ${HOSTBRIDGE_PORT}`)
  51. console.log(`Looking for standalone build at: ${coreFile}`)
  52. if (!fs.existsSync(coreFile)) {
  53. console.error(`Standalone build not found at: ${coreFile}`)
  54. console.error("Available environment variables for customization:")
  55. console.error(" PROJECT_ROOT - Override project root directory")
  56. console.error(" CLINE_DIST_DIR - Override distribution directory")
  57. console.error(" CLINE_CORE_FILE - Override core file name")
  58. console.error("")
  59. console.error("To build the standalone version, run: npm run compile-standalone")
  60. process.exit(1)
  61. }
  62. try {
  63. await ClineApiServerMock.startGlobalServer()
  64. console.log("Cline API Server started in-process")
  65. } catch (error) {
  66. console.error("Failed to start Cline API Server:", error)
  67. process.exit(1)
  68. }
  69. // Create temporary directories like e2e tests
  70. const userDataDir = mkdtempSync(path.join(os.tmpdir(), "vsce"))
  71. const extensionsDir = mkdtempSync(path.join(os.tmpdir(), "vsce"))
  72. const clineTestWorkspace = mkdtempSync(path.join(os.tmpdir(), "cline-test-workspace-"))
  73. // Start hostbridge test server in background.
  74. // We run it as a child process to emulate how the extension currently operates
  75. console.log("Starting HostBridge test server...")
  76. const hostbridge: ChildProcess = spawn("npx", ["tsx", path.join(__dirname, "test-hostbridge-server.ts")], {
  77. stdio: "pipe",
  78. detached: false,
  79. env: {
  80. ...process.env,
  81. TEST_HOSTBRIDGE_WORKSPACE_DIR: clineTestWorkspace,
  82. },
  83. })
  84. console.log(`Temp user data dir: ${userDataDir}`)
  85. console.log(`Temp extensions dir: ${extensionsDir}`)
  86. // Extract standalone.zip to the extensions directory
  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. execSync(`unzip -q "${standaloneZipPath}" -d "${extensionsDir}"`, { stdio: "inherit" })
  95. console.log(`Successfully extracted standalone.zip to: ${extensionsDir}`)
  96. } catch (error) {
  97. console.error("Failed to extract standalone.zip:", error)
  98. process.exit(1)
  99. }
  100. // Start the core service
  101. // We run it as a child process to emulate how the extension currently operates
  102. console.log("Starting Cline Core Service...")
  103. const coreService: ChildProcess = spawn("node", [clineCoreFile], {
  104. cwd: distDir,
  105. env: {
  106. ...process.env,
  107. NODE_PATH: "./node_modules",
  108. DEV_WORKSPACE_FOLDER: WORKSPACE_DIR,
  109. PROTOBUS_ADDRESS: `127.0.0.1:${PROTOBUS_PORT}`,
  110. HOST_BRIDGE_ADDRESS: `localhost:${HOSTBRIDGE_PORT}`,
  111. E2E_TEST: E2E_TEST,
  112. CLINE_ENVIRONMENT: CLINE_ENVIRONMENT,
  113. CLINE_DIR: userDataDir,
  114. INSTALL_DIR: extensionsDir,
  115. },
  116. stdio: "inherit",
  117. })
  118. // Handle graceful shutdown
  119. const shutdown = async (): Promise<void> => {
  120. console.log(`\n Shutting down services...\n${userDataDir}\n${extensionsDir}\n${clineTestWorkspace}\n`)
  121. hostbridge.kill()
  122. coreService.kill()
  123. await ClineApiServerMock.stopGlobalServer()
  124. // Cleanup temp directories
  125. try {
  126. rmSync(userDataDir, { recursive: true, force: true })
  127. rmSync(extensionsDir, { recursive: true, force: true })
  128. rmSync(clineTestWorkspace, { recursive: true, force: true })
  129. console.log("Cleaned up temporary directories")
  130. } catch (error) {
  131. console.warn("Failed to cleanup temp directories:", error)
  132. }
  133. process.exit(0)
  134. }
  135. process.on("SIGINT", shutdown)
  136. process.on("SIGTERM", shutdown)
  137. coreService.on("exit", (code) => {
  138. console.log(`Core service exited with code ${code}`)
  139. hostbridge.kill()
  140. process.exit(code || 0)
  141. })
  142. hostbridge.on("exit", (code) => {
  143. console.log(`HostBridge exited with code ${code}`)
  144. coreService.kill()
  145. process.exit(code || 0)
  146. })
  147. console.log("Cline gRPC Server is running!")
  148. console.log(`Connect to: 127.0.0.1:${PROTOBUS_PORT}`)
  149. console.log("Press Ctrl+C to stop")
  150. }
  151. if (require.main === module) {
  152. main().catch((error) => {
  153. console.error("Failed to start simple Cline server:", error)
  154. process.exit(1)
  155. })
  156. }