e2e-local.ts 3.3 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() + 60_000
  27. while (Date.now() < timeout) {
  28. const ok = await fetch(url)
  29. .then((r) => r.ok)
  30. .catch(() => false)
  31. if (ok) return
  32. await new Promise((r) => setTimeout(r, 250))
  33. }
  34. throw new Error(`Timed out waiting for server health: ${url}`)
  35. }
  36. const appDir = process.cwd()
  37. const repoDir = path.resolve(appDir, "../..")
  38. const opencodeDir = path.join(repoDir, "packages", "opencode")
  39. const modelsJson = path.join(opencodeDir, "test", "tool", "fixtures", "models-api.json")
  40. const extraArgs = (() => {
  41. const args = process.argv.slice(2)
  42. if (args[0] === "--") return args.slice(1)
  43. return args
  44. })()
  45. const [serverPort, webPort] = await Promise.all([freePort(), freePort()])
  46. const sandbox = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-e2e-"))
  47. const serverEnv = {
  48. ...process.env,
  49. MODELS_DEV_API_JSON: modelsJson,
  50. OPENCODE_DISABLE_MODELS_FETCH: "true",
  51. OPENCODE_DISABLE_SHARE: "true",
  52. OPENCODE_DISABLE_LSP_DOWNLOAD: "true",
  53. OPENCODE_DISABLE_DEFAULT_PLUGINS: "true",
  54. OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER: "true",
  55. OPENCODE_TEST_HOME: path.join(sandbox, "home"),
  56. XDG_DATA_HOME: path.join(sandbox, "share"),
  57. XDG_CACHE_HOME: path.join(sandbox, "cache"),
  58. XDG_CONFIG_HOME: path.join(sandbox, "config"),
  59. XDG_STATE_HOME: path.join(sandbox, "state"),
  60. OPENCODE_E2E_PROJECT_DIR: repoDir,
  61. OPENCODE_E2E_SESSION_TITLE: "E2E Session",
  62. OPENCODE_E2E_MESSAGE: "Seeded for UI e2e",
  63. OPENCODE_E2E_MODEL: "opencode/gpt-5-nano",
  64. OPENCODE_CLIENT: "app",
  65. } satisfies Record<string, string>
  66. const runnerEnv = {
  67. ...process.env,
  68. PLAYWRIGHT_SERVER_HOST: "localhost",
  69. PLAYWRIGHT_SERVER_PORT: String(serverPort),
  70. VITE_OPENCODE_SERVER_HOST: "localhost",
  71. VITE_OPENCODE_SERVER_PORT: String(serverPort),
  72. PLAYWRIGHT_PORT: String(webPort),
  73. } satisfies Record<string, string>
  74. const seed = Bun.spawn(["bun", "script/seed-e2e.ts"], {
  75. cwd: opencodeDir,
  76. env: serverEnv,
  77. stdout: "inherit",
  78. stderr: "inherit",
  79. })
  80. const seedExit = await seed.exited
  81. if (seedExit !== 0) {
  82. process.exit(seedExit)
  83. }
  84. const server = Bun.spawn(
  85. [
  86. "bun",
  87. "dev",
  88. "--",
  89. "--print-logs",
  90. "--log-level",
  91. "WARN",
  92. "serve",
  93. "--port",
  94. String(serverPort),
  95. "--hostname",
  96. "127.0.0.1",
  97. ],
  98. {
  99. cwd: opencodeDir,
  100. env: serverEnv,
  101. stdout: "inherit",
  102. stderr: "inherit",
  103. },
  104. )
  105. try {
  106. await waitForHealth(`http://localhost:${serverPort}/global/health`)
  107. const runner = Bun.spawn(["bun", "test:e2e", ...extraArgs], {
  108. cwd: appDir,
  109. env: runnerEnv,
  110. stdout: "inherit",
  111. stderr: "inherit",
  112. })
  113. process.exitCode = await runner.exited
  114. } finally {
  115. server.kill()
  116. }