backend.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. function done(proc: ReturnType<typeof spawn>) {
  44. return proc.exitCode !== null || proc.signalCode !== null
  45. }
  46. async function waitExit(proc: ReturnType<typeof spawn>, timeout = 10_000) {
  47. if (done(proc)) return
  48. await Promise.race([
  49. new Promise<void>((resolve) => proc.once("exit", () => resolve())),
  50. new Promise<void>((resolve) => setTimeout(resolve, timeout)),
  51. ])
  52. }
  53. const LOG_CAP = 100
  54. function cap(input: string[]) {
  55. if (input.length > LOG_CAP) input.splice(0, input.length - LOG_CAP)
  56. }
  57. function tail(input: string[]) {
  58. return input.slice(-40).join("")
  59. }
  60. export async function startBackend(label: string, input?: { llmUrl?: string }): Promise<Handle> {
  61. const port = await freePort()
  62. const sandbox = await fs.mkdtemp(path.join(os.tmpdir(), `opencode-e2e-${label}-`))
  63. const appDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..")
  64. const repoDir = path.resolve(appDir, "../..")
  65. const opencodeDir = path.join(repoDir, "packages", "opencode")
  66. const env = {
  67. ...process.env,
  68. OPENCODE_DISABLE_LSP_DOWNLOAD: "true",
  69. OPENCODE_DISABLE_DEFAULT_PLUGINS: "true",
  70. OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER: "true",
  71. OPENCODE_TEST_HOME: path.join(sandbox, "home"),
  72. XDG_DATA_HOME: path.join(sandbox, "share"),
  73. XDG_CACHE_HOME: path.join(sandbox, "cache"),
  74. XDG_CONFIG_HOME: path.join(sandbox, "config"),
  75. XDG_STATE_HOME: path.join(sandbox, "state"),
  76. OPENCODE_CLIENT: "app",
  77. OPENCODE_STRICT_CONFIG_DEPS: "true",
  78. OPENCODE_E2E_LLM_URL: input?.llmUrl,
  79. } satisfies Record<string, string | undefined>
  80. const out: string[] = []
  81. const err: string[] = []
  82. const proc = spawn(
  83. "bun",
  84. ["run", "--conditions=browser", "./src/index.ts", "serve", "--port", String(port), "--hostname", "127.0.0.1"],
  85. {
  86. cwd: opencodeDir,
  87. env,
  88. stdio: ["ignore", "pipe", "pipe"],
  89. },
  90. )
  91. proc.stdout?.on("data", (chunk) => {
  92. out.push(String(chunk))
  93. cap(out)
  94. })
  95. proc.stderr?.on("data", (chunk) => {
  96. err.push(String(chunk))
  97. cap(err)
  98. })
  99. const url = `http://127.0.0.1:${port}`
  100. try {
  101. await waitForHealth(url)
  102. } catch (error) {
  103. proc.kill("SIGTERM")
  104. await fs.rm(sandbox, { recursive: true, force: true }).catch(() => undefined)
  105. throw new Error(
  106. [
  107. `Failed to start isolated e2e backend for ${label}`,
  108. error instanceof Error ? error.message : String(error),
  109. tail(out),
  110. tail(err),
  111. ]
  112. .filter(Boolean)
  113. .join("\n"),
  114. )
  115. }
  116. return {
  117. url,
  118. async stop() {
  119. if (!done(proc)) {
  120. proc.kill("SIGTERM")
  121. await waitExit(proc)
  122. }
  123. if (!done(proc)) {
  124. proc.kill("SIGKILL")
  125. await waitExit(proc)
  126. }
  127. await fs.rm(sandbox, { recursive: true, force: true }).catch(() => undefined)
  128. },
  129. }
  130. }