e2e-local.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import fs from "node:fs/promises"
  2. import net from "node:net"
  3. import os from "node:os"
  4. import path from "node:path"
  5. async function freePort() {
  6. return await new Promise<number>((resolve, reject) => {
  7. const server = net.createServer()
  8. server.once("error", reject)
  9. server.listen(0, () => {
  10. const address = server.address()
  11. if (!address || typeof address === "string") {
  12. server.close(() => reject(new Error("Failed to acquire a free port")))
  13. return
  14. }
  15. server.close((err) => {
  16. if (err) {
  17. reject(err)
  18. return
  19. }
  20. resolve(address.port)
  21. })
  22. })
  23. })
  24. }
  25. async function waitForHealth(url: string) {
  26. const timeout = Date.now() + 120_000
  27. const errors: string[] = []
  28. while (Date.now() < timeout) {
  29. const result = await fetch(url)
  30. .then((r) => ({ ok: r.ok, error: undefined }))
  31. .catch((error) => ({
  32. ok: false,
  33. error: error instanceof Error ? error.message : String(error),
  34. }))
  35. if (result.ok) return
  36. if (result.error) errors.push(result.error)
  37. await new Promise((r) => setTimeout(r, 250))
  38. }
  39. const last = errors.length ? ` (last error: ${errors[errors.length - 1]})` : ""
  40. throw new Error(`Timed out waiting for server health: ${url}${last}`)
  41. }
  42. const appDir = process.cwd()
  43. const repoDir = path.resolve(appDir, "../..")
  44. const opencodeDir = path.join(repoDir, "packages", "opencode")
  45. const modelsJson = path.join(opencodeDir, "test", "tool", "fixtures", "models-api.json")
  46. const extraArgs = (() => {
  47. const args = process.argv.slice(2)
  48. if (args[0] === "--") return args.slice(1)
  49. return args
  50. })()
  51. const [serverPort, webPort] = await Promise.all([freePort(), freePort()])
  52. const sandbox = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-e2e-"))
  53. const serverEnv = {
  54. ...process.env,
  55. MODELS_DEV_API_JSON: modelsJson,
  56. OPENCODE_DISABLE_MODELS_FETCH: "true",
  57. OPENCODE_DISABLE_SHARE: "true",
  58. OPENCODE_DISABLE_LSP_DOWNLOAD: "true",
  59. OPENCODE_DISABLE_DEFAULT_PLUGINS: "true",
  60. OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER: "true",
  61. OPENCODE_TEST_HOME: path.join(sandbox, "home"),
  62. XDG_DATA_HOME: path.join(sandbox, "share"),
  63. XDG_CACHE_HOME: path.join(sandbox, "cache"),
  64. XDG_CONFIG_HOME: path.join(sandbox, "config"),
  65. XDG_STATE_HOME: path.join(sandbox, "state"),
  66. OPENCODE_E2E_PROJECT_DIR: repoDir,
  67. OPENCODE_E2E_SESSION_TITLE: "E2E Session",
  68. OPENCODE_E2E_MESSAGE: "Seeded for UI e2e",
  69. OPENCODE_E2E_MODEL: "opencode/gpt-5-nano",
  70. OPENCODE_CLIENT: "app",
  71. } satisfies Record<string, string>
  72. const runnerEnv = {
  73. ...serverEnv,
  74. PLAYWRIGHT_SERVER_HOST: "127.0.0.1",
  75. PLAYWRIGHT_SERVER_PORT: String(serverPort),
  76. VITE_OPENCODE_SERVER_HOST: "127.0.0.1",
  77. VITE_OPENCODE_SERVER_PORT: String(serverPort),
  78. PLAYWRIGHT_PORT: String(webPort),
  79. } satisfies Record<string, string>
  80. const seed = Bun.spawn(["bun", "script/seed-e2e.ts"], {
  81. cwd: opencodeDir,
  82. env: serverEnv,
  83. stdout: "inherit",
  84. stderr: "inherit",
  85. })
  86. const seedExit = await seed.exited
  87. if (seedExit !== 0) {
  88. process.exit(seedExit)
  89. }
  90. Object.assign(process.env, serverEnv)
  91. process.env.AGENT = "1"
  92. process.env.OPENCODE = "1"
  93. const log = await import("../../opencode/src/util/log")
  94. const install = await import("../../opencode/src/installation")
  95. await log.Log.init({
  96. print: true,
  97. dev: install.Installation.isLocal(),
  98. level: "WARN",
  99. })
  100. const servermod = await import("../../opencode/src/server/server")
  101. const server = servermod.Server.listen({ port: serverPort, hostname: "127.0.0.1" })
  102. console.log(`opencode server listening on http://127.0.0.1:${serverPort}`)
  103. try {
  104. await waitForHealth(`http://127.0.0.1:${serverPort}/global/health`)
  105. const runner = Bun.spawn(["bun", "test:e2e", ...extraArgs], {
  106. cwd: appDir,
  107. env: runnerEnv,
  108. stdout: "inherit",
  109. stderr: "inherit",
  110. })
  111. process.exitCode = await runner.exited
  112. } finally {
  113. await server.stop()
  114. }