backend.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { spawn } from "node:child_process"
  2. import fs from "node:fs/promises"
  3. import net from "node:net"
  4. import os from "node:os"
  5. import path from "node:path"
  6. import { fileURLToPath } from "node:url"
  7. type Handle = {
  8. url: string
  9. stop: () => Promise<void>
  10. }
  11. function freePort() {
  12. return new Promise<number>((resolve, reject) => {
  13. const server = net.createServer()
  14. server.once("error", reject)
  15. server.listen(0, () => {
  16. const address = server.address()
  17. if (!address || typeof address === "string") {
  18. server.close(() => reject(new Error("Failed to acquire a free port")))
  19. return
  20. }
  21. server.close((err) => {
  22. if (err) reject(err)
  23. else resolve(address.port)
  24. })
  25. })
  26. })
  27. }
  28. async function waitForHealth(url: string, probe = "/global/health") {
  29. const end = Date.now() + 120_000
  30. let last = ""
  31. while (Date.now() < end) {
  32. try {
  33. const res = await fetch(`${url}${probe}`)
  34. if (res.ok) return
  35. last = `status ${res.status}`
  36. } catch (err) {
  37. last = err instanceof Error ? err.message : String(err)
  38. }
  39. await new Promise((resolve) => setTimeout(resolve, 250))
  40. }
  41. throw new Error(`Timed out waiting for backend health at ${url}${probe}${last ? ` (${last})` : ""}`)
  42. }
  43. async function waitExit(proc: ReturnType<typeof spawn>, timeout = 10_000) {
  44. if (proc.exitCode !== null) return
  45. await Promise.race([
  46. new Promise<void>((resolve) => proc.once("exit", () => resolve())),
  47. new Promise<void>((resolve) => setTimeout(resolve, timeout)),
  48. ])
  49. }
  50. const LOG_CAP = 100
  51. function cap(input: string[]) {
  52. if (input.length > LOG_CAP) input.splice(0, input.length - LOG_CAP)
  53. }
  54. function tail(input: string[]) {
  55. return input.slice(-40).join("")
  56. }
  57. export async function startBackend(label: string, input?: { llmUrl?: string }): Promise<Handle> {
  58. const port = await freePort()
  59. const sandbox = await fs.mkdtemp(path.join(os.tmpdir(), `opencode-e2e-${label}-`))
  60. const appDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..")
  61. const repoDir = path.resolve(appDir, "../..")
  62. const opencodeDir = path.join(repoDir, "packages", "opencode")
  63. const env = {
  64. ...process.env,
  65. OPENCODE_DISABLE_LSP_DOWNLOAD: "true",
  66. OPENCODE_DISABLE_DEFAULT_PLUGINS: "true",
  67. OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER: "true",
  68. OPENCODE_TEST_HOME: path.join(sandbox, "home"),
  69. XDG_DATA_HOME: path.join(sandbox, "share"),
  70. XDG_CACHE_HOME: path.join(sandbox, "cache"),
  71. XDG_CONFIG_HOME: path.join(sandbox, "config"),
  72. XDG_STATE_HOME: path.join(sandbox, "state"),
  73. OPENCODE_CLIENT: "app",
  74. OPENCODE_STRICT_CONFIG_DEPS: "true",
  75. OPENCODE_E2E_LLM_URL: input?.llmUrl,
  76. } satisfies Record<string, string | undefined>
  77. const out: string[] = []
  78. const err: string[] = []
  79. const proc = spawn(
  80. "bun",
  81. ["run", "--conditions=browser", "./src/index.ts", "serve", "--port", String(port), "--hostname", "127.0.0.1"],
  82. {
  83. cwd: opencodeDir,
  84. env,
  85. stdio: ["ignore", "pipe", "pipe"],
  86. },
  87. )
  88. proc.stdout?.on("data", (chunk) => {
  89. out.push(String(chunk))
  90. cap(out)
  91. })
  92. proc.stderr?.on("data", (chunk) => {
  93. err.push(String(chunk))
  94. cap(err)
  95. })
  96. const url = `http://127.0.0.1:${port}`
  97. try {
  98. await waitForHealth(url)
  99. } catch (error) {
  100. proc.kill("SIGTERM")
  101. await fs.rm(sandbox, { recursive: true, force: true }).catch(() => undefined)
  102. throw new Error(
  103. [
  104. `Failed to start isolated e2e backend for ${label}`,
  105. error instanceof Error ? error.message : String(error),
  106. tail(out),
  107. tail(err),
  108. ]
  109. .filter(Boolean)
  110. .join("\n"),
  111. )
  112. }
  113. return {
  114. url,
  115. async stop() {
  116. if (proc.exitCode === null) {
  117. proc.kill("SIGTERM")
  118. await waitExit(proc)
  119. }
  120. if (proc.exitCode === null) {
  121. proc.kill("SIGKILL")
  122. await waitExit(proc)
  123. }
  124. await fs.rm(sandbox, { recursive: true, force: true }).catch(() => undefined)
  125. },
  126. }
  127. }