httpapi-ui.test.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { afterEach, describe, expect, test } from "bun:test"
  2. import { Flag } from "@opencode-ai/core/flag/flag"
  3. import * as Log from "@opencode-ai/core/util/log"
  4. import { Server } from "../../src/server/server"
  5. void Log.init({ print: false })
  6. const original = {
  7. OPENCODE_EXPERIMENTAL_HTTPAPI: Flag.OPENCODE_EXPERIMENTAL_HTTPAPI,
  8. fetch: globalThis.fetch,
  9. }
  10. afterEach(() => {
  11. Flag.OPENCODE_EXPERIMENTAL_HTTPAPI = original.OPENCODE_EXPERIMENTAL_HTTPAPI
  12. globalThis.fetch = original.fetch
  13. })
  14. describe("HttpApi UI fallback", () => {
  15. test("serves the web UI through the experimental backend", async () => {
  16. Flag.OPENCODE_EXPERIMENTAL_HTTPAPI = true
  17. let proxiedUrl: string | undefined
  18. globalThis.fetch = ((input: RequestInfo | URL) => {
  19. proxiedUrl = String(input instanceof Request ? input.url : input)
  20. return Promise.resolve(new Response("<html>opencode</html>", { headers: { "content-type": "text/html" } }))
  21. }) as typeof fetch
  22. const response = await Server.Default().app.request("/")
  23. expect(response.status).toBe(200)
  24. expect(response.headers.get("content-type")).toContain("text/html")
  25. expect(await response.text()).toBe("<html>opencode</html>")
  26. expect(proxiedUrl).toBe("https://app.opencode.ai/")
  27. })
  28. test("keeps matched API routes ahead of the UI fallback", async () => {
  29. Flag.OPENCODE_EXPERIMENTAL_HTTPAPI = true
  30. globalThis.fetch = (() => {
  31. throw new Error("UI fallback should not handle matched API routes")
  32. }) as unknown as typeof fetch
  33. const response = await Server.Default().app.request("/session/nope")
  34. expect(response.status).toBe(404)
  35. })
  36. })